~ubuntu-branches/ubuntu/hardy/uim/hardy

« back to all changes in this revision

Viewing changes to sigscheme/src/symbol.c

  • Committer: Bazaar Package Importer
  • Author(s): Masahito Omote
  • Date: 2007-04-21 03:46:09 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070421034609-gpcurkutp8vaysqj
Tags: 1:1.4.1-3
* Switch to dh_gtkmodules for the gtk 2.10 transition (Closes:
  #419318)
  - debian/control: Add ${misc:Depends} and remove libgtk2.0-bin on
    uim-gtk2.0.
  - debian/uim-gtk2.0.post{inst,rm}: Removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
 *  Filename : symbol.c
 
3
 *  About    : Scheme Symbol handling
 
4
 *
 
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>
 
9
 *
 
10
 *  All rights reserved.
 
11
 *
 
12
 *  Redistribution and use in source and binary forms, with or without
 
13
 *  modification, are permitted provided that the following conditions
 
14
 *  are met:
 
15
 *
 
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.
 
24
 *
 
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
===========================================================================*/
 
37
 
 
38
#include <config.h>
 
39
 
 
40
#include <string.h>
 
41
#include <stdlib.h>
 
42
 
 
43
#include "sigscheme.h"
 
44
#include "sigschemeinternal.h"
 
45
 
 
46
/*=======================================
 
47
  File Local Macro Definitions
 
48
=======================================*/
 
49
 
 
50
/*=======================================
 
51
  File Local Type Definitions
 
52
=======================================*/
 
53
 
 
54
/*=======================================
 
55
  Variable Definitions
 
56
=======================================*/
 
57
SCM_DEFINE_EXPORTED_VARS(symbol);
 
58
 
 
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);
 
65
 
 
66
/*=======================================
 
67
  Function Definitions
 
68
=======================================*/
 
69
SCM_EXPORT ScmObj
 
70
scm_intern(const char *name)
 
71
{
 
72
    ScmObj sym, lst, rest;
 
73
    uint32_t hash;
 
74
    DECLARE_INTERNAL_FUNCTION("scm_intern");
 
75
 
 
76
#if (SCM_USE_SRFI75 && SCM_STRICT_ARGCHECK)
 
77
#if 0
 
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);
 
81
#endif
 
82
#endif
 
83
 
 
84
    hash = symbol_name_hash(name);
 
85
    lst = scm_symbol_hash[hash];
 
86
 
 
87
    rest = lst;
 
88
    FOR_EACH (sym, rest) {
 
89
        if (strcmp(SCM_SYMBOL_NAME(sym), name) == 0)
 
90
            return sym;
 
91
    }
 
92
 
 
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);
 
96
 
 
97
    return sym;
 
98
}
 
99
 
 
100
/* lookup the symbol bound to an obj reversely */
 
101
SCM_EXPORT ScmObj
 
102
scm_symbol_bound_to(ScmObj obj)
 
103
{
 
104
    ScmObj lst, sym, val;
 
105
    size_t i;
 
106
    DECLARE_INTERNAL_FUNCTION("scm_symbol_bound_to");
 
107
 
 
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))
 
113
                return sym;
 
114
        }
 
115
    }
 
116
 
 
117
    return SCM_FALSE;
 
118
}
 
119
 
 
120
SCM_EXPORT void
 
121
scm_init_symbol(const ScmStorageConf *conf)
 
122
{
 
123
    SCM_GLOBAL_VARS_INIT(symbol);
 
124
 
 
125
    initialize_symbol_hash(conf);
 
126
}
 
127
 
 
128
SCM_EXPORT void
 
129
scm_fin_symbol(void)
 
130
{
 
131
    finalize_symbol_hash();
 
132
 
 
133
    SCM_GLOBAL_VARS_FIN(symbol);
 
134
}
 
135
 
 
136
/*===========================================================================
 
137
  Symbol table
 
138
===========================================================================*/
 
139
static void
 
140
initialize_symbol_hash(const ScmStorageConf *conf)
 
141
{
 
142
    size_t i;
 
143
 
 
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);
 
147
 
 
148
    for (i = 0; i < scm_symbol_hash_size; i++)
 
149
        scm_symbol_hash[i] = SCM_NULL;
 
150
}
 
151
 
 
152
static void
 
153
finalize_symbol_hash(void)
 
154
{
 
155
    free(scm_symbol_hash);
 
156
}
 
157
 
 
158
static uint32_t
 
159
symbol_name_hash(const char *name)
 
160
{
 
161
    uint32_t hash, c;
 
162
 
 
163
    for (hash = 0; (c = *(const scm_byte_t *)name); name++)
 
164
        hash = ((hash * 17) ^ c) % scm_symbol_hash_size;
 
165
 
 
166
    return hash;
 
167
}