struct.c 27 KB

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