Exemplo n.º 1
0
bool test_max_alc_1_byte( void ) {
	bool rslt = true;
	uint32_t c = 0;
	size_t max_sz;
	
	half_init();

	max_sz = find_max_block();

	// Allocate 1 bytes until no half_aloc returns NULL

	while ( half_alloc( 1 ) != NULL ) {
		c++;
	}

	#ifdef DO_PRINT
		printf( "Only %d 1-Byte block can be allocated within %d addressable Bytes.\n", c, max_sz );
	#endif

	if ( c == 0 || !(max_sz >= smlst_blk_sz * c  || smlst_blk_sz * c >= lrgst_blk_sz) ) {
		#ifdef DO_PRINT
			printf( "32 * %d = %d is not equal to the maximum allocable block which is %d\n", c, c*32 , max_sz );
		#endif

		rslt = false;
	}

	return rslt;
}
Exemplo n.º 2
0
bool test_static_alc_free( void ) {
	bool rslt = true;
	uint32_t max_sz;
	void *ptr_1, *ptr_2, *ptr_3, *ptr_4, *ptr_5, *ptr_6;
	
	half_init();

	max_sz = find_max_block();

	ptr_1 = half_alloc(1 << 5 + 1);
	if (ptr_1 == NULL) return false;

	ptr_2 = half_alloc(1 << 9 - 1);
	if (ptr_2 == NULL) return false;

	ptr_3 = half_alloc(1 << 5 + 1);
	if (ptr_3 == NULL) return false;

	ptr_4 = half_alloc(1 << 10);
	if (ptr_4 == NULL) return false;

	ptr_5 = half_alloc(12345);
	if (ptr_5 == NULL) return false;

	half_free(ptr_1);

	ptr_6 = half_alloc(1);
	if (ptr_6 == NULL) return false;

	half_free(ptr_3);

	half_free(ptr_4);

	ptr_1 = half_alloc(1 << 9);
	if (ptr_1 == NULL) return false;

	half_free(ptr_6);

	half_free(ptr_1);

	half_free(ptr_2);

	half_free(ptr_5);

	// Check wether all allocated memory blocks are freed.
	ptr_1 = half_alloc(max_sz);

	if ( ptr_1 == NULL ) {
		rslt = false;
		printf("Memory is defraged.\n");
	} else {
		half_free(ptr_1);
	}

	return rslt;
}
Exemplo n.º 3
0
void alloc_blk_in_arr( block_t* blks, size_t *blks_sz, size_t len ) {

	blks[*blks_sz].ptr = half_alloc( len );
	blks[*blks_sz].len = len;

	if ( blks[*blks_sz].ptr != NULL ) {
		(*blks_sz)++;
	}
}
Exemplo n.º 4
0
size_t find_max_block( void ) {
	size_t i;
	void *p = NULL;

	for ( i = lrgst_blk_sz; i > 0; --i ) {
		p = half_alloc( i );

		if ( p != NULL ) {
			half_free( p );
			return i;
		}
	}

	return 0;
}
Exemplo n.º 5
0
bool test_alc_free_max( void ) {
	bool rslt = true;
	uint32_t blk_sz;
	void* ptr;

	half_init();
	blk_sz = find_max_block();
	ptr = half_alloc( blk_sz );

	if ( ptr == NULL ) {
		rslt = false;
	}
	
	return rslt;
}
Exemplo n.º 6
0
bool test_rndm_alc_free( void ) {
	bool rslt = true;
	size_t line = 0, max_sz, blks_sz, alc_rec, tbf, blk_sz;
	int i;
	block_t blks[RNDM_TESTS << 1];
	block_t blk;
	void *ptr_1;

	blks_sz = 0;

	half_init();

	max_sz = find_max_block();
	
	// 'alc_rec' stores how many times 'half_alloc' successfully returns a requested block.
	alc_rec = 0;

	// Allocating random memory blocks
	for ( i = 0; i < RNDM_TESTS; ++i ) {

		// Making a new memory block and storing its pointer in the array 
		size_t blk_sz = get_random_block_size();
		block_t blk;
		blk.ptr = half_alloc(blk_sz);
		blk.len = blk_sz;

		if ( blk.ptr != 0 ) {
			blks[blks_sz] = blk;
			++blks_sz;
			alc_rec++;

			#ifdef DO_PRINT
				printf( "%i)The allocated %d Byte block starts from %d \n", ++line, blk.len, blk.ptr );
			#endif
		}
	}

	// Checking any violation
	if ( is_violated(find_violation(blks, blks_sz)) ) {
		return false;
	}
	
	// Free almost half of the allocation blocks
	for ( i = 0; i < RNDM_TESTS >> 1 ; ++i ) {
		if ( (rand() % 2) && (blks_sz > 0) ) {
			// Free a random block
			tbf = rand() % blks_sz;	// To be freed idex
			half_free(blks[tbf].ptr);

			#ifdef DO_PRINT
				printf( "%i)The %d Byte block starting from %d is free1\n", ++line, blks[tbf].len, blks[tbf].ptr );
			#endif

			--blks_sz;
			blks[tbf] = blks[blks_sz];

		} else {
			blk_sz = get_random_block_size();
			blk.ptr = half_alloc(blk_sz);
			blk.len = blk_sz;

			if ( blk.ptr != 0 ) {
				blks[blks_sz] = blk;
				++blks_sz;
				alc_rec++;

				#ifdef DO_PRINT
					printf( "%i)The allocated %d Byte block starts from %d \n", ++line, blk.len, blk.ptr );
				#endif
			}
		}
	}
	
	// Checking any violation
	if ( is_violated( find_violation( blks, blks_sz ) ) ) {
		return false;
	}


	for ( i = blks_sz - 1; i >= 0; --i ) {
		half_free(blks[i].ptr);
		--blks_sz;

		#ifdef DO_PRINT
			printf( "%i)The %d Byte block starting from %d is free2\n", ++line, blks[i].len, blks[i].ptr );
		#endif
	}

	// All allocated memories have to be freed now.

	#ifdef DO_PRINT
		printf("%d random blocks are allocated and freed without any violation.\n", alc_rec);
	#endif

	ptr_1 = half_alloc(max_sz);

	if ( ptr_1 == NULL ) {
		rslt = false;

		#ifdef DO_PRINT
			printf( "Memory is defraged.\n" );
		#endif
	} else {
		half_free( ptr_1 );
	}

	return rslt;
}
Exemplo n.º 7
0
bool test_static_alc_free_violation( void ) {
	bool rslt = true;
	size_t max_sz, blks_sz;
	block_t blks[5];
	void* ptr_1;
	
	half_init();
	max_sz = find_max_block();

	blks_sz = 0;

	alloc_blk_in_arr( blks, &blks_sz, (1 << 5) + 1 );
	alloc_blk_in_arr( blks, &blks_sz, (1 << 9) - 1 );
	alloc_blk_in_arr( blks, &blks_sz, (1 << 5) + 1 );
	alloc_blk_in_arr( blks, &blks_sz, (1 << 10)    );
	alloc_blk_in_arr( blks, &blks_sz, 12345        );

	if ( blks_sz == 0 ) {
		#ifdef DO_PRINT
			printf( "Failure on allocating any memory block. The memory access violation is irrelevant.\n" );
		#endif

		return false;
	}

	// Checking any violation
	if ( is_violated( find_violation( blks, blks_sz ) ) ) {
		return false;
	}

	--blks_sz;
	half_free(blks[blks_sz].ptr);

	--blks_sz;
	half_free(blks[blks_sz].ptr);

	--blks_sz;
	half_free(blks[blks_sz].ptr);

	--blks_sz;
	half_free(blks[blks_sz].ptr);

	alloc_blk_in_arr(blks, &blks_sz, (1 << 9));

	// Checking any violation
	if ( is_violated(find_violation(blks, blks_sz)) ) {
		return false;
	}

	--blks_sz;
	half_free(blks[blks_sz].ptr);

	--blks_sz;
	half_free(blks[blks_sz].ptr);

	// Check wether all allocated memory blocks are freed.
	ptr_1 = half_alloc(max_sz);

	if ( ptr_1 == NULL ) {
		rslt = false;

		#ifdef DO_PRINT
			printf( "Memory is defraged.\n" );
		#endif
	} else {
		half_free( ptr_1 );
	}

	return rslt;
}