📄 heapinit.c
字号:
*/
last = (_PBLKDESC) ((char *) first + _HEAP_EMPTYLIST_SIZE - 2 * sizeof(_BLKDESC));
next = (_PBLKDESC) ((char *) first + sizeof(_BLKDESC));
while ( first < last ) {
/* Init this descriptor */
first->pnextdesc = next;
/* onto the next block */
first = next++;
}
/*
* Take care of the last descriptor (end of the empty list)
*/
last->pnextdesc = NULL;
return 1;
}
/***
*__getempty() - get an empty heap descriptor
*
*Purpose:
* Get a descriptor from the list of empty heap descriptors. If the list
* is empty, call _heap_grow_emptylist.
*
*Entry:
* no arguments
*
*Exit:
* If successful, a pointer to the descriptor is returned.
* Otherwise, NULL is returned.
*
*Exceptions:
*
*******************************************************************************/
_PBLKDESC __cdecl __getempty(
void
)
{
_PBLKDESC pdesc;
if ( (_heap_desc.emptylist == NULL) && (_heap_grow_emptylist()
== 0) )
return NULL;
pdesc = _heap_desc.emptylist;
_heap_desc.emptylist = pdesc->pnextdesc;
return pdesc;
}
#else /* _WIN32 */
#if defined (_M_MPPC) || defined (_M_M68K)
#include <cruntime.h>
#include <dos.h>
#include <heap.h>
#include <malloc.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <dbgint.h>
#include <macos\types.h>
#include <macos\errors.h>
#include <macos\memory.h> // Mac OS interface header
#include <macos\lowmem.h>
#include <macos\segload.h>
#define _HEAP_EMPTYLIST_SIZE (1 * _PAGESIZE_)
#define DSErrCode (*(short*)(0x0af0))
/*
* Heap descriptor
*/
struct _heap_desc_ _heap_desc = {
&_heap_desc.sentinel, /* pfirstdesc */
&_heap_desc.sentinel, /* proverdesc */
NULL, /* emptylist */
NULL, /* sentinel.pnextdesc */
NULL /* sentinel.pblock */
};
/*
* Array of region structures
* [Note: We count on the fact that this is always initialized to zero
* by the compiler.]
*/
Handle hHeapRegions = NULL;
int _heap_region_table_cur = 0;
/*
* Control parameter locations
*/
unsigned int _heap_resetsize = 0xffffffff;
/* NOTE: Currenlty, _heap_growsize is a #define to _amblksiz */
unsigned int _heap_growsize = _HEAP_GROWSIZE; /* region inc size */
unsigned int _heap_regionsize = _HEAP_REGIONSIZE; /* region size */
/***
*_heap_init() - Initialize the heap
*
*Purpose:
* Setup the initial C library heap. All necessary memory and
* data bases are init'd appropriately so future requests work
* correctly.
*
* NOTES:
* (1) This routine should only be called once!
* (2) This routine must be called before any other heap requests.
*
*
*Entry:
* <void>
*Exit:
* <void>
*
*Exceptions:
* If heap cannot be initialized, the program will be terminated
* with a fatal runtime error.
*
*******************************************************************************/
void __cdecl _heap_init (
void
)
{
#define _INITREGIONSZ 0x1000
/*LATER -- do we need to do anything to init heap? Yes, in case user not malloc first*/
int oldregionsz = _heap_regionsize; /* save current region size */
struct _heap_region_ *pHeapRegions;
void *p;
void *p2;
if (hHeapRegions == NULL)
{
hHeapRegions = NewHandle(sizeof(struct _heap_region_)*_HEAP_REGIONMAX);
if (hHeapRegions == NULL)
{
DSErrCode = appMemFullErr;
ExitToShell();
}
HLock(hHeapRegions);
pHeapRegions = (struct _heap_region_ *)(*hHeapRegions);
memset(pHeapRegions, 0, sizeof(struct _heap_region_)*_HEAP_REGIONMAX);
_heap_region_table_cur = _HEAP_REGIONMAX;
}
_heap_regionsize = _INITREGIONSZ; /* set region size to 64 Kb */
/* make sure we have enough memory to do initialization */
if ((p = NewPtr(_HEAP_EMPTYLIST_SIZE)) == NULL)
{
DSErrCode = appMemFullErr;
ExitToShell();
}
if ((p2 = NewPtr(_heap_regionsize)) == NULL)
{
DSErrCode = appMemFullErr;
ExitToShell();
}
if (p)
{
DisposePtr(p);
}
if (p2)
{
DisposePtr(p2);
}
p = _malloc_base(4);
if (p == NULL)
{
DSErrCode = appMemFullErr;
ExitToShell();
}
_free_base( p ); /* malloc, then free a block */
_heap_regionsize = oldregionsz; /* restore region size */
}
/***
* _heap_grow_emptylist() - Grow the empty heap descriptor list
*
*Purpose:
* (1) Get memory from OS
* (2) Form it into a linked list of empty heap descriptors
* (3) Attach it to the master empty list
*
* NOTE: This routine assumes that the emptylist is NULL
* when called (i.e., there are no available empty heap descriptors).
*
*Entry:
* (void)
*
*Exit:
* (void)
*
*Exceptions:
*
*******************************************************************************/
static int __cdecl _heap_grow_emptylist (
void
)
{
REG1 _PBLKDESC first;
REG2 _PBLKDESC next;
_PBLKDESC last;
/*
* Get memory for the new empty heap descriptors
*
* Note that last is used to hold the returned pointer because
* first (and next) are register class.
*/
if ((last = (_PBLKDESC)NewPtr(_HEAP_EMPTYLIST_SIZE)) == NULL)
{
return 0;
}
/*
* Init the empty heap descriptor list.
*/
_heap_desc.emptylist = first = last;
/*
* Carve the memory into an empty list
*/
last = (_PBLKDESC) ((char *) first + _HEAP_EMPTYLIST_SIZE - sizeof(_BLKDESC));
next = (_PBLKDESC) ((char *) first + sizeof(_BLKDESC));
while ( first < last ) {
/* Init this descriptor */
first->pnextdesc = next;
/* onto the next block */
first = next++;
}
/*
* Take care of the last descriptor (end of the empty list)
*/
last->pnextdesc = NULL;
return 1;
}
/***
*__getempty() - get an empty heap descriptor
*
*Purpose:
* Get a descriptor from the list of empty heap descriptors. If the list
* is empty, call _heap_grow_emptylist.
*
*Entry:
* no arguments
*
*Exit:
* If successful, a pointer to the descriptor is returned.
* Otherwise, NULL is returned.
*
*Exceptions:
*
*******************************************************************************/
_PBLKDESC __cdecl __getempty(
void
)
{
_PBLKDESC pdesc;
if ( (_heap_desc.emptylist == NULL) && (_heap_grow_emptylist()
== 0) )
return NULL;
pdesc = _heap_desc.emptylist;
_heap_desc.emptylist = pdesc->pnextdesc;
return pdesc;
}
#endif /* defined (_M_MPPC) || defined (_M_M68K) */
#endif /* _WIN32 */
#endif /* WINHEAP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -