Commit 0adc9f9b authored by Bradley C. Kuszmaul's avatar Bradley C. Kuszmaul Committed by Yoni Fogel

Separate out the common malloc into newbrt/memory.c, and put the os-specific...

Separate out the common malloc into newbrt/memory.c, and put the os-specific stuff into windows and linux subdirs.  Addresses #1343.

git-svn-id: file:///svn/toku/tokudb.1032b+1343@8558 c7de825b-a66e-492c-adef-691d508d4ae1
parent f66b523f
...@@ -96,6 +96,10 @@ void *realloc(void*, size_t) __attribute__((__deprecated__)); ...@@ -96,6 +96,10 @@ void *realloc(void*, size_t) __attribute__((__deprecated__));
# endif # endif
#endif #endif
void *os_malloc(size_t);
void *os_realloc(void*,size_t);
void os_free(void*);
#if defined __cplusplus #if defined __cplusplus
}; };
#endif #endif
......
...@@ -2,120 +2,22 @@ ...@@ -2,120 +2,22 @@
#ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved." #ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
#include "toku_portability.h" #include "toku_portability.h"
#include "memory.h"
#include <assert.h>
#include <string.h>
#include <malloc.h> #include <malloc.h>
#include <errno.h>
int toku_memory_check=0;
int toku_calloc_counter = 0;
int toku_malloc_counter = 0;
int toku_realloc_counter = 0;
int toku_free_counter = 0;
typedef void *(*malloc_fun_t)(size_t);
typedef void (*free_fun_t)(void*);
typedef void *(*realloc_fun_t)(void*,size_t);
static malloc_fun_t t_malloc = 0;
static free_fun_t t_free = 0;
static realloc_fun_t t_realloc = 0;
void *toku_malloc(size_t size) { void *
toku_malloc_counter++; os_malloc(size_t size)
if (t_malloc) {
return t_malloc(size);
else
return malloc(size); return malloc(size);
} }
void *
void *toku_calloc(size_t nmemb, size_t size) { os_realloc(void *p, size_t size)
toku_calloc_counter++; {
return toku_malloc(nmemb*size);
}
void *toku_xmalloc(size_t size) {
void *r = toku_malloc(size);
if (r==0) abort();
return r;
}
void *toku_xrealloc(void *v, size_t size) {
void *r = toku_realloc(v, size);
if (r==0) abort();
return r;
}
void *toku_tagmalloc(size_t size, enum typ_tag typtag) {
//printf("%s:%d tagmalloc\n", __FILE__, __LINE__);
void *r = toku_malloc(size);
if (!r) return 0;
assert(size>sizeof(int));
((int*)r)[0] = typtag;
return r;
}
void *toku_realloc(void *p, size_t size) {
toku_realloc_counter++;
if (t_realloc)
return t_realloc(p, size);
else
return realloc(p, size); return realloc(p, size);
} }
void toku_free(void* p) { void
toku_free_counter++; os_free(void* p)
if (t_free) {
t_free(p);
else
free(p); free(p);
} }
void toku_free_n(void* p, size_t size __attribute__((unused))) {
toku_free(p);
}
void *toku_memdup (const void *v, size_t len) {
void *r=toku_malloc(len);
memcpy(r,v,len);
return r;
}
char *toku_strdup (const char *s) {
return toku_memdup(s, strlen(s)+1);
}
void toku_memory_check_all_free (void) {
}
int toku_get_n_items_malloced (void) { return 0; }
void toku_print_malloced_items (void) {
}
void toku_malloc_report (void) {
}
void toku_malloc_cleanup (void) {
}
int
toku_set_func_malloc(malloc_fun_t f) {
t_malloc = f;
return 0;
}
int
toku_set_func_realloc(realloc_fun_t f) {
t_realloc = f;
return 0;
}
int
toku_set_func_free(free_fun_t f) {
t_free = f;
return 0;
}
void *toku_malloc(size_t size) {
toku_malloc_counter++;
if (t_malloc)
return t_malloc(size);
else
return os_malloc(size);
}
void *
toku_calloc(size_t nmemb, size_t size)
{
size_t newsize = nmemb * size;
toku_calloc_counter++;
void *vp = toku_malloc(newsize);
if (vp) memset(vp, 0, newsize);
return vp;
}
void *
toku_xmalloc(size_t size) {
void *r = toku_malloc(size);
if (r==0) abort();
return r;
}
void *
toku_xrealloc(void *v, size_t size)
{
void *r = toku_realloc(v, size);
if (r==0) abort();
return r;
}
void *
toku_tagmalloc(size_t size, enum typ_tag typtag)
{
//printf("%s:%d tagmalloc\n", __FILE__, __LINE__);
void *r = toku_malloc(size);
if (!r) return 0;
assert(size>sizeof(int));
((int*)r)[0] = typtag;
return r;
}
void *
toku_realloc(void *p, size_t size)
{
toku_realloc_counter++;
if (t_realloc)
return t_realloc(p, size);
else
return os_realloc(p, size);
}
void
toku_free(void* p)
{
toku_free_counter++;
if (t_free)
t_free(p);
else
os_free(p);
}
void
toku_free_n(void* p, size_t size __attribute__((unused)))
{
toku_free(p);
}
void *
toku_memdup (const void *v, size_t len)
{
void *r=toku_malloc(len);
if (r) memcpy(r,v,len);
return r;
}
char *
toku_strdup (const char *s)
{
return toku_memdup(s, strlen(s)+1);
}
void
toku_memory_check_all_free (void)
{
}
int
toku_get_n_items_malloced (void)
{
return 0;
}
void
toku_print_malloced_items (void)
{
}
void
toku_malloc_report (void)
{
}
void
toku_malloc_cleanup (void)
{
}
int
toku_set_func_malloc(malloc_fun_t f) {
t_malloc = f;
return 0;
}
int
toku_set_func_realloc(realloc_fun_t f) {
t_realloc = f;
return 0;
}
int
toku_set_func_free(free_fun_t f) {
t_free = f;
return 0;
}
...@@ -17,6 +17,14 @@ int toku_malloc_counter = 0; ...@@ -17,6 +17,14 @@ int toku_malloc_counter = 0;
int toku_realloc_counter = 0; int toku_realloc_counter = 0;
int toku_free_counter = 0; int toku_free_counter = 0;
typedef void *(*malloc_fun_t)(size_t);
typedef void (*free_fun_t)(void*);
typedef void *(*realloc_fun_t)(void*,size_t);
static malloc_fun_t t_malloc = 0;
static free_fun_t t_free = 0;
static realloc_fun_t t_realloc = 0;
static inline size_t resize(size_t n) { static inline size_t resize(size_t n) {
if (n >= 1*1024*1024) if (n >= 1*1024*1024)
n = (n+7) & ~7; // round up to make windbg !heap happy n = (n+7) & ~7; // round up to make windbg !heap happy
...@@ -43,69 +51,20 @@ static inline size_t resize(size_t n) { ...@@ -43,69 +51,20 @@ static inline size_t resize(size_t n) {
return n; return n;
} }
void *toku_calloc(size_t nmemb, size_t size) { void *
void *vp; os_malloc(size_t size)
size_t newsize = resize(nmemb * size); {
toku_calloc_counter++;
vp = malloc(newsize);
if (vp)
memset(vp, 0, newsize);
return vp;
}
void *toku_malloc(size_t size) {
toku_malloc_counter++;
return malloc(resize(size)); return malloc(resize(size));
} }
void *toku_xmalloc(size_t size) { void *
void *r = toku_malloc(size); os_realloc(void *p, size_t size)
if (r==0) abort(); {
return r;
}
void *toku_tagmalloc(size_t size, enum typ_tag typtag) {
//printf("%s:%d tagmalloc\n", __FILE__, __LINE__);
void *r = toku_malloc(size);
if (!r) return 0;
assert(size>sizeof(int));
((int*)r)[0] = typtag;
return r;
}
void *toku_realloc(void *p, size_t size) {
toku_realloc_counter++;
return realloc(p, resize(size)); return realloc(p, resize(size));
} }
void toku_free(void* p) { void
toku_free_counter++; os_free(void* p)
{
free(p); free(p);
} }
void toku_free_n(void* p, size_t size __attribute__((unused))) {
toku_free(p);
}
void *toku_memdup (const void *v, size_t len) {
void *r=toku_malloc(len);
memcpy(r,v,len);
return r;
}
char *toku_strdup (const char *s) {
return toku_memdup(s, strlen(s)+1);
}
void toku_memory_check_all_free (void) {
}
int toku_get_n_items_malloced (void) { return 0; }
void toku_print_malloced_items (void) {
}
void toku_malloc_report (void) {
}
void toku_malloc_cleanup (void) {
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment