*** empty log message ***

This commit is contained in:
hyung-hwan 2005-05-08 10:31:25 +00:00
parent e050695338
commit 98f668d66f
6 changed files with 108 additions and 64 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Id: memory.c,v 1.4 2005-05-08 07:39:51 bacon Exp $ * $Id: memory.c,v 1.5 2005-05-08 10:31:24 bacon Exp $
*/ */
#include <xp/stx/memory.h> #include <xp/stx/memory.h>
@ -40,7 +40,7 @@ xp_stx_memory_t* xp_stx_memory_open (
return mem; return mem;
} }
void xp_stx_memory_free (xp_stx_memory_t* mem) void xp_stx_memory_close (xp_stx_memory_t* mem)
{ {
/* TODO: free all linked objects... */ /* TODO: free all linked objects... */
@ -58,25 +58,25 @@ xp_stx_memory_t* xp_stx_memory_resize (xp_stx_memory_t* mem, xp_stx_word_t capac
} }
*/ */
void xp_stx_garbage_collect (xp_stx_memory_t* mem) void xp_stx_memory_gc (xp_stx_memory_t* mem)
{ {
/* TODO: implement this function */ /* TODO: implement this function */
} }
xp_stx_word_t xp_stx_alloc_objmem (xp_stx_memory_t* mem, xp_stx_word_t nbytes) xp_stx_word_t xp_stx_memory_alloc (xp_stx_memory_t* mem, xp_stx_word_t nbytes)
{ {
xp_stx_object_t** slot; xp_stx_object_t** slot;
xp_stx_object_t* object; xp_stx_object_t* object;
/* find the free object slot */ /* find the free object slot */
if (mem->free == XP_NULL) { if (mem->free == XP_NULL) {
xp_stx_garbage_collect (mem); xp_stx_memory_gc (mem);
if (mem->free == XP_NULL) return mem->capacity;; if (mem->free == XP_NULL) return mem->capacity;;
} }
object = (xp_stx_object_t*)xp_malloc (nbytes); object = (xp_stx_object_t*)xp_malloc (nbytes);
if (object == XP_NULL) { if (object == XP_NULL) {
xp_stx_garbage_collect (mem); xp_stx_memory_gc (mem);
object = (xp_stx_object_t*)xp_malloc (nbytes); object = (xp_stx_object_t*)xp_malloc (nbytes);
if (object == XP_NULL) return mem->capacity; if (object == XP_NULL) return mem->capacity;
} }
@ -88,7 +88,7 @@ xp_stx_word_t xp_stx_alloc_objmem (xp_stx_memory_t* mem, xp_stx_word_t nbytes)
return (xp_stx_word_t)(slot - mem->slots); return (xp_stx_word_t)(slot - mem->slots);
} }
void xp_stx_dealloc_objmem (xp_stx_memory_t* mem, xp_stx_word_t object_index) void xp_stx_memory_dealloc (xp_stx_memory_t* mem, xp_stx_word_t object_index)
{ {
/* /*
* THIS IS PRIMITIVE LOW-LEVEL DEALLOC. THIS WILL NOT * THIS IS PRIMITIVE LOW-LEVEL DEALLOC. THIS WILL NOT

View File

@ -1,5 +1,5 @@
/* /*
* $Id: memory.h,v 1.4 2005-05-08 07:39:51 bacon Exp $ * $Id: memory.h,v 1.5 2005-05-08 10:31:24 bacon Exp $
*/ */
#ifndef _XP_STX_MEMORY_H_ #ifndef _XP_STX_MEMORY_H_
@ -15,9 +15,9 @@ xp_stx_memory_t* xp_stx_memory_open (
xp_stx_memory_t* mem, xp_stx_word_t capacity); xp_stx_memory_t* mem, xp_stx_word_t capacity);
void xp_stx_memory_close (xp_stx_memory_t* mem); void xp_stx_memory_close (xp_stx_memory_t* mem);
void xp_stx_gaxpage_collect (xp_stx_memory_t* mem); void xp_stx_memory_gc (xp_stx_memory_t* mem);
xp_stx_word_t xp_stx_alloc_objmem (xp_stx_memory_t* mem, xp_stx_word_t size); xp_stx_word_t xp_stx_memory_alloc (xp_stx_memory_t* mem, xp_stx_word_t size);
void xp_stx_dealloc_objmem (xp_stx_memory_t* mem, xp_stx_word_t object_index); void xp_stx_memory_dealloc (xp_stx_memory_t* mem, xp_stx_word_t object_index);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: object.c,v 1.2 2005-05-08 07:39:51 bacon Exp $ * $Id: object.c,v 1.3 2005-05-08 10:31:24 bacon Exp $
*/ */
#include <xp/stx/object.h> #include <xp/stx/object.h>
@ -8,51 +8,52 @@
/* n: number of instance variables */ /* n: number of instance variables */
xp_stx_word_t xp_stx_alloc_object (xp_stx_t* stx, xp_size_t n) xp_stx_word_t xp_stx_alloc_object (xp_stx_t* stx, xp_size_t n)
{ {
xp_stx_word_t at; xp_stx_word_t idx;
/* bytes to allocated = /* bytes to allocidxed =
* number of instance variables * word_size * number of instance variables * word_size
*/ */
at = xp_stx_alloc_objmem ( idx = xp_stx_memory_alloc (&stx->memory,
&stx->memory, n * xp_sizeof(xp_stx_word_t)); n * xp_sizeof(xp_stx_word_t) + xp_sizeof(xp_stx_object_t));
if (at >= stx->memory.capacity) return at; /* failed */ if (idx >= stx->memory.capacity) return idx; /* failed */
XP_STX_OBJECT_CLASS(&stx->memory,at) = stx->nil; XP_STX_OBJECT_CLASS(&stx->memory,idx) = stx->nil;
XP_STX_OBJECT_ACCESS(&stx->memory,at) = ((n << 2) | 0x00); XP_STX_OBJECT_ACCESS(&stx->memory,idx) = ((n << 2) | 0x00);
while (n--) XP_STX_OBJECT(&stx->memory,at)->data[n] = stx->nil; while (n--) XP_STX_OBJECT_AT(&stx->memory,idx,n) = stx->nil;
return at; return idx;
} }
/* n: number of bytes */ /* n: number of bytes */
xp_stx_word_t xp_stx_alloc_byte_object (xp_stx_t* stx, xp_size_t n) xp_stx_word_t xp_stx_alloc_byte_object (xp_stx_t* stx, xp_size_t n)
{ {
xp_stx_word_t at; xp_stx_word_t idx;
at = xp_stx_alloc_objmem (&stx->memory, n); idx = xp_stx_memory_alloc (
if (at >= stx->memory.capacity) return at; /* failed */ &stx->memory, n + xp_sizeof(xp_stx_object_t));
if (idx >= stx->memory.capacity) return idx; /* failed */
XP_STX_OBJECT_CLASS(&stx->memory,at) = stx->nil; XP_STX_OBJECT_CLASS(&stx->memory,idx) = stx->nil;
XP_STX_OBJECT_ACCESS(&stx->memory,at) = ((n << 2) | 0x01); XP_STX_OBJECT_ACCESS(&stx->memory,idx) = ((n << 2) | 0x01);
while (n--) XP_STX_BYTE_OBJECT(&stx->memory,at)->data[n] = 0; while (n--) XP_STX_OBJECT_BYTEAT(&stx->memory,idx,n) = 0;
return at; return idx;
} }
/* n: length of the string */ /* n: length of the string */
xp_stx_word_t xp_stx_alloc_string_object ( xp_stx_word_t xp_stx_alloc_string_object (
xp_stx_t* stx, xp_stx_char_t* str, xp_stx_word_t n) xp_stx_t* stx, xp_stx_char_t* str, xp_stx_word_t n)
{ {
xp_stx_word_t at; xp_stx_word_t idx;
at = xp_stx_alloc_objmem ( idx = xp_stx_memory_alloc (&stx->memory,
&stx->memory, (n + 1) * xp_sizeof(xp_stx_char_t)); (n + 1) * xp_sizeof(xp_stx_char_t) + xp_sizeof(xp_stx_object_t));
if (at >= stx->memory.capacity) return at; /* failed */ if (idx >= stx->memory.capacity) return idx; /* failed */
XP_STX_OBJECT_CLASS(&stx->memory,at) = stx->nil; XP_STX_OBJECT_CLASS(&stx->memory,idx) = stx->nil;
XP_STX_OBJECT_ACCESS(&stx->memory,at) = ((n << 2) | 0x02); XP_STX_OBJECT_ACCESS(&stx->memory,idx) = ((n << 2) | 0x02);
XP_STX_BYTE_OBJECT(&stx->memory,at)->data[n] = XP_STX_CHAR('\0'); XP_STX_OBJECT_CHARAT(&stx->memory,idx,n) = XP_STX_CHAR('\0');
while (n--) XP_STX_BYTE_OBJECT(&stx->memory,at)->data[n] = str[n]; while (n--) XP_STX_OBJECT_CHARAT(&stx->memory,idx,n) = str[n];
return at; return idx;
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: stx.c,v 1.1 2005-05-08 07:39:51 bacon Exp $ * $Id: stx.c,v 1.2 2005-05-08 10:31:24 bacon Exp $
*/ */
#include <xp/stx/stx.h> #include <xp/stx/stx.h>
@ -20,6 +20,10 @@ xp_stx_t* xp_stx_open (xp_stx_t* stx, xp_stx_word_t capacity)
return XP_NULL; return XP_NULL;
} }
stx->nil = XP_STX_NIL;
stx->true = XP_STX_TRUE;
stx->false = XP_STX_FALSE;
return stx; return stx;
} }
@ -29,3 +33,4 @@ void xp_stx_close (xp_stx_t* stx)
if (stx->__malloced) xp_free (stx); if (stx->__malloced) xp_free (stx);
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Id: stx.h,v 1.1 2005-05-08 07:39:51 bacon Exp $ * $Id: stx.h,v 1.2 2005-05-08 10:31:24 bacon Exp $
*/ */
#ifndef _XP_STX_STX_H_ #ifndef _XP_STX_STX_H_
@ -20,32 +20,39 @@ typedef struct xp_stx_string_object_t xp_stx_string_object_t;
typedef struct xp_stx_memory_t xp_stx_memory_t; typedef struct xp_stx_memory_t xp_stx_memory_t;
typedef struct xp_stx_t xp_stx_t; typedef struct xp_stx_t xp_stx_t;
#define XP_STX_CHAR(x) XP_CHAR(x) #define XP_STX_CHAR(x) XP_CHAR(x)
#define XP_STX_TEXT(x) XP_TEXT(x) #define XP_STX_TEXT(x) XP_TEXT(x)
/* common object header structure */ /* common object header structure */
#define XP_STX_OBJECT_HEADER_SIZE \
(xp_sizeof(xp_stx_object_t) - xp_sizeof(xp_stx_word_t))
struct xp_stx_object_t struct xp_stx_object_t
{ {
/* access - is_byte_indexed: 1; size: rest */ /* access - mode: 2; size: rest;
* mode - word indexed: 00 byte indexed: 01 char indexed: 10
*/
xp_stx_word_t access; xp_stx_word_t access;
xp_stx_word_t class; xp_stx_word_t class;
xp_stx_word_t data[1]; /*xp_stx_word_t didxa[1];*/
}; };
/*
struct xp_stx_byte_object_t struct xp_stx_byte_object_t
{ {
xp_stx_word_t access; xp_stx_word_t access;
xp_stx_word_t class; xp_stx_word_t class;
xp_stx_byte_t data[1]; xp_stx_byte_t didxa[1];
}; };
struct xp_stx_string_object_t struct xp_stx_string_object_t
{ {
xp_stx_word_t access; xp_stx_word_t access;
xp_stx_word_t class; xp_stx_word_t class;
xp_stx_char_t data[1]; xp_stx_char_t didxa[1];
}; };
*/
struct xp_stx_memory_t struct xp_stx_memory_t
{ {
@ -66,23 +73,49 @@ struct xp_stx_t
xp_bool_t __malloced; xp_bool_t __malloced;
}; };
#define XP_STX_OBJECT(mem,at) \ #define XP_STX_NIL 0
((xp_stx_object_t*)((mem)->slots[at])) #define XP_STX_TRUE 1
#define XP_STX_BYTE_OBJECT(mem,at) \ #define XP_STX_FALSE 2
((xp_stx_byte_object_t*)((mem)->slots[at]))
#define XP_STX_STRING_OBJECT(mem,at) \ /*
((xp_stx_string_object_t*)((mem)->slots[at])) #define XP_STX_OBJECT(mem,idx) \
((xp_stx_object_t*)((mem)->slots[idx]))
#define XP_STX_BYTE_OBJECT(mem,idx) \
((xp_stx_byte_object_t*)((mem)->slots[idx]))
#define XP_STX_STRING_OBJECT(mem,idx) \
((xp_stx_string_object_t*)((mem)->slots[idx]))
*/
#define XP_STX_OBJECT(mem,idx) ((mem)->slots[idx])
#define XP_STX_OBJECT_ACCESS(mem,idx) (XP_STX_OBJECT(mem,(idx))->access)
#define XP_STX_OBJECT_CLASS(mem,idx) (XP_STX_OBJECT(mem,(idx))->class)
/*
#define XP_STX_OBJECT_DATA(mem,idx) \
(((XP_STX_OBJECT_ACCESS(mem,idx) & 0x03) == 0x00)? \
(XP_STX_OBJECT(mem,idx)).didxa): \
(((XP_STX_OBJECT_ACCESS(mem,idx) & 0x03) == 0x01)? \
(XP_STX_BYTE_OBJECT(mem,idx)).didxa): \
(XP_STX_STRING_OBJECT(mem,idx)).didxa))
*/
#define XP_STX_OBJECT_AT(mem,idx,n) \
(((xp_stx_word_t*)(XP_STX_OBJECT(mem,idx) + 1))[n])
#define XP_STX_OBJECT_BYTEAT(mem,idx,n) \
(((xp_stx_byte_t*)(XP_STX_OBJECT(mem,idx) + 1))[n])
#define XP_STX_OBJECT_CHARAT(mem,idx,n) \
(((xp_stx_char_t*)(XP_STX_OBJECT(mem,idx) + 1))[n])
/*
#define XP_STX_OBJECT_DATA(mem,idx) \
(((XP_STX_OBJECT_ACCESS(mem,idx) & 0x03) == 0x00)? \
(((xp_stx_word_t*)XP_STX_OBJECT(mem,idx)) + 1): \
(((XP_STX_OBJECT_ACCESS(mem,idx) & 0x03) == 0x01)? \
(((xp_stx_byte_t*)XP_STX_OBJECT(mem,idx)) + 1): \
(((xp_stx_char_t*)XP_STX_OBJECT(mem,idx)) + 1)))
*/
#define XP_STX_OBJECT_ACCESS(mem,at) ((XP_STX_OBJECT(mem,at))->access)
#define XP_STX_OBJECT_CLASS(mem,at) ((XP_STX_OBJECT(mem,at))->class)
#define XP_STX_OBJECT_DATA(mem,at) \
(((XP_STX_OBJECT_ACCESS(mem,at) & 0x04) == 0x00)? \
(XP_STX_OBJECT(mem,at)).data): \
(((XP_STX_OBJECT_ACCESS(mem,at) & 0x04) == 0x01)? \
(XP_STX_BYTE_OBJECT(mem,at)).data): \
(XP_STX_STRING_OBJECT(mem,at)).data))
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

View File

@ -3,28 +3,33 @@
int xp_main () int xp_main ()
{ {
xp_stx_memory_t mem; xp_stx_t stx;
xp_stx_word_t i; xp_stx_word_t i;
if (xp_stx_memory_open (&mem, 10) == XP_NULL) { if (xp_stx_open (&stx, 10) == XP_NULL) {
xp_printf (XP_TEXT("cannot open memory\n")); xp_printf (XP_TEXT("cannot open memory\n"));
return -1; return -1;
} }
stx.nil = xp_stx_memory_alloc(&stx.memory, 0);
stx.true = xp_stx_memory_alloc(&stx.memory, 0);
stx.false = xp_stx_memory_alloc(&stx.memory, 0);
for (i = 0; i < 20; i++) { for (i = 0; i < 20; i++) {
xp_printf (XP_TEXT("%d, %d\n"), xp_printf (XP_TEXT("%d, %d\n"),
i, xp_stx_alloc_objmem(&mem, 100)); i, xp_stx_memory_alloc(&stx.memory, 100));
} }
for (i = 0; i < 5; i++) { for (i = 0; i < 5; i++) {
xp_stx_dealloc_objmem (&mem, i); xp_stx_memory_dealloc (&stx.memory, i);
} }
for (i = 0; i < 20; i++) { for (i = 0; i < 20; i++) {
xp_printf (XP_TEXT("%d, %d\n"), xp_printf (XP_TEXT("%d, %d\n"),
i, xp_stx_alloc_objmem(&mem, 100)); i, xp_stx_memory_alloc(&stx.memory, 100));
} }
xp_stx_close (&stx);
xp_printf (XP_TEXT("End of program\n")); xp_printf (XP_TEXT("End of program\n"));
return 0; return 0;
} }