smob.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006 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 <stdio.h>
  21. #include <errno.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/async.h"
  24. #include "libguile/objects.h"
  25. #include "libguile/goops.h"
  26. #include "libguile/ports.h"
  27. #ifdef HAVE_MALLOC_H
  28. #include <malloc.h>
  29. #endif
  30. #include "libguile/smob.h"
  31. /* scm_smobs scm_numsmob
  32. * implement a fixed sized array of smob records.
  33. * Indexes into this table are used when generating type
  34. * tags for smobjects (if you know a tag you can get an index and conversely).
  35. */
  36. #define MAX_SMOB_COUNT 256
  37. long scm_numsmob;
  38. scm_smob_descriptor scm_smobs[MAX_SMOB_COUNT];
  39. /* Lower 16 bit of data must be zero.
  40. */
  41. void
  42. scm_i_set_smob_flags (SCM x, scm_t_bits data)
  43. {
  44. SCM_SET_CELL_WORD_0 (x, (SCM_CELL_WORD_0 (x) & 0xFFFF) | data);
  45. }
  46. void
  47. scm_assert_smob_type (scm_t_bits tag, SCM val)
  48. {
  49. if (!SCM_SMOB_PREDICATE (tag, val))
  50. scm_wrong_type_arg_msg (NULL, 0, val, scm_smobs[SCM_TC2SMOBNUM(tag)].name);
  51. }
  52. /* {Mark}
  53. */
  54. /* This function is vestigial. It used to be the mark function's
  55. responsibility to set the mark bit on the smob or port, but now the
  56. generic marking routine in gc.c takes care of that, and a zero
  57. pointer for a mark function means "don't bother". So you never
  58. need scm_mark0.
  59. However, we leave it here because it's harmless to call it, and
  60. people out there have smob code that uses it, and there's no reason
  61. to make their links fail. */
  62. SCM
  63. scm_mark0 (SCM ptr SCM_UNUSED)
  64. {
  65. return SCM_BOOL_F;
  66. }
  67. SCM
  68. /* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
  69. be used for real pairs. */
  70. scm_markcdr (SCM ptr)
  71. {
  72. return SCM_CELL_OBJECT_1 (ptr);
  73. }
  74. /* {Free}
  75. */
  76. size_t
  77. scm_free0 (SCM ptr SCM_UNUSED)
  78. {
  79. return 0;
  80. }
  81. size_t
  82. scm_smob_free (SCM obj)
  83. {
  84. long n = SCM_SMOBNUM (obj);
  85. if (scm_smobs[n].size > 0)
  86. scm_gc_free ((void *) SCM_CELL_WORD_1 (obj),
  87. scm_smobs[n].size, SCM_SMOBNAME (n));
  88. return 0;
  89. }
  90. /* {Print}
  91. */
  92. int
  93. scm_smob_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  94. {
  95. long n = SCM_SMOBNUM (exp);
  96. scm_puts ("#<", port);
  97. scm_puts (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
  98. scm_putc (' ', port);
  99. if (scm_smobs[n].size)
  100. scm_uintprint (SCM_CELL_WORD_1 (exp), 16, port);
  101. else
  102. scm_uintprint (SCM_UNPACK (exp), 16, port);
  103. scm_putc ('>', port);
  104. return 1;
  105. }
  106. /* {Apply}
  107. */
  108. #define SCM_SMOB_APPLY0(SMOB) \
  109. SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
  110. #define SCM_SMOB_APPLY1(SMOB, A1) \
  111. SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
  112. #define SCM_SMOB_APPLY2(SMOB, A1, A2) \
  113. SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
  114. #define SCM_SMOB_APPLY3(SMOB, A1, A2, A3) \
  115. SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
  116. static SCM
  117. scm_smob_apply_0_010 (SCM smob)
  118. {
  119. return SCM_SMOB_APPLY1 (smob, SCM_UNDEFINED);
  120. }
  121. static SCM
  122. scm_smob_apply_0_020 (SCM smob)
  123. {
  124. return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_UNDEFINED);
  125. }
  126. static SCM
  127. scm_smob_apply_0_030 (SCM smob)
  128. {
  129. return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_UNDEFINED);
  130. }
  131. static SCM
  132. scm_smob_apply_0_001 (SCM smob)
  133. {
  134. return SCM_SMOB_APPLY1 (smob, SCM_EOL);
  135. }
  136. static SCM
  137. scm_smob_apply_0_011 (SCM smob)
  138. {
  139. return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_EOL);
  140. }
  141. static SCM
  142. scm_smob_apply_0_021 (SCM smob)
  143. {
  144. return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_EOL);
  145. }
  146. static SCM
  147. scm_smob_apply_0_error (SCM smob)
  148. {
  149. scm_wrong_num_args (smob);
  150. }
  151. static SCM
  152. scm_smob_apply_1_020 (SCM smob, SCM a1)
  153. {
  154. return SCM_SMOB_APPLY2 (smob, a1, SCM_UNDEFINED);
  155. }
  156. static SCM
  157. scm_smob_apply_1_030 (SCM smob, SCM a1)
  158. {
  159. return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_UNDEFINED);
  160. }
  161. static SCM
  162. scm_smob_apply_1_001 (SCM smob, SCM a1)
  163. {
  164. return SCM_SMOB_APPLY1 (smob, scm_list_1 (a1));
  165. }
  166. static SCM
  167. scm_smob_apply_1_011 (SCM smob, SCM a1)
  168. {
  169. return SCM_SMOB_APPLY2 (smob, a1, SCM_EOL);
  170. }
  171. static SCM
  172. scm_smob_apply_1_021 (SCM smob, SCM a1)
  173. {
  174. return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_EOL);
  175. }
  176. static SCM
  177. scm_smob_apply_1_error (SCM smob, SCM a1 SCM_UNUSED)
  178. {
  179. scm_wrong_num_args (smob);
  180. }
  181. static SCM
  182. scm_smob_apply_2_030 (SCM smob, SCM a1, SCM a2)
  183. {
  184. return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_UNDEFINED);
  185. }
  186. static SCM
  187. scm_smob_apply_2_001 (SCM smob, SCM a1, SCM a2)
  188. {
  189. return SCM_SMOB_APPLY1 (smob, scm_list_2 (a1, a2));
  190. }
  191. static SCM
  192. scm_smob_apply_2_011 (SCM smob, SCM a1, SCM a2)
  193. {
  194. return SCM_SMOB_APPLY2 (smob, a1, scm_list_1 (a2));
  195. }
  196. static SCM
  197. scm_smob_apply_2_021 (SCM smob, SCM a1, SCM a2)
  198. {
  199. return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_EOL);
  200. }
  201. static SCM
  202. scm_smob_apply_2_error (SCM smob, SCM a1 SCM_UNUSED, SCM a2 SCM_UNUSED)
  203. {
  204. scm_wrong_num_args (smob);
  205. }
  206. static SCM
  207. scm_smob_apply_3_030 (SCM smob, SCM a1, SCM a2, SCM rst)
  208. {
  209. if (!scm_is_null (SCM_CDR (rst)))
  210. scm_wrong_num_args (smob);
  211. return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_CAR (rst));
  212. }
  213. static SCM
  214. scm_smob_apply_3_001 (SCM smob, SCM a1, SCM a2, SCM rst)
  215. {
  216. return SCM_SMOB_APPLY1 (smob, scm_cons2 (a1, a2, rst));
  217. }
  218. static SCM
  219. scm_smob_apply_3_011 (SCM smob, SCM a1, SCM a2, SCM rst)
  220. {
  221. return SCM_SMOB_APPLY2 (smob, a1, scm_cons (a2, rst));
  222. }
  223. static SCM
  224. scm_smob_apply_3_021 (SCM smob, SCM a1, SCM a2, SCM rst)
  225. {
  226. return SCM_SMOB_APPLY3 (smob, a1, a2, rst);
  227. }
  228. static SCM
  229. scm_smob_apply_3_error (SCM smob,
  230. SCM a1 SCM_UNUSED,
  231. SCM a2 SCM_UNUSED,
  232. SCM rst SCM_UNUSED)
  233. {
  234. scm_wrong_num_args (smob);
  235. }
  236. scm_t_bits
  237. scm_make_smob_type (char const *name, size_t size)
  238. #define FUNC_NAME "scm_make_smob_type"
  239. {
  240. long new_smob;
  241. SCM_CRITICAL_SECTION_START;
  242. new_smob = scm_numsmob;
  243. if (scm_numsmob != MAX_SMOB_COUNT)
  244. ++scm_numsmob;
  245. SCM_CRITICAL_SECTION_END;
  246. if (new_smob == MAX_SMOB_COUNT)
  247. scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
  248. scm_smobs[new_smob].name = name;
  249. if (size != 0)
  250. {
  251. scm_smobs[new_smob].size = size;
  252. scm_smobs[new_smob].free = scm_smob_free;
  253. }
  254. /* Make a class object if Goops is present. */
  255. if (scm_smob_class)
  256. scm_smob_class[new_smob] = scm_make_extended_class (name, 0);
  257. return scm_tc7_smob + new_smob * 256;
  258. }
  259. #undef FUNC_NAME
  260. void
  261. scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM))
  262. {
  263. scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
  264. }
  265. void
  266. scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM))
  267. {
  268. scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
  269. }
  270. void
  271. scm_set_smob_print (scm_t_bits tc, int (*print) (SCM, SCM, scm_print_state*))
  272. {
  273. scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
  274. }
  275. void
  276. scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
  277. {
  278. scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
  279. }
  280. void
  281. scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
  282. unsigned int req, unsigned int opt, unsigned int rst)
  283. {
  284. SCM (*apply_0) (SCM);
  285. SCM (*apply_1) (SCM, SCM);
  286. SCM (*apply_2) (SCM, SCM, SCM);
  287. SCM (*apply_3) (SCM, SCM, SCM, SCM);
  288. int type = SCM_GSUBR_MAKTYPE (req, opt, rst);
  289. if (rst > 1 || req + opt + rst > 3)
  290. {
  291. puts ("Unsupported smob application type");
  292. abort ();
  293. }
  294. switch (type)
  295. {
  296. case SCM_GSUBR_MAKTYPE (0, 0, 0):
  297. apply_0 = apply; break;
  298. case SCM_GSUBR_MAKTYPE (0, 1, 0):
  299. apply_0 = scm_smob_apply_0_010; break;
  300. case SCM_GSUBR_MAKTYPE (0, 2, 0):
  301. apply_0 = scm_smob_apply_0_020; break;
  302. case SCM_GSUBR_MAKTYPE (0, 3, 0):
  303. apply_0 = scm_smob_apply_0_030; break;
  304. case SCM_GSUBR_MAKTYPE (0, 0, 1):
  305. apply_0 = scm_smob_apply_0_001; break;
  306. case SCM_GSUBR_MAKTYPE (0, 1, 1):
  307. apply_0 = scm_smob_apply_0_011; break;
  308. case SCM_GSUBR_MAKTYPE (0, 2, 1):
  309. apply_0 = scm_smob_apply_0_021; break;
  310. default:
  311. apply_0 = scm_smob_apply_0_error; break;
  312. }
  313. switch (type)
  314. {
  315. case SCM_GSUBR_MAKTYPE (1, 0, 0):
  316. case SCM_GSUBR_MAKTYPE (0, 1, 0):
  317. apply_1 = apply; break;
  318. case SCM_GSUBR_MAKTYPE (1, 1, 0):
  319. case SCM_GSUBR_MAKTYPE (0, 2, 0):
  320. apply_1 = scm_smob_apply_1_020; break;
  321. case SCM_GSUBR_MAKTYPE (1, 2, 0):
  322. case SCM_GSUBR_MAKTYPE (0, 3, 0):
  323. apply_1 = scm_smob_apply_1_030; break;
  324. case SCM_GSUBR_MAKTYPE (0, 0, 1):
  325. apply_1 = scm_smob_apply_1_001; break;
  326. case SCM_GSUBR_MAKTYPE (1, 0, 1):
  327. case SCM_GSUBR_MAKTYPE (0, 1, 1):
  328. apply_1 = scm_smob_apply_1_011; break;
  329. case SCM_GSUBR_MAKTYPE (1, 1, 1):
  330. case SCM_GSUBR_MAKTYPE (0, 2, 1):
  331. apply_1 = scm_smob_apply_1_021; break;
  332. default:
  333. apply_1 = scm_smob_apply_1_error; break;
  334. }
  335. switch (type)
  336. {
  337. case SCM_GSUBR_MAKTYPE (2, 0, 0):
  338. case SCM_GSUBR_MAKTYPE (1, 1, 0):
  339. case SCM_GSUBR_MAKTYPE (0, 2, 0):
  340. apply_2 = apply; break;
  341. case SCM_GSUBR_MAKTYPE (2, 1, 0):
  342. case SCM_GSUBR_MAKTYPE (1, 2, 0):
  343. case SCM_GSUBR_MAKTYPE (0, 3, 0):
  344. apply_2 = scm_smob_apply_2_030; break;
  345. case SCM_GSUBR_MAKTYPE (0, 0, 1):
  346. apply_2 = scm_smob_apply_2_001; break;
  347. case SCM_GSUBR_MAKTYPE (1, 0, 1):
  348. case SCM_GSUBR_MAKTYPE (0, 1, 1):
  349. apply_2 = scm_smob_apply_2_011; break;
  350. case SCM_GSUBR_MAKTYPE (2, 0, 1):
  351. case SCM_GSUBR_MAKTYPE (1, 1, 1):
  352. case SCM_GSUBR_MAKTYPE (0, 2, 1):
  353. apply_2 = scm_smob_apply_2_021; break;
  354. default:
  355. apply_2 = scm_smob_apply_2_error; break;
  356. }
  357. switch (type)
  358. {
  359. case SCM_GSUBR_MAKTYPE (3, 0, 0):
  360. case SCM_GSUBR_MAKTYPE (2, 1, 0):
  361. case SCM_GSUBR_MAKTYPE (1, 2, 0):
  362. case SCM_GSUBR_MAKTYPE (0, 3, 0):
  363. apply_3 = scm_smob_apply_3_030; break;
  364. case SCM_GSUBR_MAKTYPE (0, 0, 1):
  365. apply_3 = scm_smob_apply_3_001; break;
  366. case SCM_GSUBR_MAKTYPE (1, 0, 1):
  367. case SCM_GSUBR_MAKTYPE (0, 1, 1):
  368. apply_3 = scm_smob_apply_3_011; break;
  369. case SCM_GSUBR_MAKTYPE (2, 0, 1):
  370. case SCM_GSUBR_MAKTYPE (1, 1, 1):
  371. case SCM_GSUBR_MAKTYPE (0, 2, 1):
  372. apply_3 = scm_smob_apply_3_021; break;
  373. default:
  374. apply_3 = scm_smob_apply_3_error; break;
  375. }
  376. scm_smobs[SCM_TC2SMOBNUM (tc)].apply = apply;
  377. scm_smobs[SCM_TC2SMOBNUM (tc)].apply_0 = apply_0;
  378. scm_smobs[SCM_TC2SMOBNUM (tc)].apply_1 = apply_1;
  379. scm_smobs[SCM_TC2SMOBNUM (tc)].apply_2 = apply_2;
  380. scm_smobs[SCM_TC2SMOBNUM (tc)].apply_3 = apply_3;
  381. scm_smobs[SCM_TC2SMOBNUM (tc)].gsubr_type = type;
  382. if (scm_smob_class)
  383. scm_i_inherit_applicable (scm_smob_class[SCM_TC2SMOBNUM (tc)]);
  384. }
  385. SCM
  386. scm_make_smob (scm_t_bits tc)
  387. {
  388. long n = SCM_TC2SMOBNUM (tc);
  389. size_t size = scm_smobs[n].size;
  390. scm_t_bits data = (size > 0
  391. ? (scm_t_bits) scm_gc_malloc (size, SCM_SMOBNAME (n))
  392. : 0);
  393. return scm_cell (tc, data);
  394. }
  395. /* {Initialization for the type of free cells}
  396. */
  397. static int
  398. free_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  399. {
  400. char buf[100];
  401. sprintf (buf, "#<freed cell %p; GC missed a reference>",
  402. (void *) SCM_UNPACK (exp));
  403. scm_puts (buf, port);
  404. #if (SCM_DEBUG_CELL_ACCESSES == 1)
  405. if (scm_debug_cell_accesses_p)
  406. abort();
  407. #endif
  408. return 1;
  409. }
  410. void
  411. scm_smob_prehistory ()
  412. {
  413. long i;
  414. scm_t_bits tc;
  415. scm_numsmob = 0;
  416. for (i = 0; i < MAX_SMOB_COUNT; ++i)
  417. {
  418. scm_smobs[i].name = 0;
  419. scm_smobs[i].size = 0;
  420. scm_smobs[i].mark = 0;
  421. scm_smobs[i].free = 0;
  422. scm_smobs[i].print = scm_smob_print;
  423. scm_smobs[i].equalp = 0;
  424. scm_smobs[i].apply = 0;
  425. scm_smobs[i].apply_0 = 0;
  426. scm_smobs[i].apply_1 = 0;
  427. scm_smobs[i].apply_2 = 0;
  428. scm_smobs[i].apply_3 = 0;
  429. scm_smobs[i].gsubr_type = 0;
  430. }
  431. /* WARNING: This scm_make_smob_type call must be done first. */
  432. tc = scm_make_smob_type ("free", 0);
  433. scm_set_smob_print (tc, free_print);
  434. }
  435. /*
  436. Local Variables:
  437. c-file-style: "gnu"
  438. End:
  439. */