This is mem.c in view mode; [Download] [Up]
/* mem.c
* memory allocation routines to reduce malloc calls.
*/
/*
* Copyright (c) 1991 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
#include <mif.h>
#define CHUNKSIZE (6*1024) /* memory to allocated per chunk */
extern int errno;
extern char *malloc();
extern char *calloc();
static char *chunk; /* ->current memory chunk to store field data */
static char *bp; /* ->next available point in chunk to store data */
static char *ep; /* ->end of current data chunk */
/* return pointer to newly allocated memory.
*/
char *
getmem( nbytes )
register nbytes;
{
register char *cp;
register n;
#ifdef STRINGS_ALIGNED
/* align pointers on quadwords */
if( n = (int)bp & 3 )
bp += 4-n;
#endif
if( !chunk || (bp+nbytes >= ep))
{
init_chunk();
#ifdef STRINGS_ALIGNED
if( n = (int)bp & 3 )
bp += 4-n;
#endif
}
cp=bp;
bp+=nbytes;
return( cp );
}
/* allocate new chunk of memory. exit on malloc failure
*/
static
init_chunk()
{
if(( chunk = malloc( CHUNKSIZE )) == (char *)0 )
err( FATAL, "Malloc failed, errno=%d\n", errno );
bp = chunk;
ep=chunk+CHUNKSIZE;
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.