2013-04-16 23:59:09 -04:00
|
|
|
#ident "Copyright (c) 2007-2010 Tokutek Inc. All rights reserved."
|
|
|
|
|
2013-04-16 23:57:53 -04:00
|
|
|
#include <toku_portability.h>
|
2013-04-16 23:57:34 -04:00
|
|
|
#include "memory.h"
|
|
|
|
#include <string.h>
|
2013-04-16 23:59:09 -04:00
|
|
|
#include <stdio.h>
|
2013-04-16 23:57:34 -04:00
|
|
|
#include <stdlib.h>
|
2013-04-16 23:57:34 -04:00
|
|
|
#include "toku_assert.h"
|
2013-04-16 23:57:34 -04:00
|
|
|
|
|
|
|
int toku_memory_check=0;
|
|
|
|
|
|
|
|
static malloc_fun_t t_malloc = 0;
|
2013-04-16 23:59:38 -04:00
|
|
|
static malloc_fun_t t_xmalloc = 0;
|
2013-04-16 23:57:34 -04:00
|
|
|
static free_fun_t t_free = 0;
|
|
|
|
static realloc_fun_t t_realloc = 0;
|
2013-04-16 23:59:38 -04:00
|
|
|
static realloc_fun_t t_xrealloc = 0;
|
2013-04-16 23:57:34 -04:00
|
|
|
|
2013-04-16 23:57:34 -04:00
|
|
|
void *toku_malloc(size_t size) {
|
2013-04-16 23:59:38 -04:00
|
|
|
void *p = t_malloc ? t_malloc(size) : os_malloc(size);
|
2013-04-16 23:59:11 -04:00
|
|
|
return p;
|
2013-04-16 23:57:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_calloc(size_t nmemb, size_t size) {
|
2013-04-16 23:57:34 -04:00
|
|
|
size_t newsize = nmemb * size;
|
2013-04-16 23:59:38 -04:00
|
|
|
void *p = toku_malloc(newsize);
|
|
|
|
if (p) memset(p, 0, newsize);
|
|
|
|
return p;
|
2013-04-16 23:57:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_realloc(void *p, size_t size) {
|
|
|
|
void *q = t_realloc ? t_realloc(p, size) : os_realloc(p, size);
|
2013-04-16 23:59:11 -04:00
|
|
|
return q;
|
2013-04-16 23:57:34 -04:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:59:12 -04:00
|
|
|
void *
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_memdup(const void *v, size_t len) {
|
|
|
|
void *p = toku_malloc(len);
|
|
|
|
if (p) memcpy(p, v,len);
|
|
|
|
return p;
|
2013-04-16 23:59:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_strdup(const char *s) {
|
2013-04-16 23:59:12 -04:00
|
|
|
return toku_memdup(s, strlen(s)+1);
|
|
|
|
}
|
|
|
|
|
2013-04-16 23:57:34 -04:00
|
|
|
void
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_free(void *p) {
|
2013-04-16 23:57:34 -04:00
|
|
|
if (t_free)
|
|
|
|
t_free(p);
|
|
|
|
else
|
|
|
|
os_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_free_n(void* p, size_t size __attribute__((unused))) {
|
2013-04-16 23:57:34 -04:00
|
|
|
toku_free(p);
|
|
|
|
}
|
|
|
|
|
2013-04-16 23:58:04 -04:00
|
|
|
void *
|
2013-04-16 23:59:12 -04:00
|
|
|
toku_xmalloc(size_t size) {
|
2013-04-16 23:59:38 -04:00
|
|
|
void *p = t_xmalloc ? t_xmalloc(size) : os_malloc(size);
|
|
|
|
if (p == NULL) // avoid function call in common case
|
|
|
|
resource_assert(p);
|
|
|
|
return p;
|
2013-04-16 23:58:04 -04:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:57:34 -04:00
|
|
|
void *
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_xcalloc(size_t nmemb, size_t size) {
|
2013-04-16 23:59:12 -04:00
|
|
|
size_t newsize = nmemb * size;
|
|
|
|
void *vp = toku_xmalloc(newsize);
|
|
|
|
if (vp) memset(vp, 0, newsize);
|
|
|
|
return vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_xrealloc(void *v, size_t size) {
|
|
|
|
void *p = t_xrealloc ? t_xrealloc(v, size) : os_realloc(v, size);
|
|
|
|
if (p == 0) // avoid function call in common case
|
|
|
|
resource_assert(p);
|
|
|
|
return p;
|
2013-04-16 23:57:34 -04:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:59:12 -04:00
|
|
|
void *
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_xmemdup (const void *v, size_t len) {
|
|
|
|
void *p = toku_xmalloc(len);
|
|
|
|
memcpy(p, v, len);
|
|
|
|
return p;
|
2013-04-16 23:58:04 -04:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:57:34 -04:00
|
|
|
char *
|
2013-04-16 23:59:38 -04:00
|
|
|
toku_xstrdup (const char *s) {
|
2013-04-16 23:59:12 -04:00
|
|
|
return toku_xmemdup(s, strlen(s)+1);
|
2013-04-16 23:57:34 -04:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:59:38 -04:00
|
|
|
void
|
2013-04-16 23:57:34 -04:00
|
|
|
toku_set_func_malloc(malloc_fun_t f) {
|
|
|
|
t_malloc = f;
|
2013-04-16 23:59:38 -04:00
|
|
|
t_xmalloc = f;
|
2013-04-16 23:57:34 -04:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:59:38 -04:00
|
|
|
void
|
|
|
|
toku_set_func_xmalloc_only(malloc_fun_t f) {
|
|
|
|
t_xmalloc = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
toku_set_func_malloc_only(malloc_fun_t f) {
|
|
|
|
t_malloc = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-16 23:57:34 -04:00
|
|
|
toku_set_func_realloc(realloc_fun_t f) {
|
|
|
|
t_realloc = f;
|
2013-04-16 23:59:38 -04:00
|
|
|
t_xrealloc = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
toku_set_func_xrealloc_only(realloc_fun_t f) {
|
|
|
|
t_xrealloc = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
toku_set_func_realloc_only(realloc_fun_t f) {
|
|
|
|
t_realloc = f;
|
|
|
|
|
2013-04-16 23:57:34 -04:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:59:38 -04:00
|
|
|
void
|
2013-04-16 23:57:34 -04:00
|
|
|
toku_set_func_free(free_fun_t f) {
|
|
|
|
t_free = f;
|
|
|
|
}
|
2013-04-16 23:59:38 -04:00
|
|
|
|