MCPcopy Create free account
hub / github.com/DFHack/dfhack / apply

Method apply

library/VTableInterpose.cpp:418–539  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

416}
417
418bool VMethodInterposeLinkBase::apply(bool enable)
419{
420 if (!enable)
421 {
422 remove();
423 return true;
424 }
425
426 if (is_applied())
427 return true;
428 if (!host->vtable_ptr())
429 {
430 std::cerr << "VMethodInterposeLinkBase::apply(" << enable << "): " << name()
431 << ": no vtable pointer: " << host->getName() << endl;
432 return false;
433 }
434
435 // Retrieve the current vtable entry
436 auto l = host->get_interpose(vmethod_idx);
437
438 VMethodInterposeLinkBase* old_link = (l != nullptr) ? l : nullptr;
439 VMethodInterposeLinkBase* next_link = nullptr;
440
441 while (old_link && old_link->host == host && old_link->priority > priority)
442 {
443 next_link = old_link;
444 old_link = old_link->prev;
445 }
446
447 void *old_ptr = next_link ? next_link->saved_chain : host->get_vmethod_ptr(vmethod_idx);
448 assert(old_ptr != NULL && (!old_link || old_link->interpose_method == old_ptr));
449
450 // Apply the new method ptr
451 MemoryPatcher patcher;
452
453 set_chain(old_ptr);
454
455 if (next_link)
456 {
457 next_link->set_chain(interpose_method);
458 }
459 else if (!host->set_vmethod_ptr(patcher, vmethod_idx, interpose_method))
460 {
461 std::cerr << "VMethodInterposeLinkBase::apply(" << enable << "): " << name() << ": set_vmethod_ptr failed" << endl;
462 set_chain(NULL);
463 return false;
464 }
465
466 // Push the current link into the home host
467 applied = true;
468 prev = old_link;
469 next = next_link;
470
471 if (next_link)
472 next_link->prev = this;
473 else
474 host->set_interpose(vmethod_idx,this);
475

Callers

nothing calls this directly

Calls 14

is_appliedFunction · 0.85
nameFunction · 0.85
set_chainMethod · 0.80
set_vmethod_ptrMethod · 0.80
getNameMethod · 0.45
get_vmethod_ptrMethod · 0.45
clearMethod · 0.45
swapMethod · 0.45
countMethod · 0.45
eraseMethod · 0.45
insertMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected