struct.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /* Copyright (C) 1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2007 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/async.h"
  22. #include "libguile/chars.h"
  23. #include "libguile/eval.h"
  24. #include "libguile/alist.h"
  25. #include "libguile/weaks.h"
  26. #include "libguile/hashtab.h"
  27. #include "libguile/ports.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/struct.h"
  31. #include "libguile/eq.h"
  32. #ifdef HAVE_STRING_H
  33. #include <string.h>
  34. #endif
  35. static SCM required_vtable_fields = SCM_BOOL_F;
  36. SCM scm_struct_table;
  37. SCM_DEFINE (scm_make_struct_layout, "make-struct-layout", 1, 0, 0,
  38. (SCM fields),
  39. "Return a new structure layout object.\n\n"
  40. "@var{fields} must be a string made up of pairs of characters\n"
  41. "strung together. The first character of each pair describes a field\n"
  42. "type, the second a field protection. Allowed types are 'p' for\n"
  43. "GC-protected Scheme data, 'u' for unprotected binary data, and 's' for\n"
  44. "a field that points to the structure itself. Allowed protections\n"
  45. "are 'w' for mutable fields, 'r' for read-only fields, and 'o' for opaque\n"
  46. "fields. The last field protection specification may be capitalized to\n"
  47. "indicate that the field is a tail-array.")
  48. #define FUNC_NAME s_scm_make_struct_layout
  49. {
  50. SCM new_sym;
  51. SCM_VALIDATE_STRING (1, fields);
  52. { /* scope */
  53. const char * field_desc;
  54. size_t len;
  55. int x;
  56. len = scm_i_string_length (fields);
  57. if (len % 2 == 1)
  58. SCM_MISC_ERROR ("odd length field specification: ~S",
  59. scm_list_1 (fields));
  60. field_desc = scm_i_string_chars (fields);
  61. for (x = 0; x < len; x += 2)
  62. {
  63. switch (field_desc[x])
  64. {
  65. case 'u':
  66. case 'p':
  67. #if 0
  68. case 'i':
  69. case 'd':
  70. #endif
  71. case 's':
  72. break;
  73. default:
  74. SCM_MISC_ERROR ("unrecognized field type: ~S",
  75. scm_list_1 (SCM_MAKE_CHAR (field_desc[x])));
  76. }
  77. switch (field_desc[x + 1])
  78. {
  79. case 'w':
  80. if (field_desc[x] == 's')
  81. SCM_MISC_ERROR ("self fields not writable", SCM_EOL);
  82. case 'r':
  83. case 'o':
  84. break;
  85. case 'R':
  86. case 'W':
  87. case 'O':
  88. if (field_desc[x] == 's')
  89. SCM_MISC_ERROR ("self fields not allowed in tail array",
  90. SCM_EOL);
  91. if (x != len - 2)
  92. SCM_MISC_ERROR ("tail array field must be last field in layout",
  93. SCM_EOL);
  94. break;
  95. default:
  96. SCM_MISC_ERROR ("unrecognized ref specification: ~S",
  97. scm_list_1 (SCM_MAKE_CHAR (field_desc[x + 1])));
  98. }
  99. #if 0
  100. if (field_desc[x] == 'd')
  101. {
  102. if (field_desc[x + 2] != '-')
  103. SCM_MISC_ERROR ("missing dash field at position ~A",
  104. scm_list_1 (scm_from_int (x / 2)));
  105. x += 2;
  106. goto recheck_ref;
  107. }
  108. #endif
  109. }
  110. new_sym = scm_string_to_symbol (fields);
  111. }
  112. scm_remember_upto_here_1 (fields);
  113. return new_sym;
  114. }
  115. #undef FUNC_NAME
  116. static void
  117. scm_struct_init (SCM handle, SCM layout, scm_t_bits * mem, int tail_elts, SCM inits)
  118. {
  119. unsigned const char *fields_desc =
  120. (unsigned const char *) scm_i_symbol_chars (layout) - 2;
  121. unsigned char prot = 0;
  122. int n_fields = scm_i_symbol_length (layout) / 2;
  123. int tailp = 0;
  124. while (n_fields)
  125. {
  126. if (!tailp)
  127. {
  128. fields_desc += 2;
  129. prot = fields_desc[1];
  130. if (SCM_LAYOUT_TAILP (prot))
  131. {
  132. tailp = 1;
  133. prot = prot == 'R' ? 'r' : prot == 'W' ? 'w' : 'o';
  134. *mem++ = tail_elts;
  135. n_fields += tail_elts - 1;
  136. if (n_fields == 0)
  137. break;
  138. }
  139. }
  140. switch (*fields_desc)
  141. {
  142. #if 0
  143. case 'i':
  144. if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
  145. *mem = 0;
  146. else
  147. {
  148. *mem = scm_to_long (SCM_CAR (inits));
  149. inits = SCM_CDR (inits);
  150. }
  151. break;
  152. #endif
  153. case 'u':
  154. if ((prot != 'r' && prot != 'w') || scm_is_null (inits))
  155. *mem = 0;
  156. else
  157. {
  158. *mem = scm_to_ulong (SCM_CAR (inits));
  159. inits = SCM_CDR (inits);
  160. }
  161. break;
  162. case 'p':
  163. if ((prot != 'r' && prot != 'w') || scm_is_null (inits))
  164. *mem = SCM_UNPACK (SCM_BOOL_F);
  165. else
  166. {
  167. *mem = SCM_UNPACK (SCM_CAR (inits));
  168. inits = SCM_CDR (inits);
  169. }
  170. break;
  171. #if 0
  172. case 'd':
  173. if ((prot != 'r' && prot != 'w') || inits == SCM_EOL)
  174. *((double *)mem) = 0.0;
  175. else
  176. {
  177. *mem = scm_num2dbl (SCM_CAR (inits), "scm_struct_init");
  178. inits = SCM_CDR (inits);
  179. }
  180. fields_desc += 2;
  181. break;
  182. #endif
  183. case 's':
  184. *mem = SCM_UNPACK (handle);
  185. break;
  186. }
  187. n_fields--;
  188. mem++;
  189. }
  190. }
  191. SCM_DEFINE (scm_struct_p, "struct?", 1, 0, 0,
  192. (SCM x),
  193. "Return @code{#t} iff @var{x} is a structure object, else\n"
  194. "@code{#f}.")
  195. #define FUNC_NAME s_scm_struct_p
  196. {
  197. return scm_from_bool(SCM_STRUCTP (x));
  198. }
  199. #undef FUNC_NAME
  200. SCM_DEFINE (scm_struct_vtable_p, "struct-vtable?", 1, 0, 0,
  201. (SCM x),
  202. "Return @code{#t} iff @var{x} is a vtable structure.")
  203. #define FUNC_NAME s_scm_struct_vtable_p
  204. {
  205. SCM layout;
  206. scm_t_bits * mem;
  207. int tmp;
  208. if (!SCM_STRUCTP (x))
  209. return SCM_BOOL_F;
  210. layout = SCM_STRUCT_LAYOUT (x);
  211. if (scm_i_symbol_length (layout)
  212. < scm_i_string_length (required_vtable_fields))
  213. return SCM_BOOL_F;
  214. tmp = strncmp (scm_i_symbol_chars (layout),
  215. scm_i_string_chars (required_vtable_fields),
  216. scm_i_string_length (required_vtable_fields));
  217. scm_remember_upto_here_1 (required_vtable_fields);
  218. if (tmp)
  219. return SCM_BOOL_F;
  220. mem = SCM_STRUCT_DATA (x);
  221. return scm_from_bool (scm_is_symbol (SCM_PACK (mem[scm_vtable_index_layout])));
  222. }
  223. #undef FUNC_NAME
  224. /* All struct data must be allocated at an address whose bottom three
  225. bits are zero. This is because the tag for a struct lives in the
  226. bottom three bits of the struct's car, and the upper bits point to
  227. the data of its vtable, which is a struct itself. Thus, if the
  228. address of that data doesn't end in three zeros, tagging it will
  229. destroy the pointer.
  230. This function allocates a block of memory, and returns a pointer at
  231. least scm_struct_n_extra_words words into the block. Furthermore,
  232. it guarantees that that pointer's least three significant bits are
  233. all zero.
  234. The argument n_words should be the number of words that should
  235. appear after the returned address. (That is, it shouldn't include
  236. scm_struct_n_extra_words.)
  237. This function initializes the following fields of the struct:
  238. scm_struct_i_ptr --- the actual start of the block of memory; the
  239. address you should pass to 'free' to dispose of the block.
  240. This field allows us to both guarantee that the returned
  241. address is divisible by eight, and allow the GC to free the
  242. block.
  243. scm_struct_i_n_words --- the number of words allocated to the
  244. block, including the extra fields. This is used by the GC.
  245. Ugh. */
  246. scm_t_bits *
  247. scm_alloc_struct (int n_words, int n_extra, const char *what)
  248. {
  249. int size = sizeof (scm_t_bits) * (n_words + n_extra) + 7;
  250. void * block = scm_gc_malloc (size, what);
  251. /* Adjust the pointer to hide the extra words. */
  252. scm_t_bits * p = (scm_t_bits *) block + n_extra;
  253. /* Adjust it even further so it's aligned on an eight-byte boundary. */
  254. p = (scm_t_bits *) (((scm_t_bits) p + 7) & ~7);
  255. /* Initialize a few fields as described above. */
  256. p[scm_struct_i_free] = (scm_t_bits) scm_struct_free_standard;
  257. p[scm_struct_i_ptr] = (scm_t_bits) block;
  258. p[scm_struct_i_n_words] = n_words;
  259. p[scm_struct_i_flags] = 0;
  260. return p;
  261. }
  262. void
  263. scm_struct_free_0 (scm_t_bits * vtable SCM_UNUSED,
  264. scm_t_bits * data SCM_UNUSED)
  265. {
  266. }
  267. void
  268. scm_struct_free_light (scm_t_bits * vtable, scm_t_bits * data)
  269. {
  270. size_t n = vtable [scm_struct_i_size] & ~SCM_STRUCTF_MASK;
  271. scm_gc_free (data, n, "struct");
  272. }
  273. void
  274. scm_struct_free_standard (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
  275. {
  276. size_t n = (data[scm_struct_i_n_words] + scm_struct_n_extra_words)
  277. * sizeof (scm_t_bits) + 7;
  278. scm_gc_free ((void *) data[scm_struct_i_ptr], n, "heavy struct");
  279. }
  280. void
  281. scm_struct_free_entity (scm_t_bits * vtable SCM_UNUSED, scm_t_bits * data)
  282. {
  283. size_t n = (data[scm_struct_i_n_words] + scm_struct_entity_n_extra_words)
  284. * sizeof (scm_t_bits) + 7;
  285. scm_gc_free ((void *) data[scm_struct_i_ptr], n, "entity struct");
  286. }
  287. static void *
  288. scm_struct_gc_init (void *dummy1 SCM_UNUSED,
  289. void *dummy2 SCM_UNUSED,
  290. void *dummy3 SCM_UNUSED)
  291. {
  292. scm_i_structs_to_free = SCM_EOL;
  293. return 0;
  294. }
  295. static void *
  296. scm_free_structs (void *dummy1 SCM_UNUSED,
  297. void *dummy2 SCM_UNUSED,
  298. void *dummy3 SCM_UNUSED)
  299. {
  300. SCM newchain = scm_i_structs_to_free;
  301. do
  302. {
  303. /* Mark vtables in GC chain. GC mark set means delay freeing. */
  304. SCM chain = newchain;
  305. while (!scm_is_null (chain))
  306. {
  307. SCM vtable = SCM_STRUCT_VTABLE (chain);
  308. if (SCM_STRUCT_GC_CHAIN (vtable) != 0 && vtable != chain)
  309. SCM_SET_GC_MARK (vtable);
  310. chain = SCM_STRUCT_GC_CHAIN (chain);
  311. }
  312. /* Free unmarked structs. */
  313. chain = newchain;
  314. newchain = SCM_EOL;
  315. while (!scm_is_null (chain))
  316. {
  317. SCM obj = chain;
  318. chain = SCM_STRUCT_GC_CHAIN (chain);
  319. if (SCM_GC_MARK_P (obj))
  320. {
  321. SCM_CLEAR_GC_MARK (obj);
  322. SCM_SET_STRUCT_GC_CHAIN (obj, newchain);
  323. newchain = obj;
  324. }
  325. else
  326. {
  327. scm_t_bits * vtable_data = SCM_STRUCT_VTABLE_DATA (obj);
  328. scm_t_bits * data = SCM_STRUCT_DATA (obj);
  329. scm_t_struct_free free_struct_data
  330. = ((scm_t_struct_free) vtable_data[scm_struct_i_free]);
  331. SCM_SET_CELL_TYPE (obj, scm_tc_free_cell);
  332. free_struct_data (vtable_data, data);
  333. }
  334. }
  335. }
  336. while (!scm_is_null (newchain));
  337. return 0;
  338. }
  339. SCM_DEFINE (scm_make_struct, "make-struct", 2, 0, 1,
  340. (SCM vtable, SCM tail_array_size, SCM init),
  341. "Create a new structure.\n\n"
  342. "@var{type} must be a vtable structure (@pxref{Vtables}).\n\n"
  343. "@var{tail-elts} must be a non-negative integer. If the layout\n"
  344. "specification indicated by @var{type} includes a tail-array,\n"
  345. "this is the number of elements allocated to that array.\n\n"
  346. "The @var{init1}, @dots{} are optional arguments describing how\n"
  347. "successive fields of the structure should be initialized. Only fields\n"
  348. "with protection 'r' or 'w' can be initialized, except for fields of\n"
  349. "type 's', which are automatically initialized to point to the new\n"
  350. "structure itself; fields with protection 'o' can not be initialized by\n"
  351. "Scheme programs.\n\n"
  352. "If fewer optional arguments than initializable fields are supplied,\n"
  353. "fields of type 'p' get default value #f while fields of type 'u' are\n"
  354. "initialized to 0.\n\n"
  355. "Structs are currently the basic representation for record-like data\n"
  356. "structures in Guile. The plan is to eventually replace them with a\n"
  357. "new representation which will at the same time be easier to use and\n"
  358. "more powerful.\n\n"
  359. "For more information, see the documentation for @code{make-vtable-vtable}.")
  360. #define FUNC_NAME s_scm_make_struct
  361. {
  362. SCM layout;
  363. size_t basic_size;
  364. size_t tail_elts;
  365. scm_t_bits * data;
  366. SCM handle;
  367. SCM_VALIDATE_VTABLE (1, vtable);
  368. SCM_VALIDATE_REST_ARGUMENT (init);
  369. layout = SCM_PACK (SCM_STRUCT_DATA (vtable) [scm_vtable_index_layout]);
  370. basic_size = scm_i_symbol_length (layout) / 2;
  371. tail_elts = scm_to_size_t (tail_array_size);
  372. /* A tail array is only allowed if the layout fields string ends in "R",
  373. "W" or "O". */
  374. if (tail_elts != 0)
  375. {
  376. SCM layout_str, last_char;
  377. if (basic_size == 0)
  378. {
  379. bad_tail:
  380. SCM_MISC_ERROR ("tail array not allowed unless layout ends R, W, or O", SCM_EOL);
  381. }
  382. layout_str = scm_symbol_to_string (layout);
  383. last_char = scm_string_ref (layout_str,
  384. scm_from_size_t (2 * basic_size - 1));
  385. if (! SCM_LAYOUT_TAILP (SCM_CHAR (last_char)))
  386. goto bad_tail;
  387. }
  388. /* In guile 1.8.5 and earlier, everything below was covered by a
  389. CRITICAL_SECTION lock. This can lead to deadlocks in garbage
  390. collection, since other threads might be holding the heap_mutex, while
  391. sleeping on the CRITICAL_SECTION lock. There does not seem to be any
  392. need for a lock on the section below, as it does not access or update
  393. any globals, so the critical section has been removed. */
  394. if (SCM_STRUCT_DATA (vtable)[scm_struct_i_flags] & SCM_STRUCTF_ENTITY)
  395. {
  396. data = scm_alloc_struct (basic_size + tail_elts,
  397. scm_struct_entity_n_extra_words,
  398. "entity struct");
  399. data[scm_struct_i_procedure] = SCM_UNPACK (SCM_BOOL_F);
  400. data[scm_struct_i_setter] = SCM_UNPACK (SCM_BOOL_F);
  401. }
  402. else
  403. data = scm_alloc_struct (basic_size + tail_elts,
  404. scm_struct_n_extra_words,
  405. "struct");
  406. handle = scm_double_cell ((((scm_t_bits) SCM_STRUCT_DATA (vtable))
  407. + scm_tc3_struct),
  408. (scm_t_bits) data, 0, 0);
  409. scm_struct_init (handle, layout, data, tail_elts, init);
  410. return handle;
  411. }
  412. #undef FUNC_NAME
  413. SCM_DEFINE (scm_make_vtable_vtable, "make-vtable-vtable", 2, 0, 1,
  414. (SCM user_fields, SCM tail_array_size, SCM init),
  415. "Return a new, self-describing vtable structure.\n\n"
  416. "@var{user-fields} is a string describing user defined fields of the\n"
  417. "vtable beginning at index @code{vtable-offset-user}\n"
  418. "(see @code{make-struct-layout}).\n\n"
  419. "@var{tail-size} specifies the size of the tail-array (if any) of\n"
  420. "this vtable.\n\n"
  421. "@var{init1}, @dots{} are the optional initializers for the fields of\n"
  422. "the vtable.\n\n"
  423. "Vtables have one initializable system field---the struct printer.\n"
  424. "This field comes before the user fields in the initializers passed\n"
  425. "to @code{make-vtable-vtable} and @code{make-struct}, and thus works as\n"
  426. "a third optional argument to @code{make-vtable-vtable} and a fourth to\n"
  427. "@code{make-struct} when creating vtables:\n\n"
  428. "If the value is a procedure, it will be called instead of the standard\n"
  429. "printer whenever a struct described by this vtable is printed.\n"
  430. "The procedure will be called with arguments STRUCT and PORT.\n\n"
  431. "The structure of a struct is described by a vtable, so the vtable is\n"
  432. "in essence the type of the struct. The vtable is itself a struct with\n"
  433. "a vtable. This could go on forever if it weren't for the\n"
  434. "vtable-vtables which are self-describing vtables, and thus terminate\n"
  435. "the chain.\n\n"
  436. "There are several potential ways of using structs, but the standard\n"
  437. "one is to use three kinds of structs, together building up a type\n"
  438. "sub-system: one vtable-vtable working as the root and one or several\n"
  439. "\"types\", each with a set of \"instances\". (The vtable-vtable should be\n"
  440. "compared to the class <class> which is the class of itself.)\n\n"
  441. "@lisp\n"
  442. "(define ball-root (make-vtable-vtable \"pr\" 0))\n\n"
  443. "(define (make-ball-type ball-color)\n"
  444. " (make-struct ball-root 0\n"
  445. " (make-struct-layout \"pw\")\n"
  446. " (lambda (ball port)\n"
  447. " (format port \"#<a ~A ball owned by ~A>\"\n"
  448. " (color ball)\n"
  449. " (owner ball)))\n"
  450. " ball-color))\n"
  451. "(define (color ball) (struct-ref (struct-vtable ball) vtable-offset-user))\n"
  452. "(define (owner ball) (struct-ref ball 0))\n\n"
  453. "(define red (make-ball-type 'red))\n"
  454. "(define green (make-ball-type 'green))\n\n"
  455. "(define (make-ball type owner) (make-struct type 0 owner))\n\n"
  456. "(define ball (make-ball green 'Nisse))\n"
  457. "ball @result{} #<a green ball owned by Nisse>\n"
  458. "@end lisp")
  459. #define FUNC_NAME s_scm_make_vtable_vtable
  460. {
  461. SCM fields;
  462. SCM layout;
  463. size_t basic_size;
  464. size_t tail_elts;
  465. scm_t_bits *data;
  466. SCM handle;
  467. SCM_VALIDATE_STRING (1, user_fields);
  468. SCM_VALIDATE_REST_ARGUMENT (init);
  469. fields = scm_string_append (scm_list_2 (required_vtable_fields,
  470. user_fields));
  471. layout = scm_make_struct_layout (fields);
  472. basic_size = scm_i_symbol_length (layout) / 2;
  473. tail_elts = scm_to_size_t (tail_array_size);
  474. SCM_CRITICAL_SECTION_START;
  475. data = scm_alloc_struct (basic_size + tail_elts,
  476. scm_struct_n_extra_words,
  477. "struct");
  478. handle = scm_double_cell ((scm_t_bits) data + scm_tc3_struct,
  479. (scm_t_bits) data, 0, 0);
  480. data [scm_vtable_index_layout] = SCM_UNPACK (layout);
  481. scm_struct_init (handle, layout, data, tail_elts, scm_cons (layout, init));
  482. SCM_CRITICAL_SECTION_END;
  483. return handle;
  484. }
  485. #undef FUNC_NAME
  486. static SCM scm_i_vtable_vtable_no_extra_fields;
  487. SCM_DEFINE (scm_make_vtable, "make-vtable", 1, 1, 0,
  488. (SCM fields, SCM printer),
  489. "Create a vtable, for creating structures with the given\n"
  490. "@var{fields}.\n"
  491. "\n"
  492. "The optional @var{printer} argument is a function to be called\n"
  493. "@code{(@var{printer} struct port)} on the structures created.\n"
  494. "It should look at @var{struct} and write to @var{port}.")
  495. #define FUNC_NAME s_scm_make_vtable
  496. {
  497. if (SCM_UNBNDP (printer))
  498. printer = SCM_BOOL_F;
  499. return scm_make_struct (scm_i_vtable_vtable_no_extra_fields, SCM_INUM0,
  500. scm_list_2 (scm_make_struct_layout (fields),
  501. printer));
  502. }
  503. #undef FUNC_NAME
  504. /* Return true if S1 and S2 are equal structures, i.e., if their vtable and
  505. contents are the same. Field protections are honored. Thus, it is an
  506. error to test the equality of structures that contain opaque fields. */
  507. SCM
  508. scm_i_struct_equalp (SCM s1, SCM s2)
  509. #define FUNC_NAME "scm_i_struct_equalp"
  510. {
  511. SCM vtable1, vtable2, layout;
  512. size_t struct_size, field_num;
  513. SCM_VALIDATE_STRUCT (1, s1);
  514. SCM_VALIDATE_STRUCT (2, s2);
  515. vtable1 = SCM_STRUCT_VTABLE (s1);
  516. vtable2 = SCM_STRUCT_VTABLE (s2);
  517. if (!scm_is_eq (vtable1, vtable2))
  518. return SCM_BOOL_F;
  519. layout = SCM_STRUCT_LAYOUT (s1);
  520. struct_size = scm_i_symbol_length (layout) / 2;
  521. for (field_num = 0; field_num < struct_size; field_num++)
  522. {
  523. SCM s_field_num;
  524. SCM field1, field2;
  525. /* We have to use `scm_struct_ref ()' here so that fields are accessed
  526. consistently, notably wrt. field types and access rights. */
  527. s_field_num = scm_from_size_t (field_num);
  528. field1 = scm_struct_ref (s1, s_field_num);
  529. field2 = scm_struct_ref (s2, s_field_num);
  530. /* Self-referencing fields (type `s') must be skipped to avoid infinite
  531. recursion. */
  532. if (!(scm_is_eq (field1, s1) && (scm_is_eq (field2, s2))))
  533. if (scm_is_false (scm_equal_p (field1, field2)))
  534. return SCM_BOOL_F;
  535. }
  536. /* FIXME: Tail elements should be tested for equality. */
  537. return SCM_BOOL_T;
  538. }
  539. #undef FUNC_NAME
  540. SCM_DEFINE (scm_struct_ref, "struct-ref", 2, 0, 0,
  541. (SCM handle, SCM pos),
  542. "@deffnx {Scheme Procedure} struct-set! struct n value\n"
  543. "Access (or modify) the @var{n}th field of @var{struct}.\n\n"
  544. "If the field is of type 'p', then it can be set to an arbitrary value.\n\n"
  545. "If the field is of type 'u', then it can only be set to a non-negative\n"
  546. "integer value small enough to fit in one machine word.")
  547. #define FUNC_NAME s_scm_struct_ref
  548. {
  549. SCM answer = SCM_UNDEFINED;
  550. scm_t_bits * data;
  551. SCM layout;
  552. size_t layout_len;
  553. size_t p;
  554. scm_t_bits n_fields;
  555. const char *fields_desc;
  556. char field_type = 0;
  557. SCM_VALIDATE_STRUCT (1, handle);
  558. layout = SCM_STRUCT_LAYOUT (handle);
  559. data = SCM_STRUCT_DATA (handle);
  560. p = scm_to_size_t (pos);
  561. fields_desc = scm_i_symbol_chars (layout);
  562. layout_len = scm_i_symbol_length (layout);
  563. if (SCM_STRUCT_VTABLE_FLAGS (handle) & SCM_STRUCTF_LIGHT)
  564. /* no extra words */
  565. n_fields = layout_len / 2;
  566. else
  567. n_fields = data[scm_struct_i_n_words];
  568. SCM_ASSERT_RANGE(1, pos, p < n_fields);
  569. if (p * 2 < layout_len)
  570. {
  571. char ref;
  572. field_type = fields_desc[p * 2];
  573. ref = fields_desc[p * 2 + 1];
  574. if ((ref != 'r') && (ref != 'w'))
  575. {
  576. if ((ref == 'R') || (ref == 'W'))
  577. field_type = 'u';
  578. else
  579. SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
  580. }
  581. }
  582. else if (fields_desc[layout_len - 1] != 'O')
  583. field_type = fields_desc[layout_len - 2];
  584. else
  585. SCM_MISC_ERROR ("ref denied for field ~A", scm_list_1 (pos));
  586. switch (field_type)
  587. {
  588. case 'u':
  589. answer = scm_from_ulong (data[p]);
  590. break;
  591. #if 0
  592. case 'i':
  593. answer = scm_from_long (data[p]);
  594. break;
  595. case 'd':
  596. answer = scm_make_real (*((double *)&(data[p])));
  597. break;
  598. #endif
  599. case 's':
  600. case 'p':
  601. answer = SCM_PACK (data[p]);
  602. break;
  603. default:
  604. SCM_MISC_ERROR ("unrecognized field type: ~S",
  605. scm_list_1 (SCM_MAKE_CHAR (field_type)));
  606. }
  607. return answer;
  608. }
  609. #undef FUNC_NAME
  610. SCM_DEFINE (scm_struct_set_x, "struct-set!", 3, 0, 0,
  611. (SCM handle, SCM pos, SCM val),
  612. "Set the slot of the structure @var{handle} with index @var{pos}\n"
  613. "to @var{val}. Signal an error if the slot can not be written\n"
  614. "to.")
  615. #define FUNC_NAME s_scm_struct_set_x
  616. {
  617. scm_t_bits * data;
  618. SCM layout;
  619. size_t layout_len;
  620. size_t p;
  621. int n_fields;
  622. const char *fields_desc;
  623. char field_type = 0;
  624. SCM_VALIDATE_STRUCT (1, handle);
  625. layout = SCM_STRUCT_LAYOUT (handle);
  626. data = SCM_STRUCT_DATA (handle);
  627. p = scm_to_size_t (pos);
  628. fields_desc = scm_i_symbol_chars (layout);
  629. layout_len = scm_i_symbol_length (layout);
  630. if (SCM_STRUCT_VTABLE_FLAGS (handle) & SCM_STRUCTF_LIGHT)
  631. /* no extra words */
  632. n_fields = layout_len / 2;
  633. else
  634. n_fields = data[scm_struct_i_n_words];
  635. SCM_ASSERT_RANGE (1, pos, p < n_fields);
  636. if (p * 2 < layout_len)
  637. {
  638. char set_x;
  639. field_type = fields_desc[p * 2];
  640. set_x = fields_desc [p * 2 + 1];
  641. if (set_x != 'w')
  642. SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
  643. }
  644. else if (fields_desc[layout_len - 1] == 'W')
  645. field_type = fields_desc[layout_len - 2];
  646. else
  647. SCM_MISC_ERROR ("set! denied for field ~A", scm_list_1 (pos));
  648. switch (field_type)
  649. {
  650. case 'u':
  651. data[p] = SCM_NUM2ULONG (3, val);
  652. break;
  653. #if 0
  654. case 'i':
  655. data[p] = SCM_NUM2LONG (3, val);
  656. break;
  657. case 'd':
  658. *((double *)&(data[p])) = scm_num2dbl (val, (char *)SCM_ARG3);
  659. break;
  660. #endif
  661. case 'p':
  662. data[p] = SCM_UNPACK (val);
  663. break;
  664. case 's':
  665. SCM_MISC_ERROR ("self fields immutable", SCM_EOL);
  666. default:
  667. SCM_MISC_ERROR ("unrecognized field type: ~S",
  668. scm_list_1 (SCM_MAKE_CHAR (field_type)));
  669. }
  670. return val;
  671. }
  672. #undef FUNC_NAME
  673. SCM_DEFINE (scm_struct_vtable, "struct-vtable", 1, 0, 0,
  674. (SCM handle),
  675. "Return the vtable structure that describes the type of @var{struct}.")
  676. #define FUNC_NAME s_scm_struct_vtable
  677. {
  678. SCM_VALIDATE_STRUCT (1, handle);
  679. return SCM_STRUCT_VTABLE (handle);
  680. }
  681. #undef FUNC_NAME
  682. SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0,
  683. (SCM handle),
  684. "Return the vtable tag of the structure @var{handle}.")
  685. #define FUNC_NAME s_scm_struct_vtable_tag
  686. {
  687. SCM_VALIDATE_VTABLE (1, handle);
  688. return scm_from_ulong (((unsigned long)SCM_STRUCT_DATA (handle)) >> 3);
  689. }
  690. #undef FUNC_NAME
  691. /* {Associating names and classes with vtables}
  692. *
  693. * The name of a vtable should probably be stored as a slot. This is
  694. * a backward compatible solution until agreement has been achieved on
  695. * how to associate names with vtables.
  696. */
  697. unsigned long
  698. scm_struct_ihashq (SCM obj, unsigned long n)
  699. {
  700. /* The length of the hash table should be a relative prime it's not
  701. necessary to shift down the address. */
  702. return SCM_UNPACK (obj) % n;
  703. }
  704. SCM
  705. scm_struct_create_handle (SCM obj)
  706. {
  707. SCM handle = scm_hash_fn_create_handle_x (scm_struct_table,
  708. obj,
  709. SCM_BOOL_F,
  710. scm_struct_ihashq,
  711. scm_sloppy_assq,
  712. 0);
  713. if (scm_is_false (SCM_CDR (handle)))
  714. SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F));
  715. return handle;
  716. }
  717. SCM_DEFINE (scm_struct_vtable_name, "struct-vtable-name", 1, 0, 0,
  718. (SCM vtable),
  719. "Return the name of the vtable @var{vtable}.")
  720. #define FUNC_NAME s_scm_struct_vtable_name
  721. {
  722. SCM_VALIDATE_VTABLE (1, vtable);
  723. return SCM_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)));
  724. }
  725. #undef FUNC_NAME
  726. SCM_DEFINE (scm_set_struct_vtable_name_x, "set-struct-vtable-name!", 2, 0, 0,
  727. (SCM vtable, SCM name),
  728. "Set the name of the vtable @var{vtable} to @var{name}.")
  729. #define FUNC_NAME s_scm_set_struct_vtable_name_x
  730. {
  731. SCM_VALIDATE_VTABLE (1, vtable);
  732. SCM_VALIDATE_SYMBOL (2, name);
  733. SCM_SET_STRUCT_TABLE_NAME (SCM_CDR (scm_struct_create_handle (vtable)),
  734. name);
  735. return SCM_UNSPECIFIED;
  736. }
  737. #undef FUNC_NAME
  738. void
  739. scm_print_struct (SCM exp, SCM port, scm_print_state *pstate)
  740. {
  741. if (scm_is_true (scm_procedure_p (SCM_STRUCT_PRINTER (exp))))
  742. scm_printer_apply (SCM_STRUCT_PRINTER (exp), exp, port, pstate);
  743. else
  744. {
  745. SCM vtable = SCM_STRUCT_VTABLE (exp);
  746. SCM name = scm_struct_vtable_name (vtable);
  747. scm_puts ("#<", port);
  748. if (scm_is_true (name))
  749. scm_display (name, port);
  750. else
  751. scm_puts ("struct", port);
  752. scm_putc (' ', port);
  753. scm_uintprint (SCM_UNPACK (vtable), 16, port);
  754. scm_putc (':', port);
  755. scm_uintprint (SCM_UNPACK (exp), 16, port);
  756. scm_putc ('>', port);
  757. }
  758. }
  759. void
  760. scm_struct_prehistory ()
  761. {
  762. scm_i_structs_to_free = SCM_EOL;
  763. scm_c_hook_add (&scm_before_sweep_c_hook, scm_struct_gc_init, 0, 0);
  764. /* With the new lazy sweep GC, the point at which the entire heap is
  765. swept is just before the mark phase. */
  766. scm_c_hook_add (&scm_before_mark_c_hook, scm_free_structs, 0, 0);
  767. }
  768. void
  769. scm_init_struct ()
  770. {
  771. scm_struct_table
  772. = scm_permanent_object (scm_make_weak_key_hash_table (scm_from_int (31)));
  773. required_vtable_fields = scm_from_locale_string ("prsrpw");
  774. scm_permanent_object (required_vtable_fields);
  775. scm_i_vtable_vtable_no_extra_fields =
  776. scm_permanent_object
  777. (scm_make_vtable_vtable (scm_nullstr, SCM_INUM0, SCM_EOL));
  778. scm_c_define ("vtable-index-layout", scm_from_int (scm_vtable_index_layout));
  779. scm_c_define ("vtable-index-vtable", scm_from_int (scm_vtable_index_vtable));
  780. scm_c_define ("vtable-index-printer",
  781. scm_from_int (scm_vtable_index_printer));
  782. scm_c_define ("vtable-offset-user", scm_from_int (scm_vtable_offset_user));
  783. #include "libguile/struct.x"
  784. }
  785. /*
  786. Local Variables:
  787. c-file-style: "gnu"
  788. End:
  789. */