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 +----------------------------------------------------------------------+
18 */
19
20 #ifndef MYSQLND_ALLOC_H
21 #define MYSQLND_ALLOC_H
22
23 PHPAPI extern const char * mysqlnd_debug_std_no_trace_funcs[];
24
25 #define MYSQLND_MEM_D ZEND_FILE_LINE_DC
26 #define MYSQLND_MEM_C ZEND_FILE_LINE_CC
27
28 struct st_mysqlnd_allocator_methods
29 {
30 void * (*m_emalloc)(size_t size MYSQLND_MEM_D);
31 void * (*m_pemalloc)(size_t size, zend_bool persistent MYSQLND_MEM_D);
32 void * (*m_ecalloc)(unsigned int nmemb, size_t size MYSQLND_MEM_D);
33 void * (*m_pecalloc)(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D);
34 void * (*m_erealloc)(void *ptr, size_t new_size MYSQLND_MEM_D);
35 void * (*m_perealloc)(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D);
36 void (*m_efree)(void *ptr MYSQLND_MEM_D);
37 void (*m_pefree)(void *ptr, zend_bool persistent MYSQLND_MEM_D);
38 void * (*m_malloc)(size_t size MYSQLND_MEM_D);
39 void * (*m_calloc)(unsigned int nmemb, size_t size MYSQLND_MEM_D);
40 void * (*m_realloc)(void *ptr, size_t new_size MYSQLND_MEM_D);
41 void (*m_free)(void *ptr MYSQLND_MEM_D);
42 char * (*m_pememdup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
43 char * (*m_pestrndup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
44 char * (*m_pestrdup)(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
45 int (*m_sprintf)(char **pbuf, size_t max_len, const char *format, ...);
46 int (*m_vsprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
47 void (*m_sprintf_free)(char * p);
48 };
49
50 PHPAPI extern struct st_mysqlnd_allocator_methods mysqlnd_allocator;
51
52 #define mnd_emalloc(size) mysqlnd_allocator.m_emalloc((size) MYSQLND_MEM_C)
53 #define mnd_pemalloc(size, pers) mysqlnd_allocator.m_pemalloc((size), (pers) MYSQLND_MEM_C)
54 #define mnd_ecalloc(nmemb, size) mysqlnd_allocator.m_ecalloc((nmemb), (size) MYSQLND_MEM_C)
55 #define mnd_pecalloc(nmemb, size, p) mysqlnd_allocator.m_pecalloc((nmemb), (size), (p) MYSQLND_MEM_C)
56 #define mnd_erealloc(ptr, new_size) mysqlnd_allocator.m_erealloc((ptr), (new_size) MYSQLND_MEM_C)
57 #define mnd_perealloc(ptr, new_size, p) mysqlnd_allocator.m_perealloc((ptr), (new_size), (p) MYSQLND_MEM_C)
58 #define mnd_efree(ptr) mysqlnd_allocator.m_efree((ptr) MYSQLND_MEM_C)
59 #define mnd_pefree(ptr, pers) mysqlnd_allocator.m_pefree((ptr), (pers) MYSQLND_MEM_C)
60 #define mnd_malloc(size) mysqlnd_allocator.m_malloc((size) MYSQLND_MEM_C)
61 #define mnd_calloc(nmemb, size) mysqlnd_allocator.m_calloc((nmemb), (size) MYSQLND_MEM_C)
62 #define mnd_realloc(ptr, new_size) mysqlnd_allocator.m_realloc((ptr), (new_size) MYSQLND_MEM_C)
63 #define mnd_free(ptr) mysqlnd_allocator.m_free((ptr) MYSQLND_MEM_C)
64 #define mnd_pememdup(ptr, size, pers) mysqlnd_allocator.m_pememdup((ptr), (size), (pers) MYSQLND_MEM_C)
65 #define mnd_pestrndup(ptr, size, pers) mysqlnd_allocator.m_pestrndup((ptr), (size), (pers) MYSQLND_MEM_C)
66 #define mnd_pestrdup(ptr, pers) mysqlnd_allocator.m_pestrdup((ptr), (pers) MYSQLND_MEM_C)
67 #define mnd_sprintf(p, mx_len, fmt,...) mysqlnd_allocator.m_sprintf((p), (mx_len), (fmt), __VA_ARGS__)
68 #define mnd_vsprintf(p, mx_len, fmt,ap) mysqlnd_allocator.m_vsprintf((p), (mx_len), (fmt), (ap))
69 #define mnd_sprintf_free(p) mysqlnd_allocator.m_sprintf_free((p))
70
mnd_dup_cstring(const MYSQLND_CSTRING str,const zend_bool persistent)71 static inline MYSQLND_STRING mnd_dup_cstring(const MYSQLND_CSTRING str, const zend_bool persistent)
72 {
73 const MYSQLND_STRING ret = {(char*) mnd_pemalloc(str.l + 1, persistent), str.l};
74 if (ret.s) {
75 memcpy(ret.s, str.s, str.l);
76 ret.s[str.l] = '\0';
77 }
78 return ret;
79 }
80
mnd_str2c(const MYSQLND_STRING str)81 static inline MYSQLND_CSTRING mnd_str2c(const MYSQLND_STRING str)
82 {
83 const MYSQLND_CSTRING ret = {str.s, str.l};
84 return ret;
85 }
86
87 #endif /* MYSQLND_ALLOC_H */
88