qse/ase/stx/object.c

231 lines
5.8 KiB
C
Raw Normal View History

2005-05-06 17:18:29 +00:00
/*
2005-07-05 11:15:51 +00:00
* $Id: object.c,v 1.35 2005-07-05 11:15:51 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-07-04 16:23:13 +00:00
#include <xp/stx/class.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-07-05 06:26:33 +00:00
xp_word_t xp_stx_alloc_word_object (
2005-07-05 09:02:13 +00:00
xp_stx_t* stx, const xp_word_t* data, xp_word_t nfields,
const xp_word_t* variable_data, xp_word_t variable_nfields)
2005-05-06 17:18:29 +00:00
{
2005-07-05 09:02:13 +00:00
xp_word_t idx, n;
2005-05-22 04:34:22 +00:00
xp_stx_word_object_t* obj;
2005-05-06 17:18:29 +00:00
2005-07-05 09:02:13 +00:00
xp_assert (stx->nil == XP_STX_NIL);
/* bytes to allocated =
* (number of instance variables +
* number of variable instance variables) * word_size
2005-05-06 17:18:29 +00:00
*/
2005-07-05 09:02:13 +00:00
n = nfields + variable_nfields;
2005-05-08 10:31:25 +00:00
idx = xp_stx_memory_alloc (&stx->memory,
2005-06-08 16:05:41 +00:00
n * xp_sizeof(xp_word_t) + xp_sizeof(xp_stx_object_t));
2005-07-05 10:22:35 +00:00
if (idx >= stx->memory.capacity) return idx; /* failed TODO: return a difference value OINDEX_INVALID */
2005-05-08 07:39:51 +00:00
2005-07-05 10:22:35 +00:00
idx = XP_STX_TO_OINDEX(idx);
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;
2005-07-05 09:02:13 +00:00
if (variable_data == XP_NULL) {
2005-07-05 11:15:51 +00:00
while (n > nfields) obj->data[--n] = stx->nil;
2005-07-05 06:26:33 +00:00
}
else {
2005-07-05 09:02:13 +00:00
while (n > nfields) {
n--; obj->data[n] = variable_data[n - nfields];
}
}
if (data == XP_NULL) {
while (n > 0) obj->data[--n] = stx->nil;
}
else {
while (n > 0) {
n--; obj->data[n] = data[n];
}
2005-07-05 06:26:33 +00:00
}
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-07-04 11:32:41 +00:00
xp_word_t xp_stx_alloc_byte_object (
xp_stx_t* stx, const xp_byte_t* data, xp_word_t n)
2005-05-06 17:18:29 +00:00
{
2005-06-08 16:05:41 +00:00
xp_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-07-05 09:02:13 +00:00
xp_assert (stx->nil == XP_STX_NIL);
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-07-05 10:22:35 +00:00
idx = XP_STX_TO_OINDEX(idx);
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;
2005-07-04 11:32:41 +00:00
if (data == XP_NULL) {
while (n-- > 0) obj->data[n] = 0;
}
2005-07-05 06:26:33 +00:00
else {
2005-07-04 11:32:41 +00:00
while (n-- > 0) obj->data[n] = data[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-06-08 16:05:41 +00:00
xp_word_t xp_stx_alloc_char_object (
xp_stx_t* stx, const xp_char_t* str)
2005-05-06 17:18:29 +00:00
{
2005-07-04 16:23:13 +00:00
return (str == XP_NULL)?
xp_stx_alloc_char_objectx (stx, XP_NULL, 0):
xp_stx_alloc_char_objectx (stx, str, xp_strlen(str));
2005-05-23 15:51:03 +00:00
}
2005-07-05 09:02:13 +00:00
/* n: number of characters */
2005-06-08 16:05:41 +00:00
xp_word_t xp_stx_alloc_char_objectx (
xp_stx_t* stx, const xp_char_t* str, xp_word_t n)
2005-05-23 15:51:03 +00:00
{
2005-06-08 16:05:41 +00:00
xp_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-07-05 09:02:13 +00:00
xp_assert (stx->nil == XP_STX_NIL);
2005-05-08 10:31:25 +00:00
idx = xp_stx_memory_alloc (&stx->memory,
2005-06-08 16:05:41 +00:00
(n + 1) * xp_sizeof(xp_char_t) + xp_sizeof(xp_stx_object_t));
2005-05-08 10:31:25 +00:00
if (idx >= stx->memory.capacity) return idx; /* failed */
2005-05-06 17:18:29 +00:00
2005-07-05 10:22:35 +00:00
idx = XP_STX_TO_OINDEX(idx);
2005-06-30 12:07:02 +00:00
obj = XP_STX_CHAR_OBJECT(stx,idx);
2005-05-22 04:34:22 +00:00
obj->header.class = stx->nil;
2005-06-30 12:07:02 +00:00
obj->header.access = (n << 2) | XP_STX_CHAR_INDEXED;
2005-06-08 16:05:41 +00:00
obj->data[n] = XP_CHAR('\0');
2005-07-04 16:23:13 +00:00
if (str == XP_NULL) {
while (n-- > 0) obj->data[n] = XP_CHAR('\0');
}
else {
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-06-08 16:05:41 +00:00
xp_word_t xp_stx_allocn_char_object (xp_stx_t* stx, ...)
2005-05-10 15:15:57 +00:00
{
2005-06-08 16:05:41 +00:00
xp_word_t idx, n = 0;
const xp_char_t* p;
xp_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-07-05 10:22:35 +00:00
xp_assert (stx->nil == XP_STX_NIL);
2005-06-08 16:05:41 +00:00
xp_va_start (ap, stx);
while ((p = xp_va_arg(ap, const xp_char_t*)) != XP_NULL) {
2005-06-08 16:14:52 +00:00
n += xp_strlen(p);
2005-05-10 15:15:57 +00:00
}
2005-06-08 16:05:41 +00:00
xp_va_end (ap);
2005-05-10 15:15:57 +00:00
idx = xp_stx_memory_alloc (&stx->memory,
2005-06-08 16:05:41 +00:00
(n + 1) * xp_sizeof(xp_char_t) + xp_sizeof(xp_stx_object_t));
2005-05-10 15:15:57 +00:00
if (idx >= stx->memory.capacity) return idx; /* failed */
2005-07-05 10:22:35 +00:00
idx = XP_STX_TO_OINDEX(idx);
2005-06-30 12:07:02 +00:00
obj = XP_STX_CHAR_OBJECT(stx,idx);
2005-05-22 04:34:22 +00:00
obj->header.class = stx->nil;
2005-06-30 12:07:02 +00:00
obj->header.access = (n << 2) | XP_STX_CHAR_INDEXED;
2005-06-08 16:05:41 +00:00
obj->data[n] = XP_CHAR('\0');
2005-05-10 15:15:57 +00:00
2005-06-08 16:05:41 +00:00
xp_va_start (ap, stx);
2005-05-10 15:15:57 +00:00
n = 0;
2005-06-08 16:05:41 +00:00
while ((p = xp_va_arg(ap, const xp_char_t*)) != XP_NULL) {
while (*p != XP_CHAR('\0')) {
2005-05-22 04:34:22 +00:00
/*XP_STX_CHARAT(stx,idx,n++) = *p++;*/
obj->data[n++] = *p++;
}
2005-05-10 15:15:57 +00:00
}
2005-06-08 16:05:41 +00:00
xp_va_end (ap);
2005-05-10 15:15:57 +00:00
return idx;
}
2005-07-05 09:52:00 +00:00
xp_word_t xp_stx_hash_object (xp_stx_t* stx, xp_word_t object)
{
xp_word_t hv;
if (XP_STX_IS_SMALLINT(object)) {
2005-07-05 11:15:51 +00:00
xp_word_t tmp = XP_STX_FROM_SMALLINT(object);
hv = xp_stx_hash(&tmp, xp_sizeof(tmp));
2005-07-05 09:52:00 +00:00
}
else if (XP_STX_IS_CHAR_OBJECT(stx,object)) {
2005-07-05 11:15:51 +00:00
/*hv = xp_stx_strxhash (
XP_STX_DATA(stx,object), XP_STX_SIZE(stx,object));*/
hv = xp_stx_hash (XP_STX_DATA(stx,object),
XP_STX_SIZE(stx,object) * xp_sizeof(xp_char_t));
}
else if (XP_STX_IS_BYTE_OBJECT(stx,object)) {
hv = xp_stx_hash (
2005-07-05 09:52:00 +00:00
XP_STX_DATA(stx,object), XP_STX_SIZE(stx,object));
}
2005-07-05 11:15:51 +00:00
else {
xp_assert (XP_STX_IS_WORD_OBJECT(stx,object));
hv = xp_stx_hash (XP_STX_DATA(stx,object),
XP_STX_SIZE(stx,object) * xp_sizeof(xp_word_t));
2005-07-05 09:52:00 +00:00
}
return hv;
}
2005-07-04 16:23:13 +00:00
xp_word_t xp_stx_instantiate (
2005-07-05 09:02:13 +00:00
xp_stx_t* stx, xp_word_t class_index, const void* data,
const void* variable_data, xp_word_t variable_nfields)
2005-07-04 16:23:13 +00:00
{
xp_stx_class_t* class_obj;
xp_word_t spec, nfields, new;
int indexable;
2005-07-05 09:02:13 +00:00
class_obj = (xp_stx_class_t*)XP_STX_WORD_OBJECT(stx, class_index);
2005-07-04 16:23:13 +00:00
/* don't instantiate a metaclass whose instance must be
created in a different way */
/* TODO: maybe delete the following line */
xp_assert (class_obj->header.class != stx->class_metaclass);
2005-07-05 11:15:51 +00:00
xp_assert (XP_STX_IS_SMALLINT(class_obj->spec));
2005-07-04 16:23:13 +00:00
spec = XP_STX_FROM_SMALLINT(class_obj->spec);
2005-07-05 04:29:31 +00:00
nfields = (spec >> XP_STX_SPEC_INDEXABLE_BITS);
indexable = spec & XP_STX_SPEC_INDEXABLE_MASK;
2005-07-04 16:23:13 +00:00
if (indexable == XP_STX_SPEC_BYTE_INDEXABLE) {
2005-07-05 09:02:13 +00:00
xp_assert (nfields == 0 && data == XP_NULL);
new = xp_stx_alloc_byte_object(
stx, variable_data, variable_nfields);
2005-07-04 16:23:13 +00:00
}
else if (indexable == XP_STX_SPEC_CHAR_INDEXABLE) {
2005-07-05 09:02:13 +00:00
xp_assert (nfields == 0 && data == XP_NULL);
new = xp_stx_alloc_char_objectx(
stx, variable_data, variable_nfields);
2005-07-04 16:23:13 +00:00
}
else if (indexable == XP_STX_SPEC_WORD_INDEXABLE) {
2005-07-05 06:26:33 +00:00
new = xp_stx_alloc_word_object (
2005-07-05 09:02:13 +00:00
stx, data, nfields, variable_data, variable_nfields);
2005-07-04 16:23:13 +00:00
}
else {
2005-07-05 04:29:31 +00:00
xp_assert (indexable == XP_STX_SPEC_NOT_INDEXABLE);
2005-07-05 09:02:13 +00:00
xp_assert (variable_nfields == 0 && variable_data == XP_NULL);
2005-07-05 06:26:33 +00:00
new = xp_stx_alloc_word_object (
2005-07-05 09:02:13 +00:00
stx, data, nfields, XP_NULL, 0);
2005-07-04 16:23:13 +00:00
}
XP_STX_CLASS(stx, new) = class_index;
return new;
}