void jemallocTest(){ void *p; #ifdef WIN32 // If you use jemalloc through the static lib, // must be manual initialize jemalloc first. je_init(); #endif p = je_malloc(128); if (p) { printf("malloc(%u) result ptr = 0x%016" PRIXPTR "\n\n", 128, (unsigned long)p); je_free(p); } p = je_malloc(256); if (p) { printf("malloc(%u) result ptr = 0x%016" PRIXPTR "\n\n", 256, (unsigned long)p); je_free(p); } #ifdef WIN32 // Unload the jemalloc je_uninit(); #endif }
int simple_test() { int* ary1 = (int*)je_malloc(sizeof(int) * ARRAY_SIZE); for (int i = 0; i < ARRAY_SIZE; i++) { ary1[i] = i; } int* ary2 = (int*)je_malloc(sizeof(int) * ARRAY_SIZE); for (int i = 0; i < ARRAY_SIZE; i++) { ary2[i] = i; } return false; }
ElTy *realloc() { #ifdef SPADES_USE_JEMALLOC // First, try to expand in-place if (storage_ && sizeof(ElTy) * capacity_ * el_sz_ > 4096 && je_rallocm((void**)&storage_, NULL, sizeof(ElTy) * capacity_ * el_sz_, 0, ALLOCM_NO_MOVE) == ALLOCM_SUCCESS) return storage_; // Failed, do usual malloc / memcpy / free cycle ElTy *res = (ElTy*) je_malloc(sizeof(ElTy) * capacity_ * el_sz_); if (storage_) std::memcpy(res, storage_, size_ * sizeof(ElTy) * el_sz_); je_free(storage_); storage_ = res; #else // No JEMalloc, no cookies ElTy *res = new ElTy[capacity_ * el_sz_]; if (storage_) std:: memcpy(res, storage_, size_ * sizeof(ElTy) * el_sz_); delete[] storage_; storage_ = res; #endif return storage_; }
void chpl_mem_layerInit(void) { void* heap_base_; size_t heap_size_; chpl_comm_desired_shared_heap(&heap_base_, &heap_size_); if (heap_base_ != NULL && heap_size_ == 0) { chpl_internal_error("if heap address is specified, size must be also"); } // If we have a shared heap, initialize our shared heap. This will take care // of initializing jemalloc. If we're not using a shared heap, do a first // allocation to allow jemaloc to set up: // // jemalloc 4.0.4 man: "Once, when the first call is made to one of the // memory allocation routines, the allocator initializes its internals" if (heap_base_ != NULL) { heap_base = heap_base_; heap_size = heap_size_; cur_heap_offset = 0; if (pthread_mutex_init(&chunk_alloc_lock, NULL) != 0) { chpl_internal_error("cannot init chunk_alloc lock"); } initializeSharedHeap(); } else { void* p; if ((p = je_malloc(1)) == NULL) { chpl_internal_error("cannot init heap: je_malloc() failed"); } je_free(p); } }
static unsigned int __stdcall memory_alloc_test_thread_func(void*) { allocs_info* info = new allocs_info; info->count = 0; info->memory = new char*[loop_count]; std::size_t count = 0; while (count < run_count) { for (std::size_t i = 0; i < loop_count; ++i) { std::size_t size = ::rand() % (8 * 1024); #ifdef LARGE_MEMORY size += 20 * 1024; #endif #if defined(CRT_LFH) || defined(TBBMALLOC) || defined(TCMALLOC) info->memory[i] = new char[size]; #elif defined(MS_CONCURRENCY) info->memory[i] = reinterpret_cast<char*>(Concurrency::Alloc(size)); #elif defined(MY_ALLOCATOR) info->memory[i] = reinterpret_cast<char*>(allocator.alloc(size)); #elif defined(JEMALLOC) info->memory[i] = reinterpret_cast<char*>(je_malloc(size)); #endif ++info->count; SecureZeroMemory(info->memory[i], size); //::memset( info->memory[i], 0xff, size ); } #ifdef OTHER_THREAD_FREE unsigned int id = 0; HANDLE free_thread = reinterpret_cast< HANDLE >(::_beginthreadex(nullptr, 0, memory_free_test_thread_func, info, 0, &id)); if (free_thread != nullptr) { ::WaitForSingleObject(free_thread, INFINITE); ::CloseHandle(free_thread); } else { free_all(info); } #else free_all(info); #endif ++count; } delete[] info->memory; info->memory = nullptr; delete info; return 0; }
/* * Like new, we want to guarantee that we NEVER * return NULL. Loop until there is free memory. * */ static char* malloc_never_null(const size_t b) { char *p = NULL; do { p = static_cast<char*>(je_malloc(b)); } while (p == NULL); return p; }
void* malloc_impl(size_t size) { if (MOZ_UNLIKELY(!replace_malloc_initialized)) init(); if (MOZ_LIKELY(!replace_malloc)) return je_malloc(size); return replace_malloc(size); }
void* operator new[] (size_t sz) { void* p1 = je_malloc(FULL_SIZE(sz)); if (p1) { memset(p1, 0, STUB_SIZE); fibjs::MemPool::global().add(p1, sz); } return MEM_PTR(p1); }
extern "C" void* wrap(malloc)(size_t sz) { void* p1 = je_malloc(FULL_SIZE(sz)); if (p1) { memset(p1, 0, STUB_SIZE); fibjs::MemPool::global().add(p1, sz); } return MEM_PTR(p1); }
void* fs_malloc(size_t len){ #ifdef JEMALLOC_HAVE_ATTR return je_malloc(len); #else return malloc(len); #endif }
void utString::fromBytes(const void *bytes, int len) { // Free old value if any. if(p != NULL) je_free(p); // Copy the bytes into p. p = (char*)je_malloc(len+1); memcpy(p, bytes, len); p[len] = 0; // Make sure we are NULL terminated. }
void * mem_alloc::alloc(uint64_t size) { void * ptr; #ifdef N_MALLOC ptr = malloc(size); #else ptr = je_malloc(size); #endif DEBUG_M("alloc %ld 0x%lx\n",size,(uint64_t)ptr); assert(ptr != NULL); return ptr; }
void* DefaultAllocator::Alloc(size_t size) { OVR_ALLOC_BENCHMARK_START(); #ifdef OVR_USE_JEMALLOC void* p = je_malloc(size); #else // OVR_USE_JEMALLOC void* p = malloc(size); #endif // OVR_USE_JEMALLOC OVR_ALLOC_BENCHMARK_END(); trackAlloc(p, size); return p; }
int main (void) { size_t i; char * ptrs[num]; MALLOC_INIT(); for (i = 0; i < num; i++) { size_t size = (rand() % (max_size - min_size)) + min_size; ptrs[i] = je_malloc(size); memset(ptrs[i], 1, size); } for (i = 0; i < num; i++) je_free(ptrs[i]); return 0; }
void Memory_Pool_Test_C() { int i; #ifdef _DEBUG const int nMaxLoop = 100000; const int nMaxAlloc = 16384; #else const int nMaxLoop = 1000000; const int nMaxAlloc = 16384; //const int nMaxAlloc = 10000; #endif unsigned int dwUsedTime1, dwUsedTime2; unsigned int dwStartTickCount = GetTickCount(); for (i = 0; i < nMaxLoop; i++) { char *p = (char *)malloc((i % nMaxAlloc) + 1); if (p) free(p); } dwUsedTime1 = GetTickCount() - dwStartTickCount; mem_pool_printf("Alloc Func : malloc() Alloc Size: 1-%d\n", nMaxAlloc); mem_pool_printf("Alloc Count: %d\n", nMaxLoop); mem_pool_printf("Total Cost : %d ms.\n", dwUsedTime1); //system("pause"); mem_pool_printf("\n"); set_errno(0); je_init(); dwStartTickCount = GetTickCount(); for (i = 0; i < nMaxLoop; i++) { void *p = je_malloc((i % nMaxAlloc) + 1); if (p) je_free(p); } je_uninit(); dwUsedTime2 = GetTickCount() - dwStartTickCount; mem_pool_printf("Alloc Func : je_malloc() Alloc Size: 1-%d\n", nMaxAlloc); mem_pool_printf("Alloc Count: %d\n", nMaxLoop); mem_pool_printf("Total Cost : %d ms.\t Speed up: %0.2f %%\n", dwUsedTime2, ((double)(dwUsedTime1 * 10000 / dwUsedTime2) / 100.0)); //system("pause"); //mem_pool_printf("\n"); }
static void useUpMemNotInHeap(void) { // grab (and leak) whatever memory jemalloc got on it's own, that's not in // our shared heap // // jemalloc 4.0.4 man: "arenas may have already created chunks prior to the // application having an opportunity to take over chunk allocation." // // Note that this will also allow jemalloc to initialize itself since char* p = NULL; do { // TODO use a larger value than size_t here (must be smaller than // "arenas.hchunk.0.size" though if ((p = je_malloc(sizeof(size_t))) == NULL) { chpl_internal_error("could not use up memory outside of shared heap"); } } while ((p != NULL && (p < (char*) heap_base || p > (char*) heap_base + heap_size))); }
void * _rmt_alloc(size_t size, const char *name, int line) { void *p; ASSERT(size != 0); #ifdef RMT_JEMALLOC p = je_malloc(size); #else p = malloc(size); #endif if (p == NULL) { log_error("ERROR: malloc(%zu) failed @ %s:%d", size, name, line); } else { log_debug(LOG_VVVERB, "malloc(%zu) at %p @ %s:%d", size, p, name, line); } return p; }
extern "C" void* wrap(realloc)(void* p, size_t sz) { fibjs::MemPool& mp = fibjs::MemPool::global(); if (p == 0) { void* p1 = je_malloc(FULL_SIZE(sz)); if (p1) { memset(p1, 0, STUB_SIZE); mp.add(p1, sz); } return MEM_PTR(p1); } if (sz == 0) { void* p1 = STUB_PTR(p); if (p1) mp.remove(p1); je_free(p1); return 0; } void* p1 = STUB_PTR(p); mp.remove(p1); void* p2 = je_realloc(p1, FULL_SIZE(sz)); if (p2) { memset(p2, 0, STUB_SIZE); mp.add(p2, sz); } else mp.add(p1); return MEM_PTR(p2); }
void chpl_mem_layerInit(void) { void* start; size_t size; chpl_comm_desired_shared_heap(&start, &size); // // TODO (EJR 12/17/15): add support for shared heaps. I think we basically // need to create a custom chunk allocator. // // HPX-5 did this so I think we can too: // http://jemalloc-discuss.canonware.narkive.com/FzSQ4Qv4/need-help-in-porting-jemalloc // // http://www.canonware.com/pipermail/jemalloc-discuss/2015-October/001179.html // // TODO (EJR 12/17/15): when we support shared heaps, I need to remember to // update the third-party README // if (start || size) chpl_error("set CHPL_MEM to a more appropriate mem type", 0, 0); // // Do a first allocation, to allow jemalloc to set up: // // """ // Once, when the first call is made to one of the memory allocation // routines, the allocator initializes its internals based in part on various // options that can be specified at compile- or run-time. // """ // { void* p; if ((p = je_malloc(1)) == NULL) chpl_internal_error("cannot init heap: je_malloc() failed"); je_free(p); } }
static int create_bandwidth_nodes(int num_bandwidth, const int *bandwidth, int *num_unique, struct bandwidth_nodes_t **bandwidth_nodes) { /*************************************************************************** * num_bandwidth (IN): * * number of numa nodes and length of bandwidth vector. * * bandwidth (IN): * * A vector of length num_bandwidth that gives bandwidth for * * each numa node, zero if numa node has unknown bandwidth. * * num_unique (OUT): * * number of unique non-zero bandwidth values in bandwidth * * vector. * * bandwidth_nodes (OUT): * * A list of length num_unique sorted by bandwidth value where * * each element gives a list of the numa nodes that have the * * given bandwidth. * * RETURNS zero on success, error code on failure * ***************************************************************************/ int err = 0; int i, j, k, l, last_bandwidth; struct numanode_bandwidth_t *numanode_bandwidth = NULL; *bandwidth_nodes = NULL; /* allocate space for sorting array */ numanode_bandwidth = je_malloc(sizeof(struct numanode_bandwidth_t) * num_bandwidth); if (!numanode_bandwidth) { err = MEMKIND_ERROR_MALLOC; } if (!err) { /* set sorting array */ j = 0; for (i = 0; i < num_bandwidth; ++i) { if (bandwidth[i] != 0) { numanode_bandwidth[j].numanode = i; numanode_bandwidth[j].bandwidth = bandwidth[i]; ++j; } } /* ignore zero bandwidths */ num_bandwidth = j; if (num_bandwidth == 0) { err = MEMKIND_ERROR_PMTT; } } if (!err) { qsort(numanode_bandwidth, num_bandwidth, sizeof(struct numanode_bandwidth_t), numanode_bandwidth_compare); /* calculate the number of unique bandwidths */ *num_unique = 1; last_bandwidth = numanode_bandwidth[0].bandwidth; for (i = 1; i < num_bandwidth; ++i) { if (numanode_bandwidth[i].bandwidth != last_bandwidth) { last_bandwidth = numanode_bandwidth[i].bandwidth; ++*num_unique; } } /* allocate output array */ *bandwidth_nodes = (struct bandwidth_nodes_t*)je_malloc( sizeof(struct bandwidth_nodes_t) **num_unique + sizeof(int) * num_bandwidth); if (!*bandwidth_nodes) { err = MEMKIND_ERROR_MALLOC; } } if (!err) { /* populate output */ (*bandwidth_nodes)[0].numanodes = (int*)(*bandwidth_nodes + *num_unique); last_bandwidth = numanode_bandwidth[0].bandwidth; k = 0; l = 0; for (i = 0; i < num_bandwidth; ++i, ++l) { (*bandwidth_nodes)[0].numanodes[i] = numanode_bandwidth[i].numanode; if (numanode_bandwidth[i].bandwidth != last_bandwidth) { (*bandwidth_nodes)[k].num_numanodes = l; (*bandwidth_nodes)[k].bandwidth = last_bandwidth; l = 0; ++k; (*bandwidth_nodes)[k].numanodes = (*bandwidth_nodes)[0].numanodes + i; last_bandwidth = numanode_bandwidth[i].bandwidth; } } (*bandwidth_nodes)[k].num_numanodes = l; (*bandwidth_nodes)[k].bandwidth = last_bandwidth; } if (numanode_bandwidth) { je_free(numanode_bandwidth); } if (err) { if (*bandwidth_nodes) { je_free(*bandwidth_nodes); } } return err; }
FORCE_INLINE void * je_malloc_test::Malloc( size_t size ) { return je_malloc(size); }
void * skynet_malloc(size_t size) { void* ptr = je_malloc(size + PREFIX_SIZE); if(!ptr) malloc_oom(size); return fill_prefix(ptr); }
int main(int argc,char * argv[]) { int i = 0, j = 0, rc; MDB_env *env; MDB_dbi dbi; MDB_val key, data; MDB_txn *txn; MDB_stat mst; MDB_cursor *cursor; int count; int *values; char sval[8]; char kval[sizeof(int)]; memset(sval, 0, sizeof(sval)); count = 510; values = (int *)je_malloc(count*sizeof(int)); for(i = 0;i<count;i++) { values[i] = i*5; } E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); E(mdb_env_open(env, "/tmp/testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_open(txn, "id4", MDB_CREATE|MDB_DUPSORT|MDB_DUPFIXED, &dbi)); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; printf("Adding %d values\n", count); strcpy(kval, "001"); for (i=0;i<count;i++) { sprintf(sval, "%07x", values[i]); if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA))) j++; } if (j) printf("%d duplicates skipped\n", j); E(mdb_txn_commit(txn)); E(mdb_env_stat(env, &mst)); /* there should be one full page of dups now. */ E(mdb_txn_begin(env, NULL, 1, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_txn_abort(txn); /* test all 3 branches of split code: * 1: new key in lower half * 2: new key at split point * 3: new key in upper half */ key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; sprintf(sval, "%07x", values[3]+1); E(mdb_txn_begin(env, NULL, 0, &txn)); (void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA)); mdb_txn_abort(txn); sprintf(sval, "%07x", values[255]+1); E(mdb_txn_begin(env, NULL, 0, &txn)); (void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA)); mdb_txn_abort(txn); sprintf(sval, "%07x", values[500]+1); E(mdb_txn_begin(env, NULL, 0, &txn)); (void)RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA)); E(mdb_txn_commit(txn)); /* Try MDB_NEXT_MULTIPLE */ E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT_MULTIPLE)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_txn_abort(txn); j=0; for (i= count - 1; i > -1; i-= (rand()%3)) { j++; txn=NULL; E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(sval, "%07x", values[i]); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) { j--; mdb_txn_abort(txn); } else { E(mdb_txn_commit(txn)); } } je_free(values); printf("Deleted %d values\n", j); E(mdb_env_stat(env, &mst)); E(mdb_txn_begin(env, NULL, 1, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_close(env, dbi); mdb_txn_abort(txn); mdb_env_close(env); return 0; }
static void memkind_hbw_closest_numanode_init(void) { struct memkind_hbw_closest_numanode_t *g = &memkind_hbw_closest_numanode_g; int *bandwidth = NULL; int num_unique = 0; int high_bandwidth = 0; int node; struct bandwidth_nodes_t *bandwidth_nodes = NULL; char *hbw_nodes_env; struct bitmask *hbw_nodes_bm; g->num_cpu = numa_num_configured_cpus(); g->closest_numanode = (int *)je_malloc(sizeof(int) * g->num_cpu); bandwidth = (int *)je_malloc(sizeof(int) * NUMA_NUM_NODES); if (!(g->closest_numanode && bandwidth)) { g->init_err = MEMKIND_ERROR_MALLOC; } if (!g->init_err) { hbw_nodes_env = getenv("MEMKIND_HBW_NODES"); if (hbw_nodes_env) { hbw_nodes_bm = numa_parse_nodestring(hbw_nodes_env); if (!hbw_nodes_bm) { g->init_err = MEMKIND_ERROR_ENVIRON; } else { for (node = 0; node < NUMA_NUM_NODES; ++node) { if (numa_bitmask_isbitset(hbw_nodes_bm, node)) { bandwidth[node] = 2; } else { bandwidth[node] = 1; } } numa_bitmask_free(hbw_nodes_bm); } } else { g->init_err = parse_node_bandwidth(NUMA_NUM_NODES, bandwidth, MEMKIND_BANDWIDTH_PATH); } } if (!g->init_err) { g->init_err = create_bandwidth_nodes(NUMA_NUM_NODES, bandwidth, &num_unique, &bandwidth_nodes); } if (!g->init_err) { if (num_unique == 1) { g->init_err = MEMKIND_ERROR_UNAVAILABLE; } } if (!g->init_err) { high_bandwidth = bandwidth_nodes[num_unique-1].bandwidth; g->init_err = set_closest_numanode(num_unique, bandwidth_nodes, high_bandwidth, g->num_cpu, g->closest_numanode); } if (bandwidth_nodes) { je_free(bandwidth_nodes); } if (bandwidth) { je_free(bandwidth); } if (g->init_err) { if (g->closest_numanode) { je_free(g->closest_numanode); g->closest_numanode = NULL; } } }
int main(int argc,char * argv[]) { int i = 0, j = 0, rc; MDB_env *env; MDB_dbi dbi; MDB_val key, data; MDB_txn *txn; MDB_stat mst; MDB_cursor *cursor, *cur2; MDB_cursor_op op; int count; int *values; char sval[32] = ""; srand(time(NULL)); count = (rand()%384) + 64; values = (int *)je_malloc(count*sizeof(int)); for(i = 0;i<count;i++) { values[i] = rand()%1024; } E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_open(env, "/tmp/testdb", MDB_FIXEDMAP /*|MDB_NOSYNC*/, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_open(txn, NULL, 0, &dbi)); key.mv_size = sizeof(int); key.mv_data = sval; data.mv_size = sizeof(sval); data.mv_data = sval; printf("Adding %d values\n", count); for (i=0;i<count;i++) { sprintf(sval, "%03x %d foo bar", values[i], values[i]); if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE))) { j++; data.mv_size = sizeof(sval); data.mv_data = sval; } } if (j) printf("%d duplicates skipped\n", j); E(mdb_txn_commit(txn)); E(mdb_env_stat(env, &mst)); E(mdb_txn_begin(env, NULL, 1, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_txn_abort(txn); j=0; key.mv_data = sval; for (i= count - 1; i > -1; i-= (rand()%5)) { j++; txn=NULL; E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(sval, "%03x ", values[i]); if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, NULL))) { j--; mdb_txn_abort(txn); } else { E(mdb_txn_commit(txn)); } } je_free(values); printf("Deleted %d values\n", j); E(mdb_env_stat(env, &mst)); E(mdb_txn_begin(env, NULL, 1, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor last\n"); E(mdb_cursor_get(cursor, &key, &data, MDB_LAST)); printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor last/prev\n"); E(mdb_cursor_get(cursor, &key, &data, MDB_LAST)); printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); E(mdb_cursor_get(cursor, &key, &data, MDB_PREV)); printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); mdb_txn_abort(txn); printf("Deleting with cursor\n"); E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_cursor_open(txn, dbi, &cur2)); for (i=0; i<50; i++) { if (RES(MDB_NOTFOUND, mdb_cursor_get(cur2, &key, &data, MDB_NEXT))) break; printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); E(mdb_del(txn, dbi, &key, NULL)); } printf("Restarting cursor in txn\n"); for (op=MDB_FIRST, i=0; i<=32; op=MDB_NEXT, i++) { if (RES(MDB_NOTFOUND, mdb_cursor_get(cur2, &key, &data, op))) break; printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } mdb_cursor_close(cur2); E(mdb_txn_commit(txn)); printf("Restarting cursor outside txn\n"); E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); for (op=MDB_FIRST, i=0; i<=32; op=MDB_NEXT, i++) { if (RES(MDB_NOTFOUND, mdb_cursor_get(cursor, &key, &data, op))) break; printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } mdb_cursor_close(cursor); mdb_close(env, dbi); mdb_txn_abort(txn); mdb_env_close(env); return 0; }
static void* mallocHook(size_t size, const void*) { return je_malloc(size); }
static void * zone_malloc(malloc_zone_t *zone, size_t size) { return (je_malloc(size)); }
extern "C" void* wrap(malloc)(size_t sz) { return je_malloc(sz); }
int main(int argc,char * argv[]) { int i = 0, j = 0, rc; MDB_env *env; MDB_dbi dbi; MDB_val key, data; MDB_txn *txn; MDB_stat mst; MDB_cursor *cursor; int count; int *values; char sval[32]; char kval[sizeof(int)]; srand(time(NULL)); memset(sval, 0, sizeof(sval)); count = (rand()%384) + 64; values = (int *)je_malloc(count*sizeof(int)); for(i = 0;i<count;i++) { values[i] = rand()%1024; } E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); E(mdb_env_open(env, "/tmp/testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi)); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; printf("Adding %d values\n", count); for (i=0;i<count;i++) { if (!(i & 0x0f)) sprintf(kval, "%03x", values[i]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA))) j++; } if (j) printf("%d duplicates skipped\n", j); E(mdb_txn_commit(txn)); E(mdb_env_stat(env, &mst)); E(mdb_txn_begin(env, NULL, 1, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", key.mv_data, (int) key.mv_size, (char *) key.mv_data, data.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_txn_abort(txn); j=0; for (i= count - 1; i > -1; i-= (rand()%5)) { j++; txn=NULL; E(mdb_txn_begin(env, NULL, 0, &txn)); sprintf(kval, "%03x", values[i & ~0x0f]); sprintf(sval, "%03x %d foo bar", values[i], values[i]); key.mv_size = sizeof(int); key.mv_data = kval; data.mv_size = sizeof(sval); data.mv_data = sval; if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) { j--; mdb_txn_abort(txn); } else { E(mdb_txn_commit(txn)); } } je_free(values); printf("Deleted %d values\n", j); E(mdb_env_stat(env, &mst)); E(mdb_txn_begin(env, NULL, 1, &txn)); E(mdb_cursor_open(txn, dbi, &cursor)); printf("Cursor next\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); printf("Cursor prev\n"); while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { printf("key: %.*s, data: %.*s\n", (int) key.mv_size, (char *) key.mv_data, (int) data.mv_size, (char *) data.mv_data); } CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get"); mdb_cursor_close(cursor); mdb_close(env, dbi); mdb_txn_abort(txn); mdb_env_close(env); return 0; }
void* operator new[] (size_t sz) { return je_malloc(sz); }