xref: /PHP-7.4/ext/mysqlnd/mysqlnd_block_alloc.c (revision df982da5)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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   |          Dmitry Stogov <dmitry@php.net>                              |
18   +----------------------------------------------------------------------+
19 */
20 
21 #include "php.h"
22 #include "mysqlnd.h"
23 #include "mysqlnd_block_alloc.h"
24 #include "mysqlnd_debug.h"
25 #include "mysqlnd_priv.h"
26 
27 /* {{{ mysqlnd_mempool_free_chunk */
28 static void
mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL * pool,void * ptr)29 mysqlnd_mempool_free_chunk(MYSQLND_MEMORY_POOL * pool, void * ptr)
30 {
31 	DBG_ENTER("mysqlnd_mempool_free_chunk");
32 	/* Try to back-off and guess if this is the last block allocated */
33 #ifndef ZEND_TRACK_ARENA_ALLOC
34 	if (ptr == pool->last) {
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->arena->ptr = (char*)ptr;
40 		pool->last = NULL;
41 	}
42 #endif
43 	DBG_VOID_RETURN;
44 }
45 /* }}} */
46 
47 
48 /* {{{ mysqlnd_mempool_resize_chunk */
49 static void *
mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL * pool,void * ptr,size_t old_size,size_t size)50 mysqlnd_mempool_resize_chunk(MYSQLND_MEMORY_POOL * pool, void * ptr, size_t old_size, size_t size)
51 {
52 	DBG_ENTER("mysqlnd_mempool_resize_chunk");
53 
54 #ifndef ZEND_TRACK_ARENA_ALLOC
55 	/* Try to back-off and guess if this is the last block allocated */
56 	if (ptr == pool->last
57 	  && (ZEND_MM_ALIGNED_SIZE(size) <= ((char*)pool->arena->end - (char*)ptr))) {
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 		pool->arena->ptr = (char*)ptr + ZEND_MM_ALIGNED_SIZE(size);
63 		DBG_RETURN(ptr);
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;
70 	DBG_RETURN(ptr);
71 }
72 /* }}} */
73 
74 
75 /* {{{ mysqlnd_mempool_get_chunk */
76 static void *
mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool,size_t size)77 mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool, size_t size)
78 {
79 	void *ptr = NULL;
80 	DBG_ENTER("mysqlnd_mempool_get_chunk");
81 
82 	ptr = zend_arena_alloc(&pool->arena, size);
83 	pool->last = ptr;
84 
85 	DBG_RETURN(ptr);
86 }
87 /* }}} */
88 
89 
90 /* {{{ mysqlnd_mempool_create */
91 PHPAPI MYSQLND_MEMORY_POOL *
mysqlnd_mempool_create(size_t arena_size)92 mysqlnd_mempool_create(size_t arena_size)
93 {
94 	zend_arena * arena;
95 	MYSQLND_MEMORY_POOL * ret;
96 
97 	DBG_ENTER("mysqlnd_mempool_create");
98 	arena = zend_arena_create(MAX(arena_size, ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))));
99 	ret = zend_arena_alloc(&arena, sizeof(MYSQLND_MEMORY_POOL));
100 	ret->arena = arena;
101 	ret->last = NULL;
102 	ret->checkpoint = NULL;
103 	ret->get_chunk = mysqlnd_mempool_get_chunk;
104 	ret->free_chunk = mysqlnd_mempool_free_chunk;
105 	ret->resize_chunk = mysqlnd_mempool_resize_chunk;
106 	DBG_RETURN(ret);
107 }
108 /* }}} */
109 
110 
111 /* {{{ mysqlnd_mempool_destroy */
112 PHPAPI void
mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)113 mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)
114 {
115 	DBG_ENTER("mysqlnd_mempool_destroy");
116 	/* mnd_free will reference LOCK_access and might crash, depending on the caller...*/
117 	zend_arena_destroy(pool->arena);
118 	DBG_VOID_RETURN;
119 }
120 /* }}} */
121 
122 /* {{{ mysqlnd_mempool_save_state */
123 PHPAPI void
mysqlnd_mempool_save_state(MYSQLND_MEMORY_POOL * pool)124 mysqlnd_mempool_save_state(MYSQLND_MEMORY_POOL * pool)
125 {
126 	DBG_ENTER("mysqlnd_mempool_save_state");
127 	pool->checkpoint = zend_arena_checkpoint(pool->arena);
128 	DBG_VOID_RETURN;
129 }
130 /* }}} */
131 
132 /* {{{ mysqlnd_mempool_restore_state */
133 PHPAPI void
mysqlnd_mempool_restore_state(MYSQLND_MEMORY_POOL * pool)134 mysqlnd_mempool_restore_state(MYSQLND_MEMORY_POOL * pool)
135 {
136 	DBG_ENTER("mysqlnd_mempool_restore_state");
137 #if ZEND_DEBUG
138 	ZEND_ASSERT(pool->checkpoint);
139 #endif
140 	if (pool->checkpoint) {
141 		zend_arena_release(&pool->arena, pool->checkpoint);
142 		pool->last = NULL;
143 		pool->checkpoint = NULL;
144 	}
145 	DBG_VOID_RETURN;
146 }
147 /* }}} */
148