xref: /PHP-8.0/ext/mysqlnd/mysqlnd_block_alloc.c (revision 5d6e923d)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.php.net/license/3_01.txt                                  |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Andrey Hristov <andrey@php.net>                             |
14   |          Ulf Wendel <uw@php.net>                                     |
15   |          Dmitry Stogov <dmitry@php.net>                              |
16   +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 #include "mysqlnd.h"
21 #include "mysqlnd_block_alloc.h"
22 #include "mysqlnd_debug.h"
23 #include "mysqlnd_priv.h"
24 
25 /* {{{ mysqlnd_mempool_free_chunk */
26 static void
mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL * pool,void * ptr)27 mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL * pool, void * ptr)
28 {
29 	DBG_ENTER("mysqlnd_mempool_free_chunk");
30 	/* Try to back-off and guess if this is the last block allocated */
31 #ifndef ZEND_TRACK_ARENA_ALLOC
32 	if (ptr == pool->last) {
33 		/*
34 			This was the last allocation. Lucky us, we can free
35 			a bit of memory from the pool. Next time we will return from the same ptr.
36 		*/
37 		pool->arena->ptr = (char*)ptr;
38 		pool->last = NULL;
39 	}
40 #endif
41 	DBG_VOID_RETURN;
42 }
43 /* }}} */
44 
45 
46 /* {{{ mysqlnd_mempool_resize_chunk */
47 static void *
mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL * pool,void * ptr,size_t old_size,size_t size)48 mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL * pool, void * ptr, size_t old_size, size_t size)
49 {
50 	DBG_ENTER("mysqlnd_mempool_resize_chunk");
51 
52 #ifndef ZEND_TRACK_ARENA_ALLOC
53 	/* Try to back-off and guess if this is the last block allocated */
54 	if (ptr == pool->last
55 	  && (ZEND_MM_ALIGNED_SIZE(size) <= ((char*)pool->arena->end - (char*)ptr))) {
56 		/*
57 			This was the last allocation. Lucky us, we can free
58 			a bit of memory from the pool. Next time we will return from the same ptr.
59 		*/
60 		pool->arena->ptr = (char*)ptr + ZEND_MM_ALIGNED_SIZE(size);
61 		DBG_RETURN(ptr);
62 	}
63 #endif
64 
65 	void *new_ptr = zend_arena_alloc(&pool->arena, size);
66 	memcpy(new_ptr, ptr, MIN(old_size, size));
67 	pool->last = ptr = new_ptr;
68 	DBG_RETURN(ptr);
69 }
70 /* }}} */
71 
72 
73 /* {{{ mysqlnd_mempool_get_chunk */
74 static void *
mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool,size_t size)75 mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool, size_t size)
76 {
77 	void *ptr = NULL;
78 	DBG_ENTER("mysqlnd_mempool_get_chunk");
79 
80 	ptr = zend_arena_alloc(&pool->arena, size);
81 	pool->last = ptr;
82 
83 	DBG_RETURN(ptr);
84 }
85 /* }}} */
86 
87 
88 /* {{{ mysqlnd_mempool_create */
89 PHPAPI MYSQLND_MEMORY_POOL *
mysqlnd_mempool_create(size_t arena_size)90 mysqlnd_mempool_create(size_t arena_size)
91 {
92 	zend_arena * arena;
93 	MYSQLND_MEMORY_POOL * ret;
94 
95 	DBG_ENTER("mysqlnd_mempool_create");
96 	arena = zend_arena_create(MAX(arena_size, ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))));
97 	ret = zend_arena_alloc(&arena, sizeof(MYSQLND_MEMORY_POOL));
98 	ret->arena = arena;
99 	ret->last = NULL;
100 	ret->checkpoint = NULL;
101 	ret->get_chunk = mysqlnd_mempool_get_chunk;
102 	ret->free_chunk = mysqlnd_mempool_free_chunk;
103 	ret->resize_chunk = mysqlnd_mempool_resize_chunk;
104 	DBG_RETURN(ret);
105 }
106 /* }}} */
107 
108 
109 /* {{{ mysqlnd_mempool_destroy */
110 PHPAPI void
mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)111 mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)
112 {
113 	DBG_ENTER("mysqlnd_mempool_destroy");
114 	/* mnd_free will reference LOCK_access and might crash, depending on the caller...*/
115 	zend_arena_destroy(pool->arena);
116 	DBG_VOID_RETURN;
117 }
118 /* }}} */
119 
120 /* {{{ mysqlnd_mempool_save_state */
121 PHPAPI void
mysqlnd_mempool_save_state(MYSQLND_MEMORY_POOL * pool)122 mysqlnd_mempool_save_state(MYSQLND_MEMORY_POOL * pool)
123 {
124 	DBG_ENTER("mysqlnd_mempool_save_state");
125 	pool->checkpoint = zend_arena_checkpoint(pool->arena);
126 	DBG_VOID_RETURN;
127 }
128 /* }}} */
129 
130 /* {{{ mysqlnd_mempool_restore_state */
131 PHPAPI void
mysqlnd_mempool_restore_state(MYSQLND_MEMORY_POOL * pool)132 mysqlnd_mempool_restore_state(MYSQLND_MEMORY_POOL * pool)
133 {
134 	DBG_ENTER("mysqlnd_mempool_restore_state");
135 #if ZEND_DEBUG
136 	ZEND_ASSERT(pool->checkpoint);
137 #endif
138 	if (pool->checkpoint) {
139 		zend_arena_release(&pool->arena, pool->checkpoint);
140 		pool->last = NULL;
141 		pool->checkpoint = NULL;
142 	}
143 	DBG_VOID_RETURN;
144 }
145 /* }}} */
146