File: FastMmap.xs

package info (click to toggle)
libcache-fastmmap-perl 1.57-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 348 kB
  • sloc: ansic: 1,381; perl: 628; makefile: 3
file content (549 lines) | stat: -rw-r--r-- 11,191 bytes parent folder | download | duplicates (2)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"
#include "mmap_cache.h"

#define FC_UTF8VAL (1<<31)
#define FC_UTF8KEY (1<<30)
#define FC_UNDEF (1<<29)

#define FC_ENTRY \
    mmap_cache * cache; \
    if (!SvROK(obj)) { \
      croak("Object not reference"); \
      XSRETURN_UNDEF; \
    } \
    obj = SvRV(obj); \
    if (!SvIOKp(obj)) { \
      croak("Object not initialised correctly"); \
      XSRETURN_UNDEF; \
    } \
    cache = INT2PTR(mmap_cache *, SvIV(obj) ); \
    if (!cache) { \
      croak("Object not created correctly"); \
      XSRETURN_UNDEF; \
    }


MODULE = Cache::FastMmap		PACKAGE = Cache::FastMmap
PROTOTYPES: ENABLE

SV *
fc_new()
  INIT:
    mmap_cache * cache;
    SV * obj_pnt, * obj;
  CODE:
    cache = mmc_new();

    /* Create integer which is pointer to cache object */
    obj_pnt = newSViv(PTR2IV(cache));

    /* Create reference to integer value. This will be the object */
    obj = newRV_noinc((SV *)obj_pnt);

    RETVAL = obj;
  OUTPUT:
    RETVAL

NO_OUTPUT int
fc_set_param(obj, param, val)
    SV * obj;
    char * param;
    char * val;
  INIT:
    FC_ENTRY

  CODE:
    RETVAL = mmc_set_param(cache, param, val);
  POSTCALL:
    if (RETVAL != 0) {
      croak("%s", mmc_error(cache));
    }

NO_OUTPUT int
fc_init(obj)
    SV * obj;
  INIT:
    FC_ENTRY

  CODE:
    RETVAL = mmc_init(cache);
  POSTCALL:
    if (RETVAL != 0) {
      croak("%s", mmc_error(cache));
    }


void
fc_close(obj)
    SV * obj
  INIT:
    FC_ENTRY

  CODE:
    mmc_close(cache);
    sv_setiv(obj, 0);


void
fc_hash(obj, key);
    SV * obj;
    SV * key;
  INIT:
    int key_len;
    void * key_ptr;
    MU32 hash_page, hash_slot;
    STRLEN pl_key_len;

    FC_ENTRY

  PPCODE:

    /* Get key length, data pointer */
    key_ptr = (void *)SvPV(key, pl_key_len);
    key_len = (int)pl_key_len;

    /* Hash key to get page and slot */
    mmc_hash(cache, key_ptr, key_len, &hash_page, &hash_slot);

    XPUSHs(sv_2mortal(newSViv((IV)hash_page)));
    XPUSHs(sv_2mortal(newSViv((IV)hash_slot)));


NO_OUTPUT int
fc_lock(obj, page);
    SV * obj;
    UV page;
  INIT:
    FC_ENTRY

  CODE:
    RETVAL = mmc_lock(cache, (MU32)page);
  POSTCALL:
    if (RETVAL != 0) {
      croak("%s", mmc_error(cache));
    }


NO_OUTPUT int
fc_unlock(obj);
    SV * obj;
  INIT:
    FC_ENTRY

  CODE:
    RETVAL = mmc_unlock(cache);
  POSTCALL:
    if (RETVAL != 0) {
      croak("%s", mmc_error(cache));
    }

int
fc_is_locked(obj)
    SV * obj;
  INIT:
    FC_ENTRY

  CODE:
    /* Write value to cache */
    RETVAL = mmc_is_locked(cache);

  OUTPUT:
    RETVAL


void
fc_read(obj, hash_slot, key)
    SV * obj;
    U32  hash_slot;
    SV * key;
  INIT:
    int key_len, val_len, found;
    void * key_ptr, * val_ptr;
    MU32 expire_on = 0;
    MU32 flags = 0;
    STRLEN pl_key_len;
    SV * val;

    FC_ENTRY

  PPCODE:

    /* Get key length, data pointer */
    key_ptr = (void *)SvPV(key, pl_key_len);
    key_len = (int)pl_key_len;

    /* Get value data pointer */
    found = mmc_read(cache, (MU32)hash_slot, key_ptr, key_len, &val_ptr, &val_len, &expire_on, &flags);

    /* If not found, use undef */
    if (found == -1) {
      val = &PL_sv_undef;
    } else {

      /* Cached an undef value? */
      if (flags & FC_UNDEF) {
        val = &PL_sv_undef;

      } else {

        /* Create PERL SV */
        val = sv_2mortal(newSVpvn((const char *)val_ptr, val_len));

        /* Make UTF8 if stored from UTF8 */
        if (flags & FC_UTF8VAL) {
          SvUTF8_on(val);
        }

      }
      flags = flags & ~(FC_UTF8KEY | FC_UTF8VAL | FC_UNDEF);
    }

    XPUSHs(val);
    XPUSHs(sv_2mortal(newSViv((IV)flags)));
    XPUSHs(sv_2mortal(newSViv((IV)!found)));
    XPUSHs(sv_2mortal(newSViv((IV)expire_on)));


int
fc_write(obj, hash_slot, key, val, expire_on, in_flags)
    SV * obj;
    U32  hash_slot;
    SV * key;
    SV * val;
    U32 expire_on;
    U32 in_flags;
  INIT:
    int key_len, val_len;
    void * key_ptr, * val_ptr;
    STRLEN pl_key_len, pl_val_len;

    FC_ENTRY

  CODE:

    /* Get key length, data pointer */
    key_ptr = (void *)SvPV(key, pl_key_len);
    key_len = (int)pl_key_len;

    /* Check for storing undef, and store empty string with undef flag set */
    if (!SvOK(val)) {
      in_flags |= FC_UNDEF;

      val_ptr = "";
      val_len = 0;

    } else {

      /* Get key length, data pointer */
      val_ptr = (void *)SvPV(val, pl_val_len);
      val_len = (int)pl_val_len;

      /* Set UTF8-ness flag of stored value */
      if (SvUTF8(val)) {
        in_flags |= FC_UTF8VAL;
      }
      if (SvUTF8(key)) {
        in_flags |= FC_UTF8KEY;
      }
    }

    /* Write value to cache */
    RETVAL = mmc_write(cache, (MU32)hash_slot, key_ptr, key_len, val_ptr, val_len, (MU32)expire_on, (MU32)in_flags);

  OUTPUT:
    RETVAL

int
fc_delete(obj, hash_slot, key)
    SV * obj;
    U32  hash_slot;
    SV * key;
  INIT:
    MU32 out_flags;
    int key_len, did_delete;
    void * key_ptr;
    STRLEN pl_key_len;

    FC_ENTRY

  PPCODE:

    /* Get key length, data pointer */
    key_ptr = (void *)SvPV(key, pl_key_len);
    key_len = (int)pl_key_len;

    /* Write value to cache */
    did_delete = mmc_delete(cache, (MU32)hash_slot, key_ptr, key_len, &out_flags);

    XPUSHs(sv_2mortal(newSViv((IV)did_delete)));
    XPUSHs(sv_2mortal(newSViv((IV)out_flags)));


void
fc_get_page_details(obj)
    SV * obj;
  INIT:
    MU32 nreads = 0, nreadhits = 0;

    FC_ENTRY

  PPCODE:
    mmc_get_page_details(cache, &nreads, &nreadhits);

    XPUSHs(sv_2mortal(newSViv((IV)nreads)));
    XPUSHs(sv_2mortal(newSViv((IV)nreadhits)));


NO_OUTPUT void
fc_reset_page_details(obj)
    SV * obj;
  INIT:
    FC_ENTRY

  CODE:
    mmc_reset_page_details(cache);



void
fc_expunge(obj, mode, wb, len)
    SV * obj;
    int mode;
    int wb;
    int len;
  INIT:
    MU32 new_num_slots = 0, ** to_expunge = 0;
    int num_expunge, item;

    void * key_ptr, * val_ptr;
    int key_len, val_len;
    MU32 last_access, expire_on, flags;

    FC_ENTRY

  PPCODE:

    num_expunge = mmc_calc_expunge(cache, mode, len, &new_num_slots, &to_expunge);
    if (to_expunge) {

      /* Want list of expunged keys/values? */
      if (wb) {

        for (item = 0; item < num_expunge; item++) {
          mmc_get_details(cache, to_expunge[item],
            &key_ptr, &key_len, &val_ptr, &val_len,
            &last_access, &expire_on, &flags);

          {
          HV * ih = (HV *)sv_2mortal((SV *)newHV());

          SV * key = newSVpvn((const char *)key_ptr, key_len);
          SV * val;

          if (flags & FC_UTF8KEY) {
            SvUTF8_on(key);
            flags ^= FC_UTF8KEY;
          }

          if (flags & FC_UNDEF) {
            val = newSV(0);
            flags ^= FC_UNDEF;
          } else {
            val = newSVpvn((const char *)val_ptr, val_len);
            if (flags & FC_UTF8VAL) {
              SvUTF8_on(val);
              flags ^= FC_UTF8VAL;
            }
          }

          /* Store in hash ref */
          hv_store(ih, "key", 3, key, 0);
          hv_store(ih, "value", 5, val, 0);
          hv_store(ih, "last_access", 11, newSViv((IV)last_access), 0);
          hv_store(ih, "expire_on", 9, newSViv((IV)expire_on), 0);
          hv_store(ih, "flags", 5, newSViv((IV)flags), 0);

          /* Create reference to hash */
          XPUSHs(sv_2mortal(newRV((SV *)ih)));
          }
        }
      }

      mmc_do_expunge(cache, num_expunge, new_num_slots, to_expunge);
    }




void
fc_get_keys(obj, mode)
    SV * obj;
    int mode;
  INIT:
    mmap_cache_it * it;
    MU32 * entry_ptr;
    void * key_ptr, * val_ptr;
    int key_len, val_len;
    MU32 last_access, expire_on, flags;

    FC_ENTRY

  PPCODE:

    it = mmc_iterate_new(cache);

    /* Iterate over all items */
    while ((entry_ptr = mmc_iterate_next(it))) {
      SV *  key;
      mmc_get_details(cache, entry_ptr,
        &key_ptr, &key_len, &val_ptr, &val_len,
        &last_access, &expire_on, &flags);

      /* Create key SV, and set UTF8'ness if needed */
      key = newSVpvn((const char *)key_ptr, key_len);
      if (flags & FC_UTF8KEY) {
        SvUTF8_on(key);
        flags ^= FC_UTF8KEY;
      }

      /* Mode 0 is just list of keys */
      if (mode == 0) {
        XPUSHs(sv_2mortal(key));

      /* Mode 1/2 is list of hash-refs */
      } else if (mode == 1 || mode == 2) {
        HV * ih = (HV *)sv_2mortal((SV *)newHV());

        /* These things by default */
        hv_store(ih, "key", 3, key, 0);
        hv_store(ih, "last_access", 11, newSViv((IV)last_access), 0);
        hv_store(ih, "expire_on", 9, newSViv((IV)expire_on), 0);
        hv_store(ih, "flags", 5, newSViv((IV)flags), 0);

        /* Add value to hash-ref if mode 2 */
        if (mode == 2) {
          SV * val;
          if (flags & FC_UNDEF) {
            val = newSV(0);
            flags ^= FC_UNDEF;
          } else {
            val = newSVpvn((const char *)val_ptr, val_len);
            if (flags & FC_UTF8VAL) {
              SvUTF8_on(val);
              flags ^= FC_UTF8VAL;
            }
          }
          hv_store(ih, "value", 5, val, 0);
        }

        /* Create reference to hash */
        XPUSHs(sv_2mortal(newRV((SV *)ih)));
      }
    }

    mmc_iterate_close(it);





SV *
fc_get(obj, key)
    SV * obj;
    SV * key;
  INIT:
    int key_len, val_len, found;
    void * key_ptr, * val_ptr;
    MU32 hash_page, hash_slot, expire_on, flags;
    STRLEN pl_key_len;
    SV * val;

    FC_ENTRY

  CODE:

    /* Get key length, data pointer */
    key_ptr = (void *)SvPV(key, pl_key_len);
    key_len = (int)pl_key_len;

    /* Hash key to get page and slot */
    mmc_hash(cache, key_ptr, key_len, &hash_page, &hash_slot);

    /* Get and lock the page */
    mmc_lock(cache, hash_page);

    /* Get value data pointer */
    found = mmc_read(cache, hash_slot, key_ptr, key_len, &val_ptr, &val_len, &expire_on, &flags);

    /* If not found, use undef */
    if (found == -1) {
      val = &PL_sv_undef;
    } else {

      /* Create PERL SV */
      val = newSVpvn((const char *)val_ptr, val_len);
    }

    mmc_unlock(cache);
    RETVAL = val;
  OUTPUT:
    RETVAL


void
fc_set(obj, key, val)
    SV * obj;
    SV * key;
    SV * val;
  INIT:
    int key_len, val_len;
    void * key_ptr, * val_ptr;
    MU32 hash_page, hash_slot, flags = 0;
    STRLEN pl_key_len, pl_val_len;

    FC_ENTRY

  CODE:

    /* Get key length, data pointer */
    key_ptr = (void *)SvPV(key, pl_key_len);
    key_len = (int)pl_key_len;

    /* Get key length, data pointer */
    val_ptr = (void *)SvPV(val, pl_val_len);
    val_len = (int)pl_val_len;

    /* Hash key to get page and slot */
    mmc_hash(cache, key_ptr, key_len, &hash_page, &hash_slot);

    /* Get and lock the page */
    mmc_lock(cache, hash_page);

    /* Get value data pointer */
    mmc_write(cache, hash_slot, key_ptr, key_len, val_ptr, val_len, -1, flags);

    mmc_unlock(cache);


NO_OUTPUT void
fc_dump_page(obj);
    SV * obj;
  INIT:
    FC_ENTRY

  CODE:
    _mmc_dump_page(cache);


NO_OUTPUT void
fc_set_time_override(set_time);
    UV set_time;

  CODE:
    mmc_set_time_override((MU32)set_time);