Ejemplo n.º 1
0
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);
}
Ejemplo n.º 2
0
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);
}
Ejemplo n.º 3
0
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);
}
Ejemplo n.º 4
0
int
main(int argc, char *argv[])
{
	VMEM *vmp;
	size_t i;

	START(argc, argv, "vmem_create_in_region");

	if (argc > 1)
		UT_FATAL("usage: %s", 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)
		UT_FATAL("!vmem_create_in_region");

	for (i = 0; i < TEST_ALLOCATIONS; ++i) {
		allocs[i] = vmem_malloc(vmp, sizeof(int));

		UT_ASSERTne(allocs[i], NULL);

		/* check that pointer came from mem_pool */
		UT_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);
}