4
4
#include "ddebug.h"
5
5
6
6
#include "ngx_http_lua_pcrefix.h"
7
+ #include "stdio.h"
7
8
8
9
#if (NGX_PCRE )
9
10
10
- ngx_pool_t * ngx_http_lua_pcre_pool ;
11
+ static ngx_pool_t * ngx_http_lua_pcre_pool = NULL ;
11
12
12
13
static void * (* old_pcre_malloc )(size_t );
13
14
static void (* old_pcre_free )(void * ptr );
@@ -17,48 +18,79 @@ static void (*old_pcre_free)(void *ptr);
17
18
* to use PCRE functions. As PCRE still has memory-leaking problems,
18
19
* and nginx overwrote pcre_malloc/free hooks with its own static
19
20
* functions, so nobody else can reuse nginx regex subsystem... */
20
- void *
21
+ static void *
21
22
ngx_http_lua_pcre_malloc (size_t size )
22
23
{
23
- if (ngx_http_lua_pcre_pool ) {
24
- return ngx_palloc (ngx_http_lua_pcre_pool , size );
25
- }
24
+ dd ("lua pcre pool is %p" , ngx_http_lua_pcre_pool );
26
25
27
- dd ("returning NULL" );
26
+ if (ngx_http_lua_pcre_pool ) {
27
+ return ngx_palloc (ngx_http_lua_pcre_pool , size );
28
+ }
28
29
29
- return NULL ;
30
+ fprintf (stderr , "error: lua pcre malloc failed due to empty pcre pool" );
31
+
32
+ return NULL ;
30
33
}
31
34
32
35
33
36
static void
34
37
ngx_http_lua_pcre_free (void * ptr )
35
38
{
36
- if (ngx_http_lua_pcre_pool ) {
37
- ngx_pfree (ngx_http_lua_pcre_pool , ptr );
38
- }
39
+ dd ("lua pcre pool is %p" , ngx_http_lua_pcre_pool );
40
+
41
+ if (ngx_http_lua_pcre_pool ) {
42
+ ngx_pfree (ngx_http_lua_pcre_pool , ptr );
43
+ return ;
44
+ }
45
+
46
+ fprintf (stderr , "error: lua pcre free failed due to empty pcre pool" );
39
47
}
40
48
41
49
42
- void
50
+ ngx_pool_t *
43
51
ngx_http_lua_pcre_malloc_init (ngx_pool_t * pool )
44
52
{
45
- ngx_http_lua_pcre_pool = pool ;
53
+ ngx_pool_t * old_pool ;
54
+
55
+ if (pcre_malloc != ngx_http_lua_pcre_malloc ) {
56
+
57
+ dd ("overriding nginx pcre malloc and free" );
58
+
59
+ ngx_http_lua_pcre_pool = pool ;
46
60
47
- old_pcre_malloc = pcre_malloc ;
48
- old_pcre_free = pcre_free ;
61
+ old_pcre_malloc = pcre_malloc ;
62
+ old_pcre_free = pcre_free ;
49
63
50
- pcre_malloc = ngx_http_lua_pcre_malloc ;
51
- pcre_free = ngx_http_lua_pcre_free ;
64
+ pcre_malloc = ngx_http_lua_pcre_malloc ;
65
+ pcre_free = ngx_http_lua_pcre_free ;
66
+
67
+ return NULL ;
68
+ }
69
+
70
+ dd ("lua pcre pool was %p" , ngx_http_lua_pcre_pool );
71
+
72
+ old_pool = ngx_http_lua_pcre_pool ;
73
+ ngx_http_lua_pcre_pool = pool ;
74
+
75
+ dd ("lua pcre pool is %p" , ngx_http_lua_pcre_pool );
76
+
77
+ return old_pool ;
52
78
}
53
79
54
80
55
81
void
56
- ngx_http_lua_pcre_malloc_done ()
82
+ ngx_http_lua_pcre_malloc_done (ngx_pool_t * old_pool )
57
83
{
58
- ngx_http_lua_pcre_pool = NULL ;
84
+ dd ("lua pcre pool was %p" , ngx_http_lua_pcre_pool );
85
+
86
+ ngx_http_lua_pcre_pool = old_pool ;
87
+
88
+ dd ("lua pcre pool is %p" , ngx_http_lua_pcre_pool );
59
89
60
- pcre_malloc = old_pcre_malloc ;
61
- pcre_free = old_pcre_free ;
90
+ if (old_pool == NULL ) {
91
+ pcre_malloc = old_pcre_malloc ;
92
+ pcre_free = old_pcre_free ;
93
+ }
62
94
}
63
95
64
96
#endif /* NGX_PCRE */
0 commit comments