mirror of
https://github.com/MariaDB/server.git
synced 2025-01-22 14:54:20 +01:00
83d416fa09
{{{ svn merge -r3061:3225 https://svn.tokutek.com/tokudb/tokudb.558 }}} Fixes #630. Addresses #558. git-svn-id: file:///svn/tokudb@3226 c7de825b-a66e-492c-adef-691d508d4ae1
58 lines
1.3 KiB
C
58 lines
1.3 KiB
C
/* An implementation of memory that can be made to return NULL and ENOMEM on certain mallocs. */
|
|
#include "memory.h"
|
|
#include "toku_assert.h"
|
|
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
|
|
int *toku_dead_mallocs=0;
|
|
int toku_malloc_counter=0;
|
|
|
|
void toku_malloc_cleanup(void) {}
|
|
void toku_free(void*x) {
|
|
free(x);
|
|
}
|
|
|
|
// if it fails, return 1, and set errno
|
|
static int does_malloc_fail (void) {
|
|
if (toku_dead_mallocs) {
|
|
int mnum = *toku_dead_mallocs;
|
|
if (mnum==toku_malloc_counter) {
|
|
toku_malloc_counter++;
|
|
toku_dead_mallocs++;
|
|
errno=ENOMEM;
|
|
return 1;
|
|
}
|
|
}
|
|
toku_malloc_counter++;
|
|
return 0;
|
|
}
|
|
|
|
void* toku_malloc(size_t n) {
|
|
if (does_malloc_fail()) return 0;
|
|
return malloc(n);
|
|
}
|
|
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_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_realloc(void *p, size_t size) {
|
|
if (p==0) return toku_malloc(size);
|
|
if (does_malloc_fail()) return 0;
|
|
return realloc(p, size);
|
|
}
|