]> git.vpit.fr Git - perl/modules/re-engine-Hooks.git/blob - Hooks.xs
Rename *comp* (resp. *exec*) to *comp_node* (resp. *exec_node*)
[perl/modules/re-engine-Hooks.git] / Hooks.xs
1 /* This file is part of the re::engine::Hooks Perl module.
2  * See http://search.cpan.org/dist/re-engine-Hooks/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "re::engine::Hooks"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #define REH_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
15
16 #ifndef SvPV_const
17 # define SvPV_const(S, L) SvPV(S, L)
18 #endif
19
20 /* ... Thread safety and multiplicity ...................................... */
21
22 #ifndef REH_MULTIPLICITY
23 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
24 #  define REH_MULTIPLICITY 1
25 # else
26 #  define REH_MULTIPLICITY 0
27 # endif
28 #endif
29
30 #ifdef USE_ITHREADS
31 # define REH_LOCK(M)   MUTEX_LOCK(M)
32 # define REH_UNLOCK(M) MUTEX_UNLOCK(M)
33 #else
34 # define REH_LOCK(M)   NOOP
35 # define REH_UNLOCK(M) NOOP
36 #endif
37
38 /* --- Lexical hints ------------------------------------------------------- */
39
40 STATIC U32 reh_hash = 0;
41
42 STATIC SV *reh_hint(pTHX) {
43 #define reh_hint() reh_hint(aTHX)
44  SV *hint;
45
46 #ifdef cop_hints_fetch_pvn
47  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
48                                        reh_hash, 0);
49 #elif REH_HAS_PERL(5, 9, 5)
50  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
51                                        NULL,
52                                        __PACKAGE__, __PACKAGE_LEN__,
53                                        0,
54                                        reh_hash);
55 #else
56  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
57  if (!val)
58   return 0;
59  hint = *val;
60 #endif
61
62  return hint;
63 }
64
65 /* --- Public API ---------------------------------------------------------- */
66
67 #include "re_engine_hooks.h"
68
69 typedef struct reh_action {
70  struct reh_action *next;
71  reh_config         cbs;
72  const char        *key;
73  STRLEN             klen;
74 } reh_action;
75
76 STATIC reh_action *reh_action_list = 0;
77
78 #ifdef USE_ITHREADS
79
80 STATIC perl_mutex reh_action_list_mutex;
81
82 #endif /* USE_ITHREADS */
83
84 #undef reh_register
85 void reh_register(pTHX_ const char *key, reh_config *cfg) {
86  reh_action *a;
87  char       *key_dup;
88  STRLEN      i, len;
89
90  len = strlen(key);
91  for (i = 0; i < len; ++i)
92   if (!isALNUM(key[i]) && key[i] != ':')
93    croak("Invalid key");
94  key_dup = PerlMemShared_malloc(len + 1);
95  memcpy(key_dup, key, len);
96  key_dup[len] = '\0';
97
98  a       = PerlMemShared_malloc(sizeof *a);
99  a->cbs  = *cfg;
100  a->key  = key_dup;
101  a->klen = len;
102
103  REH_LOCK(&reh_action_list_mutex);
104  a->next         = reh_action_list;
105  reh_action_list = a;
106  REH_UNLOCK(&reh_action_list_mutex);
107
108  return;
109 }
110
111 /* --- Custom regexp engine ------------------------------------------------ */
112
113 #if PERL_VERSION <= 10
114 # define rxREGEXP(RX)  (RX)
115 #else
116 # define rxREGEXP(RX)  (SvANY(RX))
117 #endif
118
119 #if PERL_VERSION <= 10
120 EXTERN_C REGEXP *reh_regcomp(pTHX_ const SV * const, const U32);
121 #else
122 EXTERN_C REGEXP *reh_regcomp(pTHX_ SV * const, U32);
123 #endif
124 EXTERN_C I32     reh_regexec(pTHX_ REGEXP * const, char *, char *,
125                                    char *, I32, SV *, void *, U32);
126 EXTERN_C char *  reh_re_intuit_start(pTHX_ REGEXP * const, SV *, char *,
127                                            char *, U32, re_scream_pos_data *);
128 EXTERN_C SV *    reh_re_intuit_string(pTHX_ REGEXP * const);
129 EXTERN_C void    reh_regfree(pTHX_ REGEXP * const);
130 EXTERN_C void    reh_re_free(pTHX_ REGEXP * const);
131 EXTERN_C void    reh_reg_numbered_buff_fetch(pTHX_ REGEXP * const,
132                                                    const I32, SV * const);
133 EXTERN_C void    reh_reg_numbered_buff_store(pTHX_ REGEXP * const,
134                                                    const I32, SV const * const);
135 EXTERN_C I32     reh_reg_numbered_buff_length(pTHX_ REGEXP * const,
136                                                    const SV * const, const I32);
137 EXTERN_C SV *    reh_reg_named_buff(pTHX_ REGEXP * const, SV * const,
138                                           SV * const, const U32);
139 EXTERN_C SV *    reh_reg_named_buff_iter(pTHX_ REGEXP * const,
140                                                const SV * const, const U32);
141 EXTERN_C SV *    reh_reg_qr_package(pTHX_ REGEXP * const);
142 #ifdef USE_ITHREADS
143 EXTERN_C void *  reh_regdupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
144 EXTERN_C void *  reh_re_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *);
145 #endif
146
147 EXTERN_C const struct regexp_engine reh_regexp_engine;
148
149 const struct regexp_engine reh_regexp_engine = {
150  reh_regcomp,
151  reh_regexec,
152  reh_re_intuit_start,
153  reh_re_intuit_string,
154  reh_re_free,
155  reh_reg_numbered_buff_fetch,
156  reh_reg_numbered_buff_store,
157  reh_reg_numbered_buff_length,
158  reh_reg_named_buff,
159  reh_reg_named_buff_iter,
160  reh_reg_qr_package,
161 #if defined(USE_ITHREADS)
162  reh_re_dupe
163 #endif
164 };
165
166 /* --- Private API --------------------------------------------------------- */
167
168 typedef struct {
169  size_t            count;
170  const reh_config *cbs;
171  U32               refcount;
172 } reh_private;
173
174 STATIC void reh_private_free(pTHX_ reh_private *priv) {
175 #define reh_private_free(P) reh_private_free(aTHX_ (P))
176  if (priv->refcount <= 1) {
177   PerlMemShared_free((void *) priv->cbs);
178   PerlMemShared_free(priv);
179  } else {
180   --priv->refcount;
181  }
182 }
183
184 #define PTABLE_NAME        ptable_private
185 #define PTABLE_VAL_FREE(V) reh_private_free(V)
186
187 #define pPTBL  pTHX
188 #define pPTBL_ pTHX_
189 #define aPTBL  aTHX
190 #define aPTBL_ aTHX_
191
192 #include "ptable.h"
193
194 #define ptable_private_store(T, K, V) ptable_private_store(aTHX_ (T), (K), (V))
195 #define ptable_private_delete(T, K)   ptable_private_delete(aTHX_ (T), (K))
196 #define ptable_private_clear(T)       ptable_private_clear(aTHX_ (T))
197 #define ptable_private_free(T)        ptable_private_free(aTHX_ (T))
198
199 STATIC ptable *reh_private_map;
200
201 #ifdef USE_ITHREADS
202
203 STATIC perl_mutex reh_private_map_mutex;
204
205 #endif /* USE_ITHREADS */
206
207 #define REH_PRIVATE_MAP_FOREACH(C) STMT_START {      \
208  reh_private *priv;                                  \
209  REH_LOCK(&reh_private_map_mutex);                   \
210  priv = ptable_fetch(reh_private_map, rx->pprivate); \
211  if (priv) {                                         \
212   const reh_config *cbs = priv->cbs;                 \
213   if (cbs) {                                         \
214    const reh_config *end = cbs + priv->count;        \
215    for (; cbs < end; ++cbs) {                        \
216     (C);                                             \
217    }                                                 \
218   }                                                  \
219  }                                                   \
220  REH_UNLOCK(&reh_private_map_mutex);                 \
221 } STMT_END
222
223 STATIC reh_private *reh_private_map_store(pTHX_ void *ri, reh_private *priv) {
224 #define reh_private_map_store(R, P) reh_private_map_store(aTHX_ (R), (P))
225  REH_LOCK(&reh_private_map_mutex);
226  ptable_private_store(reh_private_map, ri, priv);
227  REH_UNLOCK(&reh_private_map_mutex);
228
229  return;
230 }
231
232 STATIC void reh_private_map_copy(pTHX_ void *ri_from, void *ri_to) {
233 #define reh_private_map_copy(F, T) reh_private_map_copy(aTHX_ (F), (T))
234  reh_private *priv;
235
236  REH_LOCK(&reh_private_map_mutex);
237  priv = ptable_fetch(reh_private_map, ri_from);
238  if (priv) {
239   ++priv->refcount;
240   ptable_private_store(reh_private_map, ri_to, priv);
241  }
242  REH_UNLOCK(&reh_private_map_mutex);
243 }
244
245 STATIC void reh_private_map_delete(pTHX_ void *ri) {
246 #define reh_private_map_delete(R) reh_private_map_delete(aTHX_ (R))
247  REH_LOCK(&reh_private_map_mutex);
248  ptable_private_delete(reh_private_map, ri);
249  REH_UNLOCK(&reh_private_map_mutex);
250
251  return;
252 }
253
254 void reh_call_comp_begin_hook(pTHX_ regexp *rx) {
255  SV *hint = reh_hint();
256
257  if (hint && SvPOK(hint)) {
258   STRLEN      len;
259   const char *keys  = SvPV_const(hint, len);
260   size_t      count = 0;
261
262   reh_private *priv;
263   reh_config  *cbs = NULL;
264   reh_action  *a, *root;
265
266   REH_LOCK(&reh_action_list_mutex);
267   root = reh_action_list;
268   REH_UNLOCK(&reh_action_list_mutex);
269
270   for (a = root; a; a = a->next) {
271    char *p = strstr(keys, a->key);
272
273    if (p && (p + a->klen <= keys + len) && p[a->klen] == ' ')
274     ++count;
275   }
276
277   if (count) {
278    size_t i = 0;
279
280    cbs = PerlMemShared_malloc(count * sizeof *cbs);
281
282    for (a = root; a; a = a->next) {
283     char *p = strstr(keys, a->key);
284
285     if (p && (p + a->klen <= keys + len) && p[a->klen] == ' ')
286      cbs[i++] = a->cbs;
287    }
288   }
289
290   priv = PerlMemShared_malloc(sizeof *priv);
291   priv->count    = count;
292   priv->cbs      = cbs;
293   priv->refcount = 1;
294
295   rx->engine = &reh_regexp_engine;
296
297   reh_private_map_store(rx->pprivate, priv);
298  }
299 }
300
301 void reh_call_comp_node_hook(pTHX_ regexp *rx, regnode *node) {
302  REH_PRIVATE_MAP_FOREACH(cbs->comp_node(aTHX_ rx, node));
303 }
304
305 void reh_call_exec_node_hook(pTHX_ regexp *rx, regnode *node, regmatch_info *reginfo, regmatch_state *st) {
306  REH_PRIVATE_MAP_FOREACH(cbs->exec_node(aTHX_ rx, node, reginfo, st));
307 }
308
309 void reh_re_free(pTHX_ REGEXP * const RX) {
310  regexp *rx = rxREGEXP(RX);
311
312  reh_private_map_delete(rx->pprivate);
313
314  reh_regfree(aTHX_ RX);
315 }
316
317 #ifdef USE_ITHREADS
318
319 void *reh_re_dupe(pTHX_ REGEXP * const RX, CLONE_PARAMS *param) {
320  regexp *rx = rxREGEXP(RX);
321  void   *new_ri;
322
323  new_ri = reh_regdupe(aTHX_ RX, param);
324
325  reh_private_map_copy(rx->pprivate, new_ri);
326
327  return new_ri;
328 }
329
330 #endif
331
332 STATIC void reh_teardown(pTHX_ void *root) {
333 #if REH_MULTIPLICITY
334  if (aTHX != root)
335   return;
336 #endif
337
338  ptable_private_free(reh_private_map);
339 }
340
341 /* --- XS ------------------------------------------------------------------ */
342
343 MODULE = re::engine::Hooks          PACKAGE = re::engine::Hooks
344
345 PROTOTYPES: ENABLE
346
347 BOOT:
348 {
349  reh_private_map = ptable_new();
350 #ifdef USE_ITHREADS
351  MUTEX_INIT(&reh_action_list_mutex);
352  MUTEX_INIT(&reh_private_map_mutex);
353 #endif
354  PERL_HASH(reh_hash, __PACKAGE__, __PACKAGE_LEN__);
355 #if REH_MULTIPLICITY
356  call_atexit(reh_teardown, aTHX);
357 #else
358  call_atexit(reh_teardown, NULL);
359 #endif
360 }
361
362 void
363 _ENGINE()
364 PROTOTYPE:
365 PPCODE:
366  XPUSHs(sv_2mortal(newSViv(PTR2IV(&reh_regexp_engine))));
367
368 void
369 _registered(SV *key)
370 PROTOTYPE: $
371 PREINIT:
372  SV         *ret = NULL;
373  reh_action *a;
374  STRLEN      len;
375  const char *s;
376 PPCODE:
377  REH_LOCK(&reh_action_list_mutex);
378  a = reh_action_list;
379  REH_UNLOCK(&reh_action_list_mutex);
380  s = SvPV_const(key, len);
381  while (a && !ret) {
382   if (a->klen == len && memcmp(a->key, s, len) == 0)
383    ret = &PL_sv_yes;
384   a = a->next;
385  }
386  if (!ret)
387   ret = &PL_sv_no;
388  EXTEND(SP, 1);
389  PUSHs(ret);
390  XSRETURN(1);