smob.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include <errno.h>
  43. #include "libguile/_scm.h"
  44. #include "libguile/objects.h"
  45. #include "libguile/ports.h"
  46. #ifdef HAVE_MALLOC_H
  47. #include <malloc.h>
  48. #endif
  49. #include "libguile/smob.h"
  50. /* scm_smobs scm_numsmob
  51. * implement a fixed sized array of smob records.
  52. * Indexes into this table are used when generating type
  53. * tags for smobjects (if you know a tag you can get an index and conversely).
  54. */
  55. #define MAX_SMOB_COUNT 256
  56. long scm_numsmob;
  57. scm_smob_descriptor scm_smobs[MAX_SMOB_COUNT];
  58. /* {Mark}
  59. */
  60. /* This function is vestigial. It used to be the mark function's
  61. responsibility to set the mark bit on the smob or port, but now the
  62. generic marking routine in gc.c takes care of that, and a zero
  63. pointer for a mark function means "don't bother". So you never
  64. need scm_mark0.
  65. However, we leave it here because it's harmless to call it, and
  66. people out there have smob code that uses it, and there's no reason
  67. to make their links fail. */
  68. SCM
  69. scm_mark0 (SCM ptr SCM_UNUSED)
  70. {
  71. return SCM_BOOL_F;
  72. }
  73. SCM
  74. /* Dirk::FIXME: The name markcdr is misleading, since the term cdr should only
  75. be used for real pairs. */
  76. scm_markcdr (SCM ptr)
  77. {
  78. return SCM_CELL_OBJECT_1 (ptr);
  79. }
  80. /* {Free}
  81. */
  82. size_t
  83. scm_free0 (SCM ptr SCM_UNUSED)
  84. {
  85. return 0;
  86. }
  87. size_t
  88. scm_smob_free (SCM obj)
  89. {
  90. scm_must_free ((char *) SCM_CELL_WORD_1 (obj));
  91. return scm_smobs[SCM_SMOBNUM (obj)].size;
  92. }
  93. /* {Print}
  94. */
  95. int
  96. scm_smob_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  97. {
  98. long n = SCM_SMOBNUM (exp);
  99. scm_puts ("#<", port);
  100. scm_puts (SCM_SMOBNAME (n) ? SCM_SMOBNAME (n) : "smob", port);
  101. scm_putc (' ', port);
  102. if (scm_smobs[n].size)
  103. scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
  104. else
  105. scm_intprint (SCM_UNPACK (exp), 16, port);
  106. scm_putc ('>', port);
  107. return 1;
  108. }
  109. /* {Apply}
  110. */
  111. #define SCM_SMOB_APPLY0(SMOB) \
  112. SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB)
  113. #define SCM_SMOB_APPLY1(SMOB,A1) \
  114. SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1)
  115. #define SCM_SMOB_APPLY2(SMOB,A1,A2) \
  116. SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2)
  117. #define SCM_SMOB_APPLY3(SMOB,A1,A2,A3) \
  118. SCM_SMOB_DESCRIPTOR (SMOB).apply (SMOB, A1, A2, A3)
  119. static SCM
  120. scm_smob_apply_0_010 (SCM smob)
  121. {
  122. return SCM_SMOB_APPLY1 (smob, SCM_UNDEFINED);
  123. }
  124. static SCM
  125. scm_smob_apply_0_020 (SCM smob)
  126. {
  127. return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_UNDEFINED);
  128. }
  129. static SCM
  130. scm_smob_apply_0_030 (SCM smob)
  131. {
  132. return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_UNDEFINED);
  133. }
  134. static SCM
  135. scm_smob_apply_0_001 (SCM smob)
  136. {
  137. return SCM_SMOB_APPLY1 (smob, SCM_EOL);
  138. }
  139. static SCM
  140. scm_smob_apply_0_011 (SCM smob)
  141. {
  142. return SCM_SMOB_APPLY2 (smob, SCM_UNDEFINED, SCM_EOL);
  143. }
  144. static SCM
  145. scm_smob_apply_0_021 (SCM smob)
  146. {
  147. return SCM_SMOB_APPLY3 (smob, SCM_UNDEFINED, SCM_UNDEFINED, SCM_EOL);
  148. }
  149. static SCM
  150. scm_smob_apply_0_error (SCM smob)
  151. {
  152. scm_wrong_num_args (smob);
  153. }
  154. static SCM
  155. scm_smob_apply_1_020 (SCM smob, SCM a1)
  156. {
  157. return SCM_SMOB_APPLY2 (smob, a1, SCM_UNDEFINED);
  158. }
  159. static SCM
  160. scm_smob_apply_1_030 (SCM smob, SCM a1)
  161. {
  162. return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_UNDEFINED);
  163. }
  164. static SCM
  165. scm_smob_apply_1_001 (SCM smob, SCM a1)
  166. {
  167. return SCM_SMOB_APPLY1 (smob, scm_list_1 (a1));
  168. }
  169. static SCM
  170. scm_smob_apply_1_011 (SCM smob, SCM a1)
  171. {
  172. return SCM_SMOB_APPLY2 (smob, a1, SCM_EOL);
  173. }
  174. static SCM
  175. scm_smob_apply_1_021 (SCM smob, SCM a1)
  176. {
  177. return SCM_SMOB_APPLY3 (smob, a1, SCM_UNDEFINED, SCM_EOL);
  178. }
  179. static SCM
  180. scm_smob_apply_1_error (SCM smob, SCM a1 SCM_UNUSED)
  181. {
  182. scm_wrong_num_args (smob);
  183. }
  184. static SCM
  185. scm_smob_apply_2_030 (SCM smob, SCM a1, SCM a2)
  186. {
  187. return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_UNDEFINED);
  188. }
  189. static SCM
  190. scm_smob_apply_2_001 (SCM smob, SCM a1, SCM a2)
  191. {
  192. return SCM_SMOB_APPLY1 (smob, scm_list_2 (a1, a2));
  193. }
  194. static SCM
  195. scm_smob_apply_2_011 (SCM smob, SCM a1, SCM a2)
  196. {
  197. return SCM_SMOB_APPLY2 (smob, a1, scm_list_1 (a2));
  198. }
  199. static SCM
  200. scm_smob_apply_2_021 (SCM smob, SCM a1, SCM a2)
  201. {
  202. return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_EOL);
  203. }
  204. static SCM
  205. scm_smob_apply_2_error (SCM smob, SCM a1 SCM_UNUSED, SCM a2 SCM_UNUSED)
  206. {
  207. scm_wrong_num_args (smob);
  208. }
  209. static SCM
  210. scm_smob_apply_3_030 (SCM smob, SCM a1, SCM a2, SCM rst)
  211. {
  212. if (!SCM_NULLP (SCM_CDR (rst)))
  213. scm_wrong_num_args (smob);
  214. return SCM_SMOB_APPLY3 (smob, a1, a2, SCM_CAR (rst));
  215. }
  216. static SCM
  217. scm_smob_apply_3_001 (SCM smob, SCM a1, SCM a2, SCM rst)
  218. {
  219. return SCM_SMOB_APPLY1 (smob, scm_cons2 (a1, a2, rst));
  220. }
  221. static SCM
  222. scm_smob_apply_3_011 (SCM smob, SCM a1, SCM a2, SCM rst)
  223. {
  224. return SCM_SMOB_APPLY2 (smob, a1, scm_cons (a2, rst));
  225. }
  226. static SCM
  227. scm_smob_apply_3_021 (SCM smob, SCM a1, SCM a2, SCM rst)
  228. {
  229. return SCM_SMOB_APPLY3 (smob, a1, a2, rst);
  230. }
  231. static SCM
  232. scm_smob_apply_3_error (SCM smob,
  233. SCM a1 SCM_UNUSED,
  234. SCM a2 SCM_UNUSED,
  235. SCM rst SCM_UNUSED)
  236. {
  237. scm_wrong_num_args (smob);
  238. }
  239. scm_t_bits
  240. scm_make_smob_type (char *name, size_t size)
  241. #define FUNC_NAME "scm_make_smob_type"
  242. {
  243. long new_smob;
  244. SCM_ENTER_A_SECTION; /* scm_numsmob */
  245. new_smob = scm_numsmob;
  246. if (scm_numsmob != MAX_SMOB_COUNT)
  247. ++scm_numsmob;
  248. SCM_EXIT_A_SECTION;
  249. if (new_smob == MAX_SMOB_COUNT)
  250. scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL);
  251. scm_smobs[new_smob].name = name;
  252. if (size != 0)
  253. {
  254. scm_smobs[new_smob].size = size;
  255. scm_smobs[new_smob].free = scm_smob_free;
  256. }
  257. /* Make a class object if Goops is present. */
  258. if (scm_smob_class)
  259. scm_smob_class[new_smob] = scm_make_extended_class (name);
  260. return scm_tc7_smob + new_smob * 256;
  261. }
  262. #undef FUNC_NAME
  263. void
  264. scm_set_smob_mark (scm_t_bits tc, SCM (*mark) (SCM))
  265. {
  266. scm_smobs[SCM_TC2SMOBNUM (tc)].mark = mark;
  267. }
  268. void
  269. scm_set_smob_free (scm_t_bits tc, size_t (*free) (SCM))
  270. {
  271. scm_smobs[SCM_TC2SMOBNUM (tc)].free = free;
  272. }
  273. void
  274. scm_set_smob_print (scm_t_bits tc, int (*print) (SCM, SCM, scm_print_state*))
  275. {
  276. scm_smobs[SCM_TC2SMOBNUM (tc)].print = print;
  277. }
  278. void
  279. scm_set_smob_equalp (scm_t_bits tc, SCM (*equalp) (SCM, SCM))
  280. {
  281. scm_smobs[SCM_TC2SMOBNUM (tc)].equalp = equalp;
  282. }
  283. void
  284. scm_set_smob_apply (scm_t_bits tc, SCM (*apply) (),
  285. unsigned int req, unsigned int opt, unsigned int rst)
  286. {
  287. SCM (*apply_0) (SCM);
  288. SCM (*apply_1) (SCM, SCM);
  289. SCM (*apply_2) (SCM, SCM, SCM);
  290. SCM (*apply_3) (SCM, SCM, SCM, SCM);
  291. const int type = SCM_GSUBR_MAKTYPE (req, opt, rst);
  292. const long i = SCM_TC2SMOBNUM (tc);
  293. if (rst > 1 || req + opt + rst > 3)
  294. {
  295. puts ("Unsupported smob application type");
  296. abort ();
  297. }
  298. switch (type)
  299. {
  300. case SCM_GSUBR_MAKTYPE (0, 0, 0):
  301. apply_0 = apply; break;
  302. case SCM_GSUBR_MAKTYPE (0, 1, 0):
  303. apply_0 = scm_smob_apply_0_010; break;
  304. case SCM_GSUBR_MAKTYPE (0, 2, 0):
  305. apply_0 = scm_smob_apply_0_020; break;
  306. case SCM_GSUBR_MAKTYPE (0, 3, 0):
  307. apply_0 = scm_smob_apply_0_030; break;
  308. case SCM_GSUBR_MAKTYPE (0, 0, 1):
  309. apply_0 = scm_smob_apply_0_001; break;
  310. case SCM_GSUBR_MAKTYPE (0, 1, 1):
  311. apply_0 = scm_smob_apply_0_011; break;
  312. case SCM_GSUBR_MAKTYPE (0, 2, 1):
  313. apply_0 = scm_smob_apply_0_021; break;
  314. default:
  315. apply_0 = scm_smob_apply_0_error; break;
  316. }
  317. switch (type)
  318. {
  319. case SCM_GSUBR_MAKTYPE (1, 0, 0):
  320. case SCM_GSUBR_MAKTYPE (0, 1, 0):
  321. apply_1 = apply; break;
  322. case SCM_GSUBR_MAKTYPE (1, 1, 0):
  323. case SCM_GSUBR_MAKTYPE (0, 2, 0):
  324. apply_1 = scm_smob_apply_1_020; break;
  325. case SCM_GSUBR_MAKTYPE (1, 2, 0):
  326. case SCM_GSUBR_MAKTYPE (0, 3, 0):
  327. apply_1 = scm_smob_apply_1_030; break;
  328. case SCM_GSUBR_MAKTYPE (0, 0, 1):
  329. apply_1 = scm_smob_apply_1_001; break;
  330. case SCM_GSUBR_MAKTYPE (1, 0, 1):
  331. case SCM_GSUBR_MAKTYPE (0, 1, 1):
  332. apply_1 = scm_smob_apply_1_011; break;
  333. case SCM_GSUBR_MAKTYPE (1, 1, 1):
  334. case SCM_GSUBR_MAKTYPE (0, 2, 1):
  335. apply_1 = scm_smob_apply_1_021; break;
  336. default:
  337. apply_1 = scm_smob_apply_1_error; break;
  338. }
  339. switch (type)
  340. {
  341. case SCM_GSUBR_MAKTYPE (2, 0, 0):
  342. case SCM_GSUBR_MAKTYPE (1, 1, 0):
  343. case SCM_GSUBR_MAKTYPE (0, 2, 0):
  344. apply_2 = apply; break;
  345. case SCM_GSUBR_MAKTYPE (2, 1, 0):
  346. case SCM_GSUBR_MAKTYPE (1, 2, 0):
  347. case SCM_GSUBR_MAKTYPE (0, 3, 0):
  348. apply_2 = scm_smob_apply_2_030; break;
  349. case SCM_GSUBR_MAKTYPE (0, 0, 1):
  350. apply_2 = scm_smob_apply_2_001; break;
  351. case SCM_GSUBR_MAKTYPE (1, 0, 1):
  352. case SCM_GSUBR_MAKTYPE (0, 1, 1):
  353. apply_2 = scm_smob_apply_2_011; break;
  354. case SCM_GSUBR_MAKTYPE (2, 0, 1):
  355. case SCM_GSUBR_MAKTYPE (1, 1, 1):
  356. case SCM_GSUBR_MAKTYPE (0, 2, 1):
  357. apply_2 = scm_smob_apply_2_021; break;
  358. default:
  359. apply_2 = scm_smob_apply_2_error; break;
  360. }
  361. switch (type)
  362. {
  363. case SCM_GSUBR_MAKTYPE (3, 0, 0):
  364. case SCM_GSUBR_MAKTYPE (2, 1, 0):
  365. case SCM_GSUBR_MAKTYPE (1, 2, 0):
  366. case SCM_GSUBR_MAKTYPE (0, 3, 0):
  367. apply_3 = scm_smob_apply_3_030; break;
  368. case SCM_GSUBR_MAKTYPE (0, 0, 1):
  369. apply_3 = scm_smob_apply_3_001; break;
  370. case SCM_GSUBR_MAKTYPE (1, 0, 1):
  371. case SCM_GSUBR_MAKTYPE (0, 1, 1):
  372. apply_3 = scm_smob_apply_3_011; break;
  373. case SCM_GSUBR_MAKTYPE (2, 0, 1):
  374. case SCM_GSUBR_MAKTYPE (1, 1, 1):
  375. case SCM_GSUBR_MAKTYPE (0, 2, 1):
  376. apply_3 = scm_smob_apply_3_021; break;
  377. default:
  378. apply_3 = scm_smob_apply_3_error; break;
  379. }
  380. /* We compute i above and use it below because just using
  381. SCM_TC2SMOBNUM (tc) directly as the array index below was causing
  382. a build segfault on m68k machines */
  383. scm_smobs[i].apply = apply;
  384. scm_smobs[i].apply_0 = apply_0;
  385. scm_smobs[i].apply_1 = apply_1;
  386. scm_smobs[i].apply_2 = apply_2;
  387. scm_smobs[i].apply_3 = apply_3;
  388. scm_smobs[i].gsubr_type = type;
  389. }
  390. SCM
  391. scm_make_smob (scm_t_bits tc)
  392. {
  393. long n = SCM_TC2SMOBNUM (tc);
  394. size_t size = scm_smobs[n].size;
  395. SCM z;
  396. SCM_NEWCELL (z);
  397. if (size != 0)
  398. {
  399. #if 0
  400. if (scm_smobs[n].mark != 0)
  401. {
  402. fprintf
  403. (stderr,
  404. "forbidden operation for smobs with GC data, use SCM_NEWSMOB\n");
  405. abort ();
  406. }
  407. #endif
  408. SCM_SET_SMOB_DATA (z, scm_must_malloc (size, SCM_SMOBNAME (n)));
  409. }
  410. SCM_SET_CELL_TYPE (z, tc);
  411. return z;
  412. }
  413. /* {Deprecated stuff}
  414. */
  415. #if (SCM_DEBUG_DEPRECATED == 0)
  416. long
  417. scm_make_smob_type_mfpe (char *name, size_t size,
  418. SCM (*mark) (SCM),
  419. size_t (*free) (SCM),
  420. int (*print) (SCM, SCM, scm_print_state *),
  421. SCM (*equalp) (SCM, SCM))
  422. {
  423. long answer = scm_make_smob_type (name, size);
  424. scm_set_smob_mfpe (answer, mark, free, print, equalp);
  425. return answer;
  426. }
  427. void
  428. scm_set_smob_mfpe (long tc,
  429. SCM (*mark) (SCM),
  430. size_t (*free) (SCM),
  431. int (*print) (SCM, SCM, scm_print_state *),
  432. SCM (*equalp) (SCM, SCM))
  433. {
  434. if (mark) scm_set_smob_mark (tc, mark);
  435. if (free) scm_set_smob_free (tc, free);
  436. if (print) scm_set_smob_print (tc, print);
  437. if (equalp) scm_set_smob_equalp (tc, equalp);
  438. }
  439. #endif /* SCM_DEBUG_DEPRECATED == 0 */
  440. /* {Initialization for i/o types, float, bignum, the type of free cells}
  441. */
  442. static int
  443. free_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
  444. {
  445. char buf[100];
  446. sprintf (buf, "#<freed cell %p; GC missed a reference>",
  447. (void *) SCM_UNPACK (exp));
  448. scm_puts (buf, port);
  449. return 1;
  450. }
  451. void
  452. scm_smob_prehistory ()
  453. {
  454. long i;
  455. scm_t_bits tc;
  456. scm_numsmob = 0;
  457. for (i = 0; i < MAX_SMOB_COUNT; ++i)
  458. {
  459. scm_smobs[i].name = 0;
  460. scm_smobs[i].size = 0;
  461. scm_smobs[i].mark = 0;
  462. scm_smobs[i].free = 0;
  463. scm_smobs[i].print = scm_smob_print;
  464. scm_smobs[i].equalp = 0;
  465. scm_smobs[i].apply = 0;
  466. scm_smobs[i].apply_0 = 0;
  467. scm_smobs[i].apply_1 = 0;
  468. scm_smobs[i].apply_2 = 0;
  469. scm_smobs[i].apply_3 = 0;
  470. scm_smobs[i].gsubr_type = 0;
  471. }
  472. /* WARNING: These scm_make_smob_type calls must be done in this order */
  473. tc = scm_make_smob_type ("free", 0);
  474. scm_set_smob_print (tc, free_print);
  475. tc = scm_make_smob_type ("big", 0); /* freed in gc */
  476. scm_set_smob_print (tc, scm_bigprint);
  477. scm_set_smob_equalp (tc, scm_bigequal);
  478. tc = scm_make_smob_type ("real", 0); /* freed in gc */
  479. scm_set_smob_print (tc, scm_print_real);
  480. scm_set_smob_equalp (tc, scm_real_equalp);
  481. tc = scm_make_smob_type ("complex", 0); /* freed in gc */
  482. scm_set_smob_print (tc, scm_print_complex);
  483. scm_set_smob_equalp (tc, scm_complex_equalp);
  484. }
  485. /*
  486. Local Variables:
  487. c-file-style: "gnu"
  488. End:
  489. */