Mercurial > hg > aim92
view stitch/wrap.c @ 0:5242703e91d3 tip
Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author | tomwalters |
---|---|
date | Fri, 20 May 2011 15:19:45 +0100 |
parents | |
children |
line wrap: on
line source
/* wrap.c ====== encapsulate memory allocation for debugging en masse. */ #include "stitch.h" #include "wrap.h" #define LOGGING /* linked list of framed allocations */ static struct _head *lastAllocated = (struct _head *) 0 ; static void wraperr( id, hsz, tsz ) char *id ; unsigned hsz, tsz ; { printf( "memerr %s %d %d\n", id, hsz, tsz ) ; } Pointer wrap( mem, size, where ) char *mem ; unsigned size ; char where[] ; { register struct _head *head = (struct _head *) mem ; register Pointer wrapped = (Address) head + ROUNDUP( sizeof ( *head ) ) ; register struct _tail *tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( size ) ) ; #ifdef LOGGING (void) printf( "wrapping %x, %d bytes in %s\n", mem, size, where ) ; #endif head->where = where ; head->size = size ; tail->size = size ; head->previous = lastAllocated ; if( lastAllocated != 0 ) lastAllocated->pointing = &head->previous ; lastAllocated = head ; lastAllocated->pointing = &lastAllocated ; return ( wrapped ) ; } Pointer unwrap( wrapped ) Pointer wrapped ; { register struct _head *head = (struct _head *) ( (Address) wrapped - ROUNDUP( sizeof ( *head ) ) ) ; register struct _tail *tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( head->size ) ) ; #ifdef LOGGING (void) printf( "unwrapping %x, %d bytes in %s\n", head, head->size, head->where ) ; #endif if( head->size != tail->size ) wraperr( head->where, head->size, tail->size ) ; *head->pointing = head->previous ; if( head->previous != (struct _head *) 0 ) head->previous->pointing = head->pointing ; return ( (char *) head ) ; } check_wrapping( printer ) { register struct _head *head = lastAllocated ; register Pointer wrapped ; register struct _tail *tail ; while( head != (struct _head *) 0 ) { wrapped = (Pointer) ( head + 1 ) ; tail = ( struct _tail * ) ( (Address) wrapped + ROUNDUP( head->size ) ) ; if( tail->size != head->size ) wraperr( head->where, head->size, tail->size ) ; head = head->previous ; } return ; } print_wrapping() { register struct _head *head = lastAllocated ; register Pointer wrapped ; register struct _tail *tail ; while( head != (struct _head *) 0 ) { wrapped = (Pointer) ( head + 1 ) ; tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( head->size ) ) ; printf( "%d bytes \"%s\"\n", head->size, head->where ) ; if( tail->size != head->size ) wraperr( head->where, head->size, tail->size ) ; head = head->previous ; } printf( "\n" ) ; return ; }