qse/ase/stx/object.c

146 lines
3.8 KiB
C
Raw Normal View History

2005-05-06 17:18:29 +00:00
/*
2005-05-23 15:51:03 +00:00
* $Id: object.c,v 1.23 2005-05-23 15:51:03 bacon Exp $
2005-05-06 17:18:29 +00:00
*/
#include <xp/stx/object.h>
2005-05-08 07:39:51 +00:00
#include <xp/stx/memory.h>
2005-05-18 04:01:51 +00:00
#include <xp/stx/symbol.h>
2005-05-10 15:15:58 +00:00
#include <xp/stx/hash.h>
2005-05-15 18:37:00 +00:00
#include <xp/stx/misc.h>
2005-05-10 15:15:57 +00:00
2005-05-06 17:18:29 +00:00
/* n: number of instance variables */
2005-05-22 04:34:22 +00:00
xp_stx_word_t xp_stx_alloc_word_object (xp_stx_t* stx, xp_stx_word_t n)
2005-05-06 17:18:29 +00:00
{
2005-05-08 10:31:25 +00:00
xp_stx_word_t idx;
2005-05-22 04:34:22 +00:00
xp_stx_word_object_t* obj;
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
/* bytes to allocidxed =
2005-05-06 17:18:29 +00:00
* number of instance variables * word_size
*/
2005-05-08 10:31:25 +00:00
idx = xp_stx_memory_alloc (&stx->memory,
n * xp_sizeof(xp_stx_word_t) + xp_sizeof(xp_stx_object_t));
if (idx >= stx->memory.capacity) return idx; /* failed */
2005-05-08 07:39:51 +00:00
2005-05-19 16:41:10 +00:00
xp_stx_assert (stx->nil == XP_STX_NIL);
2005-05-22 04:34:22 +00:00
/*
2005-05-08 15:22:45 +00:00
XP_STX_CLASS(stx,idx) = stx->nil;
2005-05-22 04:34:22 +00:00
XP_STX_ACCESS(stx,idx) = (n << 2) | XP_STX_WORD_INDEXED;
2005-05-18 16:34:51 +00:00
while (n-- > 0) XP_STX_AT(stx,idx,n) = stx->nil;
2005-05-22 04:34:22 +00:00
*/
obj = XP_STX_WORD_OBJECT(stx,idx);
obj->header.class = stx->nil;
obj->header.access = (n << 2) | XP_STX_WORD_INDEXED;
while (n-- > 0) obj->data[n] = stx->nil;
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
return idx;
2005-05-06 17:18:29 +00:00
}
2005-05-08 07:39:51 +00:00
/* n: number of bytes */
2005-05-08 11:16:07 +00:00
xp_stx_word_t xp_stx_alloc_byte_object (xp_stx_t* stx, xp_stx_word_t n)
2005-05-06 17:18:29 +00:00
{
2005-05-08 10:31:25 +00:00
xp_stx_word_t idx;
2005-05-22 04:34:22 +00:00
xp_stx_byte_object_t* obj;
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
idx = xp_stx_memory_alloc (
&stx->memory, n + xp_sizeof(xp_stx_object_t));
if (idx >= stx->memory.capacity) return idx; /* failed */
2005-05-08 07:39:51 +00:00
2005-05-19 16:41:10 +00:00
xp_stx_assert (stx->nil == XP_STX_NIL);
2005-05-22 04:34:22 +00:00
/*
2005-05-08 15:22:45 +00:00
XP_STX_CLASS(stx,idx) = stx->nil;
2005-05-10 08:21:10 +00:00
XP_STX_ACCESS(stx,idx) = (n << 2) | XP_STX_BYTE_INDEXED;
2005-05-18 16:34:51 +00:00
while (n-- > 0) XP_STX_BYTEAT(stx,idx,n) = 0;
2005-05-22 04:34:22 +00:00
*/
obj = XP_STX_BYTE_OBJECT(stx,idx);
obj->header.class = stx->nil;
obj->header.access = (n << 2) | XP_STX_BYTE_INDEXED;
while (n-- > 0) obj->data[n] = 0;
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
return idx;
2005-05-06 17:18:29 +00:00
}
2005-05-21 15:55:50 +00:00
xp_stx_word_t xp_stx_alloc_char_object (
2005-05-10 08:21:10 +00:00
xp_stx_t* stx, const xp_stx_char_t* str)
2005-05-06 17:18:29 +00:00
{
2005-05-23 15:51:03 +00:00
return xp_stx_alloc_char_objectx (stx, str, xp_stx_strlen(str));
}
xp_stx_word_t xp_stx_alloc_char_objectx (
xp_stx_t* stx, const xp_stx_char_t* str, xp_stx_word_t n)
{
xp_stx_word_t idx;
2005-05-22 04:34:22 +00:00
xp_stx_char_object_t* obj;
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
idx = xp_stx_memory_alloc (&stx->memory,
(n + 1) * xp_sizeof(xp_stx_char_t) + xp_sizeof(xp_stx_object_t));
if (idx >= stx->memory.capacity) return idx; /* failed */
2005-05-06 17:18:29 +00:00
2005-05-19 16:41:10 +00:00
xp_stx_assert (stx->nil == XP_STX_NIL);
2005-05-22 04:34:22 +00:00
/*
2005-05-08 15:22:45 +00:00
XP_STX_CLASS(stx,idx) = stx->nil;
2005-05-10 08:21:10 +00:00
XP_STX_ACCESS(stx,idx) = (n << 2) | XP_STX_CHAR_INDEXED;
2005-05-08 15:22:45 +00:00
XP_STX_CHARAT(stx,idx,n) = XP_STX_CHAR('\0');
2005-05-18 16:34:51 +00:00
while (n-- > 0) XP_STX_CHARAT(stx,idx,n) = str[n];
2005-05-22 04:34:22 +00:00
*/
obj = XP_STX_CHAR_OBJECT(stx,idx);
obj->header.class = stx->nil;
obj->header.access = (n << 2) | XP_STX_CHAR_INDEXED;
obj->data[n] = XP_STX_CHAR('\0');
while (n-- > 0) obj->data[n] = str[n];
2005-05-06 17:18:29 +00:00
2005-05-08 10:31:25 +00:00
return idx;
2005-05-06 17:18:29 +00:00
}
2005-05-08 13:45:51 +00:00
2005-05-21 15:55:50 +00:00
xp_stx_word_t xp_stx_allocn_char_object (xp_stx_t* stx, ...)
2005-05-10 15:15:57 +00:00
{
xp_stx_word_t idx, n = 0;
const xp_stx_char_t* p;
2005-05-19 16:41:10 +00:00
xp_stx_va_list ap;
2005-05-22 04:34:22 +00:00
xp_stx_char_object_t* obj;
2005-05-10 15:15:57 +00:00
2005-05-19 16:41:10 +00:00
xp_stx_va_start (ap, stx);
while ((p = xp_stx_va_arg(ap, const xp_stx_char_t*)) != XP_NULL) {
2005-05-15 18:37:00 +00:00
n += xp_stx_strlen(p);
2005-05-10 15:15:57 +00:00
}
2005-05-19 16:41:10 +00:00
xp_stx_va_end (ap);
2005-05-10 15:15:57 +00:00
idx = xp_stx_memory_alloc (&stx->memory,
(n + 1) * xp_sizeof(xp_stx_char_t) + xp_sizeof(xp_stx_object_t));
if (idx >= stx->memory.capacity) return idx; /* failed */
2005-05-19 16:41:10 +00:00
xp_stx_assert (stx->nil == XP_STX_NIL);
2005-05-22 04:34:22 +00:00
/*
2005-05-10 15:15:57 +00:00
XP_STX_CLASS(stx,idx) = stx->nil;
XP_STX_ACCESS(stx,idx) = (n << 2) | XP_STX_CHAR_INDEXED;
XP_STX_CHARAT(stx,idx,n) = XP_STX_CHAR('\0');
2005-05-22 04:34:22 +00:00
*/
obj = XP_STX_CHAR_OBJECT(stx,idx);
obj->header.class = stx->nil;
obj->header.access = (n << 2) | XP_STX_CHAR_INDEXED;
obj->data[n] = XP_STX_CHAR('\0');
2005-05-10 15:15:57 +00:00
2005-05-19 16:41:10 +00:00
xp_stx_va_start (ap, stx);
2005-05-10 15:15:57 +00:00
n = 0;
2005-05-19 16:41:10 +00:00
while ((p = xp_stx_va_arg(ap, const xp_stx_char_t*)) != XP_NULL) {
2005-05-22 04:34:22 +00:00
while (*p != XP_STX_CHAR('\0')) {
/*XP_STX_CHARAT(stx,idx,n++) = *p++;*/
obj->data[n++] = *p++;
}
2005-05-10 15:15:57 +00:00
}
2005-05-19 16:41:10 +00:00
xp_stx_va_end (ap);
2005-05-10 15:15:57 +00:00
return idx;
}
2005-05-21 15:55:50 +00:00
xp_stx_word_t xp_stx_hash_char_object (xp_stx_t* stx, xp_stx_word_t idx)
2005-05-10 08:21:10 +00:00
{
2005-05-19 16:41:10 +00:00
xp_stx_assert (XP_STX_TYPE(stx,idx) == XP_STX_CHAR_INDEXED);
2005-05-15 18:37:00 +00:00
return xp_stx_strxhash (
2005-05-23 14:43:03 +00:00
XP_STX_DATA(stx,idx), XP_STX_SIZE(stx,idx));
2005-05-10 16:20:53 +00:00
}