Mercurial > hg > aim92
comparison stitch/stitch.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 Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989 | |
3 =========================================================================== | |
4 | |
5 Permission to use, copy, modify, and distribute this software without fee | |
6 is hereby granted for research purposes, provided that this copyright | |
7 notice appears in all copies and in all supporting documentation, and that | |
8 the software is not redistributed for any fee (except for a nominal shipping | |
9 charge). Anyone wanting to incorporate all or part of this software in a | |
10 commercial product must obtain a license from the Medical Research Council. | |
11 | |
12 The MRC makes no representations about the suitability of this | |
13 software for any purpose. It is provided "as is" without express or implied | |
14 warranty. | |
15 | |
16 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING | |
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE | |
18 A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY | |
19 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN | |
20 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
22 */ | |
23 | |
24 /* | |
25 stitch.h | |
26 ======== | |
27 | |
28 interface for stitch system. | |
29 | |
30 This contains many regrettable #defines which are the act of a parnoid | |
31 bad typist who wishes to lerave nothing to chance. They are not as hostile | |
32 as they might first seem being mostly to do with methodical means of | |
33 allocation making more use of typeing to avoid any chance of a badly | |
34 allocated data buffer. This file is supported by two other abstractions: | |
35 buffer.[ch] which ensures a buffer is the size required without realoocation. | |
36 wrap.[ch] which encapsulates allocations to detect buffer overflows. | |
37 | |
38 In general there are two levels of indirection between the macros defined | |
39 below and the final calls for memory allocation from the C library function. | |
40 The first level is a C pre-processor macro to get all the catsings right | |
41 for lint and do things with types that procedures just can't do. | |
42 The second level is just passes the allocation call on so it can | |
43 be intercepted as an aid to portability. | |
44 | |
45 */ | |
46 | |
47 | |
48 #ifndef _STITCH_H_ | |
49 #define _STITCH_H_ | |
50 | |
51 /* redefinitions of general memory allocation to check number and cast argumnets */ | |
52 | |
53 #define Malloc( _size, _where ) stitch_malloc( (unsigned) ( _size ), _where ) | |
54 #define Ralloc( _size, _where ) stitch_ralloc( (unsigned) ( _size ), _where ) | |
55 #define Calloc( _size, _items, _where ) stitch_malloc( (unsigned) ( _size ), (unsigned) ( _items ), _where ) | |
56 #define Realloc( _old, _size , _where ) stitch_realloc( (Pointer) ( _old ), (unsigned) ( _size ) ) | |
57 | |
58 /* deallocation - delete destroys pointer to keep it out of trouble */ | |
59 | |
60 #define Free( _old ) stitch_free( (Pointer) ( _old ) ) | |
61 #define Delete( _variable ) ( Free( _variable ), _variable = 0 ) | |
62 | |
63 /* more names for the allocation */ | |
64 | |
65 #define Allocate( _size, _where ) stitch_malloc( (unsigned) ( _size ), _where ) | |
66 #define AllocateZeroed( _size, _where ) stitch_zeroed_malloc( (unsigned) ( _size ), _where ) | |
67 #define AllocateCopied( _size, _model, _where ) stitch_copied_malloc( (unsigned) ( _size ), (Pointer) _model, _where ) | |
68 | |
69 #define Reallocate( _old, _size ) stitchRealloc( (Pointer) ( _old ), (unsigned) ( _size ) ) | |
70 | |
71 /* macros for allocation making more use of type of variable receiving pointer */ | |
72 | |
73 #define TypeSize( _pointer ) ( sizeof * (_pointer) 0 ) | |
74 | |
75 /* return pointer to New structure, random, zeroed or copied */ | |
76 | |
77 #define New( _type ) (_type ) Allocate( TypeSize( _type ), stitchStructStr ) | |
78 #define NewZeroed( _type ) (_type ) AllocateZeroed( TypeSize( _type ), stitchStructStr ) | |
79 #define NewCopied( _type, _model ) (_type ) AllocateCopied( TypeSize( _type ), _model, stitchStructStr ) | |
80 | |
81 /* return pointer to array of structures */ | |
82 | |
83 #define NewArray( _type, _n, _where ) (_type *) Allocate( sizeof (_type) * (unsigned) ( _n ), _where ) | |
84 #define NewZeroedArray( _type, _n, _where ) (_type *) AllocateZeroed( sizeof (_type) * (unsigned) ( _n ), _where ) | |
85 #define NewCopiedArray( _type, _n, _model, _where ) (_type *) AllocateCopied( sizeof (_type) * (unsigned) ( _n ), _model, _where ) | |
86 | |
87 /* as above but declare variable to receive pointer as well like new in C++ */ | |
88 | |
89 #define DeclareNew( _type, _name ) _type _name = New( _type ) | |
90 #define DeclareNewZeroed( _type, _name ) _type _name = NewZeroed( _type ) | |
91 #define DeclareNewCopied( _type, _name, _model ) _type _name = NewCopied( _type, _model ) | |
92 | |
93 #define DeclareNewArray( _type, _name, _n, _where ) _type *_name = NewArray( _type, _n, _where ) | |
94 #define DeclareNewZeroedArray( _type, _name, _n, _where ) _type *_name = NewZeroedArray( _type, _n, _where ) | |
95 #define DeclareNewCopiedArray( _type, _name, _n, _model, _where ) _type *_name = NewCopiedArray( _type, _n, _model, _where ) | |
96 | |
97 /* structure utilities */ | |
98 | |
99 #define CopyArray( _from, _to, _number ) ( stitch_bcopy( _from, _to, sizeof ( * ( _to ) ) * ( _number ) ), _to ) | |
100 #define ZeroArray( _to, _number ) ( stitch_bzero( _to, sizeof ( * ( _to ) ) * ( _number ) ), _to ) | |
101 | |
102 #define Copy( _from, _to ) ( stitch_bcopy( _from, _to, sizeof ( * ( _to ) ) ), _to ) | |
103 #define Zero( _to ) ( stitch_bzero( _to, sizeof ( * ( _to ) ) ), _to ) | |
104 | |
105 #if 0 | |
106 | |
107 /* defines for new names for now ( removed for clarity ) */ | |
108 | |
109 #define stitchMalloc( _size, _where ) stitch_malloc( (unsigned) _size, _where ) | |
110 #define stitchZeroedMalloc( _size, _where ) stitch_zeroed_malloc( (unsigned) _size, _where ) | |
111 #define stitchCopiedMalloc( _size, _model, _where ) stitch_copied_malloc( (unsigned) _size, _model, _where ) | |
112 | |
113 #define stitchRealloc( _pt, _size, _where ) stitch_ralloc( (Pointer) _pt, (unsigned) _size, _where ) | |
114 | |
115 #define stitchFree( _old ) stitch_free( _old ) | |
116 #endif | |
117 | |
118 | |
119 /* realy important typedefs.... */ | |
120 | |
121 #if PEDANTIC | |
122 typedef struct _void *Pointer ; /* pointer to object of unknown size effectively void * */ | |
123 #else | |
124 typedef char *Pointer ; | |
125 #endif | |
126 typedef char *Address ; | |
127 | |
128 | |
129 /* a portable inteface to the system */ | |
130 | |
131 extern void stitch_error() ; | |
132 | |
133 extern Pointer stitch_malloc() ; | |
134 extern Pointer stitch_copied_malloc() ; | |
135 extern Pointer stitch_zeroed_malloc() ; | |
136 extern Pointer stitch_ralloc() ; | |
137 extern Pointer stitch_calloc() ; | |
138 | |
139 extern void stitch_free() ; | |
140 extern void stitch_exit() ; | |
141 | |
142 /* beware these definitions, they are for speed and fail if stitch.h not included */ | |
143 | |
144 extern void (*stitch_bcopy)() ; | |
145 extern void (*stitch_bzero)() ; | |
146 | |
147 extern char stitchStructStr[] ; | |
148 | |
149 #endif |