malloc.c
void *malloc(size_t size)
{
	size_t required_block_size;
	union Header *block;

	/* calculate the minimum block size needed for this allocation */
	required_block_size = round_to_multiple(size + sizeof(union Header), sizeof(union Header));

	/* search list for an unallocated block of sufficient size */
	for (block = s_head; block != 0; block = block->h.next) {
		if (block->h.size >= required_block_size && !is_allocated(block)) {
			break;
		}
	}

	/* if no sufficiently-large block was found, allocate one and append it to list */
	if (block == 0) {
		block = alloc_block(required_block_size);
		if (block == 0) {
			/* failed to allocate a new block */
			errno = ENOMEM;
			return 0;
		}
	}

	/* if block size exceeds required block size by more than the size of one header,
	 * then split it */
	split_block_if_necessary(block, required_block_size);

	/* mark the block as allocated */
	block->h.flags |= ALLOCATED;

#ifdef DEBUG_MALLOC
	printf("After malloc (of block %p):\n", block);
	dump_block_list();
#endif

	return (void*) (block + 1);
}
/*
 * Allocate a buffer of given size.
 */
Allocate a buffer of given size.
Allocate a buffer of given size .
void *malloc(size_t size)
{
	size_t required_block_size;
	union Header *block;
	required_block_size = round_to_multiple(size + sizeof(union Header), sizeof(union Header));
	for (block = s_head; block != 0; block = block->h.next) {
		if (block->h.size >= required_block_size && !is_allocated(block)) {
			break;
		}
	}
	if (block == 0) {
		block = alloc_block(required_block_size);
		if (block == 0) {
			errno = ENOMEM;
			return 0;
		}
	}
	split_block_if_necessary(block, required_block_size);
	block->h.flags |= ALLOCATED;
#ifdef DEBUG_MALLOC
	printf("After malloc (of block %p):\n", block);
	dump_block_list();
#endif
	return (void*) (block + 1);
}
void * malloc ( size_t size ) { size_t required_block_size ; union Header * block ; required_block_size = round_to_multiple ( size + sizeof ( union Header ) , sizeof ( union Header ) ) ; for ( block = s_head ; block != 0 ; block = block -> h . next ) { if ( block -> h . size >= required_block_size && ! is_allocated ( block ) ) { break ; } } if ( block == 0 ) { block = alloc_block ( required_block_size ) ; if ( block == 0 ) { errno = ENOMEM ; return 0 ; } } split_block_if_necessary ( block , required_block_size ) ; block -> h . flags |= ALLOCATED ; #ifdef DEBUG_MALLOC printf ( " \n " , block ) ; dump_block_list ( ) ; #endif return ( void * ) ( block + 1 ) ; }
Allocate a buffer of given size.
Allocate a buffer of given size .
/* calculate the minimum block size needed for this allocation */ /* search list for an unallocated block of sufficient size */ /* if no sufficiently-large block was found, allocate one and append it to list */ /* failed to allocate a new block */ /* if block size exceeds required block size by more than the size of one header,
	 * then split it */ /* mark the block as allocated */