int main(int argc, char *argv[]) { char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_check"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP(NULL, VMEM_MIN_POOL*2, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create"); } ASSERTeq(1, vmem_check(vmp)); /* create pool in this same memory region */ if (dir == NULL) { unsigned long Pagesize = (unsigned long) sysconf(_SC_PAGESIZE); void *mem_pool2 = (void *)(((uintptr_t)mem_pool + VMEM_MIN_POOL/2) & ~(Pagesize-1)); VMEM *vmp2 = vmem_create_in_region(mem_pool2, VMEM_MIN_POOL); if (vmp2 == NULL) FATAL("!vmem_create_in_region"); /* detect memory range collision */ ASSERTne(1, vmem_check(vmp)); ASSERTne(1, vmem_check(vmp2)); vmem_delete(vmp2); ASSERTne(1, vmem_check(vmp2)); } vmem_delete(vmp); /* for vmem_create() memory unmapped after delete pool */ if (!dir) ASSERTne(1, vmem_check(vmp)); DONE(NULL); }
int main(int argc, char *argv[]) { char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_check"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP_ANON_ALIGNED(VMEM_MIN_POOL * 2, 4 << 20); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create"); } ASSERTeq(1, vmem_check(vmp)); /* create pool in this same memory region */ if (dir == NULL) { void *mem_pool2 = (void *)(((uintptr_t)mem_pool + VMEM_MIN_POOL / 2) & ~(Ut_pagesize - 1)); VMEM *vmp2 = vmem_create_in_region(mem_pool2, VMEM_MIN_POOL); if (vmp2 == NULL) FATAL("!vmem_create_in_region"); /* detect memory range collision */ ASSERTne(1, vmem_check(vmp)); ASSERTne(1, vmem_check(vmp2)); vmem_delete(vmp2); ASSERTne(1, vmem_check(vmp2)); } vmem_delete(vmp); /* for vmem_create() memory unmapped after delete pool */ if (!dir) ASSERTne(1, vmem_check(vmp)); DONE(NULL); }
int main(int argc, char *argv[]) { const int test_value = 123456; char *dir = NULL; int count = DEFAULT_COUNT; int n = DEFAULT_N; VMEM *vmp; int opt; int i, j; int use_calloc = 0; START(argc, argv, "vmem_pages_purging"); while ((opt = getopt(argc, argv, "z")) != -1) { switch (opt) { case 'z': use_calloc = 1; break; default: usage(argv[0]); } } if (optind < argc) { dir = argv[optind]; } else { usage(argv[0]); } vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create"); for (i = 0; i < n; i++) { int *test = NULL; if (use_calloc) test = vmem_calloc(vmp, 1, count * sizeof (int)); else test = vmem_malloc(vmp, count * sizeof (int)); ASSERTne(test, NULL); if (use_calloc) { /* vmem_calloc should return zeroed memory */ for (j = 0; j < count; j++) ASSERTeq(test[j], 0); } for (j = 0; j < count; j++) test[j] = test_value; for (j = 0; j < count; j++) ASSERTeq(test[j], test_value); vmem_free(vmp, test); } vmem_delete(vmp); DONE(NULL); }
void EMBX_OS_PhysMemUnMap(EMBX_VOID *vMem) { EMBX_Info(EMBX_INFO_OS, (">>>>PhysMemUnMap\n")); vmem_delete(vMem); EMBX_Info(EMBX_INFO_OS, ("<<<<PhysMemUnMap\n")); }
int main(int argc, char *argv[]) { const int test_value = 123456; char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_realloc"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP(NULL, VMEM_MIN_POOL, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create"); } int *test = vmem_realloc(vmp, NULL, sizeof (int)); ASSERTne(test, NULL); test[0] = test_value; ASSERTeq(test[0], test_value); /* check that pointer came from mem_pool */ if (dir == NULL) { ASSERTrange(test, mem_pool, VMEM_MIN_POOL); } test = vmem_realloc(vmp, test, sizeof (int) * 10); ASSERTne(test, NULL); ASSERTeq(test[0], test_value); test[1] = test_value; test[9] = test_value; /* check that pointer came from mem_pool */ if (dir == NULL) { ASSERTrange(test, mem_pool, VMEM_MIN_POOL); } vmem_free(vmp, test); vmem_delete(vmp); DONE(NULL); }
int main(int argc, char *argv[]) { char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_out_of_memory"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP_ANON_ALIGNED(VMEM_MIN_POOL, 4 << 20); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create"); } /* allocate all memory */ void *prev = NULL; for (;;) { void **next = vmem_malloc(vmp, sizeof (void *)); if (next == NULL) { /* out of memory */ break; } /* check that pointer came from mem_pool */ if (dir == NULL) { ASSERTrange(next, mem_pool, VMEM_MIN_POOL); } *next = prev; prev = next; } ASSERTne(prev, NULL); /* free all allocations */ while (prev != NULL) { void **act = prev; prev = *act; vmem_free(vmp, act); } vmem_delete(vmp); DONE(NULL); }
/* * signal_handler -- called on SIGSEGV */ static void signal_handler(int sig) { OUT("signal: %s", strsignal(sig)); vmem_delete(Vmp); DONE(NULL); }
int main(int argc, char *argv[]) { char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; size_t obj_size; int *ptr[COUNT]; int i = 0; size_t sum_alloc = 0; START(argc, argv, "vmem_mix_allocations"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { UT_FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP_ANON_ALIGNED(POOL_SIZE, 4 << 20); vmp = vmem_create_in_region(mem_pool, POOL_SIZE); if (vmp == NULL) UT_FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, POOL_SIZE); if (vmp == NULL) UT_FATAL("!vmem_create"); } obj_size = MAX_SIZE; /* test with multiple size of allocations from 4MB to 2B */ for (i = 0; i < COUNT; ++i, obj_size /= 2) { ptr[i] = vmem_malloc(vmp, obj_size); if (ptr[i] == NULL) continue; sum_alloc += obj_size; /* check that pointer came from mem_pool */ if (dir == NULL) UT_ASSERTrange(ptr[i], mem_pool, POOL_SIZE); } /* allocate more than half of pool size */ UT_ASSERT(sum_alloc * 2 > POOL_SIZE); while (i > 0) vmem_free(vmp, ptr[--i]); vmem_delete(vmp); DONE(NULL); }
/* * signal_handler -- called on SIGSEGV */ static void signal_handler(int sig) { UT_OUT("signal: %s", os_strsignal(sig)); vmem_delete(Vmp); DONEW(NULL); }
int main(int argc, char *argv[]) { const char *text = "Some test text"; const char *text_empty = ""; char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_strdup"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { UT_FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP_ANON_ALIGNED(VMEM_MIN_POOL, 4 << 20); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) UT_FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) UT_FATAL("!vmem_create"); } char *str1 = vmem_strdup(vmp, text); UT_ASSERTne(str1, NULL); UT_ASSERTeq(strcmp(text, str1), 0); /* check that pointer came from mem_pool */ if (dir == NULL) { UT_ASSERTrange(str1, mem_pool, VMEM_MIN_POOL); } char *str2 = vmem_strdup(vmp, text_empty); UT_ASSERTne(str2, NULL); UT_ASSERTeq(strcmp(text_empty, str2), 0); /* check that pointer came from mem_pool */ if (dir == NULL) { UT_ASSERTrange(str2, mem_pool, VMEM_MIN_POOL); } vmem_free(vmp, str1); vmem_free(vmp, str2); vmem_delete(vmp); DONE(NULL); }
int main(int argc, char *argv[]) { char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; void *alloc; size_t usable_size; size_t size; unsigned i; START(argc, argv, "vmem_malloc_usable_size"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP_ANON_ALIGNED(POOL_SIZE, 4 << 20); vmp = vmem_create_in_region(mem_pool, POOL_SIZE); if (vmp == NULL) FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, POOL_SIZE); if (vmp == NULL) FATAL("!vmem_create"); } ASSERTeq(vmem_malloc_usable_size(vmp, NULL), 0); for (i = 0; i < (sizeof (Check_sizes) / sizeof (Check_sizes[0])); ++i) { size = Check_sizes[i].size; alloc = vmem_malloc(vmp, size); ASSERTne(alloc, NULL); usable_size = vmem_malloc_usable_size(vmp, alloc); ASSERT(usable_size >= size); if (usable_size - size > Check_sizes[i].spacing) { FATAL("Size %zu: spacing %zu is bigger" "than expected: %zu", size, (usable_size - size), Check_sizes[i].spacing); } memset(alloc, 0xEE, usable_size); vmem_free(vmp, alloc); } ASSERTeq(vmem_check(vmp), 1); vmem_delete(vmp); DONE(NULL); }
int main(int argc, char *argv[]) { START(argc, argv, "vmem_multiple_pools"); if (argc < 4) UT_FATAL("usage: %s directory npools nthreads", argv[0]); dir = argv[1]; npools = atoi(argv[2]); int nthreads = atoi(argv[3]); UT_OUT("create %d pools in %d thread(s)", npools, nthreads); const unsigned mem_pools_size = (npools / 2 + npools % 2) * nthreads; mem_pools = MALLOC(mem_pools_size * sizeof(char *)); pools = CALLOC(npools * nthreads, sizeof(VMEM *)); os_thread_t *threads = CALLOC(nthreads, sizeof(os_thread_t)); UT_ASSERTne(threads, NULL); int *pool_idx = CALLOC(nthreads, sizeof(int)); UT_ASSERTne(pool_idx, NULL); for (unsigned pool_id = 0; pool_id < mem_pools_size; ++pool_id) { /* allocate memory for function vmem_create_in_region() */ mem_pools[pool_id] = MMAP_ANON_ALIGNED(VMEM_MIN_POOL, 4 << 20); } /* create and destroy pools multiple times */ for (int t = 0; t < nthreads; t++) { pool_idx[t] = npools * t; PTHREAD_CREATE(&threads[t], NULL, thread_func, &pool_idx[t]); } for (int t = 0; t < nthreads; t++) PTHREAD_JOIN(&threads[t], NULL); for (int pool_id = 0; pool_id < npools * nthreads; ++pool_id) { if (pools[pool_id] != NULL) { vmem_delete(pools[pool_id]); pools[pool_id] = NULL; } } FREE(mem_pools); FREE(pools); FREE(threads); FREE(pool_idx); DONE(NULL); }
int main(int argc, char *argv[]) { const int test_value = 123456; char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_calloc"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { UT_FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP_ANON_ALIGNED(VMEM_MIN_POOL, 4 << 20); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) UT_FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) UT_FATAL("!vmem_create"); } int *test = vmem_calloc(vmp, 1, sizeof(int)); UT_ASSERTne(test, NULL); /* pool_calloc should return zeroed memory */ UT_ASSERTeq(*test, 0); *test = test_value; UT_ASSERTeq(*test, test_value); /* check that pointer came from mem_pool */ if (dir == NULL) { UT_ASSERTrange(test, mem_pool, VMEM_MIN_POOL); } vmem_free(vmp, test); vmem_delete(vmp); DONE(NULL); }
int main(int argc, char *argv[]) { char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_realloc_inplace"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP_ANON_ALIGNED(POOL_SIZE, 4 << 20); vmp = vmem_create_in_region(mem_pool, POOL_SIZE); if (vmp == NULL) FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, POOL_SIZE); if (vmp == NULL) FATAL("!vmem_create"); } int *test = vmem_malloc(vmp, 12 * 1024 * 1024); ASSERTne(test, NULL); test = vmem_realloc(vmp, test, 6 * 1024 * 1024); ASSERTne(test, NULL); test = vmem_realloc(vmp, test, 12 * 1024 * 1024); ASSERTne(test, NULL); vmem_free(vmp, test); vmem_delete(vmp); DONE(NULL); }
/* * pool_test -- test pool * * This function creates a memory pool in a file (if dir is not NULL), * or in RAM (if dir is NULL) and allocates memory for the test. */ void pool_test(const char *dir) { VMEM *vmp = NULL; if (dir != NULL) { vmp = vmem_create(dir, VMEM_MIN_POOL); } else { /* allocate memory for function vmem_create_in_region() */ void *mem_pool = MMAP(NULL, VMEM_MIN_POOL, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); } if (expect_create_pool == 0) { ASSERTeq(vmp, NULL); DONE(NULL); } else { if (vmp == NULL) { if (dir == NULL) { FATAL("!vmem_create_in_region"); } else { FATAL("!vmem_create"); } } } char *test = vmem_malloc(vmp, strlen(TEST_STRING_VALUE) + 1); ASSERTne(test, NULL); strcpy(test, TEST_STRING_VALUE); ASSERTeq(strcmp(test, TEST_STRING_VALUE), 0); ASSERT(vmem_malloc_usable_size(vmp, test) > 0); vmem_free(vmp, test); vmem_delete(vmp); }
static void * thread_func(void *arg) { int start_idx = *(int *)arg; for (int repeat = 0; repeat < TEST_REPEAT_CREATE_POOLS; ++repeat) { for (int idx = 0; idx < npools; ++idx) { int pool_id = start_idx + idx; /* delete old pool with the same id if exist */ if (pools[pool_id] != NULL) { vmem_delete(pools[pool_id]); pools[pool_id] = NULL; } if (pool_id % 2 == 0) { /* for even pool_id, create in region */ pools[pool_id] = vmem_create_in_region( mem_pools[pool_id / 2], VMEM_MIN_POOL); if (pools[pool_id] == NULL) UT_FATAL("!vmem_create_in_region"); } else { /* for odd pool_id, create in file */ pools[pool_id] = vmem_create(dir, VMEM_MIN_POOL); if (pools[pool_id] == NULL) UT_FATAL("!vmem_create"); } void *test = vmem_malloc(pools[pool_id], sizeof(void *)); UT_ASSERTne(test, NULL); vmem_free(pools[pool_id], test); } } return NULL; }
/* * pool_test -- test pool * * This function creates a memory pool in a file (if dir is not NULL), * or in RAM (if dir is NULL) and allocates memory for the test. */ static void pool_test(const char *dir) { VMEM *vmp = NULL; if (dir != NULL) { vmp = vmem_create(dir, VMEM_MIN_POOL); } else { /* allocate memory for function vmem_create_in_region() */ void *mem_pool = MMAP_ANON_ALIGNED(VMEM_MIN_POOL, 4 << 20); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); } if (vmp == NULL) { if (dir == NULL) { FATAL("!vmem_create_in_region"); } else { FATAL("!vmem_create"); } } char *test = vmem_malloc(vmp, strlen(TEST_STRING_VALUE) + 1); if (expect_malloc == 0) { ASSERTeq(test, NULL); } else { strcpy(test, TEST_STRING_VALUE); ASSERTeq(strcmp(test, TEST_STRING_VALUE), 0); ASSERT(vmem_malloc_usable_size(vmp, test) > 0); vmem_free(vmp, test); } vmem_delete(vmp); }
int main(int argc, char *argv[]) { VMEM *vmp; size_t i; START(argc, argv, "vmem_create_in_region"); if (argc > 1) FATAL("usage: %s", argv[0]); /* allocate memory for function vmem_create_in_region() */ void *mem_pool = MMAP(NULL, VMEM_MIN_POOL, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create_in_region"); for (i = 0; i < TEST_ALLOCATIONS; ++i) { allocs[i] = vmem_malloc(vmp, sizeof (int)); ASSERTne(allocs[i], NULL); /* check that pointer came from mem_pool */ ASSERTrange(allocs[i], mem_pool, VMEM_MIN_POOL); } for (i = 0; i < TEST_ALLOCATIONS; ++i) { vmem_free(vmp, allocs[i]); } vmem_delete(vmp); DONE(NULL); }
int main(int argc, char *argv[]) { START(argc, argv, "out_err_mt"); if (argc != 5) UT_FATAL("usage: %s filename1 filename2 filename3 dir", argv[0]); PMEMobjpool *pop = pmemobj_create(argv[1], "test", PMEMOBJ_MIN_POOL, 0666); PMEMlogpool *plp = pmemlog_create(argv[2], PMEMLOG_MIN_POOL, 0666); PMEMblkpool *pbp = pmemblk_create(argv[3], 128, PMEMBLK_MIN_POOL, 0666); #ifndef _WIN32 /* XXX - vmem not implemented in windows yet */ VMEM *vmp = vmem_create(argv[4], VMEM_MIN_POOL); #endif util_init(); pmem_check_version(10000, 0); pmemobj_check_version(10001, 0); pmemlog_check_version(10002, 0); pmemblk_check_version(10003, 0); #ifndef _WIN32 /* XXX - vmem not implemented in windows yet */ vmem_check_version(10004, 0); #endif pmempool_check_version(10005, 0); print_errors("version check"); void *ptr = NULL; /* * We are testing library error reporting and we don't want this test * to fail under memcheck. */ VALGRIND_DO_DISABLE_ERROR_REPORTING; pmem_msync(ptr, 1); VALGRIND_DO_ENABLE_ERROR_REPORTING; print_errors("pmem_msync"); pmemlog_append(plp, NULL, PMEMLOG_MIN_POOL); print_errors("pmemlog_append"); size_t nblock = pmemblk_nblock(pbp); pmemblk_set_error(pbp, nblock + 1); print_errors("pmemblk_set_error"); #ifndef _WIN32 /* XXX - vmem not implemented in windows yet */ VMEM *vmp2 = vmem_create_in_region(NULL, 1); UT_ASSERTeq(vmp2, NULL); print_errors("vmem_create_in_region"); #endif run_mt_test(do_test); pmemobj_close(pop); pmemlog_close(plp); pmemblk_close(pbp); #ifndef _WIN32 /* XXX - vmem not implemented in windows yet */ vmem_delete(vmp); #endif PMEMpoolcheck *ppc; struct pmempool_check_args args = {0, }; ppc = pmempool_check_init(&args, sizeof(args) / 2); UT_ASSERTeq(ppc, NULL); print_errors("pmempool_check_init"); DONE(NULL); }
int main(int argc, char *argv[]) { char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_realloc_inplace"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { UT_FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ mem_pool = MMAP_ANON_ALIGNED(POOL_SIZE, 4 << 20); vmp = vmem_create_in_region(mem_pool, POOL_SIZE); if (vmp == NULL) UT_FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, POOL_SIZE); if (vmp == NULL) UT_FATAL("!vmem_create"); } int *test1 = vmem_malloc(vmp, 12 * 1024 * 1024); UT_ASSERTne(test1, NULL); int *test1r = vmem_realloc(vmp, test1, 6 * 1024 * 1024); UT_ASSERTeq(test1r, test1); test1r = vmem_realloc(vmp, test1, 12 * 1024 * 1024); UT_ASSERTeq(test1r, test1); test1r = vmem_realloc(vmp, test1, 8 * 1024 * 1024); UT_ASSERTeq(test1r, test1); int *test2 = vmem_malloc(vmp, 4 * 1024 * 1024); UT_ASSERTne(test2, NULL); /* 4MB => 16B */ int *test2r = vmem_realloc(vmp, test2, 16); UT_ASSERTeq(test2r, NULL); /* ... but the usable size is still 4MB. */ UT_ASSERTeq(vmem_malloc_usable_size(vmp, test2), 4 * 1024 * 1024); /* 8MB => 16B */ test1r = vmem_realloc(vmp, test1, 16); /* * If the old size of the allocation is larger than * the chunk size (4MB), we can reallocate it to 4MB first (in place), * releasing some space, which makes it possible to do the actual * shrinking... */ UT_ASSERTne(test1r, NULL); UT_ASSERTne(test1r, test1); UT_ASSERTeq(vmem_malloc_usable_size(vmp, test1r), 16); /* ... and leaves some memory for new allocations. */ int *test3 = vmem_malloc(vmp, 3 * 1024 * 1024); UT_ASSERTne(test3, NULL); vmem_free(vmp, test1r); vmem_free(vmp, test2r); vmem_free(vmp, test3); vmem_delete(vmp); DONE(NULL); }
int main(int argc, char *argv[]) { char *dir = NULL; VMEM *vmp; int *ptr; int test_case = -1; int expect_custom_alloc = 0; START(argc, argv, "vmem_valgrind"); if (argc >= 2 && argc <= 3) { test_case = atoi(argv[1]); if (test_case > 9) test_case = -1; if (argc > 2) dir = argv[2]; } if (test_case < 0) FATAL("usage: %s <test-number from 0 to 9> [directory]", argv[0]); if (test_case < 5) { OUT("use default allocator"); expect_custom_alloc = 0; } else { OUT("use custom alloc functions"); test_case -= 5; expect_custom_alloc = 1; vmem_set_funcs(malloc_custom, free_custom, realloc_custom, strdup_custom, NULL); } if (dir == NULL) { /* allocate memory for function vmem_create_in_region() */ void *mem_pool = MMAP_ANON_ALIGNED(VMEM_MIN_POOL, 4 << 20); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create"); } switch (test_case) { case 0: { OUT("remove all allocations and delete pool"); ptr = vmem_malloc(vmp, sizeof (int)); if (ptr == NULL) FATAL("!vmem_malloc"); vmem_free(vmp, ptr); vmem_delete(vmp); break; } case 1: { OUT("only remove allocations"); ptr = vmem_malloc(vmp, sizeof (int)); if (ptr == NULL) FATAL("!vmem_malloc"); vmem_free(vmp, ptr); break; } case 2: { OUT("only delete pool"); ptr = vmem_malloc(vmp, sizeof (int)); if (ptr == NULL) FATAL("!vmem_malloc"); vmem_delete(vmp); /* prevent reporting leaked memory as still reachable */ ptr = NULL; break; } case 3: { OUT("memory leaks"); ptr = vmem_malloc(vmp, sizeof (int)); if (ptr == NULL) FATAL("!vmem_malloc"); /* prevent reporting leaked memory as still reachable */ ptr = NULL; break; } case 4: { OUT("heap block overrun"); ptr = vmem_malloc(vmp, 12 * sizeof (int)); if (ptr == NULL) FATAL("!vmem_malloc"); /* heap block overrun */ ptr[12] = 7; vmem_free(vmp, ptr); vmem_delete(vmp); break; } default: { FATAL("!unknown test-number"); } } /* check memory leak in custom allocator */ ASSERTeq(custom_allocs, 0); if (expect_custom_alloc == 0) { ASSERTeq(custom_alloc_calls, 0); } else { ASSERTne(custom_alloc_calls, 0); } DONE(NULL); }
int main(int argc, char *argv[]) { START(argc, argv, "vmem_delete"); VMEM *vmp; void *ptr; if (argc < 2) FATAL("usage: %s op:h|f|m|c|r|a|s|d", argv[0]); /* allocate memory for function vmem_create_in_region() */ void *mem_pool = MMAP_ANON_ALIGNED(VMEM_MIN_POOL, 4 << 20); vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create_in_region"); ptr = vmem_malloc(vmp, sizeof (long long int)); if (ptr == NULL) ERR("!vmem_malloc"); vmem_delete(vmp); /* arrange to catch SEGV */ struct sigaction v; sigemptyset(&v.sa_mask); v.sa_flags = 0; v.sa_handler = signal_handler; SIGACTION(SIGSEGV, &v, NULL); SIGACTION(SIGABRT, &v, NULL); SIGACTION(SIGILL, &v, NULL); /* go through all arguments one by one */ for (int arg = 1; arg < argc; arg++) { /* Scan the character of each argument. */ if (strchr("hfmcrasd", argv[arg][0]) == NULL || argv[arg][1] != '\0') FATAL("op must be one of: h, f, m, c, r, a, s, d"); switch (argv[arg][0]) { case 'h': OUT("Testing vmem_check..."); if (!sigsetjmp(Jmp, 1)) { OUT("\tvmem_check returned %i", vmem_check(vmp)); } break; case 'f': OUT("Testing vmem_free..."); if (!sigsetjmp(Jmp, 1)) { vmem_free(vmp, ptr); OUT("\tvmem_free succeeded"); } break; case 'm': OUT("Testing vmem_malloc..."); if (!sigsetjmp(Jmp, 1)) { ptr = vmem_malloc(vmp, sizeof (long long int)); if (ptr != NULL) OUT("\tvmem_malloc succeeded"); else OUT("\tvmem_malloc returned NULL"); } break; case 'c': OUT("Testing vmem_calloc..."); if (!sigsetjmp(Jmp, 1)) { ptr = vmem_calloc(vmp, 10, sizeof (int)); if (ptr != NULL) OUT("\tvmem_calloc succeeded"); else OUT("\tvmem_calloc returned NULL"); } break; case 'r': OUT("Testing vmem_realloc..."); if (!sigsetjmp(Jmp, 1)) { ptr = vmem_realloc(vmp, ptr, 128); if (ptr != NULL) OUT("\tvmem_realloc succeeded"); else OUT("\tvmem_realloc returned NULL"); } break; case 'a': OUT("Testing vmem_aligned_alloc..."); if (!sigsetjmp(Jmp, 1)) { ptr = vmem_aligned_alloc(vmp, 128, 128); if (ptr != NULL) OUT("\tvmem_aligned_alloc succeeded"); else OUT("\tvmem_aligned_alloc" " returned NULL"); } break; case 's': OUT("Testing vmem_strdup..."); if (!sigsetjmp(Jmp, 1)) { ptr = vmem_strdup(vmp, "Test string"); if (ptr != NULL) OUT("\tvmem_strdup succeeded"); else OUT("\tvmem_strdup returned NULL"); } break; case 'd': OUT("Testing vmem_delete..."); if (!sigsetjmp(Jmp, 1)) { vmem_delete(vmp); if (errno != 0) OUT("\tvmem_delete failed: %s", vmem_errormsg()); else OUT("\tvmem_delete succeeded"); } break; } } DONE(NULL); }
int main(int argc, char *argv[]) { const int test_value = 123456; char *dir = NULL; VMEM *vmp; size_t alignment; unsigned i; int *ptr; int *ptrs[MAX_ALLOCS]; START(argc, argv, "vmem_aligned_alloc"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } /* allocate memory for function vmem_create_in_region() */ void *mem_pool = MMAP_ANON_ALIGNED(VMEM_MIN_POOL, 4 << 20); /* use custom alloc functions to check for memory leaks */ vmem_set_funcs(malloc_custom, free_custom, realloc_custom, strdup_custom, NULL); /* test with address alignment from 2B to 4MB */ for (alignment = 2; alignment <= 4 * 1024 * 1024; alignment *= 2) { custom_alloc_calls = 0; if (dir == NULL) { vmp = vmem_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create_in_region"); } else { vmp = vmem_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_create"); } memset(ptrs, 0, MAX_ALLOCS * sizeof (ptrs[0])); for (i = 0; i < MAX_ALLOCS; ++i) { ptr = vmem_aligned_alloc(vmp, alignment, sizeof (int)); ptrs[i] = ptr; /* at least one allocation must succeed */ ASSERT(i != 0 || ptr != NULL); if (ptr == NULL) break; /* ptr should be usable */ *ptr = test_value; ASSERTeq(*ptr, test_value); /* check for correct address alignment */ ASSERTeq((uintptr_t)(ptr) & (alignment - 1), 0); /* check that pointer came from mem_pool */ if (dir == NULL) { ASSERTrange(ptr, mem_pool, VMEM_MIN_POOL); } } for (i = 0; i < MAX_ALLOCS; ++i) { if (ptrs[i] == NULL) break; vmem_free(vmp, ptrs[i]); } vmem_delete(vmp); /* check memory leaks */ ASSERTne(custom_alloc_calls, 0); ASSERTeq(custom_allocs, 0); } DONE(NULL); }
static void UnMapMemory(void* pVMem) { vmem_delete(pVMem); }