Change the way the allocator is called - through an abstraction

to give the possibility of pluggability. Thanks to the macros
already used the changes are not pervasive.
This commit is contained in:
Andrey Hristov 2010-04-22 16:28:50 +00:00
parent 5431f235db
commit 4842f96e2d
2 changed files with 216 additions and 51 deletions

View file

@ -28,6 +28,59 @@
#include "mysqlnd_statistics.h"
#include "zend_builtin_functions.h"
static void * mysqlnd_zend_mm_emalloc(size_t size MYSQLND_MEM_D);
static void * mysqlnd_zend_mm_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D);
static void * mysqlnd_zend_mm_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D);
static void * mysqlnd_zend_mm_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D);
static void * mysqlnd_zend_mm_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D);
static void * mysqlnd_zend_mm_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D);
static void mysqlnd_zend_mm_efree(void * ptr MYSQLND_MEM_D);
static void mysqlnd_zend_mm_pefree(void * ptr, zend_bool persistent MYSQLND_MEM_D);
static void * mysqlnd_zend_mm_malloc(size_t size MYSQLND_MEM_D);
static void * mysqlnd_zend_mm_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D);
static void * mysqlnd_zend_mm_realloc(void * ptr, size_t new_size MYSQLND_MEM_D);
static void mysqlnd_zend_mm_free(void * ptr MYSQLND_MEM_D);
static char * mysqlnd_zend_mm_pestrndup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D);
static char * mysqlnd_zend_mm_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
#define MYSQLND_DEBUG_MEMORY 1
struct st_mysqlnd_allocator_methods mysqlnd_allocator =
{
#if MYSQLND_DEBUG_MEMORY
_mysqlnd_emalloc,
_mysqlnd_pemalloc,
_mysqlnd_ecalloc,
_mysqlnd_pecalloc,
_mysqlnd_erealloc,
_mysqlnd_perealloc,
_mysqlnd_efree,
_mysqlnd_pefree,
_mysqlnd_malloc,
_mysqlnd_calloc,
_mysqlnd_realloc,
_mysqlnd_free,
_mysqlnd_pestrndup,
_mysqlnd_pestrdup
#else
mysqlnd_zend_mm_emalloc,
mysqlnd_zend_mm_pemalloc,
mysqlnd_zend_mm_ecalloc,
mysqlnd_zend_mm_pecalloc,
mysqlnd_zend_mm_erealloc,
mysqlnd_zend_mm_perealloc,
mysqlnd_zend_mm_efree,
mysqlnd_zend_mm_pefree,
mysqlnd_zend_mm_malloc,
mysqlnd_zend_mm_calloc,
mysqlnd_zend_mm_realloc,
mysqlnd_zend_mm_free,
mysqlnd_zend_mm_pestrndup,
mysqlnd_zend_mm_pestrdup
#endif
};
static const char * const mysqlnd_debug_default_trace_file = "/tmp/mysqlnd.trace";
@ -994,6 +1047,118 @@ char * _mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_ME
/* }}} */
/* {{{ mysqlnd_zend_mm_emalloc */
static void * mysqlnd_zend_mm_emalloc(size_t size MYSQLND_MEM_D)
{
return emalloc(size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_pemalloc */
static void * mysqlnd_zend_mm_pemalloc(size_t size, zend_bool persistent MYSQLND_MEM_D)
{
return pemalloc(size, persistent);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_ecalloc */
static void * mysqlnd_zend_mm_ecalloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
{
return ecalloc(nmemb, size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_pecalloc */
static void * mysqlnd_zend_mm_pecalloc(unsigned int nmemb, size_t size, zend_bool persistent MYSQLND_MEM_D)
{
return pecalloc(nmemb, size, persistent);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_erealloc */
static void * mysqlnd_zend_mm_erealloc(void *ptr, size_t new_size MYSQLND_MEM_D)
{
return erealloc(ptr, new_size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_perealloc */
static void * mysqlnd_zend_mm_perealloc(void *ptr, size_t new_size, zend_bool persistent MYSQLND_MEM_D)
{
return perealloc(ptr, new_size, persistent);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_efree */
static void mysqlnd_zend_mm_efree(void * ptr MYSQLND_MEM_D)
{
efree(ptr);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_pefree */
static void mysqlnd_zend_mm_pefree(void * ptr, zend_bool persistent MYSQLND_MEM_D)
{
pefree(ptr, persistent);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_malloc */
static void * mysqlnd_zend_mm_malloc(size_t size MYSQLND_MEM_D)
{
return malloc(size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_calloc */
static void * mysqlnd_zend_mm_calloc(unsigned int nmemb, size_t size MYSQLND_MEM_D)
{
return calloc(nmemb, size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_realloc */
static void * mysqlnd_zend_mm_realloc(void * ptr, size_t new_size MYSQLND_MEM_D)
{
return realloc(ptr, new_size);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_free */
static void mysqlnd_zend_mm_free(void * ptr MYSQLND_MEM_D)
{
free(ptr);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_pestrndup */
static char * mysqlnd_zend_mm_pestrndup(const char * const ptr, size_t length, zend_bool persistent MYSQLND_MEM_D)
{
return pestrndup(ptr, length, persistent);
}
/* }}} */
/* {{{ mysqlnd_zend_mm_pestrdup */
static char * mysqlnd_zend_mm_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D)
{
return pestrdup(ptr, persistent);
}
/* }}} */
/* Follows code borrowed from zend_builtin_functions.c because the functions there are static */