Exemple #1
0
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);

	/* 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);
				OUT("\tvmem_delete succeeded");
			}
			break;
		}
	}

	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);
}
Exemple #3
0
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) {

		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);
}