annotate 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
rev   line source
tomwalters@0 1 /*
tomwalters@0 2 wrap.c
tomwalters@0 3 ======
tomwalters@0 4
tomwalters@0 5 encapsulate memory allocation for debugging en masse.
tomwalters@0 6
tomwalters@0 7 */
tomwalters@0 8
tomwalters@0 9 #include "stitch.h"
tomwalters@0 10 #include "wrap.h"
tomwalters@0 11
tomwalters@0 12 #define LOGGING
tomwalters@0 13
tomwalters@0 14 /* linked list of framed allocations */
tomwalters@0 15
tomwalters@0 16 static struct _head *lastAllocated = (struct _head *) 0 ;
tomwalters@0 17
tomwalters@0 18
tomwalters@0 19 static void wraperr( id, hsz, tsz )
tomwalters@0 20 char *id ;
tomwalters@0 21 unsigned hsz, tsz ;
tomwalters@0 22 {
tomwalters@0 23 printf( "memerr %s %d %d\n", id, hsz, tsz ) ;
tomwalters@0 24 }
tomwalters@0 25
tomwalters@0 26
tomwalters@0 27 Pointer wrap( mem, size, where )
tomwalters@0 28 char *mem ;
tomwalters@0 29 unsigned size ;
tomwalters@0 30 char where[] ;
tomwalters@0 31 {
tomwalters@0 32 register struct _head *head = (struct _head *) mem ;
tomwalters@0 33 register Pointer wrapped = (Address) head + ROUNDUP( sizeof ( *head ) ) ;
tomwalters@0 34 register struct _tail *tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( size ) ) ;
tomwalters@0 35
tomwalters@0 36 #ifdef LOGGING
tomwalters@0 37 (void) printf( "wrapping %x, %d bytes in %s\n", mem, size, where ) ;
tomwalters@0 38 #endif
tomwalters@0 39
tomwalters@0 40 head->where = where ;
tomwalters@0 41 head->size = size ;
tomwalters@0 42
tomwalters@0 43 tail->size = size ;
tomwalters@0 44
tomwalters@0 45 head->previous = lastAllocated ;
tomwalters@0 46
tomwalters@0 47 if( lastAllocated != 0 )
tomwalters@0 48 lastAllocated->pointing = &head->previous ;
tomwalters@0 49
tomwalters@0 50 lastAllocated = head ;
tomwalters@0 51 lastAllocated->pointing = &lastAllocated ;
tomwalters@0 52
tomwalters@0 53 return ( wrapped ) ;
tomwalters@0 54 }
tomwalters@0 55
tomwalters@0 56
tomwalters@0 57 Pointer unwrap( wrapped )
tomwalters@0 58 Pointer wrapped ;
tomwalters@0 59 {
tomwalters@0 60 register struct _head *head = (struct _head *) ( (Address) wrapped - ROUNDUP( sizeof ( *head ) ) ) ;
tomwalters@0 61 register struct _tail *tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( head->size ) ) ;
tomwalters@0 62
tomwalters@0 63 #ifdef LOGGING
tomwalters@0 64 (void) printf( "unwrapping %x, %d bytes in %s\n", head, head->size, head->where ) ;
tomwalters@0 65 #endif
tomwalters@0 66
tomwalters@0 67 if( head->size != tail->size )
tomwalters@0 68 wraperr( head->where, head->size, tail->size ) ;
tomwalters@0 69
tomwalters@0 70 *head->pointing = head->previous ;
tomwalters@0 71
tomwalters@0 72 if( head->previous != (struct _head *) 0 )
tomwalters@0 73 head->previous->pointing = head->pointing ;
tomwalters@0 74
tomwalters@0 75 return ( (char *) head ) ;
tomwalters@0 76 }
tomwalters@0 77
tomwalters@0 78 check_wrapping( printer )
tomwalters@0 79 {
tomwalters@0 80 register struct _head *head = lastAllocated ;
tomwalters@0 81 register Pointer wrapped ;
tomwalters@0 82 register struct _tail *tail ;
tomwalters@0 83
tomwalters@0 84 while( head != (struct _head *) 0 ) {
tomwalters@0 85
tomwalters@0 86 wrapped = (Pointer) ( head + 1 ) ;
tomwalters@0 87
tomwalters@0 88 tail = ( struct _tail * ) ( (Address) wrapped + ROUNDUP( head->size ) ) ;
tomwalters@0 89
tomwalters@0 90 if( tail->size != head->size )
tomwalters@0 91 wraperr( head->where, head->size, tail->size ) ;
tomwalters@0 92
tomwalters@0 93 head = head->previous ;
tomwalters@0 94 }
tomwalters@0 95
tomwalters@0 96 return ;
tomwalters@0 97 }
tomwalters@0 98
tomwalters@0 99 print_wrapping()
tomwalters@0 100 {
tomwalters@0 101 register struct _head *head = lastAllocated ;
tomwalters@0 102 register Pointer wrapped ;
tomwalters@0 103 register struct _tail *tail ;
tomwalters@0 104
tomwalters@0 105 while( head != (struct _head *) 0 ) {
tomwalters@0 106
tomwalters@0 107 wrapped = (Pointer) ( head + 1 ) ;
tomwalters@0 108
tomwalters@0 109 tail = (struct _tail *) ( (Address) wrapped + ROUNDUP( head->size ) ) ;
tomwalters@0 110
tomwalters@0 111 printf( "%d bytes \"%s\"\n", head->size, head->where ) ;
tomwalters@0 112 if( tail->size != head->size )
tomwalters@0 113 wraperr( head->where, head->size, tail->size ) ;
tomwalters@0 114
tomwalters@0 115 head = head->previous ;
tomwalters@0 116 }
tomwalters@0 117
tomwalters@0 118 printf( "\n" ) ;
tomwalters@0 119
tomwalters@0 120 return ;
tomwalters@0 121 }
tomwalters@0 122