symbols.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* Copyright (C) 1995,1996,1997,1998,2000,2001, 2003, 2004, 2006, 2009 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/chars.h"
  22. #include "libguile/eval.h"
  23. #include "libguile/hash.h"
  24. #include "libguile/smob.h"
  25. #include "libguile/variable.h"
  26. #include "libguile/alist.h"
  27. #include "libguile/fluids.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/vectors.h"
  30. #include "libguile/hashtab.h"
  31. #include "libguile/weaks.h"
  32. #include "libguile/modules.h"
  33. #include "libguile/read.h"
  34. #include "libguile/srfi-13.h"
  35. #include "libguile/validate.h"
  36. #include "libguile/symbols.h"
  37. #ifdef HAVE_STRING_H
  38. #include <string.h>
  39. #endif
  40. SCM scm_i_symbols;
  41. static scm_i_pthread_mutex_t symbols_mutex;
  42. #ifdef GUILE_DEBUG
  43. SCM_DEFINE (scm_sys_symbols, "%symbols", 0, 0, 0,
  44. (),
  45. "Return the system symbol obarray.")
  46. #define FUNC_NAME s_scm_sys_symbols
  47. {
  48. return scm_i_symbols;
  49. }
  50. #undef FUNC_NAME
  51. #endif
  52. /* {Symbols}
  53. */
  54. /* In order to optimize reading speed, this function breaks part of
  55. * the hashtable abstraction. The optimizations are:
  56. *
  57. * 1. The argument string can be compared directly to symbol objects
  58. * without first creating an SCM string object. (This would have
  59. * been necessary if we had used the hashtable API in hashtab.h.)
  60. *
  61. * 2. We can use the raw hash value stored in scm_i_symbol_hash (sym)
  62. * to speed up lookup.
  63. *
  64. * Both optimizations might be possible without breaking the
  65. * abstraction if the API in hashtab.c is improved.
  66. */
  67. unsigned long
  68. scm_i_hash_symbol (SCM obj, unsigned long n, void *closure)
  69. {
  70. return scm_i_symbol_hash (obj) % n;
  71. }
  72. static SCM
  73. locked_lookup (const char *name, size_t len, unsigned long raw_hash, unsigned long hash)
  74. {
  75. /* Look up a symbol in the symbols table, assuming that the symbols
  76. mutex is already locked. */
  77. SCM l;
  78. for (l = SCM_HASHTABLE_BUCKET (scm_i_symbols, hash);
  79. !scm_is_null (l);
  80. l = SCM_CDR (l))
  81. {
  82. SCM sym = SCM_CAAR (l);
  83. if ((scm_i_symbol_hash (sym) == raw_hash) &&
  84. (scm_i_symbol_length (sym) == len))
  85. {
  86. const char *chrs = scm_i_symbol_chars (sym);
  87. size_t i = len;
  88. while (i != 0)
  89. {
  90. --i;
  91. if (name[i] != chrs[i])
  92. goto next_symbol;
  93. }
  94. return sym;
  95. }
  96. next_symbol:
  97. ;
  98. }
  99. return SCM_BOOL_F;
  100. }
  101. static SCM
  102. lookup_interned_symbol (const char *name, size_t len,
  103. unsigned long raw_hash)
  104. {
  105. /* Try to find the symbol in the symbols table */
  106. unsigned long hash;
  107. SCM sym;
  108. scm_i_pthread_mutex_lock (&symbols_mutex);
  109. hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (scm_i_symbols);
  110. sym = locked_lookup (name, len, raw_hash, hash);
  111. scm_i_pthread_mutex_unlock (&symbols_mutex);
  112. return sym;
  113. }
  114. /* Intern SYMBOL, an uninterned symbol. */
  115. static SCM
  116. intern_symbol (SCM symbol)
  117. {
  118. SCM sym, new_bucket;
  119. unsigned long raw_hash, hash;
  120. /* Allocate new cell and bucket before locking the mutex. */
  121. new_bucket = scm_acons (symbol, SCM_UNDEFINED, SCM_BOOL_F);
  122. scm_i_pthread_mutex_lock (&symbols_mutex);
  123. raw_hash = scm_i_symbol_hash (symbol);
  124. hash = raw_hash % SCM_HASHTABLE_N_BUCKETS (scm_i_symbols);
  125. sym = locked_lookup (scm_i_symbol_chars (symbol),
  126. scm_i_symbol_length (symbol),
  127. raw_hash,
  128. hash);
  129. if (scm_is_false (sym))
  130. {
  131. SCM_SETCDR (new_bucket, SCM_HASHTABLE_BUCKET (scm_i_symbols, hash));
  132. SCM_SET_HASHTABLE_BUCKET (scm_i_symbols, hash, new_bucket);
  133. SCM_HASHTABLE_INCREMENT (scm_i_symbols);
  134. if (SCM_HASHTABLE_N_ITEMS (scm_i_symbols) > SCM_HASHTABLE_UPPER (scm_i_symbols))
  135. scm_i_rehash (scm_i_symbols, scm_i_hash_symbol, 0, "intern_symbol",
  136. &symbols_mutex);
  137. sym = symbol;
  138. }
  139. scm_i_pthread_mutex_unlock (&symbols_mutex);
  140. return sym;
  141. }
  142. static SCM
  143. scm_i_c_mem2symbol (const char *name, size_t len)
  144. {
  145. SCM symbol;
  146. size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
  147. symbol = lookup_interned_symbol (name, len, raw_hash);
  148. if (scm_is_false (symbol))
  149. {
  150. /* The symbol was not found, create it. */
  151. symbol = scm_i_c_make_symbol (name, len, 0, raw_hash,
  152. scm_cons (SCM_BOOL_F, SCM_EOL));
  153. symbol = intern_symbol (symbol);
  154. }
  155. return symbol;
  156. }
  157. static SCM
  158. scm_i_mem2symbol (SCM str)
  159. {
  160. SCM symbol;
  161. const char *name = scm_i_string_chars (str);
  162. size_t len = scm_i_string_length (str);
  163. size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
  164. symbol = lookup_interned_symbol (name, len, raw_hash);
  165. if (scm_is_false (symbol))
  166. {
  167. /* The symbol was not found, create it. */
  168. symbol = scm_i_make_symbol (str, 0, raw_hash,
  169. scm_cons (SCM_BOOL_F, SCM_EOL));
  170. symbol = intern_symbol (symbol);
  171. }
  172. return symbol;
  173. }
  174. void
  175. scm_i_rehash_symbols_after_gc ()
  176. {
  177. scm_i_pthread_mutex_lock (&symbols_mutex);
  178. scm_i_rehash (scm_i_symbols, scm_i_hash_symbol, 0, "rehash_after_gc",
  179. &symbols_mutex);
  180. scm_i_pthread_mutex_unlock (&symbols_mutex);
  181. }
  182. static SCM
  183. scm_i_mem2uninterned_symbol (SCM str)
  184. {
  185. const char *name = scm_i_string_chars (str);
  186. size_t len = scm_i_string_length (str);
  187. size_t raw_hash = scm_string_hash ((const unsigned char *) name, len);
  188. return scm_i_make_symbol (str, SCM_I_F_SYMBOL_UNINTERNED,
  189. raw_hash, scm_cons (SCM_BOOL_F, SCM_EOL));
  190. }
  191. SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0,
  192. (SCM obj),
  193. "Return @code{#t} if @var{obj} is a symbol, otherwise return\n"
  194. "@code{#f}.")
  195. #define FUNC_NAME s_scm_symbol_p
  196. {
  197. return scm_from_bool (scm_is_symbol (obj));
  198. }
  199. #undef FUNC_NAME
  200. SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 1, 0, 0,
  201. (SCM symbol),
  202. "Return @code{#t} if @var{symbol} is interned, otherwise return\n"
  203. "@code{#f}.")
  204. #define FUNC_NAME s_scm_symbol_interned_p
  205. {
  206. SCM_VALIDATE_SYMBOL (1, symbol);
  207. return scm_from_bool (scm_i_symbol_is_interned (symbol));
  208. }
  209. #undef FUNC_NAME
  210. SCM_DEFINE (scm_make_symbol, "make-symbol", 1, 0, 0,
  211. (SCM name),
  212. "Return a new uninterned symbol with the name @var{name}. "
  213. "The returned symbol is guaranteed to be unique and future "
  214. "calls to @code{string->symbol} will not return it.")
  215. #define FUNC_NAME s_scm_make_symbol
  216. {
  217. SCM_VALIDATE_STRING (1, name);
  218. return scm_i_mem2uninterned_symbol (name);
  219. }
  220. #undef FUNC_NAME
  221. SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0,
  222. (SCM s),
  223. "Return the name of @var{symbol} as a string. If the symbol was\n"
  224. "part of an object returned as the value of a literal expression\n"
  225. "(section @pxref{Literal expressions,,,r5rs, The Revised^5\n"
  226. "Report on Scheme}) or by a call to the @code{read} procedure,\n"
  227. "and its name contains alphabetic characters, then the string\n"
  228. "returned will contain characters in the implementation's\n"
  229. "preferred standard case---some implementations will prefer\n"
  230. "upper case, others lower case. If the symbol was returned by\n"
  231. "@code{string->symbol}, the case of characters in the string\n"
  232. "returned will be the same as the case in the string that was\n"
  233. "passed to @code{string->symbol}. It is an error to apply\n"
  234. "mutation procedures like @code{string-set!} to strings returned\n"
  235. "by this procedure.\n"
  236. "\n"
  237. "The following examples assume that the implementation's\n"
  238. "standard case is lower case:\n"
  239. "\n"
  240. "@lisp\n"
  241. "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
  242. "(symbol->string 'Martin) @result{} \"martin\"\n"
  243. "(symbol->string\n"
  244. " (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
  245. "@end lisp")
  246. #define FUNC_NAME s_scm_symbol_to_string
  247. {
  248. SCM_VALIDATE_SYMBOL (1, s);
  249. return scm_i_symbol_substring (s, 0, scm_i_symbol_length (s));
  250. }
  251. #undef FUNC_NAME
  252. SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0,
  253. (SCM string),
  254. "Return the symbol whose name is @var{string}. This procedure\n"
  255. "can create symbols with names containing special characters or\n"
  256. "letters in the non-standard case, but it is usually a bad idea\n"
  257. "to create such symbols because in some implementations of\n"
  258. "Scheme they cannot be read as themselves. See\n"
  259. "@code{symbol->string}.\n"
  260. "\n"
  261. "The following examples assume that the implementation's\n"
  262. "standard case is lower case:\n"
  263. "\n"
  264. "@lisp\n"
  265. "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
  266. "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
  267. "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
  268. "(eq? 'JollyWog\n"
  269. " (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
  270. "(string=? \"K. Harper, M.D.\"\n"
  271. " (symbol->string\n"
  272. " (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
  273. "@end lisp")
  274. #define FUNC_NAME s_scm_string_to_symbol
  275. {
  276. SCM_VALIDATE_STRING (1, string);
  277. return scm_i_mem2symbol (string);
  278. }
  279. #undef FUNC_NAME
  280. SCM_DEFINE (scm_string_ci_to_symbol, "string-ci->symbol", 1, 0, 0,
  281. (SCM str),
  282. "Return the symbol whose name is @var{str}. @var{str} is\n"
  283. "converted to lowercase before the conversion is done, if Guile\n"
  284. "is currently reading symbols case-insensitively.")
  285. #define FUNC_NAME s_scm_string_ci_to_symbol
  286. {
  287. return scm_string_to_symbol (SCM_CASE_INSENSITIVE_P
  288. ? scm_string_downcase(str)
  289. : str);
  290. }
  291. #undef FUNC_NAME
  292. #define MAX_PREFIX_LENGTH 30
  293. SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
  294. (SCM prefix),
  295. "Create a new symbol with a name constructed from a prefix and\n"
  296. "a counter value. The string @var{prefix} can be specified as\n"
  297. "an optional argument. Default prefix is @code{ g}. The counter\n"
  298. "is increased by 1 at each call. There is no provision for\n"
  299. "resetting the counter.")
  300. #define FUNC_NAME s_scm_gensym
  301. {
  302. static int gensym_counter = 0;
  303. SCM suffix, name;
  304. int n, n_digits;
  305. char buf[SCM_INTBUFLEN];
  306. if (SCM_UNBNDP (prefix))
  307. prefix = scm_from_locale_string (" g");
  308. /* mutex in case another thread looks and incs at the exact same moment */
  309. scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
  310. n = gensym_counter++;
  311. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  312. n_digits = scm_iint2str (n, 10, buf);
  313. suffix = scm_from_locale_stringn (buf, n_digits);
  314. name = scm_string_append (scm_list_2 (prefix, suffix));
  315. return scm_string_to_symbol (name);
  316. }
  317. #undef FUNC_NAME
  318. SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0,
  319. (SCM symbol),
  320. "Return a hash value for @var{symbol}.")
  321. #define FUNC_NAME s_scm_symbol_hash
  322. {
  323. SCM_VALIDATE_SYMBOL (1, symbol);
  324. return scm_from_ulong (scm_i_symbol_hash (symbol));
  325. }
  326. #undef FUNC_NAME
  327. SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
  328. (SCM s),
  329. "Return the contents of @var{symbol}'s @dfn{function slot}.")
  330. #define FUNC_NAME s_scm_symbol_fref
  331. {
  332. SCM_VALIDATE_SYMBOL (1, s);
  333. return SCM_CAR (SCM_CELL_OBJECT_3 (s));
  334. }
  335. #undef FUNC_NAME
  336. SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
  337. (SCM s),
  338. "Return the @dfn{property list} currently associated with @var{symbol}.")
  339. #define FUNC_NAME s_scm_symbol_pref
  340. {
  341. SCM_VALIDATE_SYMBOL (1, s);
  342. return SCM_CDR (SCM_CELL_OBJECT_3 (s));
  343. }
  344. #undef FUNC_NAME
  345. SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
  346. (SCM s, SCM val),
  347. "Change the binding of @var{symbol}'s function slot.")
  348. #define FUNC_NAME s_scm_symbol_fset_x
  349. {
  350. SCM_VALIDATE_SYMBOL (1, s);
  351. SCM_SETCAR (SCM_CELL_OBJECT_3 (s), val);
  352. return SCM_UNSPECIFIED;
  353. }
  354. #undef FUNC_NAME
  355. SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
  356. (SCM s, SCM val),
  357. "Change the binding of @var{symbol}'s property slot.")
  358. #define FUNC_NAME s_scm_symbol_pset_x
  359. {
  360. SCM_VALIDATE_SYMBOL (1, s);
  361. SCM_SETCDR (SCM_CELL_OBJECT_3 (s), val);
  362. return SCM_UNSPECIFIED;
  363. }
  364. #undef FUNC_NAME
  365. SCM
  366. scm_from_locale_symbol (const char *sym)
  367. {
  368. return scm_i_c_mem2symbol (sym, strlen (sym));
  369. }
  370. SCM
  371. scm_from_locale_symboln (const char *sym, size_t len)
  372. {
  373. return scm_i_c_mem2symbol (sym, len);
  374. }
  375. SCM
  376. scm_take_locale_symboln (char *sym, size_t len)
  377. {
  378. SCM res;
  379. unsigned long raw_hash;
  380. if (len == (size_t)-1)
  381. len = strlen (sym);
  382. else
  383. {
  384. /* Ensure STR is null terminated. A realloc for 1 extra byte should
  385. often be satisfied from the alignment padding after the block, with
  386. no actual data movement. */
  387. sym = scm_realloc (sym, len+1);
  388. sym[len] = '\0';
  389. }
  390. raw_hash = scm_string_hash ((unsigned char *)sym, len);
  391. res = lookup_interned_symbol (sym, len, raw_hash);
  392. if (scm_is_false (res))
  393. {
  394. res = scm_i_c_take_symbol (sym, len, 0, raw_hash,
  395. scm_cons (SCM_BOOL_F, SCM_EOL));
  396. res = intern_symbol (res);
  397. }
  398. else
  399. free (sym);
  400. return res;
  401. }
  402. SCM
  403. scm_take_locale_symbol (char *sym)
  404. {
  405. return scm_take_locale_symboln (sym, (size_t)-1);
  406. }
  407. void
  408. scm_symbols_prehistory ()
  409. {
  410. scm_i_symbols = scm_make_weak_key_hash_table (scm_from_int (2139));
  411. scm_permanent_object (scm_i_symbols);
  412. scm_i_pthread_mutex_init (&symbols_mutex, NULL);
  413. }
  414. void
  415. scm_init_symbols ()
  416. {
  417. #include "libguile/symbols.x"
  418. }
  419. /*
  420. Local Variables:
  421. c-file-style: "gnu"
  422. End:
  423. */