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); }
/* * 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); }
/* * 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[]) { 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); }