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