qse/ase/stx/memory.c

99 lines
2.2 KiB
C
Raw Normal View History

2005-05-06 15:54:47 +00:00
/*
2007-03-22 11:19:28 +00:00
* $Id: memory.c,v 1.14 2007-03-22 11:19:28 bacon Exp $
2005-05-06 15:54:47 +00:00
*/
2007-03-22 11:19:28 +00:00
#include <ase/stx/memory.h>
#include <ase/stx/misc.h>
2005-05-06 15:54:47 +00:00
2007-03-22 11:19:28 +00:00
ase_stx_memory_t* ase_stx_memory_open (
ase_stx_memory_t* mem, ase_word_t capacity)
2005-05-06 15:54:47 +00:00
{
2007-03-22 11:19:28 +00:00
ase_stx_object_t** slots;
ase_word_t n;
ase_assert (capacity > 0);
if (mem == ASE_NULL) {
mem = (ase_stx_memory_t*)ase_malloc(ase_sizeof(ase_stx_memory_t));
if (mem == ASE_NULL) return ASE_NULL;
mem->__dynamic = ase_true;
2005-05-06 15:54:47 +00:00
}
2007-03-22 11:19:28 +00:00
else mem->__dynamic = ase_false;
2005-05-06 15:54:47 +00:00
2007-03-22 11:19:28 +00:00
slots = (ase_stx_object_t**)ase_malloc (
capacity * ase_sizeof(ase_stx_object_t*));
if (slots == ASE_NULL) {
if (mem->__dynamic) ase_free (mem);
mem = ASE_NULL;
2005-05-06 15:54:47 +00:00
}
mem->capacity = capacity;
mem->slots = slots;
/* weave the free slot list */
2005-05-08 10:58:26 +00:00
mem->free = &slots[0];
for (n = 0; n < capacity - 1; n++) {
2007-03-22 11:19:28 +00:00
mem->slots[n] = (ase_stx_object_t*)&mem->slots[n + 1];
2005-05-08 10:58:26 +00:00
}
2007-03-22 11:19:28 +00:00
mem->slots[n] = ASE_NULL;
2005-05-06 15:54:47 +00:00
return mem;
}
2007-03-22 11:19:28 +00:00
void ase_stx_memory_close (ase_stx_memory_t* mem)
2005-05-06 15:54:47 +00:00
{
/* TODO: free all linked objects... */
2007-03-22 11:19:28 +00:00
ase_free (mem->slots);
2005-05-06 15:54:47 +00:00
mem->capacity = 0;
2007-03-22 11:19:28 +00:00
mem->slots = ASE_NULL;
mem->free = ASE_NULL;
if (mem->__dynamic) ase_free (mem);
2005-05-06 15:54:47 +00:00
}
2007-03-22 11:19:28 +00:00
void ase_stx_memory_gc (ase_stx_memory_t* mem)
2005-05-06 15:54:47 +00:00
{
/* TODO: implement this function */
}
2007-03-22 11:19:28 +00:00
ase_word_t ase_stx_memory_alloc (ase_stx_memory_t* mem, ase_word_t nbytes)
2005-05-06 15:54:47 +00:00
{
2007-03-22 11:19:28 +00:00
ase_stx_object_t** slot;
ase_stx_object_t* object;
2005-05-06 15:54:47 +00:00
/* find the free object slot */
2007-03-22 11:19:28 +00:00
if (mem->free == ASE_NULL) {
ase_stx_memory_gc (mem);
if (mem->free == ASE_NULL) return mem->capacity;;
2005-05-06 15:54:47 +00:00
}
2007-03-22 11:19:28 +00:00
object = (ase_stx_object_t*)ase_malloc (nbytes);
if (object == ASE_NULL) {
ase_stx_memory_gc (mem);
object = (ase_stx_object_t*)ase_malloc (nbytes);
/*if (object == ASE_NULL) return mem->capacity;*/
if (object == ASE_NULL) {
ase_assert (ASE_T("MEMORY ALLOCATION ERROR\n") == ASE_NULL);
2005-05-20 04:01:12 +00:00
exit (1);
}
2005-05-06 15:54:47 +00:00
}
slot = mem->free;
2007-03-22 11:19:28 +00:00
mem->free = (ase_stx_object_t**)*slot;
2005-05-06 15:54:47 +00:00
*slot = object;
2007-03-22 11:19:28 +00:00
return (ase_word_t)(slot - mem->slots);
2005-05-06 15:54:47 +00:00
}
2007-03-22 11:19:28 +00:00
void ase_stx_memory_dealloc (ase_stx_memory_t* mem, ase_word_t object_index)
2005-05-06 15:54:47 +00:00
{
/*
* THIS IS PRIMITIVE LOW-LEVEL DEALLOC. THIS WILL NOT
* DEALLOCATE MEMORY ALLOCATED FOR ITS INSTANCE VARIABLES.
*/
2007-03-22 11:19:28 +00:00
ase_free (mem->slots[object_index]);
mem->slots[object_index] = (ase_stx_object_t*)mem->free;
2005-05-06 15:54:47 +00:00
mem->free = &mem->slots[object_index];
}