moo/lib/sym.c

214 lines
6.9 KiB
C
Raw Normal View History

/*
* $Id$
*
Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2017-01-09 09:54:49 +00:00
#include "moo-prv.h"
2017-01-09 09:54:49 +00:00
static moo_oop_oop_t expand_bucket (moo_t* moo, moo_oop_oop_t oldbuc)
{
2017-01-09 09:54:49 +00:00
moo_oop_oop_t newbuc;
moo_oow_t oldsz, newsz, index;
moo_oop_char_t symbol;
2017-01-09 09:54:49 +00:00
oldsz = MOO_OBJ_GET_SIZE(oldbuc);
/* TODO: better growth policy? */
if (oldsz < 5000) newsz = oldsz + oldsz;
else if (oldsz < 50000) newsz = oldsz + (oldsz / 2);
else if (oldsz < 100000) newsz = oldsz + (oldsz / 4);
else if (oldsz < 200000) newsz = oldsz + (oldsz / 8);
else if (oldsz < 400000) newsz = oldsz + (oldsz / 16);
else if (oldsz < 800000) newsz = oldsz + (oldsz / 32);
else if (oldsz < 1600000) newsz = oldsz + (oldsz / 64);
else
{
2017-01-09 09:54:49 +00:00
moo_oow_t inc, inc_max;
inc = oldsz / 128;
2017-01-09 09:54:49 +00:00
inc_max = MOO_OBJ_SIZE_MAX - oldsz;
if (inc > inc_max)
{
if (inc_max > 0) inc = inc_max;
else
{
2018-12-16 17:35:46 +00:00
moo_seterrbfmt (moo, MOO_EOOMEM, "unable to grow symbol table");
2017-01-09 09:54:49 +00:00
return MOO_NULL;
}
}
newsz = oldsz + inc;
}
moo_pushvolat (moo, (moo_oop_t*)&oldbuc);
2018-12-09 17:16:47 +00:00
newbuc = (moo_oop_oop_t)moo_instantiate(moo, moo->_array, MOO_NULL, newsz);
moo_popvolat (moo);
2017-01-09 09:54:49 +00:00
if (!newbuc) return MOO_NULL;
while (oldsz > 0)
{
oldsz = oldsz - 1;
symbol = (moo_oop_char_t)MOO_OBJ_GET_OOP_VAL(oldbuc, oldsz);
2017-01-09 09:54:49 +00:00
if ((moo_oop_t)symbol != moo->_nil)
{
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,symbol) == moo->_symbol);
/*MOO_ASSERT (moo, sym->size > 0);*/
2019-03-06 01:58:51 +00:00
index = moo_hash_oochars(MOO_OBJ_GET_CHAR_SLOT(symbol), MOO_OBJ_GET_SIZE(symbol)) % newsz;
while (MOO_OBJ_GET_OOP_VAL(newbuc, index) != moo->_nil) index = (index + 1) % newsz;
MOO_STORE_OOP (moo, MOO_OBJ_GET_OOP_PTR(newbuc, index), (moo_oop_t)symbol);
}
}
return newbuc;
}
2017-01-09 09:54:49 +00:00
static moo_oop_t find_or_make_symbol (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len, int create)
{
2017-01-09 09:54:49 +00:00
moo_ooi_t tally;
moo_oow_t index;
moo_oop_char_t symbol;
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_CLASSOF(moo,moo->symtab->bucket) == moo->_array);
2019-03-06 01:58:51 +00:00
index = moo_hash_oochars(ptr, len) % MOO_OBJ_GET_SIZE(moo->symtab->bucket);
/* find a matching symbol in the open-addressed symbol table */
while ((moo_oop_t)(symbol = (moo_oop_char_t)MOO_OBJ_GET_OOP_VAL(moo->symtab->bucket, index)) != moo->_nil)
{
MOO_ASSERT (moo, MOO_CLASSOF(moo,symbol) == moo->_symbol);
2017-01-09 09:54:49 +00:00
if (len == MOO_OBJ_GET_SIZE(symbol) &&
2018-12-16 17:35:46 +00:00
moo_equal_oochars(ptr, MOO_OBJ_GET_CHAR_SLOT(symbol), len))
{
2017-01-09 09:54:49 +00:00
return (moo_oop_t)symbol;
}
2017-01-09 09:54:49 +00:00
index = (index + 1) % MOO_OBJ_GET_SIZE(moo->symtab->bucket);
}
if (!create)
{
moo_seterrnum (moo, MOO_ENOENT);
2017-01-09 09:54:49 +00:00
return MOO_NULL;
}
/* make a new symbol and insert it */
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, MOO_OOP_IS_SMOOI(moo->symtab->tally));
tally = MOO_OOP_TO_SMOOI(moo->symtab->tally);
if (tally >= MOO_SMOOI_MAX)
{
/* this built-in table is not allowed to hold more than
2017-01-09 09:54:49 +00:00
* MOO_SMOOI_MAX items for efficiency sake */
2018-12-16 17:35:46 +00:00
moo_seterrbfmt (moo, MOO_EDFULL, "unable to add a symbol %.*js - symbol table full", len, ptr);
2017-01-09 09:54:49 +00:00
return MOO_NULL;
}
2017-01-09 09:54:49 +00:00
/* no conversion to moo_oow_t is necessary for tally + 1.
* the maximum value of tally is checked to be MOO_SMOOI_MAX - 1.
* tally + 1 can produce at most MOO_SMOOI_MAX. above all,
* MOO_SMOOI_MAX is way smaller than MOO_TYPE_MAX(moo_ooi_t). */
if (tally + 1 >= MOO_OBJ_GET_SIZE(moo->symtab->bucket))
{
2018-12-31 16:47:54 +00:00
moo_oop_oop_t tmp;
/* TODO: make the growth policy configurable instead of growing
it just before it gets full. The polcy can be grow it
if it's 70% full */
/* enlarge the symbol table before it gets full to
* make sure that it has at least one free slot left
* after having added a new symbol. this is to help
* traversal end at a _nil slot if no entry is found. */
2018-12-31 16:47:54 +00:00
tmp = expand_bucket(moo, moo->symtab->bucket);
if (!tmp) return MOO_NULL;
2018-12-31 16:47:54 +00:00
MOO_STORE_OOP (moo, (moo_oop_t*)&moo->symtab->bucket, (moo_oop_t)tmp);
/* recalculate the index for the expanded bucket */
2019-03-06 01:58:51 +00:00
index = moo_hash_oochars(ptr, len) % MOO_OBJ_GET_SIZE(moo->symtab->bucket);
while (MOO_OBJ_GET_OOP_VAL(moo->symtab->bucket, index) != moo->_nil)
2017-01-09 09:54:49 +00:00
index = (index + 1) % MOO_OBJ_GET_SIZE(moo->symtab->bucket);
}
/* create a new symbol since it isn't found in the symbol table */
2017-01-09 09:54:49 +00:00
symbol = (moo_oop_char_t)moo_instantiate(moo, moo->_symbol, ptr, len);
if (symbol)
{
2017-01-09 09:54:49 +00:00
MOO_ASSERT (moo, tally < MOO_SMOOI_MAX);
moo->symtab->tally = MOO_SMOOI_TO_OOP(tally + 1);
MOO_STORE_OOP (moo, MOO_OBJ_GET_OOP_PTR(moo->symtab->bucket, index), (moo_oop_t)symbol);
}
2017-01-09 09:54:49 +00:00
return (moo_oop_t)symbol;
}
2017-01-09 09:54:49 +00:00
moo_oop_t moo_makesymbol (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len)
{
2018-12-16 17:35:46 +00:00
return find_or_make_symbol(moo, ptr, len, 1);
}
2017-01-09 09:54:49 +00:00
moo_oop_t moo_findsymbol (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len)
{
2018-12-16 17:35:46 +00:00
return find_or_make_symbol(moo, ptr, len, 0);
}
2015-05-26 16:31:47 +00:00
moo_oop_t moo_makestringwithbchars (moo_t* moo, const moo_bch_t* ptr, moo_oow_t len)
2015-05-26 16:31:47 +00:00
{
#if defined(MOO_OOCH_IS_UCH)
moo_oow_t inlen, outlen;
moo_oop_t obj;
inlen = len;
2018-12-16 17:35:46 +00:00
if (moo_convbtouchars(moo, ptr, &inlen, MOO_NULL, &outlen) <= -1) return MOO_NULL;
2018-12-09 17:16:47 +00:00
obj = moo_instantiate(moo, moo->_string, MOO_NULL, outlen);
if (!obj) return MOO_NULL;
inlen = len;
moo_convbtouchars (moo, ptr, &inlen, MOO_OBJ_GET_CHAR_SLOT(obj), &outlen);
return obj;
#else
2018-12-09 17:16:47 +00:00
return moo_instantiate(moo, moo->_string, ptr, len);
#endif
}
moo_oop_t moo_makestringwithuchars (moo_t* moo, const moo_uch_t* ptr, moo_oow_t len)
{
#if defined(MOO_OOCH_IS_UCH)
2018-12-09 17:16:47 +00:00
return moo_instantiate(moo, moo->_string, ptr, len);
#else
moo_oow_t inlen, outlen;
moo_oop_t obj;
inlen = len;
2018-12-16 17:35:46 +00:00
if (moo_convutobchars(moo, ptr, &inlen, MOO_NULL, &outlen) <= -1) return MOO_NULL;
2018-12-09 17:16:47 +00:00
obj = moo_instantiate(moo, moo->_string, MOO_NULL, outlen);
if (!obj) return MOO_NULL;
inlen = len;
moo_convutobchars (moo, ptr, &inlen, MOO_OBJ_GET_CHAR_SLOT(obj), &outlen);
return obj;
#endif
2015-05-26 16:31:47 +00:00
}