tomwalters@0
|
1 /*
|
tomwalters@0
|
2 wrap.h
|
tomwalters@0
|
3 ======
|
tomwalters@0
|
4
|
tomwalters@0
|
5 wrap alocations with debugging information for program verifiaction.
|
tomwalters@0
|
6
|
tomwalters@0
|
7 */
|
tomwalters@0
|
8
|
tomwalters@0
|
9 /*
|
tomwalters@0
|
10 The following routines "wrap" memory with a frame as it is allocated.
|
tomwalters@0
|
11 The information in the frame is sufficient to check if the bounds of
|
tomwalters@0
|
12 the alloacation have been exceeded. Part of the frame is used as a
|
tomwalters@0
|
13 pointer for a linked list which is also assembled containing all
|
tomwalters@0
|
14 allocated memory chunks. This enables a call at any time to the
|
tomwalters@0
|
15 routine "checkWrapping()" to examine all the frames of all memory
|
tomwalters@0
|
16 allocations for corruption while an application runs.
|
tomwalters@0
|
17
|
tomwalters@0
|
18
|
tomwalters@0
|
19 Memory map of a framed allocation after "wrapping"
|
tomwalters@0
|
20
|
tomwalters@0
|
21 +--------------+
|
tomwalters@0
|
22 head--> | struct _head |
|
tomwalters@0
|
23 +--------------+
|
tomwalters@0
|
24 wrapped--> | user data |
|
tomwalters@0
|
25 | . |
|
tomwalters@0
|
26 | . |
|
tomwalters@0
|
27 | . |
|
tomwalters@0
|
28 | |
|
tomwalters@0
|
29 +--------------+
|
tomwalters@0
|
30 tail--> | struct _tail |
|
tomwalters@0
|
31 +--------------+
|
tomwalters@0
|
32
|
tomwalters@0
|
33
|
tomwalters@0
|
34 On allocation the pointer returned from tyhe system becomes the
|
tomwalters@0
|
35 pointer to in instance of the stucture "_head". A tail structure is
|
tomwalters@0
|
36 then constructed containing a repeat of the size feild of the header
|
tomwalters@0
|
37 for consistency checking and a pointer to the previous allocation's
|
tomwalters@0
|
38 frame. The user receives a pointer to the memory bytes availiable
|
tomwalters@0
|
39 after the head structure and is therefore unaware of the wrapping
|
tomwalters@0
|
40 process. On deallocation or reallocation the framed memory is simply
|
tomwalters@0
|
41 unwrapped by the reverse process checking for corruption before being
|
tomwalters@0
|
42 passed to the system memory allocation routines.
|
tomwalters@0
|
43
|
tomwalters@0
|
44 */
|
tomwalters@0
|
45
|
tomwalters@0
|
46 /* bits rounded up in alligning the tail structure */
|
tomwalters@0
|
47
|
tomwalters@0
|
48 #define BITS_ROUND 4
|
tomwalters@0
|
49
|
tomwalters@0
|
50 /* rounds up byte size specification to ensure tail structure's elements are aligned */
|
tomwalters@0
|
51
|
tomwalters@0
|
52 #define ROUNDUP( _size ) ( ( _size ) + ( 1 << BITS_ROUND ) - 1 >> BITS_ROUND << BITS_ROUND )
|
tomwalters@0
|
53
|
tomwalters@0
|
54 /* Calculates the size required for the wrapped allocation including frame. */
|
tomwalters@0
|
55
|
tomwalters@0
|
56 #define WRAPPED_SIZE( _size ) ( ROUNDUP( sizeof ( struct _head ) ) + ROUNDUP( _size ) + sizeof( struct _tail ) )
|
tomwalters@0
|
57
|
tomwalters@0
|
58
|
tomwalters@0
|
59 struct _head { char *where ; struct _head **pointing, *previous ; unsigned size ; } ;
|
tomwalters@0
|
60 struct _tail { unsigned size ; } ;
|
tomwalters@0
|
61
|
tomwalters@0
|
62 extern Pointer wrap( /* char *mem, unsigned size, char where[] */ ) ;
|
tomwalters@0
|
63 extern char *unwrap( /* Pointer mem, unsigned size */ ) ;
|
tomwalters@0
|
64
|
tomwalters@0
|
65
|