home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_09_02
/
9n02100a
< prev
next >
Wrap
Text File
|
1990-12-16
|
2KB
|
87 lines
#include <stdlib.h>
/**
*
* name sbrk -- Adjust linear heap break pointer
* rbrk -- Reset linear heap
*
* synopsis b = sbrk(n);
* rbrk();
* void *b; block address
* int n; number of bytes
*
* description This is an implementation of sbrk/rbrk
* based upon the ANSI memory allocation
* routines. Note that it uses a public
* item named _LHINCR to define the
* default size of the linear heap. This
* size is used to allocate space via
* malloc on the first call to sbrk
* unless the requested size is larger.
*
**/
#define SBRK_ERR (void *)(-1)
/**
*
* Linear heap parameters
*
**/
unsigned _LHINCR = 8192; // allocation size of
linear heap
static char *base = 0; // base of linear heap
static unsigned size; // size of linear heap
static unsigned xbrk = 0; // current break index
/**
*
* Allocate space from the linear heap.
*
**/
void *sbrk(int n)
{
unsigned x;
if(base == 0)
{
if(n <= 0) return(SBRK_ERR);
size = (n > _LHINCR) ? n : _LHINCR;
base = malloc(size);
if(base == 0)
{
size = 0;
return(SBRK_ERR);
}
xbrk = 0;
}
if(n < 0)
{
if((xbrk + n) < 0) return(SBRK_ERR);
}
else
{
if((xbrk + n) > size) return(SBRK_ERR);
}
x = xbrk;
xbrk += n;
if(n > 0) memset(&base[x],0,n);
return(&base[x]);
}
/**
*
* Reset the linear heap
*
**/
void rbrk(void)
{
if(base != 0)
{
free(base);
base = 0;
size = 0;
xbrk = 0;
}
}