tomwalters@0: /* tomwalters@0: wrap.c tomwalters@0: ====== tomwalters@0: tomwalters@0: encapsulate memory allocation for debugging en masse. tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: #include "stitch.h" tomwalters@0: #include "wrap.h" tomwalters@0: tomwalters@0: #define LOGGING tomwalters@0: tomwalters@0: /* linked list of framed allocations */ tomwalters@0: tomwalters@0: static struct _head *lastAllocated = (struct _head *) 0 ; tomwalters@0: tomwalters@0: tomwalters@0: static void wraperr( id, hsz, tsz ) tomwalters@0: char *id ; tomwalters@0: unsigned hsz, tsz ; tomwalters@0: { tomwalters@0: printf( "memerr %s %d %d\n", id, hsz, tsz ) ; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: Pointer wrap( mem, size, where ) tomwalters@0: char *mem ; tomwalters@0: unsigned size ; tomwalters@0: char where[] ; tomwalters@0: { tomwalters@0: register struct _head *head = (struct _head *) mem ; tomwalters@0: register Pointer wrapped = (Address) head + ROUNDUP( sizeof ( *head ) ) ; tomwalters@0: register struct _tail *tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( size ) ) ; tomwalters@0: tomwalters@0: #ifdef LOGGING tomwalters@0: (void) printf( "wrapping %x, %d bytes in %s\n", mem, size, where ) ; tomwalters@0: #endif tomwalters@0: tomwalters@0: head->where = where ; tomwalters@0: head->size = size ; tomwalters@0: tomwalters@0: tail->size = size ; tomwalters@0: tomwalters@0: head->previous = lastAllocated ; tomwalters@0: tomwalters@0: if( lastAllocated != 0 ) tomwalters@0: lastAllocated->pointing = &head->previous ; tomwalters@0: tomwalters@0: lastAllocated = head ; tomwalters@0: lastAllocated->pointing = &lastAllocated ; tomwalters@0: tomwalters@0: return ( wrapped ) ; tomwalters@0: } tomwalters@0: tomwalters@0: tomwalters@0: Pointer unwrap( wrapped ) tomwalters@0: Pointer wrapped ; tomwalters@0: { tomwalters@0: register struct _head *head = (struct _head *) ( (Address) wrapped - ROUNDUP( sizeof ( *head ) ) ) ; tomwalters@0: register struct _tail *tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( head->size ) ) ; tomwalters@0: tomwalters@0: #ifdef LOGGING tomwalters@0: (void) printf( "unwrapping %x, %d bytes in %s\n", head, head->size, head->where ) ; tomwalters@0: #endif tomwalters@0: tomwalters@0: if( head->size != tail->size ) tomwalters@0: wraperr( head->where, head->size, tail->size ) ; tomwalters@0: tomwalters@0: *head->pointing = head->previous ; tomwalters@0: tomwalters@0: if( head->previous != (struct _head *) 0 ) tomwalters@0: head->previous->pointing = head->pointing ; tomwalters@0: tomwalters@0: return ( (char *) head ) ; tomwalters@0: } tomwalters@0: tomwalters@0: check_wrapping( printer ) tomwalters@0: { tomwalters@0: register struct _head *head = lastAllocated ; tomwalters@0: register Pointer wrapped ; tomwalters@0: register struct _tail *tail ; tomwalters@0: tomwalters@0: while( head != (struct _head *) 0 ) { tomwalters@0: tomwalters@0: wrapped = (Pointer) ( head + 1 ) ; tomwalters@0: tomwalters@0: tail = ( struct _tail * ) ( (Address) wrapped + ROUNDUP( head->size ) ) ; tomwalters@0: tomwalters@0: if( tail->size != head->size ) tomwalters@0: wraperr( head->where, head->size, tail->size ) ; tomwalters@0: tomwalters@0: head = head->previous ; tomwalters@0: } tomwalters@0: tomwalters@0: return ; tomwalters@0: } tomwalters@0: tomwalters@0: print_wrapping() tomwalters@0: { tomwalters@0: register struct _head *head = lastAllocated ; tomwalters@0: register Pointer wrapped ; tomwalters@0: register struct _tail *tail ; tomwalters@0: tomwalters@0: while( head != (struct _head *) 0 ) { tomwalters@0: tomwalters@0: wrapped = (Pointer) ( head + 1 ) ; tomwalters@0: tomwalters@0: tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( head->size ) ) ; tomwalters@0: tomwalters@0: printf( "%d bytes \"%s\"\n", head->size, head->where ) ; tomwalters@0: if( tail->size != head->size ) tomwalters@0: wraperr( head->where, head->size, tail->size ) ; tomwalters@0: tomwalters@0: head = head->previous ; tomwalters@0: } tomwalters@0: tomwalters@0: printf( "\n" ) ; tomwalters@0: tomwalters@0: return ; tomwalters@0: } tomwalters@0: