Mercurial > hg > aim92
comparison stitch/stitch.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 |
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 ========== | |
26 stitch.c | |
27 ========== | |
28 | |
29 | |
30 Copyright (c), 1989 John Holdsworth, Medical Research Council, Applied Psychology Unit. | |
31 | |
32 | |
33 Author : John Holdsworth | |
34 Written : 22th March, 1989. | |
35 | |
36 Edited : | |
37 | |
38 | |
39 provides an unvarying interface for allocation and copying operations. | |
40 | |
41 */ | |
42 | |
43 #include <stdio.h> | |
44 | |
45 #include <string.h> | |
46 #if defined( NeXT ) | |
47 #include <stdlib.h> | |
48 #else | |
49 #include <malloc.h> | |
50 #endif | |
51 | |
52 #ifndef _STITCH_H_ | |
53 #include "stitch.h" | |
54 #endif | |
55 | |
56 #if defined( sun ) | |
57 extern bcopy(), bzero(), exit() ; | |
58 #else | |
59 #ifndef NeXT | |
60 extern void bcopy(), bzero(), free(), exit() ; | |
61 #endif | |
62 #endif | |
63 | |
64 #ifndef lint | |
65 static char *stitch_sccs_id = "@(#)stitch.c 1.19 J. Holdsworth (MRC-APU) 6/6/91" ; | |
66 #endif | |
67 | |
68 #ifndef WRAPPING | |
69 #define WRAPPING 0 | |
70 #endif | |
71 | |
72 #if WRAPPING | |
73 | |
74 #include "wrap.h" | |
75 | |
76 Pointer wrap_malloc( size, where ) | |
77 unsigned size ; | |
78 char where[] ; | |
79 { | |
80 return ( wrap( malloc( WRAPPED_SIZE( size ) ), size, where ) ) ; | |
81 } | |
82 | |
83 Pointer wrap_realloc( old, size, where ) | |
84 char *old ; | |
85 unsigned size ; | |
86 char where[] ; | |
87 { | |
88 return ( wrap( realloc( unwrap( old ), WRAPPED_SIZE( size ) ), where ) ) ; | |
89 } | |
90 | |
91 void wrap_free( ptr ) | |
92 Pointer ptr ; | |
93 { | |
94 free( (char *) unwrap( ptr ) ) ; | |
95 | |
96 return ; | |
97 } | |
98 #else | |
99 Pointer wrap_malloc( size, where ) | |
100 unsigned size ; | |
101 char where[] ; | |
102 { | |
103 #ifdef lint | |
104 where ; | |
105 #endif | |
106 return ( malloc( size ) ) ; | |
107 } | |
108 | |
109 Pointer wrap_realloc( old, size, where ) | |
110 char *old ; | |
111 unsigned size ; | |
112 char where[] ; | |
113 { | |
114 #ifdef lint | |
115 where ; | |
116 #endif | |
117 #ifdef DSP32 | |
118 stitch_error( "realloc called" ) ; | |
119 #else | |
120 return ( realloc( old, size ) ) ; | |
121 #endif | |
122 } | |
123 | |
124 void wrap_free( ptr ) | |
125 Pointer ptr ; | |
126 { | |
127 free( ptr ) ; | |
128 | |
129 return ; | |
130 } | |
131 #endif | |
132 | |
133 #ifdef DSP32 | |
134 extern char _EAdata[], _EUdata[] ; | |
135 char *freemem = _EUdata ; | |
136 char errstr[200] ; | |
137 | |
138 int abs( n ) | |
139 int n ; | |
140 { | |
141 if( n >= 0 ) | |
142 return n ; | |
143 else | |
144 return -n ; | |
145 } | |
146 | |
147 #endif | |
148 | |
149 void stitch_error( string, thing ) | |
150 char *string ; | |
151 char *thing ; | |
152 { | |
153 int i ; | |
154 #ifdef DSP32 | |
155 extern int errno ; | |
156 | |
157 (void) strcpy( errstr, string ) ; | |
158 errno = thing - (char *) 0 ; | |
159 #else | |
160 (void) fprintf( stderr, string, thing ) ; | |
161 #endif | |
162 | |
163 #ifdef PC | |
164 /* put in delay to be able to read message if any */ | |
165 | |
166 for(i=0 ; i<3000 ; i++ ) | |
167 (void) sqrt( 2. ) ; | |
168 #endif | |
169 | |
170 stitch_exit( 1 ) ; | |
171 } | |
172 | |
173 static void failed( bytes, where ) | |
174 unsigned bytes ; | |
175 char *where ; | |
176 { | |
177 static char msg[200] = "Memory allocation error, allocating %u bytes in " ; | |
178 | |
179 stitch_error( strcat( msg, where ), ( Pointer ) 0 + bytes ) ; | |
180 } | |
181 | |
182 char stitchStructStr[] = "allocating a structure somewhere" ; | |
183 | |
184 /* memory usage indicators */ | |
185 | |
186 static long sused ; | |
187 static long bused ; | |
188 | |
189 Pointer stitch_malloc( bytes, where ) | |
190 unsigned bytes ; | |
191 char *where ; | |
192 { | |
193 Pointer ptr ; | |
194 | |
195 if( where == stitchStructStr ) | |
196 sused += bytes ; | |
197 else | |
198 bused += bytes ; | |
199 | |
200 #ifdef NOT_DSP32_ANYMORE | |
201 int size = bytes + 3 >> 2 << 2 ; | |
202 | |
203 ptr = freemem ; | |
204 | |
205 if( ( freemem += size ) > _EAdata ) | |
206 #else | |
207 if( ( ptr = wrap_malloc( bytes, where ) ) == ( Pointer ) 0 ) | |
208 #endif | |
209 failed( bytes, where ) ; | |
210 | |
211 return ( ptr ) ; | |
212 } | |
213 | |
214 Pointer stitch_ralloc( bytes, where ) | |
215 unsigned bytes ; | |
216 char *where ; | |
217 { | |
218 #ifdef DSP32 | |
219 extern char _EAram2[], _EUram2[] ; | |
220 static char *mempt = _EUram2 ; | |
221 int size ; | |
222 | |
223 size = bytes + 3 >> 2 << 2 ; | |
224 | |
225 if( mempt + size <= _EAram2 ) | |
226 return ( ( mempt += size ) - size ) ; | |
227 #endif | |
228 #ifdef NICHE | |
229 | |
230 #ifdef NICHEE | |
231 #include <trillium/ram.h> | |
232 #else | |
233 #define ONCHIP 2 | |
234 #endif | |
235 | |
236 Pointer ptr ; | |
237 | |
238 if( ( ptr = valloc( items * size, ONCHIP, size ) ) == ( char * ) 0 ) | |
239 return ( ptr ) ; | |
240 #endif | |
241 | |
242 return ( stitch_malloc( bytes, where ) ) ; | |
243 } | |
244 | |
245 Pointer stitch_calloc( number, size, where ) | |
246 unsigned number, size ; | |
247 char *where ; | |
248 { | |
249 Pointer ptr ; | |
250 #if defined( NICHE ) || defined( DSP32 ) | |
251 if( ( ptr = stitch_malloc( number * size ) ) == ( char * ) 0 ) | |
252 failed( number * size, where ) ; | |
253 | |
254 stitch_bzero( ptr, ( int ) number * size ) ; | |
255 | |
256 #else | |
257 if( ( ptr = ( char * ) calloc( number, size ) ) == ( char * ) 0 ) | |
258 failed( number * size, where ) ; | |
259 #endif | |
260 return ( ptr ) ; | |
261 } | |
262 | |
263 Pointer stitch_zeroed_malloc( size, where ) | |
264 unsigned size ; | |
265 char *where ; | |
266 { | |
267 Pointer ptr = stitch_malloc( size, where ) ; | |
268 | |
269 stitch_bzero( ptr, size ) ; | |
270 | |
271 return ( ptr ) ; | |
272 } | |
273 | |
274 Pointer stitch_copied_malloc( size, model, where ) | |
275 unsigned size ; | |
276 Pointer model ; | |
277 char *where ; | |
278 { | |
279 Pointer ptr = stitch_malloc( size, where ) ; | |
280 | |
281 stitch_bcopy( model, ptr, size ) ; | |
282 | |
283 return ( ptr ) ; | |
284 } | |
285 | |
286 void stitch_free( pointer ) | |
287 Pointer pointer ; | |
288 { | |
289 if( pointer == (Pointer) 0 ) | |
290 stitch_error( "attempt to free null pointer" ) ; | |
291 | |
292 #ifndef DSP32 | |
293 wrap_free( pointer ) ; | |
294 #endif | |
295 return ; | |
296 } | |
297 | |
298 void stitch_exit( status ) | |
299 int status ; | |
300 { | |
301 #if 00 | |
302 (void) fprintf( stderr, "memory usage: %d for structures, %d for buffers\n", sused, bused ) ; | |
303 #endif | |
304 | |
305 #ifdef NICHE | |
306 kexit( status ) ; | |
307 #else | |
308 exit( status ) ; | |
309 #endif | |
310 } | |
311 | |
312 /* beware these defintions - they make stitch.h mandatory */ | |
313 | |
314 #if defined( vax ) || defined( mips ) || defined( sun ) | |
315 | |
316 void (*stitch_bcopy)() = ( void ( * ) () ) bcopy ; | |
317 | |
318 #else | |
319 | |
320 #ifdef PC | |
321 | |
322 /* memmove must be able to cope with overlapping buffers */ | |
323 | |
324 static void call_memmove( from, to, bytes ) | |
325 char *from, *to ; | |
326 unsigned bytes ; | |
327 { | |
328 ( void ) memmove( to, from, bytes ) ; | |
329 return ; | |
330 } | |
331 | |
332 void (*stitch_bcopy)() = call_memmove ; | |
333 | |
334 #else | |
335 | |
336 static void reserve_bcopy( from, to, number ) | |
337 Pointer from, to ; | |
338 int number ; | |
339 { | |
340 register char *i, *j, *end ; | |
341 | |
342 if( from > to ) { | |
343 for( i = from, j = to, end = from + number ; ( end - i ) % 4 > 0 ; ) | |
344 *j++ = *i++ ; | |
345 | |
346 if( i < end ) | |
347 do | |
348 { | |
349 *j++ = *i++ ; | |
350 *j++ = *i++ ; | |
351 *j++ = *i++ ; | |
352 *j++ = *i++ ; | |
353 } | |
354 while( i < end ) ; | |
355 } | |
356 else if( from < to ) { | |
357 for( i = from + number, j = to + number, end = from ; ( i - end ) % 4 > 0 ; ) | |
358 *--j = *--i ; | |
359 | |
360 if( i > end ) | |
361 do | |
362 { | |
363 *--j = *--i ; | |
364 *--j = *--i ; | |
365 *--j = *--i ; | |
366 *--j = *--i ; | |
367 } | |
368 while( i > end ) ; | |
369 } | |
370 | |
371 return ; | |
372 } | |
373 | |
374 void (*stitch_bcopy)() = reserve_bcopy ; | |
375 | |
376 #endif | |
377 | |
378 #endif | |
379 | |
380 | |
381 | |
382 #if defined( vax ) || defined( mips ) || defined( sun ) | |
383 | |
384 void (*stitch_bzero)() = ( void ( * ) () ) bzero ; | |
385 | |
386 #else | |
387 | |
388 #if defined( PC ) || defined( DSP32 ) | |
389 | |
390 static void call_memset( to, bytes ) | |
391 char *to ; | |
392 int bytes ; | |
393 { | |
394 (void) memset( to, '\000', bytes ) ; | |
395 } | |
396 | |
397 void (*stitch_bzero)() = call_memset ; | |
398 | |
399 #else | |
400 | |
401 static void reserve_bzero( to, bytes ) | |
402 char *to ; | |
403 int bytes ; | |
404 { | |
405 register char *ptr = to ; | |
406 register char *end = to + bytes ; | |
407 | |
408 while( ptr < end ) | |
409 *ptr++ = 0 ; | |
410 | |
411 return ; | |
412 } | |
413 | |
414 void (*stitch_bzero)() = reserve_bzero ; | |
415 | |
416 #endif | |
417 | |
418 #endif | |
419 |