~ubuntu-branches/ubuntu/wily/libsereal-encoder-perl/wily-proposed

« back to all changes in this revision

Viewing changes to Encoder.xs

  • Committer: Package Import Robot
  • Author(s): Alexandre Mestiashvili
  • Date: 2013-02-20 08:29:14 UTC
  • Revision ID: package-import@ubuntu.com-20130220082914-dljb6eixvtj2m1v2
Tags: upstream-0.31
ImportĀ upstreamĀ versionĀ 0.31

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Must be defined before including Perl header files or we slow down by 2x! */
 
2
#define PERL_NO_GET_CONTEXT
 
3
 
 
4
#include "EXTERN.h"
 
5
#include "perl.h"
 
6
#include "XSUB.h"
 
7
 
 
8
#define NEED_newSV_type
 
9
#include "ppport.h"
 
10
 
 
11
#include "srl_encoder.h"
 
12
#include "srl_buffer.h"
 
13
 
 
14
/* Generated code for exposing C constants to Perl */
 
15
#include "srl_protocol.h"
 
16
#include "const-c.inc"
 
17
 
 
18
#include "ptable.h"
 
19
 
 
20
MODULE = Sereal::Encoder        PACKAGE = Sereal::Encoder
 
21
PROTOTYPES: DISABLE
 
22
 
 
23
srl_encoder_t *
 
24
new(CLASS, opt = NULL)
 
25
    char *CLASS;
 
26
    HV *opt;
 
27
  CODE:
 
28
    RETVAL = srl_build_encoder_struct(aTHX_ opt);
 
29
    RETVAL->flags |= SRL_F_REUSE_ENCODER;
 
30
  OUTPUT: RETVAL
 
31
 
 
32
void
 
33
DESTROY(enc)
 
34
    srl_encoder_t *enc;
 
35
  CODE:
 
36
    srl_destroy_encoder(aTHX_ enc);
 
37
 
 
38
void
 
39
encode(enc, src)
 
40
    srl_encoder_t *enc;
 
41
    SV *src;
 
42
  PPCODE:
 
43
    assert(enc != NULL);
 
44
    srl_dump_data_structure(aTHX_ enc, src);
 
45
    assert(enc->pos > enc->buf_start);
 
46
    /* We always copy the string since we might reuse the string buffer. That means
 
47
     * we already have to do a malloc and we might as well use the opportunity to
 
48
     * allocate only as much memory as we really need to hold the output. */
 
49
    ST(0) = sv_2mortal(newSVpvn(enc->buf_start, (STRLEN)BUF_POS_OFS(enc)));
 
50
    XSRETURN(1);
 
51
 
 
52
void
 
53
encode_sereal(src, opt = NULL)
 
54
    SV *src;
 
55
    HV *opt;
 
56
  PREINIT:
 
57
    srl_encoder_t *enc;
 
58
  PPCODE:
 
59
    enc = srl_build_encoder_struct(aTHX_ opt);
 
60
    assert(enc != NULL);
 
61
    srl_dump_data_structure(aTHX_ enc, src);
 
62
    /* Avoid copy by stealing string buffer if it is not too large.
 
63
     * This makes sense in the functional interface since the string
 
64
     * buffer isn't ever going to be reused. */
 
65
    assert(enc->buf_start < enc->pos);
 
66
    if (BUF_POS_OFS(enc) > 20 && BUF_SPACE(enc) < BUF_POS_OFS(enc) ) {
 
67
      /* If not wasting more than 2x memory - FIXME fungible */
 
68
      SV *sv = sv_2mortal(newSV_type(SVt_PV));
 
69
      ST(0) = sv;
 
70
      SvPV_set(sv, enc->buf_start);
 
71
      SvLEN_set(sv, BUF_SIZE(enc));
 
72
      SvCUR_set(sv, BUF_POS_OFS(enc));
 
73
      SvPOK_on(sv);
 
74
 
 
75
      enc->buf_start = enc->pos = NULL; /* no need to free these guys now */
 
76
    }
 
77
    else {
 
78
      ST(0) = sv_2mortal(newSVpvn(enc->buf_start, (STRLEN)BUF_POS_OFS(enc)));
 
79
    }
 
80
    XSRETURN(1);
 
81
 
 
82
 
 
83
MODULE = Sereal::Encoder        PACKAGE = Sereal::Encoder::Constants
 
84
PROTOTYPES: DISABLE
 
85
 
 
86
INCLUDE: const-xs.inc
 
87
 
 
88
MODULE = Sereal::Encoder        PACKAGE = Sereal::Encoder::_ptabletest
 
89
 
 
90
void
 
91
test()
 
92
  PREINIT:
 
93
    PTABLE_t *tbl;
 
94
    PTABLE_ITER_t *iter;
 
95
    PTABLE_ENTRY_t *ent;
 
96
    UV i, n = 20;
 
97
    char *check[20];
 
98
    char fail[5] = "not ";
 
99
    char noop[1] = "";
 
100
  CODE:
 
101
    tbl = PTABLE_new_size(10);
 
102
    for (i = 0; i < (UV)n; ++i) {
 
103
      PTABLE_store(tbl, (void *)(1000+i), (void *)(1000+i));
 
104
      check[i] = fail;
 
105
    }
 
106
    for (i = 0; i < (UV)n; ++i) {
 
107
      const UV res = (UV)PTABLE_fetch(tbl, (void *)(1000+i));
 
108
      printf("%sok %u - fetch %u\n", (res == (UV)(1000+i)) ? noop : fail, (unsigned int)(1+i), (unsigned int)(i+1));
 
109
    }
 
110
    iter = PTABLE_iter_new(tbl);
 
111
    while ( NULL != (ent = PTABLE_iter_next(iter)) ) {
 
112
      const UV res = ((UV)ent->value) - 1000;
 
113
      if (res < 20)
 
114
        check[res] = noop;
 
115
      else
 
116
        abort();
 
117
    }
 
118
    for (i = 0; i < (UV)n; ++i) {
 
119
      printf("%sok %u - iter %u\n", check[i], (unsigned int)(21+i), (unsigned int)(i+1));
 
120
    }
 
121
    PTABLE_iter_free(iter);
 
122
    PTABLE_free(tbl);