summaryrefslogtreecommitdiff
path: root/ext/Hash/Util/FieldHash/FieldHash.xs
blob: 14e60f15a5c19b7e788ec211f614a54c1369c41d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

/* support for Hash::Util::FieldHash, prefix HUF_ */

/* The object registry, a package variable */
#define HUF_OB_REG "Hash::Util::FieldHash::ob_reg"
/* Magic cookies to recognize object id's.  Hi, Eva, David */
#define HUF_COOKIE 2805.1980
#define HUF_REFADDR_COOKIE 1811.1976


/* For global cache of object registry */
#define MY_CXT_KEY "Hash::Util::FieldHash::_guts" XS_VERSION
typedef struct {
    HV* ob_reg; /* Cache object registry */
} my_cxt_t;
START_MY_CXT

/* Deal with global context */
#define HUF_INIT 1
#define HUF_CLONE 0
#define HUF_RESET -1

void HUF_global(I32 how) {
    if (how == HUF_INIT) {
        MY_CXT_INIT;
        MY_CXT.ob_reg = get_hv(HUF_OB_REG, 1);
    } else if (how == HUF_CLONE) {
        MY_CXT_CLONE;
        MY_CXT.ob_reg = get_hv(HUF_OB_REG, 0);
    } else if (how == HUF_RESET) {
        dMY_CXT;
        MY_CXT.ob_reg = get_hv(HUF_OB_REG, 0);
    }
}

/* the id as an SV, optionally marked in the nv (unused feature) */
SV* HUF_id(SV* ref, NV cookie) {
    SV* id = sv_newmortal();
    if (cookie == 0 ) {
        SvUPGRADE(id, SVt_PVIV);
    } else {
        SvUPGRADE(id, SVt_PVNV);
        SvNV_set(id, cookie);
        SvNOK_on(id);
    }
    SvIV_set(id, (IV)SvRV(ref));
    SvIOK_on(id);
    return id;
}

/* plain id, only used for field hash entries in field lists */
SV* HUF_field_id(SV* obj) {
    return HUF_id(obj, 0.0);
}

/* object id (may be different in future) */
SV* HUF_obj_id(SV* obj) {
    return HUF_id(obj, 0.0);
}

/* set up uvar magic for any sv */
void HUF_add_uvar_magic(
    SV* sv,                    /* the sv to enchant, visible to * get/set */
    I32(* val)(pTHX_ IV, SV*), /* "get" function */
    I32(* set)(pTHX_ IV, SV*), /* "set" function */
    I32 index,                 /* get/set will see this */
    SV* thing                  /* any associated info */
) {
    struct ufuncs uf;
        uf.uf_val = val;
        uf.uf_set = set;
        uf.uf_index = index;
    sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
}

/* Fetch the data container of a trigger */
AV* HUF_get_trigger_content(SV* trigger) {
    MAGIC* mg;
    if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar)))
        return (AV*)mg->mg_obj;
    return NULL;
}

/* Delete an object from all field hashes it may occur in.  Also delete
 * the object's entry from the object registry.
 */
I32 HUF_destroy_obj(pTHX_ IV index, SV* trigger) {
    /* Do nothing if the weakref wasn't undef'd.  Also don't bother
     * during global destruction.  (MY_CXT.ob_reg is sometimes funny there) */
    if (!SvROK(trigger) && (!PL_in_clean_all)) {
        dMY_CXT;
        AV* cont = HUF_get_trigger_content(trigger);
        SV* ob_id = *av_fetch(cont, 0, 0);
        HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
        HE* ent;
        hv_iterinit(field_tab);
        while (ent = hv_iternext(field_tab)) {
            SV* field_ref = HeVAL(ent);
            SV* field = SvRV(field_ref);
            hv_delete_ent((HV*)field, ob_id, G_DISCARD, 0);
        }
        /* make it safe in case we must run in global clenaup, after all */
        if (PL_in_clean_all)
            HUF_global(HUF_RESET);
        hv_delete_ent(MY_CXT.ob_reg, ob_id, G_DISCARD, 0);
    }
    return 0;
}

/* Create a trigger for an object.  The trigger is a magical weak ref
 * that fires when the weak ref expires.  it holds the original id of
 * the object, and a list of field hashes from which the object may
 * have to be deleted.  The trigger is stored in the object registry
 * and also deleted when the object expires.
 */
SV* HUF_new_trigger(SV* obj, SV* ob_id) {
    dMY_CXT;
    SV* trigger = sv_rvweaken(newRV_inc(SvRV(obj)));
    AV* cont = newAV();
    sv_2mortal((SV*)cont);
    av_store(cont, 0, SvREFCNT_inc(ob_id));
    av_store(cont, 1, (SV*)newHV());
    HUF_add_uvar_magic(trigger, NULL, &HUF_destroy_obj, 0, (SV*)cont);
    hv_store_ent(MY_CXT.ob_reg, ob_id, trigger, 0);
    return trigger;
}

/* retrieve a trigger for obj if one exists, return NULL otherwise */
SV* HUF_ask_trigger(SV* ob_id) {
    dMY_CXT;
    HE* ent;
    if (ent = hv_fetch_ent(MY_CXT.ob_reg, ob_id, 0, 0))
        return HeVAL(ent);
    return NULL;
}

/* get the trigger for an object, creating it if necessary */
SV* HUF_get_trigger(SV* obj, SV* ob_id) {
    SV* trigger;
    if (!(trigger = HUF_ask_trigger(ob_id)))
        trigger = HUF_new_trigger(obj, ob_id);
    return trigger;
}

/* mark an object (trigger) as having been used with a field */
void HUF_mark_field(SV* trigger, SV* field) {
    AV* cont = HUF_get_trigger_content(trigger);
    HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
    SV* field_ref = newRV_inc(field);
    SV* field_id = HUF_field_id(field_ref);
    hv_store_ent(field_tab, field_id, field_ref, 0);
}

#define HV_FETCH_ISSTORE   0x01
#define HV_FETCH_ISEXISTS  0x02
#define HV_FETCH_LVALUE    0x04
#define HV_FETCH_JUST_SV   0x08

#define HUF_WOULD_CREATE_KEY(x) ((x) != -1 && ((x) & (HV_FETCH_ISSTORE | HV_FETCH_LVALUE)))

/* The key exchange function.  It communicates with S_hv_magic_uvar_xkey
 * in hv.c */
I32 HUF_watch_key(pTHX_ IV action, SV* field) {
    MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
    SV* keysv = mg->mg_obj;
    if (keysv && SvROK(keysv)) {
        SV* ob_id = HUF_obj_id(keysv);
        mg->mg_obj = ob_id; /* key replacement */
        if (HUF_WOULD_CREATE_KEY(action)) {
            SV* trigger = HUF_get_trigger(keysv, ob_id);
            HUF_mark_field(trigger, field);
        }
    }
    return 0;
}

/* see if something is a field hash */
int HUF_get_status(HV* hash) {
    int ans = 0;
    if (hash && (SvTYPE(hash) == SVt_PVHV)) {
        dMY_CXT;
        MAGIC* mg;
        struct ufuncs* uf;
        ans = (mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) &&
            (uf = (struct ufuncs *)mg->mg_ptr) &&
            (uf->uf_val == &HUF_watch_key) &&
            (uf->uf_set == NULL);
    }
    return ans;
}

/* Thread support.  These routines are called by CLONE (and nothing else) */

/* Fix entries for one object in all field hashes */
void HUF_fix_trigger(SV* trigger, SV* new_id) {
    AV* cont = HUF_get_trigger_content(trigger);
    HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
    HV* new_tab = newHV();
    HE* ent;
    SV* old_id = *av_fetch(cont, 0, 0);
    hv_iterinit(field_tab);
    while (ent = hv_iternext(field_tab)) {
        SV* field_ref = HeVAL(ent);
        SV* field_id = HUF_field_id(field_ref);
        HV* field = (HV*)SvRV(field_ref);
        SV* val;
        /* recreate field tab entry */
        hv_store_ent(new_tab, field_id, SvREFCNT_inc(field_ref), 0);
        /* recreate field entry, if any */
        if (val = hv_delete_ent(field, old_id, 0, 0))
            hv_store_ent(field, new_id, SvREFCNT_inc(val), 0);
    }
    /* update the trigger */
    av_store(cont, 0, SvREFCNT_inc(new_id));
    av_store(cont, 1, (SV*)new_tab);
}

/* Go over object registry and fix all objects.  Also fix the object
 * registry.
 */
void HUF_fix_objects() {
    dMY_CXT;
    I32 i, len;
    HE* ent;
    AV* oblist = (AV*)sv_2mortal((SV*)newAV());
    hv_iterinit(MY_CXT.ob_reg);
    while(ent = hv_iternext(MY_CXT.ob_reg))
        av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent)));
    len = av_len(oblist);
    for (i = 0; i <= len; ++i) {
        SV* old_id = *av_fetch(oblist, i, 0);
        SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0);
        SV* new_id = HUF_obj_id(trigger);
        HUF_fix_trigger(trigger, new_id);
        hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0);
    }
}

/* test support (not needed for functionality) */

static SV* counter;
I32 HUF_inc_var(pTHX_ IV index, SV* which) {
    sv_setiv(counter, 1 + SvIV(counter));
    return 0;
}

MODULE = Hash::Util::FieldHash          PACKAGE = Hash::Util::FieldHash

BOOT:
{
    HUF_global(HUF_INIT); /* create variables */
}

int
_fieldhash(SV* href, int mode)
PROTOTYPE: $$
CODE:
    HV* field;
    RETVAL = 0;
    if (mode &&
        href && SvROK(href) &&
        (field = (HV*)SvRV(href)) &&
        SvTYPE(field) == SVt_PVHV
    ) {
        HUF_add_uvar_magic(
            SvRV(href),
            &HUF_watch_key,
            NULL,
            0,
            NULL
        );
        RETVAL = HUF_get_status(field);
    }
OUTPUT:
    RETVAL

void
CLONE(char* class)
CODE:
    if (0 == strcmp(class, "Hash::Util::FieldHash")) {
        HUF_global(HUF_CLONE);
        HUF_fix_objects();
    }

SV*
_get_obj_id(SV* obj)
CODE:
    RETVAL = NULL;
    if (SvROK(obj))
        RETVAL = HUF_obj_id(obj);
OUTPUT:
    RETVAL

SV*
_active_fields(SV* obj)
PPCODE:
    if (SvROK(obj)) {
        SV* ob_id = HUF_obj_id(obj);
        SV* trigger = HUF_ask_trigger(ob_id);
        if (trigger) {
            AV* cont = HUF_get_trigger_content(trigger);
            HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
            HE* ent;
            hv_iterinit(field_tab);
            while (ent = hv_iternext(field_tab)) {
                HV* field = (HV*)SvRV(HeVAL(ent));
                if (hv_exists_ent(field, ob_id, 0))
                    XPUSHs(sv_2mortal(newRV_inc((SV*)field)));
            }
        }
    }

void
_test_uvar_get(SV* svref, SV* countref)
CODE:
    if (SvROK(svref) && SvROK(countref)) {
        counter = SvRV(countref);
        sv_setiv(counter, 0);
        HUF_add_uvar_magic(
            SvRV(svref),
            &HUF_inc_var,
            NULL,
            0,
            SvRV(countref)
        );
    }

void
_test_uvar_set(SV* svref, SV* countref)
CODE:
    if (SvROK(svref) && SvROK(countref)) {
        counter = SvRV(countref);
        sv_setiv(counter, 0);
        counter = SvRV(countref);
        HUF_add_uvar_magic(
            SvRV(svref),
            NULL,
            &HUF_inc_var,
            0,
            SvRV(countref)
        );
    }

void
_test_uvar_same(SV* svref, SV* countref)
CODE:
    if (SvROK(svref) && SvROK(countref)) {
        counter = SvRV(countref);
        sv_setiv(counter, 0);
        HUF_add_uvar_magic(
            SvRV(svref),
            &HUF_inc_var,
            &HUF_inc_var,
            0,
            NULL
        );
    }