int main(int argc, char *argv[]) { int expect_custom_alloc = 0; START(argc, argv, "vmem_custom_alloc"); if (argc < 2 || argc > 3 || strlen(argv[1]) != 1) FATAL("usage: %s (0-2) [directory]", argv[0]); switch (argv[1][0]) { case '0': { /* use default allocator */ expect_custom_alloc = 0; expect_create_pool = 1; break; } case '1': { /* error in custom malloc function */ expect_custom_alloc = 1; expect_create_pool = 0; vmem_set_funcs(malloc_null, free_custom, realloc_custom, strdup_custom, NULL); break; } case '2': { /* use custom alloc functions */ expect_custom_alloc = 1; expect_create_pool = 1; vmem_set_funcs(malloc_custom, free_custom, realloc_custom, strdup_custom, NULL); break; } default: { FATAL("usage: %s (0-2) [directory]", argv[0]); break; } } if (argc == 3) { pool_test(argv[2]); } else { int i; /* repeat create pool */ for (i = 0; i < TEST_REPEAT_CREATE_POOLS; ++i) pool_test(NULL); } /* 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[]) { 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); }
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); }