tomwalters@0: /* tomwalters@0: Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989 tomwalters@0: =========================================================================== tomwalters@0: tomwalters@0: Permission to use, copy, modify, and distribute this software without fee tomwalters@0: is hereby granted for research purposes, provided that this copyright tomwalters@0: notice appears in all copies and in all supporting documentation, and that tomwalters@0: the software is not redistributed for any fee (except for a nominal shipping tomwalters@0: charge). Anyone wanting to incorporate all or part of this software in a tomwalters@0: commercial product must obtain a license from the Medical Research Council. tomwalters@0: tomwalters@0: The MRC makes no representations about the suitability of this tomwalters@0: software for any purpose. It is provided "as is" without express or implied tomwalters@0: warranty. tomwalters@0: tomwalters@0: THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING tomwalters@0: ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE tomwalters@0: A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY tomwalters@0: DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN tomwalters@0: AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF tomwalters@0: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. tomwalters@0: */ tomwalters@0: tomwalters@0: /* tomwalters@0: stitch.h tomwalters@0: ======== tomwalters@0: tomwalters@0: interface for stitch system. tomwalters@0: tomwalters@0: This contains many regrettable #defines which are the act of a parnoid tomwalters@0: bad typist who wishes to lerave nothing to chance. They are not as hostile tomwalters@0: as they might first seem being mostly to do with methodical means of tomwalters@0: allocation making more use of typeing to avoid any chance of a badly tomwalters@0: allocated data buffer. This file is supported by two other abstractions: tomwalters@0: buffer.[ch] which ensures a buffer is the size required without realoocation. tomwalters@0: wrap.[ch] which encapsulates allocations to detect buffer overflows. tomwalters@0: tomwalters@0: In general there are two levels of indirection between the macros defined tomwalters@0: below and the final calls for memory allocation from the C library function. tomwalters@0: The first level is a C pre-processor macro to get all the catsings right tomwalters@0: for lint and do things with types that procedures just can't do. tomwalters@0: The second level is just passes the allocation call on so it can tomwalters@0: be intercepted as an aid to portability. tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: tomwalters@0: #ifndef _STITCH_H_ tomwalters@0: #define _STITCH_H_ tomwalters@0: tomwalters@0: /* redefinitions of general memory allocation to check number and cast argumnets */ tomwalters@0: tomwalters@0: #define Malloc( _size, _where ) stitch_malloc( (unsigned) ( _size ), _where ) tomwalters@0: #define Ralloc( _size, _where ) stitch_ralloc( (unsigned) ( _size ), _where ) tomwalters@0: #define Calloc( _size, _items, _where ) stitch_malloc( (unsigned) ( _size ), (unsigned) ( _items ), _where ) tomwalters@0: #define Realloc( _old, _size , _where ) stitch_realloc( (Pointer) ( _old ), (unsigned) ( _size ) ) tomwalters@0: tomwalters@0: /* deallocation - delete destroys pointer to keep it out of trouble */ tomwalters@0: tomwalters@0: #define Free( _old ) stitch_free( (Pointer) ( _old ) ) tomwalters@0: #define Delete( _variable ) ( Free( _variable ), _variable = 0 ) tomwalters@0: tomwalters@0: /* more names for the allocation */ tomwalters@0: tomwalters@0: #define Allocate( _size, _where ) stitch_malloc( (unsigned) ( _size ), _where ) tomwalters@0: #define AllocateZeroed( _size, _where ) stitch_zeroed_malloc( (unsigned) ( _size ), _where ) tomwalters@0: #define AllocateCopied( _size, _model, _where ) stitch_copied_malloc( (unsigned) ( _size ), (Pointer) _model, _where ) tomwalters@0: tomwalters@0: #define Reallocate( _old, _size ) stitchRealloc( (Pointer) ( _old ), (unsigned) ( _size ) ) tomwalters@0: tomwalters@0: /* macros for allocation making more use of type of variable receiving pointer */ tomwalters@0: tomwalters@0: #define TypeSize( _pointer ) ( sizeof * (_pointer) 0 ) tomwalters@0: tomwalters@0: /* return pointer to New structure, random, zeroed or copied */ tomwalters@0: tomwalters@0: #define New( _type ) (_type ) Allocate( TypeSize( _type ), stitchStructStr ) tomwalters@0: #define NewZeroed( _type ) (_type ) AllocateZeroed( TypeSize( _type ), stitchStructStr ) tomwalters@0: #define NewCopied( _type, _model ) (_type ) AllocateCopied( TypeSize( _type ), _model, stitchStructStr ) tomwalters@0: tomwalters@0: /* return pointer to array of structures */ tomwalters@0: tomwalters@0: #define NewArray( _type, _n, _where ) (_type *) Allocate( sizeof (_type) * (unsigned) ( _n ), _where ) tomwalters@0: #define NewZeroedArray( _type, _n, _where ) (_type *) AllocateZeroed( sizeof (_type) * (unsigned) ( _n ), _where ) tomwalters@0: #define NewCopiedArray( _type, _n, _model, _where ) (_type *) AllocateCopied( sizeof (_type) * (unsigned) ( _n ), _model, _where ) tomwalters@0: tomwalters@0: /* as above but declare variable to receive pointer as well like new in C++ */ tomwalters@0: tomwalters@0: #define DeclareNew( _type, _name ) _type _name = New( _type ) tomwalters@0: #define DeclareNewZeroed( _type, _name ) _type _name = NewZeroed( _type ) tomwalters@0: #define DeclareNewCopied( _type, _name, _model ) _type _name = NewCopied( _type, _model ) tomwalters@0: tomwalters@0: #define DeclareNewArray( _type, _name, _n, _where ) _type *_name = NewArray( _type, _n, _where ) tomwalters@0: #define DeclareNewZeroedArray( _type, _name, _n, _where ) _type *_name = NewZeroedArray( _type, _n, _where ) tomwalters@0: #define DeclareNewCopiedArray( _type, _name, _n, _model, _where ) _type *_name = NewCopiedArray( _type, _n, _model, _where ) tomwalters@0: tomwalters@0: /* structure utilities */ tomwalters@0: tomwalters@0: #define CopyArray( _from, _to, _number ) ( stitch_bcopy( _from, _to, sizeof ( * ( _to ) ) * ( _number ) ), _to ) tomwalters@0: #define ZeroArray( _to, _number ) ( stitch_bzero( _to, sizeof ( * ( _to ) ) * ( _number ) ), _to ) tomwalters@0: tomwalters@0: #define Copy( _from, _to ) ( stitch_bcopy( _from, _to, sizeof ( * ( _to ) ) ), _to ) tomwalters@0: #define Zero( _to ) ( stitch_bzero( _to, sizeof ( * ( _to ) ) ), _to ) tomwalters@0: tomwalters@0: #if 0 tomwalters@0: tomwalters@0: /* defines for new names for now ( removed for clarity ) */ tomwalters@0: tomwalters@0: #define stitchMalloc( _size, _where ) stitch_malloc( (unsigned) _size, _where ) tomwalters@0: #define stitchZeroedMalloc( _size, _where ) stitch_zeroed_malloc( (unsigned) _size, _where ) tomwalters@0: #define stitchCopiedMalloc( _size, _model, _where ) stitch_copied_malloc( (unsigned) _size, _model, _where ) tomwalters@0: tomwalters@0: #define stitchRealloc( _pt, _size, _where ) stitch_ralloc( (Pointer) _pt, (unsigned) _size, _where ) tomwalters@0: tomwalters@0: #define stitchFree( _old ) stitch_free( _old ) tomwalters@0: #endif tomwalters@0: tomwalters@0: tomwalters@0: /* realy important typedefs.... */ tomwalters@0: tomwalters@0: #if PEDANTIC tomwalters@0: typedef struct _void *Pointer ; /* pointer to object of unknown size effectively void * */ tomwalters@0: #else tomwalters@0: typedef char *Pointer ; tomwalters@0: #endif tomwalters@0: typedef char *Address ; tomwalters@0: tomwalters@0: tomwalters@0: /* a portable inteface to the system */ tomwalters@0: tomwalters@0: extern void stitch_error() ; tomwalters@0: tomwalters@0: extern Pointer stitch_malloc() ; tomwalters@0: extern Pointer stitch_copied_malloc() ; tomwalters@0: extern Pointer stitch_zeroed_malloc() ; tomwalters@0: extern Pointer stitch_ralloc() ; tomwalters@0: extern Pointer stitch_calloc() ; tomwalters@0: tomwalters@0: extern void stitch_free() ; tomwalters@0: extern void stitch_exit() ; tomwalters@0: tomwalters@0: /* beware these definitions, they are for speed and fail if stitch.h not included */ tomwalters@0: tomwalters@0: extern void (*stitch_bcopy)() ; tomwalters@0: extern void (*stitch_bzero)() ; tomwalters@0: tomwalters@0: extern char stitchStructStr[] ; tomwalters@0: tomwalters@0: #endif