Commit 3bba4f46 authored by Bradley C. Kuszmaul's avatar Bradley C. Kuszmaul

Make a malloc that reuses things (if you tell it the size of the free'd thing)

git-svn-id: file:///svn/tokudb@94 c7de825b-a66e-492c-adef-691d508d4ae1
parent f94ef8fc
...@@ -98,6 +98,7 @@ int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__un ...@@ -98,6 +98,7 @@ int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__un
printf("Shutdown %.6fs\n", tdiff(&t3, &t2)); printf("Shutdown %.6fs\n", tdiff(&t3, &t2));
printf("Total time %.6fs for %lld insertions = %8.0f/s\n", tdiff(&t3, &t1), 2*total_n_items, 2*total_n_items/tdiff(&t3, &t1)); printf("Total time %.6fs for %lld insertions = %8.0f/s\n", tdiff(&t3, &t1), 2*total_n_items, 2*total_n_items/tdiff(&t3, &t1));
malloc_report(); malloc_report();
malloc_cleanup();
return 0; return 0;
} }
...@@ -73,5 +73,6 @@ void test_serialize(void) { ...@@ -73,5 +73,6 @@ void test_serialize(void) {
int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) { int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
memory_check = 1; memory_check = 1;
test_serialize(); test_serialize();
malloc_cleanup();
return 0; return 0;
} }
...@@ -875,6 +875,7 @@ static void brt_blackbox_test (void) { ...@@ -875,6 +875,7 @@ static void brt_blackbox_test (void) {
int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) { int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
brt_blackbox_test(); brt_blackbox_test();
malloc_cleanup();
printf("ok\n"); printf("ok\n");
return 0; return 0;
} }
...@@ -279,6 +279,7 @@ int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__un ...@@ -279,6 +279,7 @@ int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__un
test0(); test0();
test_nested_pin(); test_nested_pin();
test_multi_filehandles (); test_multi_filehandles ();
malloc_cleanup();
printf("ok\n"); printf("ok\n");
return 0; return 0;
} }
#include "key.h" #include "key.h"
#include "hashtable.h" #include "hashtable.h"
#include "memory.h"
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
...@@ -137,5 +138,6 @@ void test1(void) { ...@@ -137,5 +138,6 @@ void test1(void) {
int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) { int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
test0(); test0();
test1(); test1();
malloc_cleanup();
return 0; return 0;
} }
...@@ -24,7 +24,7 @@ static inline struct kv_pair *kv_pair_malloc(void *key, int keylen, void *val, i ...@@ -24,7 +24,7 @@ static inline struct kv_pair *kv_pair_malloc(void *key, int keylen, void *val, i
} }
static inline void kv_pair_free(struct kv_pair *pair) { static inline void kv_pair_free(struct kv_pair *pair) {
toku_free(pair); toku_free_n(pair, sizeof(struct kv_pair)+pair->keylen+pair->vallen);
} }
static inline void *kv_pair_key(struct kv_pair *pair) { static inline void *kv_pair_key(struct kv_pair *pair) {
......
...@@ -134,14 +134,26 @@ void *toku_calloc(long nmemb, long size) { ...@@ -134,14 +134,26 @@ void *toku_calloc(long nmemb, long size) {
//if ((long)r==0x80523f8) { printf("%s:%d %p\n", __FILE__, __LINE__, r); } //if ((long)r==0x80523f8) { printf("%s:%d %p\n", __FILE__, __LINE__, r); }
return r; return r;
} }
#define FREELIST_LIMIT 33
void *freelist[FREELIST_LIMIT];
#define MALLOC_SIZE_COUNTING_LIMIT 256 #define MALLOC_SIZE_COUNTING_LIMIT 256
int malloc_counts[MALLOC_SIZE_COUNTING_LIMIT]; int malloc_counts[MALLOC_SIZE_COUNTING_LIMIT];
int other_malloc_count=0; int other_malloc_count=0;
void *toku_malloc(long size) { void *toku_malloc(unsigned long size) {
void * r; void * r;
errno=0; errno=0;
if (size<MALLOC_SIZE_COUNTING_LIMIT) malloc_counts[size]++; if (size<MALLOC_SIZE_COUNTING_LIMIT) malloc_counts[size]++;
else other_malloc_count++; else other_malloc_count++;
if (size>=sizeof(void*) && size<FREELIST_LIMIT) {
if (freelist[size]) {
r = freelist[size];
freelist[size] = *(void**)r;
note_did_malloc(r, size);
return r;
}
}
r=actual_malloc(size); r=actual_malloc(size);
//printf("%s:%d malloc(%ld)->%p\n", __FILE__, __LINE__, size,r); //printf("%s:%d malloc(%ld)->%p\n", __FILE__, __LINE__, size,r);
note_did_malloc(r, size); note_did_malloc(r, size);
...@@ -172,6 +184,18 @@ void toku_free(void* p) { ...@@ -172,6 +184,18 @@ void toku_free(void* p) {
actual_free(p); actual_free(p);
} }
void toku_free_n(void* p, unsigned long size) {
//printf("%s:%d free(%p)\n", __FILE__, __LINE__, p);
note_did_free(p);
if (size>=sizeof(void*) && size<FREELIST_LIMIT) {
//printf("freelist[%lu] ||= %p\n", size, p);
*(void**)p = freelist[size];
freelist[size]=p;
} else {
actual_free(p);
}
}
void *memdup (const void *v, unsigned int len) { void *memdup (const void *v, unsigned int len) {
void *r=toku_malloc(len); void *r=toku_malloc(len);
memcpy(r,v,len); memcpy(r,v,len);
...@@ -206,3 +230,14 @@ void malloc_report (void) { ...@@ -206,3 +230,14 @@ void malloc_report (void) {
} }
printf("Other: %d\n", other_malloc_count); printf("Other: %d\n", other_malloc_count);
} }
void malloc_cleanup (void) {
int i;
for (i=0; i<FREELIST_LIMIT; i++) {
void *p;
while ((p = freelist[i])) {
freelist[i] = *(void**)p;
actual_free(p);
}
}
}
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
/* errno is set to 0 or a value to indicate problems. */ /* errno is set to 0 or a value to indicate problems. */
void *toku_calloc(long nmemb, long size); void *toku_calloc(long nmemb, long size);
void *toku_malloc(long size); void *toku_malloc(unsigned long size);
void *tagmalloc(unsigned long size, int typ); void *tagmalloc(unsigned long size, int typ);
void toku_free(void*); void toku_free(void*);
void toku_free_n(void*, unsigned long size);
void *toku_realloc(void *, long size); void *toku_realloc(void *, long size);
#define MALLOC(v) v = toku_malloc(sizeof(*v)) #define MALLOC(v) v = toku_malloc(sizeof(*v))
...@@ -23,3 +24,4 @@ extern int memory_check; // Set to nonzero to get a (much) slower version of mal ...@@ -23,3 +24,4 @@ extern int memory_check; // Set to nonzero to get a (much) slower version of mal
int get_n_items_malloced(void); int get_n_items_malloced(void);
void print_malloced_items(void); void print_malloced_items(void);
void malloc_report (void); void malloc_report (void);
void malloc_cleanup (void);
...@@ -1021,5 +1021,6 @@ void pma_tests (void) { ...@@ -1021,5 +1021,6 @@ void pma_tests (void) {
int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) { int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
pma_tests(); pma_tests();
malloc_cleanup();
return 0; return 0;
} }
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