fixed strict aliasing issues in xma.c
This commit is contained in:
		
							
								
								
									
										34
									
								
								t/t-008.c
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								t/t-008.c
									
									
									
									
									
								
							| @ -5,32 +5,40 @@ | ||||
| #include <time.h> | ||||
| #include "tap.h" | ||||
|  | ||||
| #define NUM_ITERATIONS 1000000 | ||||
| #define NUM_ITERATIONS 10000 | ||||
| #define MIN_ALLOC_SIZE 16 | ||||
| #define MAX_ALLOC_SIZE 1024 | ||||
| 				 | ||||
| #define OK_X(test) OK(test, #test) | ||||
|  | ||||
| static size_t random_size() | ||||
| static size_t random_size(hawk_oow_t max_alloc_size) | ||||
| { | ||||
| 	return MIN_ALLOC_SIZE + rand() % (MAX_ALLOC_SIZE - MIN_ALLOC_SIZE + 1); | ||||
| 	return MIN_ALLOC_SIZE + rand() % (max_alloc_size - MIN_ALLOC_SIZE + 1); | ||||
| } | ||||
|  | ||||
| int main() | ||||
| int main(int argc, char* argv[]) | ||||
| { | ||||
| 	int test_bad = 0; | ||||
| 	hawk_mmgr_t xma_mmgr; | ||||
| 	hawk_oow_t num_iterations = NUM_ITERATIONS; | ||||
| 	hawk_oow_t max_alloc_size = MAX_ALLOC_SIZE; | ||||
|  | ||||
| 	clock_t start_time, end_time; | ||||
| 	double malloc_time = 0.0, free_time = 0.0; | ||||
| 	void **ptr_array; | ||||
|  | ||||
| 	if (argc >= 3) | ||||
| 	{ | ||||
| 		num_iterations = strtoul(argv[1], NULL, 10); | ||||
| 		max_alloc_size = strtoul(argv[2], NULL, 10); | ||||
| 	} | ||||
|  | ||||
| 	no_plan(); | ||||
| 	hawk_init_xma_mmgr(&xma_mmgr, NUM_ITERATIONS * MAX_ALLOC_SIZE); | ||||
| 	hawk_init_xma_mmgr(&xma_mmgr, num_iterations * max_alloc_size); | ||||
|  | ||||
| 	srand((unsigned int)time(NULL)); | ||||
|  | ||||
| 	ptr_array = malloc(NUM_ITERATIONS * sizeof(void *)); | ||||
| 	ptr_array = malloc(num_iterations * sizeof(void *)); | ||||
| 	OK_X (ptr_array != NULL); | ||||
| 	if (!ptr_array) { | ||||
| 		fprintf(stderr, "malloc failed for pointer array\n"); | ||||
| @ -38,8 +46,8 @@ int main() | ||||
| 	} | ||||
|  | ||||
| 	start_time = clock(); | ||||
| 	for (size_t i = 0; i < NUM_ITERATIONS; ++i) { | ||||
| 		size_t size = random_size(); | ||||
| 	for (size_t i = 0; i < num_iterations; ++i) { | ||||
| 		size_t size = random_size(max_alloc_size); | ||||
| 		/*ptr_array[i] = malloc(size);*/ | ||||
| 		ptr_array[i] = HAWK_MMGR_ALLOC(&xma_mmgr, size); | ||||
| 		if (!ptr_array[i]) { | ||||
| @ -56,7 +64,7 @@ int main() | ||||
| 	malloc_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; | ||||
|  | ||||
| 	start_time = clock(); | ||||
| 	for (size_t i = 0; i < NUM_ITERATIONS; ++i) { | ||||
| 	for (size_t i = 0; i < num_iterations; ++i) { | ||||
| 		/*free(ptr_array[i]);*/ | ||||
| 		HAWK_MMGR_FREE(&xma_mmgr, ptr_array[i]); | ||||
| 	} | ||||
| @ -65,12 +73,14 @@ int main() | ||||
|  | ||||
| 	free(ptr_array); | ||||
|  | ||||
| 	printf("Performed %d allocations and frees\n", NUM_ITERATIONS); | ||||
| 	printf("Performed %lu allocations and frees - min alloc size %lu max_alloc_size %lu\n", | ||||
| 		(unsigned long)num_iterations, (unsigned long)MIN_ALLOC_SIZE, (unsigned long)max_alloc_size); | ||||
| 	printf("Total malloc time: %.6f seconds\n", malloc_time); | ||||
| 	printf("Total free time  : %.6f seconds\n", free_time); | ||||
| 	printf("Average malloc time: %.9f seconds\n", malloc_time / NUM_ITERATIONS); | ||||
| 	printf("Average free time  : %.9f seconds\n", free_time / NUM_ITERATIONS); | ||||
| 	printf("Average malloc time: %.9f seconds\n", malloc_time / num_iterations); | ||||
| 	printf("Average free time  : %.9f seconds\n", free_time / num_iterations); | ||||
|  | ||||
| 	hawk_fini_xma_mmgr(&xma_mmgr); | ||||
| 	return exit_status(); | ||||
| } | ||||
|  | ||||
|  | ||||
							
								
								
									
										125
									
								
								t/t-009.c
									
									
									
									
									
								
							
							
						
						
									
										125
									
								
								t/t-009.c
									
									
									
									
									
								
							| @ -5,8 +5,8 @@ | ||||
| #include <time.h> | ||||
| #include "tap.h" | ||||
|  | ||||
| #define NUM_OPERATIONS 1000000 | ||||
| #define MAX_ALLOCATIONS 10000 | ||||
| #define NUM_ITERATIONS 20000 | ||||
| #define MAX_ALLOC_ACTIVE 1000 | ||||
| #define MIN_ALLOC_SIZE 16 | ||||
| #define MAX_ALLOC_SIZE 1024 | ||||
|  | ||||
| @ -27,34 +27,129 @@ static void print_xma (void* ctx, const hawk_bch_t* fmt, ...) | ||||
|      va_end (ap); | ||||
| } | ||||
|  | ||||
| static size_t random_size() | ||||
| static size_t random_size(hawk_oow_t max_alloc_size) | ||||
| { | ||||
| 	return MIN_ALLOC_SIZE + rand() % (MAX_ALLOC_SIZE - MIN_ALLOC_SIZE + 1); | ||||
| 	return MIN_ALLOC_SIZE + rand() % (max_alloc_size - MIN_ALLOC_SIZE + 1); | ||||
| } | ||||
|  | ||||
| int main() | ||||
| static void test1(hawk_mmgr_t* mmgr) | ||||
| { | ||||
| 	void* x1, * x2, * x3; | ||||
|  | ||||
| 	x1 = HAWK_MMGR_ALLOC(mmgr, 416); | ||||
| 	OK_X (x1 != HAWK_NULL); | ||||
| 	x2 = HAWK_MMGR_ALLOC(mmgr, 688); | ||||
| 	OK_X (x2 != HAWK_NULL); | ||||
|  | ||||
| 	HAWK_MMGR_FREE(mmgr, x1); | ||||
| 	HAWK_MMGR_FREE(mmgr, x2); | ||||
| 	x3 = HAWK_MMGR_ALLOC(mmgr, 144); | ||||
| 	OK_X (x2 != HAWK_NULL); | ||||
|  | ||||
| 	HAWK_MMGR_FREE(mmgr, x3); | ||||
| 	/*hawk_xma_dump(mmgr.ctx, print_xma, HAWK_NULL);*/ | ||||
| } | ||||
|  | ||||
| static void test2(hawk_mmgr_t* mmgr) | ||||
| { | ||||
| 	int x[] = | ||||
| 	{ | ||||
| 		960, 688, -688, -960, 560, 32, -560, -32, 336, 624, -624, -336, 672, -672, 304, -304, 992, -992, 608, -608, | ||||
| 		576, 304, 256,  -304, -256, 416, | ||||
| 	}; | ||||
| 	int i, j; | ||||
| 	int count; | ||||
| 	void *p[100]; | ||||
| 	int sz[100]; | ||||
| 	int test2_bad = 0; | ||||
| 	 | ||||
| 	count = 0; | ||||
| 	for (i = 0; i < HAWK_COUNTOF(x); i++) | ||||
| 	{ | ||||
| 		if (x[i] > 0) | ||||
| 		{ | ||||
| 			p[count] = HAWK_MMGR_ALLOC(mmgr, x[i]); | ||||
| 			if (!p[count]) | ||||
| 			{ | ||||
| 				test2_bad = 1; | ||||
| 				break; | ||||
| 			} | ||||
| 			sz[count] = x[i]; | ||||
| 			count++; | ||||
| 		} | ||||
| 		else if (x[i] < 0) | ||||
| 		{ | ||||
| 			for (j = 0; j < count; j++) | ||||
| 			{ | ||||
| 				if (sz[j] == -x[i]) | ||||
| 				{ | ||||
| 					HAWK_MMGR_FREE(mmgr, p[j]); | ||||
| 					count--; | ||||
| 					p[j] = p[count]; | ||||
| 					sz[j] = sz[count];	 | ||||
| 				} | ||||
| 			}	 | ||||
| 		} | ||||
| 	} | ||||
| 	/*hawk_xma_dump(mmgr->ctx, print_xma, HAWK_NULL);*/ | ||||
|  | ||||
| 	OK_X(test2_bad == 0); | ||||
| 	for (j = 0; j < count; j++) HAWK_MMGR_FREE(mmgr, p[j]); | ||||
| } | ||||
|  | ||||
| int main(int argc, char* argv[]) | ||||
| { | ||||
| 	int test_bad = 0; | ||||
| 	hawk_mmgr_t xma_mmgr; | ||||
| 	hawk_oow_t max_alloc_size = MAX_ALLOC_SIZE; | ||||
| 	hawk_oow_t num_iterations = NUM_ITERATIONS; | ||||
| 	hawk_oow_t max_alloc_active = MAX_ALLOC_ACTIVE; | ||||
| 	hawk_oow_t pool_size = 1000000; | ||||
|  | ||||
| 	Allocation allocations[MAX_ALLOCATIONS] = {0}; /* pool of active allocations */ | ||||
| 	Allocation* allocations; /* pool of active allocations */ | ||||
| 	size_t num_active = 0; | ||||
|  | ||||
| 	clock_t start_time, end_time; | ||||
| 	double malloc_time = 0.0, free_time = 0.0; | ||||
|  | ||||
| 	if (argc >= 5) | ||||
| 	{ | ||||
| 		num_iterations = strtoul(argv[1], NULL, 10); | ||||
| 		max_alloc_size = strtoul(argv[2], NULL, 10); | ||||
| 		max_alloc_active = strtoul(argv[3], NULL, 10); | ||||
| 		pool_size = strtoul(argv[4], NULL, 10); | ||||
| 	} | ||||
| 	else if (argc != 1) | ||||
| 	{ | ||||
| 		fprintf (stderr, "Usage: %s num_iterations max_alloc_size max_alloc_active pool_size\n", argv[0]); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	no_plan(); | ||||
| 	hawk_init_xma_mmgr(&xma_mmgr, 20000000); | ||||
| 	hawk_init_xma_mmgr(&xma_mmgr, pool_size); | ||||
|  | ||||
| 	allocations = HAWK_MMGR_ALLOC(&xma_mmgr, max_alloc_active * sizeof(*allocations)); | ||||
| 	if (!allocations) | ||||
| 	{ | ||||
| 		bail_out("base allocation failed"); | ||||
| 		return -1;	 | ||||
| 	} | ||||
| 	 | ||||
| 	/* ------------------------------- */ | ||||
| 	test1(&xma_mmgr); | ||||
| 	test2(&xma_mmgr); | ||||
|  | ||||
| 	/* ------------------------------- */ | ||||
|  | ||||
| 	srand((unsigned int)time(NULL)); | ||||
| 	start_time = clock(); | ||||
|  | ||||
| 	for (size_t i = 0; i < NUM_OPERATIONS; ++i) | ||||
| 	for (size_t i = 0; i < num_iterations; ++i) | ||||
| 	{ | ||||
| 		int do_alloc = (num_active == 0) || (rand() % 2 == 0 && num_active < MAX_ALLOCATIONS); | ||||
| 		int do_alloc = (num_active == 0) || (rand() % 2 == 0 && num_active < max_alloc_active); | ||||
|  | ||||
| 		if (do_alloc) { | ||||
| 			size_t size = random_size(); | ||||
| 			size_t size = random_size(max_alloc_size); | ||||
| 			/*void *ptr = malloc(size);*/ | ||||
| 			void *ptr = HAWK_MMGR_ALLOC(&xma_mmgr, size); | ||||
| 			if (!ptr) { | ||||
| @ -67,7 +162,7 @@ int main() | ||||
| 			allocations[num_active].size = size; | ||||
| 			++num_active; | ||||
| 		} else { | ||||
| 			/* Free a random active allocation */ | ||||
| 			/* free a random active allocation */ | ||||
| 			size_t index = rand() % num_active; | ||||
| 			void *ptr_to_free = allocations[index].ptr; | ||||
|  | ||||
| @ -77,7 +172,7 @@ int main() | ||||
| 			clock_t t2 = clock(); | ||||
| 			free_time += (double)(t2 - t1) / CLOCKS_PER_SEC; | ||||
|  | ||||
| 			/* Replace with last active allocation */ | ||||
| 			/* replace with last active allocation */ | ||||
| 			allocations[index] = allocations[num_active - 1]; | ||||
| 			--num_active; | ||||
| 		} | ||||
| @ -97,11 +192,13 @@ int main() | ||||
|  | ||||
| 	malloc_time = (double)(end_time - start_time) / CLOCKS_PER_SEC - free_time; | ||||
|  | ||||
| 	printf("Performed %d interleaved malloc/free operations\n", NUM_OPERATIONS); | ||||
| 	printf("Performed %d interleaved malloc/free operations\n", num_iterations); | ||||
| 	printf("Total malloc time (estimated): %.6f seconds\n", malloc_time); | ||||
| 	printf("Total free time              : %.6f seconds\n", free_time); | ||||
| 	printf("Average time per operation   : %.9f seconds\n", (malloc_time + free_time) / NUM_OPERATIONS); | ||||
| 	printf("Average time per operation   : %.9f seconds\n", (malloc_time + free_time) / num_iterations); | ||||
|  | ||||
| 	HAWK_MMGR_FREE(&xma_mmgr, allocations); | ||||
| 	hawk_fini_xma_mmgr(&xma_mmgr); | ||||
| 	return exit_status(); | ||||
| } | ||||
|  | ||||
|  | ||||
		Reference in New Issue
	
	Block a user