malloc.c
void free(void *p)
{
	union Header *block;

	if (p == 0) {
		return;
	}

	/* find header */
	block = ((union Header *)p) - 1;

	/* ensure that this is actually an allocated block */
	if (!is_allocated(block)) {
		fprintf(stderr, "Invalid free at %p\n", p);
		return;
	}

	/* mark block as being free */
	block->h.flags &= ~(ALLOCATED);

	/* Attempt to coalesce with predecessor and successor blocks */
	coalesce_if_necessary(block);
	coalesce_if_necessary(block->h.prev);

#ifdef DEBUG_MALLOC
	/* scan block list to ensure that there are no pairs of adjacent
	 * free blocks */
	{
		union Header *p;
		for (p = s_head; p != 0 && p->h.next != 0; p = p->h.next) {
			union Header *succ = p->h.next;
			if (!is_allocated(p) && !is_allocated(succ)) {
				fprintf(stderr, "Freeing block %p: adjacent unallocated blocks at %p, %p\n",
					block, p, succ);
				dump_block_list();
				abort();
			}
		}
	}
#endif

#ifdef DEBUG_MALLOC
	printf("After free (of block %p):\n", block);
	dump_block_list();
#endif
}
/*
 * Free a buffer allocated with malloc.
 */
Free a buffer allocated with malloc.
Free a buffer allocated with malloc .
void free(void *p)
{
	union Header *block;
	if (p == 0) {
		return;
	}
	block = ((union Header *)p) - 1;
	if (!is_allocated(block)) {
		fprintf(stderr, "Invalid free at %p\n", p);
		return;
	}
	block->h.flags &= ~(ALLOCATED);
	coalesce_if_necessary(block);
	coalesce_if_necessary(block->h.prev);
#ifdef DEBUG_MALLOC
	{
		union Header *p;
		for (p = s_head; p != 0 && p->h.next != 0; p = p->h.next) {
			union Header *succ = p->h.next;
			if (!is_allocated(p) && !is_allocated(succ)) {
				fprintf(stderr, "Freeing block %p: adjacent unallocated blocks at %p, %p\n",
					block, p, succ);
				dump_block_list();
				abort();
			}
		}
	}
#endif
#ifdef DEBUG_MALLOC
	printf("After free (of block %p):\n", block);
	dump_block_list();
#endif
}
void free ( void * p ) { union Header * block ; if ( p == 0 ) { return ; } block = ( ( union Header * ) p ) - 1 ; if ( ! is_allocated ( block ) ) { fprintf ( stderr , " \n " , p ) ; return ; } block -> h . flags &= ~ ( ALLOCATED ) ; coalesce_if_necessary ( block ) ; coalesce_if_necessary ( block -> h . prev ) ; #ifdef DEBUG_MALLOC { union Header * p ; for ( p = s_head ; p != 0 && p -> h . next != 0 ; p = p -> h . next ) { union Header * succ = p -> h . next ; if ( ! is_allocated ( p ) && ! is_allocated ( succ ) ) { fprintf ( stderr , " \n " , block , p , succ ) ; dump_block_list ( ) ; abort ( ) ; } } } #endif #ifdef DEBUG_MALLOC printf ( " \n " , block ) ; dump_block_list ( ) ; #endif }
Free a buffer allocated with malloc.
Free a buffer allocated with malloc .
/* find header */ /* ensure that this is actually an allocated block */ /* mark block as being free */ /* Attempt to coalesce with predecessor and successor blocks */ /* scan block list to ensure that there are no pairs of adjacent
	 * free blocks */