/* * 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_pool_create(dir, VMEM_MIN_POOL); } else { vmp = vmem_pool_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_pool_create_in_region"); } else { FATAL("!vmem_pool_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); vmem_free(vmp, test); vmem_pool_delete(vmp); }
/* * task_malloc -- allocates MALLOC_SIZE memory in allocated_mem array */ int task_malloc(int i, void *arg, struct random_data *rand_state) { int size_to_alloc; int random_number; switch (allocation_type) { case ALLOCATION_STATIC: size_to_alloc = allocation_max; break; case ALLOCATION_RANGE: random_r(rand_state, &random_number); size_to_alloc = random_number % (allocation_max - allocation_min) + allocation_min; break; case ALLOCATION_UNKNOWN: break; } if (allocator == ALLOCATOR_VMEM) { VMEM *pool = arg; allocated_mem[i] = vmem_malloc(pool, size_to_alloc); } else { allocated_mem[i] = malloc(size_to_alloc); } if (allocated_mem[i] == NULL) { return FAILURE; } return SUCCESS; }
int main(int argc, char *argv[]) { VMEM *vmp; char *ptr; /* create minimum size pool of memory */ if ((vmp = vmem_create("/pmem-fs", VMEM_MIN_POOL)) == NULL) { perror("vmem_create"); exit(1); } if ((ptr = vmem_malloc(vmp, 100)) == NULL) { perror("vmem_malloc"); exit(1); } strcpy(ptr, "hello, world"); /* give the memory back */ vmem_free(vmp, ptr); /* ... */ }
int main(int argc, char *argv[]) { VMEM *vmp; size_t i; START(argc, argv, "vmem_pool_create_in_region"); if (argc > 1) FATAL("usage: %s", argv[0]); vmp = vmem_pool_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_pool_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_pool_delete(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); }
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); }
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); }
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 < 2 || argc > 3) FATAL("usage: %s directory", argv[0]); const char *dir = argv[1]; /* create and destroy pools multiple times */ size_t repeat; size_t pool_id; for (repeat = 0; repeat < TEST_REPEAT_CREATE_POOLS; ++repeat) { for (pool_id = 0; pool_id < TEST_POOLS_MAX; ++pool_id) { /* delete old pool with this same id if exist */ if (pools[pool_id] != NULL) { vmem_pool_delete(pools[pool_id]); pools[pool_id] = NULL; } if (pool_id % 2 == 0) { /* for even pool_id, create in region */ pools[pool_id] = vmem_pool_create_in_region( mem_pools[pool_id % 2], VMEM_MIN_POOL); if (pools[pool_id] == NULL) FATAL("!vmem_pool_create_in_region"); } else { /* for odd pool_id, create in file */ pools[pool_id] = vmem_pool_create(dir, VMEM_MIN_POOL); if (pools[pool_id] == NULL) FATAL("!vmem_pool_create"); } void *test = vmem_malloc(pools[pool_id], sizeof (void *)); ASSERTne(test, NULL); vmem_free(pools[pool_id], test); } } for (pool_id = 0; pool_id < TEST_POOLS_MAX; ++pool_id) { if (pools[pool_id] != NULL) { vmem_pool_delete(pools[pool_id]); pools[pool_id] = NULL; } } 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_malloc"); 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"); } int *test = vmem_malloc(vmp, sizeof (int)); ASSERTne(test, NULL); *test = test_value; ASSERTeq(*test, 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_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[]) { char *dir = NULL; void *mem_pool = NULL; VMEM *vmp; START(argc, argv, "vmem_check_allocations"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } size_t object_size; for (object_size = 8; object_size <= TEST_MAX_ALLOCATION_SIZE; object_size *= 2) { size_t i; size_t j; if (dir == NULL) { mem_pool = MMAP(NULL, VMEM_MIN_POOL, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); vmp = vmem_pool_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_pool_create_in_region"); } else { vmp = vmem_pool_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_pool_create"); } memset(allocs, 0, TEST_ALLOCS_SIZE); for (i = 0; i < TEST_ALLOCS_SIZE; ++i) { allocs[i] = vmem_malloc(vmp, object_size); if (allocs[i] == NULL) { /* out of memory in pool */ break; } /* check that pointer came from mem_pool */ if (dir == NULL) { ASSERTrange(allocs[i], mem_pool, VMEM_MIN_POOL); } /* fill each allocation with a unique value */ memset(allocs[i], (char)i, object_size); } ASSERT((i > 0) && (i + 1 < TEST_MAX_ALLOCATION_SIZE)); /* check for unexpected modifications of the data */ for (i = 0; i < TEST_ALLOCS_SIZE && allocs[i] != NULL; ++i) { char *buffer = allocs[i]; for (j = 0; j < object_size; ++j) { if (buffer[j] != (char)i) FATAL("Content of data object was " "modified unexpectedly for " "object size: %zu, id: %zu", object_size, j); } } vmem_pool_delete(vmp); } DONE(NULL); }
int main(int argc, char *argv[]) { char *dir = NULL; VMEM *vmp; START(argc, argv, "vmem_freespace"); if (argc == 2) { dir = argv[1]; } else if (argc > 2) { FATAL("usage: %s [directory]", argv[0]); } if (dir == NULL) { /* allocate memory for function vmem_pool_create_in_region() */ void *mem_pool = MMAP(NULL, VMEM_MIN_POOL, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); vmp = vmem_pool_create_in_region(mem_pool, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_pool_create_in_region"); } else { vmp = vmem_pool_create(dir, VMEM_MIN_POOL); if (vmp == NULL) FATAL("!vmem_pool_create"); } size_t total_space = vmem_pool_freespace(vmp); size_t free_space = total_space; /* allocate all memory */ void *prev = NULL; void **next; while ((next = vmem_malloc(vmp, 128)) != NULL) { *next = prev; prev = next; size_t space = vmem_pool_freespace(vmp); /* free space can only decrease */ ASSERT(space <= free_space); free_space = space; } ASSERTne(prev, NULL); /* for small allocations use all memory */ ASSERTeq(free_space, 0); while (prev != NULL) { void **act = prev; prev = *act; vmem_free(vmp, act); size_t space = vmem_pool_freespace(vmp); /* free space can only increase */ ASSERT(space >= free_space); free_space = space; } free_space = vmem_pool_freespace(vmp); /* * Depending on the distance of the 'mem_pool' from the * chunk alignment (4MB) a different size of free memory * will be wasted on base_alloc inside jemalloc. * Rest of the internal data should not waste more than 10% of space. */ ASSERT(free_space > ((total_space - 4L * MB) * 9) / 10); vmem_pool_delete(vmp); DONE(NULL); }
static pthread_rwlock_t g_vmem_rwlock = PTHREAD_RWLOCK_INITIALIZER; /****************************************************************************** ** JNI implementations *****************************************************************************/ JNIEXPORT jlong JNICALL Java_com_intel_bigdatamem_BigDataMemAllocator_nallocate( JNIEnv* env, jobject this, jlong id, jlong size, jboolean initzero) { pthread_rwlock_rdlock(&g_vmem_rwlock); pthread_mutex_lock(g_vmem_mutex_ptr + id); void* nativebuf = initzero ? vmem_calloc(*(g_vmp_ptr + id), 1, size) : vmem_malloc(*(g_vmp_ptr + id), size); pthread_mutex_unlock(g_vmem_mutex_ptr + id); pthread_rwlock_unlock(&g_vmem_rwlock); return addr_to_java(nativebuf); } JNIEXPORT jlong JNICALL Java_com_intel_bigdatamem_BigDataMemAllocator_nreallocate( JNIEnv* env, jobject this, jlong id, jlong address, jlong size, jboolean initzero) { pthread_rwlock_rdlock(&g_vmem_rwlock); pthread_mutex_lock(g_vmem_mutex_ptr + id);
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[]) { 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[]) { 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); }