27
27
#include "mysqlnd_statistics.h"
28
28
#include "mysqlnd_charset.h"
29
29
#include "mysqlnd_debug.h"
30
+ #include "mysqlnd_block_alloc.h"
30
31
#include "php_ini.h"
31
32
#include "ext/standard/basic_functions.h"
32
33
#include "ext/standard/php_lcg.h"
@@ -66,7 +67,6 @@ const char * mysqlnd_out_of_sync = "Commands out of sync; you can't run this com
66
67
MYSQLND_STATS * mysqlnd_global_stats = NULL ;
67
68
static zend_bool mysqlnd_library_initted = FALSE;
68
69
69
- MYSQLND_MEMORY_POOL mysqlnd_memory_pool ;
70
70
71
71
static enum_func_status mysqlnd_send_close (MYSQLND * conn TSRMLS_DC );
72
72
@@ -127,140 +127,6 @@ void * _mysqlnd_fetch_thread(void *arg)
127
127
/* }}} */
128
128
#endif /* MYSQLND_THREADED */
129
129
130
- /************************************************************************************************/
131
- /* Let's don't use pool allocation for now */
132
- /* {{{ mysqlnd_mempool_free_chunk */
133
- static
134
- void mysqlnd_mempool_free_contents (MYSQLND_MEMORY_POOL * pool TSRMLS_DC )
135
- {
136
- DBG_ENTER ("mysqlnd_mempool_dtor" );
137
- uint i ;
138
- for (i = 0 ; i < pool -> free_chunk_list_elements ; i ++ ) {
139
- MYSQLND_MEMORY_POOL_CHUNK * chunk = pool -> free_chunk_list [i ];
140
- chunk -> free_chunk (chunk , FALSE TSRMLS_CC );
141
- }
142
-
143
- DBG_VOID_RETURN ;
144
- }
145
- /* }}} */
146
-
147
- /* Let's don't use pool allocation for now */
148
- /* {{{ mysqlnd_mempool_free_chunk */
149
- static
150
- void mysqlnd_mempool_free_chunk (MYSQLND_MEMORY_POOL_CHUNK * chunk , zend_bool cache_it TSRMLS_DC )
151
- {
152
- DBG_ENTER ("mysqlnd_mempool_free_chunk" );
153
- MYSQLND_MEMORY_POOL * pool = chunk -> pool ;
154
- if (chunk -> from_pool ) {
155
- /* Try to back-off and guess if this is the last block allocated */
156
- if (chunk -> ptr == (pool -> arena + (pool -> arena_size - pool -> free_size - chunk -> size ))) {
157
- /*
158
- This was the last allocation. Lucky us, we can free
159
- a bit of memory from the pool. Next time we will return from the same ptr.
160
- */
161
- pool -> free_size += chunk -> size ;
162
- }
163
- pool -> refcount -- ;
164
- } else {
165
- mnd_free (chunk -> ptr );
166
- }
167
- if (cache_it && pool -> free_chunk_list_elements < MYSQLND_MEMORY_POOL_CHUNK_LIST_SIZE ) {
168
- chunk -> ptr = NULL ;
169
- pool -> free_chunk_list [pool -> free_chunk_list_elements ++ ] = chunk ;
170
- } else {
171
- /* We did not cache it -> free it */
172
- mnd_free (chunk );
173
- }
174
- DBG_VOID_RETURN ;
175
- }
176
- /* }}} */
177
-
178
-
179
- /* {{{ mysqlnd_mempool_resize_chunk */
180
- static void
181
- mysqlnd_mempool_resize_chunk (MYSQLND_MEMORY_POOL_CHUNK * chunk , uint size TSRMLS_DC )
182
- {
183
- DBG_ENTER ("mysqlnd_mempool_resize_chunk" );
184
- if (chunk -> from_pool ) {
185
- MYSQLND_MEMORY_POOL * pool = chunk -> pool ;
186
- /* Try to back-off and guess if this is the last block allocated */
187
- if (chunk -> ptr == (pool -> arena + (pool -> arena_size - pool -> free_size - chunk -> size ))) {
188
- /*
189
- This was the last allocation. Lucky us, we can free
190
- a bit of memory from the pool. Next time we will return from the same ptr.
191
- */
192
- if ((chunk -> size + pool -> free_size ) < size ) {
193
- zend_uchar * new_ptr ;
194
- new_ptr = mnd_malloc (size );
195
- memcpy (new_ptr , chunk -> ptr , chunk -> size );
196
- chunk -> ptr = new_ptr ;
197
- pool -> free_size += chunk -> size ;
198
- chunk -> size = size ;
199
- chunk -> pool = NULL ; /* now we have no pool memory */
200
- pool -> refcount -- ;
201
- } else {
202
- /* If the chunk is > than asked size then free_memory increases, otherwise decreases*/
203
- pool -> free_size += (chunk -> size - size );
204
- }
205
- } else {
206
- /* Not last chunk, if the user asks for less, give it to him */
207
- if (chunk -> size >= size ) {
208
- ; /* nop */
209
- } else {
210
- zend_uchar * new_ptr ;
211
- new_ptr = mnd_malloc (size );
212
- memcpy (new_ptr , chunk -> ptr , chunk -> size );
213
- chunk -> ptr = new_ptr ;
214
- chunk -> size = size ;
215
- chunk -> pool = NULL ; /* now we have no pool memory */
216
- pool -> refcount -- ;
217
- }
218
- }
219
- } else {
220
- chunk -> ptr = mnd_realloc (chunk -> ptr , size );
221
- }
222
- DBG_VOID_RETURN ;
223
- }
224
- /* }}} */
225
-
226
-
227
- /* {{{ mysqlnd_mempool_get_chunk */
228
- static
229
- MYSQLND_MEMORY_POOL_CHUNK * mysqlnd_mempool_get_chunk (MYSQLND_MEMORY_POOL * pool , uint size TSRMLS_DC )
230
- {
231
- MYSQLND_MEMORY_POOL_CHUNK * chunk = NULL ;
232
- DBG_ENTER ("mysqlnd_mempool_get_chunk" );
233
-
234
- if (pool -> free_chunk_list_elements ) {
235
- chunk = pool -> free_chunk_list [-- pool -> free_chunk_list_elements ];
236
- } else {
237
- chunk = mnd_malloc (sizeof (MYSQLND_MEMORY_POOL_CHUNK ));
238
- }
239
-
240
- chunk -> free_chunk = mysqlnd_mempool_free_chunk ;
241
- chunk -> resize_chunk = mysqlnd_mempool_resize_chunk ;
242
- chunk -> size = size ;
243
- /*
244
- Should not go over MYSQLND_MAX_PACKET_SIZE, since we
245
- expect non-arena memory in mysqlnd_wireprotocol.c . We
246
- realloc the non-arena memory.
247
- */
248
- chunk -> pool = pool ;
249
- if (size > pool -> free_size ) {
250
- chunk -> ptr = mnd_malloc (size );
251
- chunk -> from_pool = FALSE;
252
- } else {
253
- chunk -> from_pool = TRUE;
254
- ++ pool -> refcount ;
255
- chunk -> ptr = pool -> arena + (pool -> arena_size - pool -> free_size );
256
- /* Last step, update free_size */
257
- pool -> free_size -= size ;
258
- }
259
- DBG_RETURN (chunk );
260
- }
261
- /* }}} */
262
- /************************************************************************************************/
263
-
264
130
265
131
/* {{{ mysqlnd_library_init */
266
132
static
@@ -274,13 +140,6 @@ void mysqlnd_library_init(TSRMLS_D)
274
140
#ifdef ZTS
275
141
mysqlnd_global_stats -> LOCK_access = tsrm_mutex_alloc ();
276
142
#endif
277
- mysqlnd_memory_pool .arena_size = 16000 ;
278
- mysqlnd_memory_pool .free_size = mysqlnd_memory_pool .arena_size ;
279
- mysqlnd_memory_pool .refcount = 0 ;
280
- /* OOM ? */
281
- mysqlnd_memory_pool .arena = mnd_malloc (mysqlnd_memory_pool .arena_size );
282
- mysqlnd_memory_pool .get_chunk = mysqlnd_mempool_get_chunk ;
283
- mysqlnd_memory_pool .free_contents = mysqlnd_mempool_free_contents ;
284
143
}
285
144
}
286
145
/* }}} */
@@ -291,9 +150,6 @@ static
291
150
void mysqlnd_library_end (TSRMLS_D )
292
151
{
293
152
if (mysqlnd_library_initted == TRUE) {
294
- /* mnd_free will reference LOCK_access and won't crash...*/
295
- mysqlnd_memory_pool .free_contents (& mysqlnd_memory_pool TSRMLS_CC );
296
- free (mysqlnd_memory_pool .arena );
297
153
#ifdef ZTS
298
154
tsrm_mutex_free (mysqlnd_global_stats -> LOCK_access );
299
155
#endif
@@ -422,6 +278,10 @@ MYSQLND_METHOD(mysqlnd_conn, free_contents)(MYSQLND *conn TSRMLS_DC)
422
278
mysqlnd_palloc_free_thd_cache_reference (& conn -> zval_cache );
423
279
conn -> zval_cache = NULL ;
424
280
}
281
+ if (conn -> result_set_memory_pool ) {
282
+ mysqlnd_mempool_destroy (conn -> result_set_memory_pool TSRMLS_CC );
283
+ conn -> result_set_memory_pool = NULL ;
284
+ }
425
285
if (conn -> qcache ) {
426
286
DBG_INF ("Freeing qcache reference" );
427
287
mysqlnd_qcache_free_cache_reference (& conn -> qcache );
@@ -971,6 +831,7 @@ PHPAPI MYSQLND *mysqlnd_connect(MYSQLND *conn,
971
831
}
972
832
973
833
DBG_INF_FMT ("connection_id=%llu" , conn -> thread_id );
834
+ conn -> result_set_memory_pool = mysqlnd_mempool_create (16000 TSRMLS_CC );
974
835
#if PHP_MAJOR_VERSION >= 6
975
836
{
976
837
uint as_unicode = 1 ;
0 commit comments