Line data Source code
1 :
2 : /*
3 : * Copyright (C) Xiaozhe Wang (chaoslawful)
4 : * Copyright (C) Yichun Zhang (agentzh)
5 : */
6 :
7 :
8 : #ifndef DDEBUG
9 : #define DDEBUG 0
10 : #endif
11 : #include "ddebug.h"
12 :
13 :
14 : #include "ngx_http_lua_pcrefix.h"
15 : #include "stdio.h"
16 :
17 : #if (NGX_PCRE)
18 :
19 : static ngx_pool_t *ngx_http_lua_pcre_pool = NULL;
20 :
21 : static void *(*old_pcre_malloc)(size_t);
22 : static void (*old_pcre_free)(void *ptr);
23 :
24 :
25 : /* XXX: work-around to nginx regex subsystem, must init a memory pool
26 : * to use PCRE functions. As PCRE still has memory-leaking problems,
27 : * and nginx overwrote pcre_malloc/free hooks with its own static
28 : * functions, so nobody else can reuse nginx regex subsystem... */
29 : static void *
30 0 : ngx_http_lua_pcre_malloc(size_t size)
31 : {
32 : dd("lua pcre pool is %p", ngx_http_lua_pcre_pool);
33 :
34 0 : if (ngx_http_lua_pcre_pool) {
35 0 : return ngx_palloc(ngx_http_lua_pcre_pool, size);
36 : }
37 :
38 0 : fprintf(stderr, "error: lua pcre malloc failed due to empty pcre pool");
39 :
40 0 : return NULL;
41 : }
42 :
43 :
44 : static void
45 0 : ngx_http_lua_pcre_free(void *ptr)
46 : {
47 : dd("lua pcre pool is %p", ngx_http_lua_pcre_pool);
48 :
49 0 : if (ngx_http_lua_pcre_pool) {
50 0 : ngx_pfree(ngx_http_lua_pcre_pool, ptr);
51 0 : return;
52 : }
53 :
54 0 : fprintf(stderr, "error: lua pcre free failed due to empty pcre pool");
55 : }
56 :
57 :
58 : ngx_pool_t *
59 2 : ngx_http_lua_pcre_malloc_init(ngx_pool_t *pool)
60 : {
61 : ngx_pool_t *old_pool;
62 :
63 2 : if (pcre_malloc != ngx_http_lua_pcre_malloc) {
64 :
65 : dd("overriding nginx pcre malloc and free");
66 :
67 2 : ngx_http_lua_pcre_pool = pool;
68 :
69 2 : old_pcre_malloc = pcre_malloc;
70 2 : old_pcre_free = pcre_free;
71 :
72 2 : pcre_malloc = ngx_http_lua_pcre_malloc;
73 2 : pcre_free = ngx_http_lua_pcre_free;
74 :
75 2 : return NULL;
76 : }
77 :
78 : dd("lua pcre pool was %p", ngx_http_lua_pcre_pool);
79 :
80 0 : old_pool = ngx_http_lua_pcre_pool;
81 0 : ngx_http_lua_pcre_pool = pool;
82 :
83 : dd("lua pcre pool is %p", ngx_http_lua_pcre_pool);
84 :
85 0 : return old_pool;
86 : }
87 :
88 :
89 : void
90 2 : ngx_http_lua_pcre_malloc_done(ngx_pool_t *old_pool)
91 : {
92 : dd("lua pcre pool was %p", ngx_http_lua_pcre_pool);
93 :
94 2 : ngx_http_lua_pcre_pool = old_pool;
95 :
96 : dd("lua pcre pool is %p", ngx_http_lua_pcre_pool);
97 :
98 2 : if (old_pool == NULL) {
99 2 : pcre_malloc = old_pcre_malloc;
100 2 : pcre_free = old_pcre_free;
101 : }
102 2 : }
103 :
104 : #endif /* NGX_PCRE */
105 :
106 : /* vi:set ft=c ts=4 sw=4 et fdm=marker: */
|