| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #undef NDEBUG |
| 6 |
|
| #include <assert.h> |
| 7 |
|
| |
| 8 |
|
| static int duphook(pTHX_ MAGIC *mg, CLONE_PARAMS *param); |
| 9 |
|
| static MGVTBL vtbl = { |
| 10 |
|
| .svt_dup = &duphook |
| 11 |
|
| }; |
| 12 |
|
| |
| 13 |
0 |
| static int duphook(pTHX_ MAGIC *mg, CLONE_PARAMS *param) |
| 14 |
|
| { |
| 15 |
|
| /*Make sure our CLONE gets called first*/ |
| 16 |
0 |
| av_unshift(param->stashes, 1); |
| 17 |
0 |
| HV *mystash = gv_stashpv("Devel::PtrTable", 0); |
| 18 |
0 |
| SvREFCNT_inc(mystash); |
| 19 |
0 |
| av_store(param->stashes, 0, (SV*)mystash); |
| 20 |
0 |
| mg->mg_ptr = (char*)ptr_table_new(); |
| 21 |
0 |
| } |
| 22 |
|
| |
| 23 |
0 |
| void _PtrTable_init(SV *self) |
| 24 |
|
| { |
| 25 |
0 |
| MAGIC *mg = sv_magicext(SvRV(self), NULL, PERL_MAGIC_ext, &vtbl, NULL, 0); |
| 26 |
0 |
| mg->mg_flags |= MGf_DUP; |
| 27 |
0 |
| } |
| 28 |
|
| |
| 29 |
|
| static inline PTR_TBL_t* |
| 30 |
0 |
| get_our_table(SV *self) |
| 31 |
|
| { |
| 32 |
|
| MAGIC *mg; |
| 33 |
0 |
| for(mg = mg_find(SvRV(self), PERL_MAGIC_ext); |
| 34 |
0 |
▶ | mg; |
Branch: 1 branch
| Branch | True | False | Total |
| mg; |
0 |
0 |
0 |
|
| 35 |
0 |
| mg = mg->mg_moremagic |
| 36 |
|
| ) { |
| 37 |
0 |
▶ | if(mg->mg_virtual == &vtbl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(mg->mg_virtual == &vtbl) { |
0 |
0 |
0 |
|
| 38 |
0 |
| break; |
| 39 |
|
| } |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
0 |
▶ | if(!mg) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!mg) { |
0 |
0 |
0 |
|
| 43 |
0 |
| sv_dump(self); |
| 44 |
0 |
| die("Couldn't find our magic!"); |
| 45 |
|
| } |
| 46 |
0 |
| return (PTR_TBL_t*)mg->mg_ptr; |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
0 |
| void _PtrTable_make_our_table(SV *self) |
| 50 |
|
| { |
| 51 |
0 |
| PTR_TBL_t *our_table = get_our_table(self); |
| 52 |
0 |
▶ | assert(PL_ptr_table); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(PL_ptr_table); |
0 |
0 |
0 |
|
| 53 |
0 |
| UV max = PL_ptr_table->tbl_max; |
| 54 |
0 |
| UV i = 0; |
| 55 |
|
| PTR_TBL_ENT_t *head_ent; |
| 56 |
0 |
▶ | for(i = 0; i <= max; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i <= max; i++) { |
0 |
0 |
0 |
|
| 57 |
0 |
| for(head_ent = PL_ptr_table->tbl_ary[i]; |
| 58 |
0 |
▶ | head_ent; |
Branch: 1 branch
| Branch | True | False | Total |
| head_ent; |
0 |
0 |
0 |
|
| 59 |
0 |
| head_ent = head_ent->next) |
| 60 |
|
| { |
| 61 |
0 |
| ptr_table_store(our_table, head_ent->oldval, head_ent->newval); |
| 62 |
|
| } |
| 63 |
|
| } |
| 64 |
0 |
| } |
| 65 |
|
| |
| 66 |
0 |
| SV *_PtrTable_get(SV *self, UV addr) |
| 67 |
|
| { |
| 68 |
0 |
| PTR_TBL_t *our_table = get_our_table(self); |
| 69 |
0 |
| SV *ret = ptr_table_fetch(our_table, (void*)addr); |
| 70 |
0 |
▶ | if(!ret) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!ret) { |
0 |
0 |
0 |
|
| 71 |
0 |
| ret = &PL_sv_undef; |
| 72 |
|
| } else { |
| 73 |
0 |
| ret = newRV_inc(ret); |
| 74 |
|
| } |
| 75 |
|
| /*else*/ |
| 76 |
0 |
| return ret; |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
0 |
| void _PtrTable_freecopied(SV *self) |
| 80 |
|
| { |
| 81 |
0 |
| PTR_TBL_t *our_table = get_our_table(self); |
| 82 |
0 |
| ptr_table_free(our_table); |
| 83 |
0 |
| } |
| 84 |
|
| MODULE = Devel::PtrTable PACKAGE = Devel::PtrTable |
| 85 |
|
| |
| 86 |
|
| PROTOTYPES: DISABLE |
| 87 |
|
| |
| 88 |
|
| |
| 89 |
|
| void |
| 90 |
|
| _PtrTable_init (self) |
| 91 |
|
| SV * self |
| 92 |
|
| PREINIT: |
| 93 |
|
| I32* temp; |
| 94 |
|
| PPCODE: |
| 95 |
0 |
| temp = PL_markstack_ptr++; |
| 96 |
0 |
| _PtrTable_init(self); |
| 97 |
0 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
0 |
0 |
|
| 98 |
|
| /* truly void, because dXSARGS not invoked */ |
| 99 |
0 |
| PL_markstack_ptr = temp; |
| 100 |
0 |
▶ | XSRETURN_EMPTY; /* return empty stack */ |
Branch: 1 branch
| Branch | True | False | Total |
| XSRETURN_EMPTY; /* return empty stack */ |
0 |
0 |
0 |
|
| 101 |
|
| } |
| 102 |
|
| /* must have used dXSARGS; list context implied */ |
| 103 |
0 |
| return; /* assume stack size is correct */ |
| 104 |
|
| |
| 105 |
|
| void |
| 106 |
|
| _PtrTable_make_our_table (self) |
| 107 |
|
| SV * self |
| 108 |
|
| PREINIT: |
| 109 |
|
| I32* temp; |
| 110 |
|
| PPCODE: |
| 111 |
0 |
| temp = PL_markstack_ptr++; |
| 112 |
0 |
| _PtrTable_make_our_table(self); |
| 113 |
0 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
0 |
0 |
|
| 114 |
|
| /* truly void, because dXSARGS not invoked */ |
| 115 |
0 |
| PL_markstack_ptr = temp; |
| 116 |
0 |
▶ | XSRETURN_EMPTY; /* return empty stack */ |
Branch: 1 branch
| Branch | True | False | Total |
| XSRETURN_EMPTY; /* return empty stack */ |
0 |
0 |
0 |
|
| 117 |
|
| } |
| 118 |
|
| /* must have used dXSARGS; list context implied */ |
| 119 |
0 |
| return; /* assume stack size is correct */ |
| 120 |
|
| |
| 121 |
|
| SV * |
| 122 |
|
| _PtrTable_get (self, addr) |
| 123 |
|
| SV * self |
| 124 |
|
| UV addr |
| 125 |
|
| |
| 126 |
|
| void |
| 127 |
|
| _PtrTable_freecopied (self) |
| 128 |
|
| SV * self |
| 129 |
|
| PREINIT: |
| 130 |
|
| I32* temp; |
| 131 |
|
| PPCODE: |
| 132 |
0 |
| temp = PL_markstack_ptr++; |
| 133 |
0 |
| _PtrTable_freecopied(self); |
| 134 |
0 |
▶ | if (PL_markstack_ptr != temp) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (PL_markstack_ptr != temp) { |
0 |
0 |
0 |
|
| 135 |
|
| /* truly void, because dXSARGS not invoked */ |
| 136 |
0 |
| PL_markstack_ptr = temp; |
| 137 |
0 |
▶ | XSRETURN_EMPTY; /* return empty stack */ |
Branch: 1 branch
| Branch | True | False | Total |
| XSRETURN_EMPTY; /* return empty stack */ |
0 |
0 |
0 |
|
| 138 |
|
| } |
| 139 |
|
| /* must have used dXSARGS; list context implied */ |
| 140 |
0 |
| return; /* assume stack size is correct */ |
| 141 |
|
| |