xref: /PHP-8.1/ext/mysqlnd/mysqlnd_block_alloc.c (revision 01b3fc03)
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   | https://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_get_chunk */
26 static void *
mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool,size_t size)27 mysqlnd_mempool_get_chunk(MYSQLND_MEMORY_POOL * pool, size_t size)
28 {
29 	DBG_ENTER("mysqlnd_mempool_get_chunk");
30 	DBG_RETURN(zend_arena_alloc(&pool->arena, size));
31 }
32 /* }}} */
33 
34 
35 /* {{{ mysqlnd_mempool_create */
36 PHPAPI MYSQLND_MEMORY_POOL *
mysqlnd_mempool_create(size_t arena_size)37 mysqlnd_mempool_create(size_t arena_size)
38 {
39 	zend_arena * arena;
40 	MYSQLND_MEMORY_POOL * ret;
41 
42 	DBG_ENTER("mysqlnd_mempool_create");
43 	arena = zend_arena_create(MAX(arena_size, ZEND_MM_ALIGNED_SIZE(sizeof(zend_arena))));
44 	ret = zend_arena_alloc(&arena, sizeof(MYSQLND_MEMORY_POOL));
45 	ret->arena = arena;
46 	ret->checkpoint = NULL;
47 	ret->get_chunk = mysqlnd_mempool_get_chunk;
48 	DBG_RETURN(ret);
49 }
50 /* }}} */
51 
52 
53 /* {{{ mysqlnd_mempool_destroy */
54 PHPAPI void
mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)55 mysqlnd_mempool_destroy(MYSQLND_MEMORY_POOL * pool)
56 {
57 	DBG_ENTER("mysqlnd_mempool_destroy");
58 	/* mnd_free will reference LOCK_access and might crash, depending on the caller...*/
59 	zend_arena_destroy(pool->arena);
60 	DBG_VOID_RETURN;
61 }
62 /* }}} */
63 
64 /* {{{ mysqlnd_mempool_save_state */
65 PHPAPI void
mysqlnd_mempool_save_state(MYSQLND_MEMORY_POOL * pool)66 mysqlnd_mempool_save_state(MYSQLND_MEMORY_POOL * pool)
67 {
68 	DBG_ENTER("mysqlnd_mempool_save_state");
69 	pool->checkpoint = zend_arena_checkpoint(pool->arena);
70 	DBG_VOID_RETURN;
71 }
72 /* }}} */
73 
74 /* {{{ mysqlnd_mempool_restore_state */
75 PHPAPI void
mysqlnd_mempool_restore_state(MYSQLND_MEMORY_POOL * pool)76 mysqlnd_mempool_restore_state(MYSQLND_MEMORY_POOL * pool)
77 {
78 	DBG_ENTER("mysqlnd_mempool_restore_state");
79 #if ZEND_DEBUG
80 	ZEND_ASSERT(pool->checkpoint);
81 #endif
82 	if (pool->checkpoint) {
83 		zend_arena_release(&pool->arena, pool->checkpoint);
84 		pool->checkpoint = NULL;
85 	}
86 	DBG_VOID_RETURN;
87 }
88 /* }}} */
89