diff --git a/ase/stx/memory.c b/ase/stx/memory.c index a02bf9ff..da8b0b35 100644 --- a/ase/stx/memory.c +++ b/ase/stx/memory.c @@ -1,5 +1,5 @@ /* - * $Id: memory.c,v 1.3 2005-05-06 17:17:59 bacon Exp $ + * $Id: memory.c,v 1.4 2005-05-08 07:39:51 bacon Exp $ */ #include @@ -63,7 +63,7 @@ void xp_stx_garbage_collect (xp_stx_memory_t* mem) /* TODO: implement this function */ } -xp_stx_word_t xp_stx_alloc_object (xp_stx_memory_t* mem, xp_stx_word_t nbytes) +xp_stx_word_t xp_stx_alloc_objmem (xp_stx_memory_t* mem, xp_stx_word_t nbytes) { xp_stx_object_t** slot; xp_stx_object_t* object; @@ -88,7 +88,7 @@ xp_stx_word_t xp_stx_alloc_object (xp_stx_memory_t* mem, xp_stx_word_t nbytes) return (xp_stx_word_t)(slot - mem->slots); } -void xp_stx_dealloc_object (xp_stx_memory_t* mem, xp_stx_word_t object_index) +void xp_stx_dealloc_objmem (xp_stx_memory_t* mem, xp_stx_word_t object_index) { /* * THIS IS PRIMITIVE LOW-LEVEL DEALLOC. THIS WILL NOT diff --git a/ase/stx/memory.h b/ase/stx/memory.h index 35ff2c86..fd1abef9 100644 --- a/ase/stx/memory.h +++ b/ase/stx/memory.h @@ -1,76 +1,11 @@ /* - * $Id: memory.h,v 1.3 2005-05-06 17:17:59 bacon Exp $ + * $Id: memory.h,v 1.4 2005-05-08 07:39:51 bacon Exp $ */ #ifndef _XP_STX_MEMORY_H_ #define _XP_STX_MEMORY_H_ -#include -#include - -typedef struct xp_stx_memory_t xp_stx_memory_t; -typedef struct xp_stx_object_t xp_stx_object_t; -typedef struct xp_stx_byte_object_t xp_stx_byte_object_t; -typedef struct xp_stx_string_object_t xp_stx_string_object_t; - -typedef xp_byte_t xp_stx_byte_t; -typedef xp_char_t xp_stx_char_t; -typedef xp_size_t xp_stx_word_t; -typedef xp_size_t xp_stx_size_t; -typedef xp_size_t xp_stx_index_t; -typedef xp_stx_object_t* xp_stx_pointer_t; - - -/* common object header structure */ -struct xp_stx_object_t -{ - /* access - is_byte_indexed: 1; size: rest */ - xp_stx_word_t access; - xp_stx_word_t class; - xp_stx_word_t data[1]; -}; - -struct xp_stx_byte_object_t -{ - xp_stx_word_t access; - xp_stx_word_t class; - xp_stx_byte_t data[1]; -}; - -struct xp_stx_string_object_t -{ - xp_stx_word_t access; - xp_stx_word_t class; - xp_stx_char_t data[1]; -}; - -struct xp_stx_memory_t -{ - xp_stx_word_t capacity; - xp_stx_object_t** slots; - xp_stx_object_t** free; - xp_bool_t __malloced; -}; - -#define XP_STX_NIL (0) - -#define XP_STX_OBJECT(mem,at) \ - ((xp_stx_object_t*)(mem->slots[at])) -#define XP_STX_BYTE_OBJECT(mem,at) \ - ((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_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)) - +#include #ifdef __cplusplus extern "C" { @@ -81,8 +16,8 @@ xp_stx_memory_t* xp_stx_memory_open ( void xp_stx_memory_close (xp_stx_memory_t* mem); void xp_stx_gaxpage_collect (xp_stx_memory_t* mem); -xp_stx_word_t xp_stx_alloc_object (xp_stx_memory_t* mem, xp_stx_word_t size); -void xp_stx_dealloc_object (xp_stx_memory_t* mem, xp_stx_word_t object_index); +xp_stx_word_t xp_stx_alloc_objmem (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); #ifdef __cplusplus } diff --git a/ase/stx/object.c b/ase/stx/object.c index e86a43be..b4db5e94 100644 --- a/ase/stx/object.c +++ b/ase/stx/object.c @@ -1,50 +1,58 @@ /* - * $Id: object.c,v 1.1 2005-05-06 17:17:59 bacon Exp $ + * $Id: object.c,v 1.2 2005-05-08 07:39:51 bacon Exp $ */ #include +#include /* n: number of instance variables */ -xp_stx_word_t xp_stx_instantiate (xp_stx_memory_t* mem, 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; /* bytes to allocated = * number of instance variables * word_size */ - at = xp_stx_alloc_object (mem, n * xp_sizeof(xp_stx_word_t)); - if (at >= mem->capacity) return at; /* failed */ + at = xp_stx_alloc_objmem ( + &stx->memory, n * xp_sizeof(xp_stx_word_t)); + if (at >= stx->memory.capacity) return at; /* failed */ + + XP_STX_OBJECT_CLASS(&stx->memory,at) = stx->nil; + XP_STX_OBJECT_ACCESS(&stx->memory,at) = ((n << 2) | 0x00); + while (n--) XP_STX_OBJECT(&stx->memory,at)->data[n] = stx->nil; - XP_STX_OBJECT_CLASS(mem,at) = XP_STX_NIL; - XP_STX_OBJECT_ACCESS(mem,at) = ((n << 2) | 0x00); - while (n--) XP_STX_OBJECT(mem,at)->data[n] = XP_STX_NIL; return at; } -xp_stx_word_t xp_stx_instantiate_byte (xp_stx_memory_t* mem, xp_size_t n) +/* 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 at; - at = xp_stx_alloc_object (mem, n); - if (at >= mem->capacity) return at; /* failed */ + at = xp_stx_alloc_objmem (&stx->memory, n); + if (at >= stx->memory.capacity) return at; /* failed */ + + XP_STX_OBJECT_CLASS(&stx->memory,at) = stx->nil; + XP_STX_OBJECT_ACCESS(&stx->memory,at) = ((n << 2) | 0x01); + while (n--) XP_STX_BYTE_OBJECT(&stx->memory,at)->data[n] = 0; - XP_STX_OBJECT_CLASS(mem,at) = XP_STX_NIL; - XP_STX_OBJECT_ACCESS(mem,at) = ((n << 2) | 0x01); - while (n--) XP_STX_BYTE_OBJECT(mem,at)->data[n] = 0; return at; } -xp_stx_word_t xp_stx_instantiate_string ( - xp_stx_memory_t* mem, xp_stx_char_t* str, xp_stx_word_t n) +/* n: length of the string */ +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_word_t at; - at = xp_stx_alloc_object (mem, n * xp_sizeof(xp_stx_char_t)); - if (at >= mem->capacity) return at; /* failed */ + at = xp_stx_alloc_objmem ( + &stx->memory, (n + 1) * xp_sizeof(xp_stx_char_t)); + if (at >= stx->memory.capacity) return at; /* failed */ - XP_STX_OBJECT_CLASS(mem,at) = XP_STX_NIL; - XP_STX_OBJECT_ACCESS(mem,at) = ((n << 2) | 0x02); - while (n--) XP_STX_BYTE_OBJECT(mem,at)->data[n] = str[n]; + XP_STX_OBJECT_CLASS(&stx->memory,at) = stx->nil; + XP_STX_OBJECT_ACCESS(&stx->memory,at) = ((n << 2) | 0x02); + XP_STX_BYTE_OBJECT(&stx->memory,at)->data[n] = XP_STX_CHAR('\0'); + while (n--) XP_STX_BYTE_OBJECT(&stx->memory,at)->data[n] = str[n]; return at; } diff --git a/ase/stx/object.h b/ase/stx/object.h index 20ff9c66..715bda9c 100644 --- a/ase/stx/object.h +++ b/ase/stx/object.h @@ -1,14 +1,27 @@ /* - * $Id: object.h,v 1.1 2005-05-06 17:17:59 bacon Exp $ + * $Id: object.h,v 1.2 2005-05-08 07:39:51 bacon Exp $ */ #ifndef _XP_STX_OBJECT_H_ #define _XP_STX_OBJECT_H_ -#include +#include #define XP_STX_IS_SMALLINT(x) (((x) & 0x01) == 0x01) #define XP_STX_TO_SMALLINT(x) ((x) << 1) | 0x01) #define XP_STX_FROM_SMALLINT(x) ((x) >> 1) +#ifdef __cplusplus +extern "C" { +#endif + +xp_stx_word_t xp_stx_alloc_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 xp_stx_alloc_string_object ( + xp_stx_t* stx, xp_stx_char_t* str, xp_size_t n); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/ase/stx/stx.c b/ase/stx/stx.c new file mode 100644 index 00000000..a5dfbcfd --- /dev/null +++ b/ase/stx/stx.c @@ -0,0 +1,31 @@ +/* + * $Id: stx.c,v 1.1 2005-05-08 07:39:51 bacon Exp $ + */ + +#include +#include +#include + +xp_stx_t* xp_stx_open (xp_stx_t* stx, xp_stx_word_t capacity) +{ + if (stx == XP_NULL) { + stx = (xp_stx_t*) xp_malloc (xp_sizeof(stx)); + if (stx == XP_NULL) return XP_NULL; + stx->__malloced = xp_true; + } + else stx->__malloced = xp_false; + + if (xp_stx_memory_open (&stx->memory, capacity) == XP_NULL) { + if (stx->__malloced) xp_free (stx); + return XP_NULL; + } + + return stx; +} + +void xp_stx_close (xp_stx_t* stx) +{ + xp_stx_memory_close (&stx->memory); + if (stx->__malloced) xp_free (stx); +} + diff --git a/ase/stx/stx.h b/ase/stx/stx.h new file mode 100644 index 00000000..bec6f8d1 --- /dev/null +++ b/ase/stx/stx.h @@ -0,0 +1,98 @@ +/* + * $Id: stx.h,v 1.1 2005-05-08 07:39:51 bacon Exp $ + */ + +#ifndef _XP_STX_STX_H_ +#define _XP_STX_STX_H_ + +#include +#include + +typedef xp_byte_t xp_stx_byte_t; +typedef xp_char_t xp_stx_char_t; +typedef xp_size_t xp_stx_word_t; +typedef xp_size_t xp_stx_size_t; +typedef xp_size_t xp_stx_index_t; + +typedef struct xp_stx_object_t xp_stx_object_t; +typedef struct xp_stx_byte_object_t xp_stx_byte_object_t; +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_t xp_stx_t; + + +#define XP_STX_CHAR(x) XP_CHAR(x) +#define XP_STX_TEXT(x) XP_TEXT(x) + +/* common object header structure */ +struct xp_stx_object_t +{ + /* access - is_byte_indexed: 1; size: rest */ + xp_stx_word_t access; + xp_stx_word_t class; + xp_stx_word_t data[1]; +}; + +struct xp_stx_byte_object_t +{ + xp_stx_word_t access; + xp_stx_word_t class; + xp_stx_byte_t data[1]; +}; + +struct xp_stx_string_object_t +{ + xp_stx_word_t access; + xp_stx_word_t class; + xp_stx_char_t data[1]; +}; + +struct xp_stx_memory_t +{ + xp_stx_word_t capacity; + xp_stx_object_t** slots; + xp_stx_object_t** free; + xp_bool_t __malloced; +}; + +struct xp_stx_t +{ + xp_stx_memory_t memory; + + xp_stx_word_t nil; + xp_stx_word_t true; + xp_stx_word_t false; + + xp_bool_t __malloced; +}; + +#define XP_STX_OBJECT(mem,at) \ + ((xp_stx_object_t*)((mem)->slots[at])) +#define XP_STX_BYTE_OBJECT(mem,at) \ + ((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_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 +extern "C" { +#endif + +xp_stx_t* xp_stx_open (xp_stx_t* stx, xp_stx_word_t capacity); +void xp_stx_close (xp_stx_t* stx); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ase/stx/xpstx.tgt b/ase/stx/xpstx.tgt index d49c17ad..aec7a0d8 100644 --- a/ase/stx/xpstx.tgt +++ b/ase/stx/xpstx.tgt @@ -42,7 +42,7 @@ WVList 0 10 WPickList -6 +8 11 MItem 3 @@ -139,26 +139,26 @@ WVList 0 33 MItem -3 -*.h +5 +stx.c 34 WString -3 -NIL +4 +COBJ 35 WVList 0 36 WVList 0 --1 +11 1 1 0 37 MItem -8 -memory.h +3 +*.h 38 WString 3 @@ -169,14 +169,14 @@ WVList 40 WVList 0 -33 +-1 1 1 0 41 MItem 8 -object.h +memory.h 42 WString 3 @@ -187,7 +187,43 @@ WVList 44 WVList 0 -33 +37 +1 +1 +0 +45 +MItem +8 +object.h +46 +WString +3 +NIL +47 +WVList +0 +48 +WVList +0 +37 +1 +1 +0 +49 +MItem +5 +stx.h +50 +WString +3 +NIL +51 +WVList +0 +52 +WVList +0 +37 1 1 0 diff --git a/ase/stx/xpstx.wpj b/ase/stx/xpstx.wpj index f6e34827..a37b93bf 100644 --- a/ase/stx/xpstx.wpj +++ b/ase/stx/xpstx.wpj @@ -39,5 +39,5 @@ WFileName 9 xpstx.tgt 0 -0 +4 7 diff --git a/ase/test/stx/stx.c b/ase/test/stx/stx.c index 23400674..61d2174a 100644 --- a/ase/test/stx/stx.c +++ b/ase/test/stx/stx.c @@ -13,16 +13,16 @@ int xp_main () for (i = 0; i < 20; i++) { xp_printf (XP_TEXT("%d, %d\n"), - i, xp_stx_alloc_object(&mem, 100)); + i, xp_stx_alloc_objmem(&mem, 100)); } for (i = 0; i < 5; i++) { - xp_stx_dealloc_object (&mem, i); + xp_stx_dealloc_objmem (&mem, i); } for (i = 0; i < 20; i++) { xp_printf (XP_TEXT("%d, %d\n"), - i, xp_stx_alloc_object(&mem, 100)); + i, xp_stx_alloc_objmem(&mem, 100)); } xp_printf (XP_TEXT("End of program\n"));