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