1
/*===========================================================================
3
* About : Scheme Symbol handling
5
* Copyright (C) 2005 Kazuki Ohta <mover AT hct.zaq.ne.jp>
6
* Copyright (C) 2005 Jun Inoue <jun.lambda AT gmail.com>
7
* Copyright (C) 2005-2006 YAMAMOTO Kengo <yamaken AT bp.iij4u.or.jp>
8
* Copyright (c) 2007 SigScheme Project <uim AT freedesktop.org>
10
* All rights reserved.
12
* Redistribution and use in source and binary forms, with or without
13
* modification, are permitted provided that the following conditions
16
* 1. Redistributions of source code must retain the above copyright
17
* notice, this list of conditions and the following disclaimer.
18
* 2. Redistributions in binary form must reproduce the above copyright
19
* notice, this list of conditions and the following disclaimer in the
20
* documentation and/or other materials provided with the distribution.
21
* 3. Neither the name of authors nor the names of its contributors
22
* may be used to endorse or promote products derived from this software
23
* without specific prior written permission.
25
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
26
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
29
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
===========================================================================*/
43
#include "sigscheme.h"
44
#include "sigschemeinternal.h"
46
/*=======================================
47
File Local Macro Definitions
48
=======================================*/
50
/*=======================================
51
File Local Type Definitions
52
=======================================*/
54
/*=======================================
56
=======================================*/
57
SCM_DEFINE_EXPORTED_VARS(symbol);
59
/*=======================================
60
File Local Function Declarations
61
=======================================*/
62
static void initialize_symbol_hash(const ScmStorageConf *conf);
63
static void finalize_symbol_hash(void);
64
static uint32_t symbol_name_hash(const char *name);
66
/*=======================================
68
=======================================*/
70
scm_intern(const char *name)
72
ScmObj sym, lst, rest;
74
DECLARE_INTERNAL_FUNCTION("scm_intern");
76
#if (SCM_USE_SRFI75 && SCM_STRICT_ARGCHECK)
78
/* FIXME: detect error correctly */
79
if (scm_mb_bare_c_strlen(scm_identifier_codec, name) <= 0)
80
ERR("invalid string for identifier: ~S", name);
84
hash = symbol_name_hash(name);
85
lst = scm_symbol_hash[hash];
88
FOR_EACH (sym, rest) {
89
if (strcmp(SCM_SYMBOL_NAME(sym), name) == 0)
93
/* if not found, allocate new symbol object and prepend it into the list */
94
sym = MAKE_SYMBOL(scm_strdup(name), SCM_UNBOUND);
95
scm_symbol_hash[hash] = CONS(sym, lst);
100
/* lookup the symbol bound to an obj reversely */
102
scm_symbol_bound_to(ScmObj obj)
104
ScmObj lst, sym, val;
106
DECLARE_INTERNAL_FUNCTION("scm_symbol_bound_to");
108
for (i = 0; i < scm_symbol_hash_size; i++) {
109
lst = scm_symbol_hash[i];
110
FOR_EACH (sym, lst) {
111
val = SCM_SYMBOL_VCELL(sym);
112
if (!EQ(val, SCM_UNBOUND) && EQ(val, obj))
121
scm_init_symbol(const ScmStorageConf *conf)
123
SCM_GLOBAL_VARS_INIT(symbol);
125
initialize_symbol_hash(conf);
131
finalize_symbol_hash();
133
SCM_GLOBAL_VARS_FIN(symbol);
136
/*===========================================================================
138
===========================================================================*/
140
initialize_symbol_hash(const ScmStorageConf *conf)
144
scm_symbol_hash_size = conf->symbol_hash_size;
145
SCM_ASSERT(scm_symbol_hash_size <= (UINT32_MAX / sizeof(ScmObj)));
146
scm_symbol_hash = scm_malloc(sizeof(ScmObj) * scm_symbol_hash_size);
148
for (i = 0; i < scm_symbol_hash_size; i++)
149
scm_symbol_hash[i] = SCM_NULL;
153
finalize_symbol_hash(void)
155
free(scm_symbol_hash);
159
symbol_name_hash(const char *name)
163
for (hash = 0; (c = *(const scm_byte_t *)name); name++)
164
hash = ((hash * 17) ^ c) % scm_symbol_hash_size;