2005-05-06 15:54:47 +00:00
|
|
|
/*
|
2005-06-08 16:00:51 +00:00
|
|
|
* $Id: memory.c,v 1.12 2005-06-08 16:00:51 bacon Exp $
|
2005-05-06 15:54:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/stx/memory.h>
|
2005-05-19 16:41:10 +00:00
|
|
|
#include <xp/stx/misc.h>
|
2005-05-06 15:54:47 +00:00
|
|
|
|
|
|
|
xp_stx_memory_t* xp_stx_memory_open (
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_stx_memory_t* mem, xp_word_t capacity)
|
2005-05-06 15:54:47 +00:00
|
|
|
{
|
|
|
|
xp_stx_object_t** slots;
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_word_t n;
|
2005-05-06 15:54:47 +00:00
|
|
|
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_assert (capacity > 0);
|
2005-05-06 15:54:47 +00:00
|
|
|
if (mem == XP_NULL) {
|
2005-06-08 16:00:51 +00:00
|
|
|
mem = (xp_stx_memory_t*)xp_malloc(xp_sizeof(xp_stx_memory_t));
|
2005-05-06 15:54:47 +00:00
|
|
|
if (mem == XP_NULL) return XP_NULL;
|
|
|
|
mem->__malloced = xp_true;
|
|
|
|
}
|
|
|
|
else mem->__malloced = xp_false;
|
|
|
|
|
2005-06-08 16:00:51 +00:00
|
|
|
slots = (xp_stx_object_t**)xp_malloc (
|
2005-05-06 15:54:47 +00:00
|
|
|
capacity * xp_sizeof(xp_stx_object_t*));
|
|
|
|
if (slots == XP_NULL) {
|
2005-06-08 16:00:51 +00:00
|
|
|
if (mem->__malloced) xp_free (mem);
|
2005-05-06 15:54:47 +00:00
|
|
|
mem = XP_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
mem->slots[n] = (xp_stx_object_t*)&mem->slots[n + 1];
|
|
|
|
}
|
2005-05-18 16:34:51 +00:00
|
|
|
mem->slots[n] = XP_NULL;
|
2005-05-06 15:54:47 +00:00
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2005-05-08 10:31:25 +00:00
|
|
|
void xp_stx_memory_close (xp_stx_memory_t* mem)
|
2005-05-06 15:54:47 +00:00
|
|
|
{
|
|
|
|
/* TODO: free all linked objects... */
|
|
|
|
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_free (mem->slots);
|
2005-05-06 15:54:47 +00:00
|
|
|
mem->capacity = 0;
|
|
|
|
mem->slots = XP_NULL;
|
|
|
|
mem->free = XP_NULL;
|
2005-06-08 16:00:51 +00:00
|
|
|
if (mem->__malloced) xp_free (mem);
|
2005-05-06 15:54:47 +00:00
|
|
|
}
|
|
|
|
|
2005-05-08 10:31:25 +00:00
|
|
|
void xp_stx_memory_gc (xp_stx_memory_t* mem)
|
2005-05-06 15:54:47 +00:00
|
|
|
{
|
|
|
|
/* TODO: implement this function */
|
|
|
|
}
|
|
|
|
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_word_t xp_stx_memory_alloc (xp_stx_memory_t* mem, xp_word_t nbytes)
|
2005-05-06 15:54:47 +00:00
|
|
|
{
|
|
|
|
xp_stx_object_t** slot;
|
|
|
|
xp_stx_object_t* object;
|
|
|
|
|
|
|
|
/* find the free object slot */
|
|
|
|
if (mem->free == XP_NULL) {
|
2005-05-08 10:31:25 +00:00
|
|
|
xp_stx_memory_gc (mem);
|
2005-05-06 17:18:29 +00:00
|
|
|
if (mem->free == XP_NULL) return mem->capacity;;
|
2005-05-06 15:54:47 +00:00
|
|
|
}
|
|
|
|
|
2005-06-08 16:00:51 +00:00
|
|
|
object = (xp_stx_object_t*)xp_malloc (nbytes);
|
2005-05-06 15:54:47 +00:00
|
|
|
if (object == XP_NULL) {
|
2005-05-08 10:31:25 +00:00
|
|
|
xp_stx_memory_gc (mem);
|
2005-06-08 16:00:51 +00:00
|
|
|
object = (xp_stx_object_t*)xp_malloc (nbytes);
|
2005-05-20 04:01:12 +00:00
|
|
|
/*if (object == XP_NULL) return mem->capacity;*/
|
|
|
|
if (object == XP_NULL) {
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_assert (XP_TEXT("MEMORY ALLOCATION ERROR\n") == XP_NULL);
|
2005-05-20 04:01:12 +00:00
|
|
|
exit (1);
|
|
|
|
}
|
2005-05-06 15:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
slot = mem->free;
|
|
|
|
mem->free = (xp_stx_object_t**)*slot;
|
|
|
|
*slot = object;
|
|
|
|
|
2005-06-08 16:00:51 +00:00
|
|
|
return (xp_word_t)(slot - mem->slots);
|
2005-05-06 15:54:47 +00:00
|
|
|
}
|
|
|
|
|
2005-06-08 16:00:51 +00:00
|
|
|
void xp_stx_memory_dealloc (xp_stx_memory_t* mem, xp_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.
|
|
|
|
*/
|
|
|
|
|
2005-06-08 16:00:51 +00:00
|
|
|
xp_free (mem->slots[object_index]);
|
2005-05-06 15:54:47 +00:00
|
|
|
mem->slots[object_index] = (xp_stx_object_t*)mem->free;
|
|
|
|
mem->free = &mem->slots[object_index];
|
|
|
|
}
|
|
|
|
|