This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches. git-svn-id: svn://svn.cc65.org/cc65/trunk@3 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
65
libsrc/common/free.c
Normal file
65
libsrc/common/free.c
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* free.c
|
||||
*
|
||||
* Ullrich von Bassewitz, 11.08.1998
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "_heap.h"
|
||||
|
||||
|
||||
|
||||
void free (void* block)
|
||||
/* Release an allocated memory block. The function will accept NULL pointers
|
||||
* (and do nothing in this case).
|
||||
*/
|
||||
{
|
||||
unsigned* b;
|
||||
unsigned size;
|
||||
struct freeblock* f;
|
||||
|
||||
|
||||
/* Allow NULL arguments */
|
||||
if (block == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get a pointer to the real memory block, then get the size */
|
||||
b = (unsigned*) block;
|
||||
size = *--b;
|
||||
|
||||
/* Check if the block is at the top of the heap */
|
||||
if (((int) b) + size == (int) _hptr) {
|
||||
|
||||
/* Decrease _hptr to release the block */
|
||||
_hptr = (unsigned*) (((int) _hptr) - size);
|
||||
|
||||
/* Check if the last block in the freelist is now at heap top. If so,
|
||||
* remove this block from the freelist.
|
||||
*/
|
||||
if (f = _hlast) {
|
||||
if (((int) f) + f->size == (int) _hptr) {
|
||||
/* Remove the last block */
|
||||
_hptr = (unsigned*) (((int) _hptr) - f->size);
|
||||
if (_hlast = f->prev) {
|
||||
/* Block before is now last block */
|
||||
f->prev->next = 0;
|
||||
} else {
|
||||
/* The freelist is empty now */
|
||||
_hfirst = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
/* Not at heap top, enter the block into the free list */
|
||||
_hadd (b, size);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user