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