Mercurial > hg > aim92
view 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 |
line wrap: on
line source
/* Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989 =========================================================================== Permission to use, copy, modify, and distribute this software without fee is hereby granted for research purposes, provided that this copyright notice appears in all copies and in all supporting documentation, and that the software is not redistributed for any fee (except for a nominal shipping charge). Anyone wanting to incorporate all or part of this software in a commercial product must obtain a license from the Medical Research Council. The MRC makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* ========== stitch.c ========== Copyright (c), 1989 John Holdsworth, Medical Research Council, Applied Psychology Unit. Author : John Holdsworth Written : 22th March, 1989. Edited : provides an unvarying interface for allocation and copying operations. */ #include <stdio.h> #include <string.h> #if defined( NeXT ) #include <stdlib.h> #else #include <malloc.h> #endif #ifndef _STITCH_H_ #include "stitch.h" #endif #if defined( sun ) extern bcopy(), bzero(), exit() ; #else #ifndef NeXT extern void bcopy(), bzero(), free(), exit() ; #endif #endif #ifndef lint static char *stitch_sccs_id = "@(#)stitch.c 1.19 J. Holdsworth (MRC-APU) 6/6/91" ; #endif #ifndef WRAPPING #define WRAPPING 0 #endif #if WRAPPING #include "wrap.h" Pointer wrap_malloc( size, where ) unsigned size ; char where[] ; { return ( wrap( malloc( WRAPPED_SIZE( size ) ), size, where ) ) ; } Pointer wrap_realloc( old, size, where ) char *old ; unsigned size ; char where[] ; { return ( wrap( realloc( unwrap( old ), WRAPPED_SIZE( size ) ), where ) ) ; } void wrap_free( ptr ) Pointer ptr ; { free( (char *) unwrap( ptr ) ) ; return ; } #else Pointer wrap_malloc( size, where ) unsigned size ; char where[] ; { #ifdef lint where ; #endif return ( malloc( size ) ) ; } Pointer wrap_realloc( old, size, where ) char *old ; unsigned size ; char where[] ; { #ifdef lint where ; #endif #ifdef DSP32 stitch_error( "realloc called" ) ; #else return ( realloc( old, size ) ) ; #endif } void wrap_free( ptr ) Pointer ptr ; { free( ptr ) ; return ; } #endif #ifdef DSP32 extern char _EAdata[], _EUdata[] ; char *freemem = _EUdata ; char errstr[200] ; int abs( n ) int n ; { if( n >= 0 ) return n ; else return -n ; } #endif void stitch_error( string, thing ) char *string ; char *thing ; { int i ; #ifdef DSP32 extern int errno ; (void) strcpy( errstr, string ) ; errno = thing - (char *) 0 ; #else (void) fprintf( stderr, string, thing ) ; #endif #ifdef PC /* put in delay to be able to read message if any */ for(i=0 ; i<3000 ; i++ ) (void) sqrt( 2. ) ; #endif stitch_exit( 1 ) ; } static void failed( bytes, where ) unsigned bytes ; char *where ; { static char msg[200] = "Memory allocation error, allocating %u bytes in " ; stitch_error( strcat( msg, where ), ( Pointer ) 0 + bytes ) ; } char stitchStructStr[] = "allocating a structure somewhere" ; /* memory usage indicators */ static long sused ; static long bused ; Pointer stitch_malloc( bytes, where ) unsigned bytes ; char *where ; { Pointer ptr ; if( where == stitchStructStr ) sused += bytes ; else bused += bytes ; #ifdef NOT_DSP32_ANYMORE int size = bytes + 3 >> 2 << 2 ; ptr = freemem ; if( ( freemem += size ) > _EAdata ) #else if( ( ptr = wrap_malloc( bytes, where ) ) == ( Pointer ) 0 ) #endif failed( bytes, where ) ; return ( ptr ) ; } Pointer stitch_ralloc( bytes, where ) unsigned bytes ; char *where ; { #ifdef DSP32 extern char _EAram2[], _EUram2[] ; static char *mempt = _EUram2 ; int size ; size = bytes + 3 >> 2 << 2 ; if( mempt + size <= _EAram2 ) return ( ( mempt += size ) - size ) ; #endif #ifdef NICHE #ifdef NICHEE #include <trillium/ram.h> #else #define ONCHIP 2 #endif Pointer ptr ; if( ( ptr = valloc( items * size, ONCHIP, size ) ) == ( char * ) 0 ) return ( ptr ) ; #endif return ( stitch_malloc( bytes, where ) ) ; } Pointer stitch_calloc( number, size, where ) unsigned number, size ; char *where ; { Pointer ptr ; #if defined( NICHE ) || defined( DSP32 ) if( ( ptr = stitch_malloc( number * size ) ) == ( char * ) 0 ) failed( number * size, where ) ; stitch_bzero( ptr, ( int ) number * size ) ; #else if( ( ptr = ( char * ) calloc( number, size ) ) == ( char * ) 0 ) failed( number * size, where ) ; #endif return ( ptr ) ; } Pointer stitch_zeroed_malloc( size, where ) unsigned size ; char *where ; { Pointer ptr = stitch_malloc( size, where ) ; stitch_bzero( ptr, size ) ; return ( ptr ) ; } Pointer stitch_copied_malloc( size, model, where ) unsigned size ; Pointer model ; char *where ; { Pointer ptr = stitch_malloc( size, where ) ; stitch_bcopy( model, ptr, size ) ; return ( ptr ) ; } void stitch_free( pointer ) Pointer pointer ; { if( pointer == (Pointer) 0 ) stitch_error( "attempt to free null pointer" ) ; #ifndef DSP32 wrap_free( pointer ) ; #endif return ; } void stitch_exit( status ) int status ; { #if 00 (void) fprintf( stderr, "memory usage: %d for structures, %d for buffers\n", sused, bused ) ; #endif #ifdef NICHE kexit( status ) ; #else exit( status ) ; #endif } /* beware these defintions - they make stitch.h mandatory */ #if defined( vax ) || defined( mips ) || defined( sun ) void (*stitch_bcopy)() = ( void ( * ) () ) bcopy ; #else #ifdef PC /* memmove must be able to cope with overlapping buffers */ static void call_memmove( from, to, bytes ) char *from, *to ; unsigned bytes ; { ( void ) memmove( to, from, bytes ) ; return ; } void (*stitch_bcopy)() = call_memmove ; #else static void reserve_bcopy( from, to, number ) Pointer from, to ; int number ; { register char *i, *j, *end ; if( from > to ) { for( i = from, j = to, end = from + number ; ( end - i ) % 4 > 0 ; ) *j++ = *i++ ; if( i < end ) do { *j++ = *i++ ; *j++ = *i++ ; *j++ = *i++ ; *j++ = *i++ ; } while( i < end ) ; } else if( from < to ) { for( i = from + number, j = to + number, end = from ; ( i - end ) % 4 > 0 ; ) *--j = *--i ; if( i > end ) do { *--j = *--i ; *--j = *--i ; *--j = *--i ; *--j = *--i ; } while( i > end ) ; } return ; } void (*stitch_bcopy)() = reserve_bcopy ; #endif #endif #if defined( vax ) || defined( mips ) || defined( sun ) void (*stitch_bzero)() = ( void ( * ) () ) bzero ; #else #if defined( PC ) || defined( DSP32 ) static void call_memset( to, bytes ) char *to ; int bytes ; { (void) memset( to, '\000', bytes ) ; } void (*stitch_bzero)() = call_memset ; #else static void reserve_bzero( to, bytes ) char *to ; int bytes ; { register char *ptr = to ; register char *end = to + bytes ; while( ptr < end ) *ptr++ = 0 ; return ; } void (*stitch_bzero)() = reserve_bzero ; #endif #endif