xref: /PHP-7.2/ext/mysqlnd/mysqlnd_block_alloc.c (revision 7a7ec01a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2006-2018 The PHP Group                                |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Andrey Hristov <andrey@php.net>                             |
16   |          Ulf Wendel <uw@php.net>                                     |
17   +----------------------------------------------------------------------+
18 */
19 
20 #include "php.h"
21 #include "mysqlnd.h"
22 #include "mysqlnd_block_alloc.h"
23 #include "mysqlnd_debug.h"
24 #include "mysqlnd_priv.h"
25 
26 
27 /* {{{ mysqlnd_mempool_free_chunk */
28 static void
mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL * pool,MYSQLND_MEMORY_POOL_CHUNK * chunk)29 mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL * pool, MYSQLND_MEMORY_POOL_CHUNK * chunk)
30 {
31 	DBG_ENTER("mysqlnd_mempool_free_chunk");
32 	if (chunk->from_pool) {
33 		/* Try to back-off and guess if this is the last block allocated */
34 		if (chunk->ptr == (pool->arena + (pool->arena_size - pool->free_size - chunk->size))) {
35 			/*
36 				This was the last allocation. Lucky us, we can free
37 				a bit of memory from the pool. Next time we will return from the same ptr.
38 			*/
39 			pool->free_size += chunk->size;
40 		}
41 	} else {
42 		mnd_efree(chunk->ptr);
43 	}
44 	mnd_efree(chunk);
45 	DBG_VOID_RETURN;
46 }
47 /* }}} */
48 
49 
50 /* {{{ mysqlnd_mempool_resize_chunk */
51 static enum_func_status
mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL * pool,MYSQLND_MEMORY_POOL_CHUNK * chunk,unsigned int size)52 mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL * pool, MYSQLND_MEMORY_POOL_CHUNK * chunk, unsigned int size)
53 {
54 	DBG_ENTER("mysqlnd_mempool_resize_chunk");
55 	if (chunk->from_pool) {
56 		/* Try to back-off and guess if this is the last block allocated */
57 		if (chunk->ptr == (pool->arena + (pool->arena_size - pool->free_size - chunk->size))) {
58 			/*
59 				This was the last allocation. Lucky us, we can free
60 				a bit of memory from the pool. Next time we will return from the same ptr.
61 			*/
62 			if ((chunk->size + pool->free_size) < size) {
63 				zend_uchar *new_ptr;
64 				new_ptr = mnd_emalloc(size);
65 				if (!new_ptr) {
66 					DBG_RETURN(FAIL);
67 				}
68 				memcpy(new_ptr, chunk->ptr, chunk->size);
69 				chunk->ptr = new_ptr;
70 				pool->free_size += chunk->size;
71 				chunk->size = size;
72 				chunk->from_pool = FALSE; /* now we have no pool memory */
73 			} else {
74 				/* If the chunk is > than asked size then free_memory increases, otherwise decreases*/
75 				pool->free_size += (chunk->size - size);
76 			}
77 		} else {
78 			/* Not last chunk, if the user asks for less, give it to him */
79 			if (chunk->size >= size) {
80 				; /* nop */
81 			} else {
82 				zend_uchar *new_ptr;
83 				new_ptr = mnd_emalloc(size);
84 				if (!new_ptr) {
85 					DBG_RETURN(FAIL);
86 				}
87 				memcpy(new_ptr, chunk->ptr, chunk->size);
88 				chunk->ptr = new_ptr;
89 				chunk->size = size;
90 				chunk->from_pool = FALSE; /* now we have non-pool memory */
91 			}
92 		}
93 	} else {
94 		zend_uchar *new_ptr = mnd_erealloc(chunk->ptr, size);
95 		if (!new_ptr) {
96 			DBG_RETURN(FAIL);
97 		}
98 		chunk->ptr = new_ptr;
99 	}
100 	DBG_RETURN(PASS);
101 }
102 /* }}} */
103 
104 
105 /* {{{ mysqlnd_mempool_get_chunk */
106 static
mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool,unsigned int size)107 MYSQLND_MEMORY_POOL_CHUNK * mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool, unsigned int size)
108 {
109 	MYSQLND_MEMORY_POOL_CHUNK *chunk = NULL;
110 	DBG_ENTER("mysqlnd_mempool_get_chunk");
111 
112 	chunk = mnd_emalloc(sizeof(MYSQLND_MEMORY_POOL_CHUNK));
113 	if (chunk) {
114 		chunk->size = size;
115 		/*
116 		  Should not go over MYSQLND_MAX_PACKET_SIZE, since we
117 		  expect non-arena memory in mysqlnd_wireprotocol.c . We
118 		  realloc the non-arena memory.
119 		*/
120 		if (size > pool->free_size) {
121 			chunk->from_pool = FALSE;
122 			chunk->ptr = mnd_emalloc(size);
123 			if (!chunk->ptr) {
124 				pool->free_chunk(pool, chunk);
125 				chunk = NULL;
126 			}
127 		} else {
128 			chunk->from_pool = TRUE;
129 			chunk->ptr = pool->arena + (pool->arena_size - pool->free_size);
130 			/* Last step, update free_size */
131 			pool->free_size -= size;
132 		}
133 	}
134 	DBG_RETURN(chunk);
135 }
136 /* }}} */
137 
138 
139 /* {{{ mysqlnd_mempool_create */
140 PHPAPI MYSQLND_MEMORY_POOL *
mysqlnd_mempool_create(size_t arena_size)141 mysqlnd_mempool_create(size_t arena_size)
142 {
143 	/* We calloc, because we free(). We don't mnd_calloc()  for a reason. */
144 	MYSQLND_MEMORY_POOL * ret = mnd_ecalloc(1, sizeof(MYSQLND_MEMORY_POOL));
145 	DBG_ENTER("mysqlnd_mempool_create");
146 	if (ret) {
147 		ret->get_chunk = mysqlnd_mempool_get_chunk;
148 		ret->free_chunk = mysqlnd_mempool_free_chunk;
149 		ret->resize_chunk = mysqlnd_mempool_resize_chunk;
150 		ret->free_size = ret->arena_size = arena_size ? arena_size : 0;
151 		/* OOM ? */
152 		ret->arena = mnd_emalloc(ret->arena_size);
153 		if (!ret->arena) {
154 			mysqlnd_mempool_destroy(ret);
155 			ret = NULL;
156 		}
157 	}
158 	DBG_RETURN(ret);
159 }
160 /* }}} */
161 
162 
163 /* {{{ mysqlnd_mempool_destroy */
164 PHPAPI void
mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)165 mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)
166 {
167 	DBG_ENTER("mysqlnd_mempool_destroy");
168 	/* mnd_free will reference LOCK_access and might crash, depending on the caller...*/
169 	mnd_efree(pool->arena);
170 	mnd_efree(pool);
171 	DBG_VOID_RETURN;
172 }
173 /* }}} */
174 
175 
176 /*
177  * Local variables:
178  * tab-width: 4
179  * c-basic-offset: 4
180  * End:
181  * vim600: noet sw=4 ts=4 fdm=marker
182  * vim<600: noet sw=4 ts=4
183  */
184