GEOS SDK TechDocs
|
|
3 Using Global Memory
malloc(), calloc(), realloc(), free()
GEOS provides support for the Standard C memory allocation routines. However, support is limited by the nature of the 80x86 and the GEOS memory management system.
A geode can request memory with
malloc()
or
calloc()
. When a geode does this for the first time, the memory manager will allocate a fixed block and return a pointer to within the fixed block. (This block is actually a special kind of LMem heap.) Because the memory is in a fixed block, the geode does not need to access it with handles; it can use the pointer directly. If the block fills up, the manager can allocate another fixed block for these requests.
However, there are some problems with this. The main problem is that fixed blocks degrade the memory manager's performance. The more a geode uses
malloc()
, the more memory is tied up in fixed blocks. And, as always, contiguous memory is limited to 64K by the 80x86 segmented addressing scheme.
Most of the time, geodes should use other types of memory allocation. For allocating small chunks of data, applications should use
LMem
routines or techniques built on top of them (database items, chunk arrays, etc.); for larger chunks of memory, applications should use memory manager routines or HugeArrays. However, to help writers port C code to GEOS,
malloc()
and its relatives are available.
To get a stretch of contiguous memory, use the routines
malloc()
or
calloc()
.
malloc()
takes one argument, a size in bytes; it returns a void pointer to that many bytes of fixed memory.
calloc()
takes two arguments: a number of structures, and the size of each such structure. It allocates enough memory for that many structures and returns a void pointer to the memory. Both
malloc()
and
calloc()
zero-initialize the memory when they allocate it.
If a routine wants to change the size of memory allocated with
malloc()
or
calloc()
it can use
realloc()
.
realloc()
takes two arguments: a pointer to a piece of memory allocated with
malloc()
or
calloc()
, and a new size in bytes. It returns a void pointer to the memory, which may have been moved to satisfy the request. If it could not satisfy the request, it returns a null pointer, and the original memory is untouched. Note that the pointer you pass
realloc()
must
be the same pointer that was returned by
malloc/calloc
; if (for example) you allocate 100 bytes and are returned 008Bh:30h, and try to resize it by passing 008Bh:40h to
realloc()
, inappropriate memory will be affected, and the results are undefined.
If you decrease the size of a memory section with
realloc()
, the routine is guaranteed to succeed. If you increase the size, it may fail; if it does succeed, the new memory will
not
be zero-initialized. Reallocating a block down to zero memory is the same as freeing it. You can pass a null pointer to
realloc()
along with the size; this makes
realloc()
function like
malloc()
.
When you are done with memory allocated by
malloc
-family routines, you should call
free()
to free the memory for other
malloc()
calls. As with
realloc()
, you must pass the same pointer that you were originally given.
GEOS SDK TechDocs
|
|
3 Using Global Memory