24
24
#include "mysqlnd_debug.h"
25
25
#include "mysqlnd_priv.h"
26
26
27
-
28
- /* {{{ mysqlnd_arena_create */
29
- static zend_always_inline zend_arena * mysqlnd_arena_create (size_t size )
30
- {
31
- zend_arena * arena = (zend_arena * )mnd_emalloc (size );
32
-
33
- arena -> ptr = (char * ) arena + ZEND_MM_ALIGNED_SIZE (sizeof (zend_arena ));
34
- arena -> end = (char * ) arena + size ;
35
- arena -> prev = NULL ;
36
- return arena ;
37
- }
38
- /* }}} */
39
-
40
- /* {{{ mysqlnd_arena_destroy */
41
- static zend_always_inline void mysqlnd_arena_destroy (zend_arena * arena )
42
- {
43
- do {
44
- zend_arena * prev = arena -> prev ;
45
- mnd_efree (arena );
46
- arena = prev ;
47
- } while (arena );
48
- }
49
- /* }}} */
50
-
51
- /* {{{ mysqlnd_arena_alloc */
52
- static zend_always_inline void * mysqlnd_arena_alloc (zend_arena * * arena_ptr , size_t size )
53
- {
54
- zend_arena * arena = * arena_ptr ;
55
- char * ptr = arena -> ptr ;
56
-
57
- size = ZEND_MM_ALIGNED_SIZE (size );
58
-
59
- if (EXPECTED (size <= (size_t )(arena -> end - ptr ))) {
60
- arena -> ptr = ptr + size ;
61
- } else {
62
- size_t arena_size =
63
- UNEXPECTED ((size + ZEND_MM_ALIGNED_SIZE (sizeof (zend_arena ))) > (size_t )(arena -> end - (char * ) arena )) ?
64
- (size + ZEND_MM_ALIGNED_SIZE (sizeof (zend_arena ))) :
65
- (size_t )(arena -> end - (char * ) arena );
66
- zend_arena * new_arena = (zend_arena * )mnd_emalloc (arena_size );
67
-
68
- ptr = (char * ) new_arena + ZEND_MM_ALIGNED_SIZE (sizeof (zend_arena ));
69
- new_arena -> ptr = (char * ) new_arena + ZEND_MM_ALIGNED_SIZE (sizeof (zend_arena )) + size ;
70
- new_arena -> end = (char * ) new_arena + arena_size ;
71
- new_arena -> prev = arena ;
72
- * arena_ptr = new_arena ;
73
- }
74
-
75
- return (void * ) ptr ;
76
- }
77
- /* }}} */
78
-
79
- static zend_always_inline void * mysqlnd_arena_checkpoint (zend_arena * arena )
80
- {
81
- return arena -> ptr ;
82
- }
83
-
84
- static zend_always_inline void mysqlnd_arena_release (zend_arena * * arena_ptr , void * checkpoint )
85
- {
86
- zend_arena * arena = * arena_ptr ;
87
-
88
- while (UNEXPECTED ((char * )checkpoint > arena -> end ) ||
89
- UNEXPECTED ((char * )checkpoint <= (char * )arena )) {
90
- zend_arena * prev = arena -> prev ;
91
- mnd_efree (arena );
92
- * arena_ptr = arena = prev ;
93
- }
94
- ZEND_ASSERT ((char * )checkpoint > (char * )arena && (char * )checkpoint <= arena -> end );
95
- arena -> ptr = (char * )checkpoint ;
96
- }
97
-
98
27
/* {{{ mysqlnd_mempool_free_chunk */
99
28
static void
100
29
mysqlnd_mempool_free_chunk (MYSQLND_MEMORY_POOL * pool , void * ptr )
101
30
{
102
31
DBG_ENTER ("mysqlnd_mempool_free_chunk" );
103
32
/* Try to back-off and guess if this is the last block allocated */
33
+ #ifndef ZEND_TRACK_ARENA_ALLOC
104
34
if (ptr == pool -> last ) {
105
35
/*
106
36
This was the last allocation. Lucky us, we can free
@@ -109,6 +39,7 @@ mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL * pool, void * ptr)
109
39
pool -> arena -> ptr = (char * )ptr ;
110
40
pool -> last = NULL ;
111
41
}
42
+ #endif
112
43
DBG_VOID_RETURN ;
113
44
}
114
45
/* }}} */
@@ -120,6 +51,7 @@ mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL * pool, void * ptr, size_t old_
120
51
{
121
52
DBG_ENTER ("mysqlnd_mempool_resize_chunk" );
122
53
54
+ #ifndef ZEND_TRACK_ARENA_ALLOC
123
55
/* Try to back-off and guess if this is the last block allocated */
124
56
if (ptr == pool -> last
125
57
&& (ZEND_MM_ALIGNED_SIZE (size ) <= ((char * )pool -> arena -> end - (char * )ptr ))) {
@@ -128,11 +60,13 @@ mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL * pool, void * ptr, size_t old_
128
60
a bit of memory from the pool. Next time we will return from the same ptr.
129
61
*/
130
62
pool -> arena -> ptr = (char * )ptr + ZEND_MM_ALIGNED_SIZE (size );
131
- } else {
132
- void * new_ptr = mysqlnd_arena_alloc (& pool -> arena , size );
133
- memcpy (new_ptr , ptr , MIN (old_size , size ));
134
- pool -> last = ptr = new_ptr ;
63
+ DBG_RETURN (ptr );
135
64
}
65
+ #endif
66
+
67
+ void * new_ptr = zend_arena_alloc (& pool -> arena , size );
68
+ memcpy (new_ptr , ptr , MIN (old_size , size ));
69
+ pool -> last = ptr = new_ptr ;
136
70
DBG_RETURN (ptr );
137
71
}
138
72
/* }}} */
@@ -145,7 +79,7 @@ mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool, size_t size)
145
79
void * ptr = NULL ;
146
80
DBG_ENTER ("mysqlnd_mempool_get_chunk" );
147
81
148
- ptr = mysqlnd_arena_alloc (& pool -> arena , size );
82
+ ptr = zend_arena_alloc (& pool -> arena , size );
149
83
pool -> last = ptr ;
150
84
151
85
DBG_RETURN (ptr );
@@ -161,8 +95,8 @@ mysqlnd_mempool_create(size_t arena_size)
161
95
MYSQLND_MEMORY_POOL * ret ;
162
96
163
97
DBG_ENTER ("mysqlnd_mempool_create" );
164
- arena = mysqlnd_arena_create (MAX (arena_size , sizeof (zend_arena )));
165
- ret = mysqlnd_arena_alloc (& arena , sizeof (MYSQLND_MEMORY_POOL ));
98
+ arena = zend_arena_create (MAX (arena_size , sizeof (zend_arena )));
99
+ ret = zend_arena_alloc (& arena , sizeof (MYSQLND_MEMORY_POOL ));
166
100
ret -> arena = arena ;
167
101
ret -> last = NULL ;
168
102
ret -> checkpoint = NULL ;
@@ -180,7 +114,7 @@ mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)
180
114
{
181
115
DBG_ENTER ("mysqlnd_mempool_destroy" );
182
116
/* mnd_free will reference LOCK_access and might crash, depending on the caller...*/
183
- mysqlnd_arena_destroy (pool -> arena );
117
+ zend_arena_destroy (pool -> arena );
184
118
DBG_VOID_RETURN ;
185
119
}
186
120
/* }}} */
@@ -190,7 +124,7 @@ PHPAPI void
190
124
mysqlnd_mempool_save_state (MYSQLND_MEMORY_POOL * pool )
191
125
{
192
126
DBG_ENTER ("mysqlnd_mempool_save_state" );
193
- pool -> checkpoint = mysqlnd_arena_checkpoint (pool -> arena );
127
+ pool -> checkpoint = zend_arena_checkpoint (pool -> arena );
194
128
DBG_VOID_RETURN ;
195
129
}
196
130
/* }}} */
@@ -201,7 +135,7 @@ mysqlnd_mempool_restore_state(MYSQLND_MEMORY_POOL * pool)
201
135
{
202
136
DBG_ENTER ("mysqlnd_mempool_restore_state" );
203
137
if (pool -> checkpoint ) {
204
- mysqlnd_arena_release (& pool -> arena , pool -> checkpoint );
138
+ zend_arena_release (& pool -> arena , pool -> checkpoint );
205
139
pool -> last = NULL ;
206
140
pool -> checkpoint = NULL ;
207
141
}
0 commit comments