b2f48cdd8aa2234a46f0d0a3a0ecf7ed145de7f2
[bpt/guile.git] / libguile / mallocs.c
1 /* classes: src_files */
2
3 /* Copyright (C) 1995, 1997 Free Software Foundation, Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA */
19
20 \f
21
22 #include <stdio.h>
23 #include "_scm.h"
24 #include "genio.h"
25 #include "smob.h"
26
27 #include "mallocs.h"
28
29 #ifdef HAVE_MALLOC_H
30 #include <malloc.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36
37 \f
38
39
40
41 static scm_sizet fmalloc SCM_P ((SCM ptr));
42
43 static scm_sizet
44 fmalloc(ptr)
45 SCM ptr;
46 {
47 if (SCM_MALLOCDATA (ptr))
48 free (SCM_MALLOCDATA (ptr));
49 return 0;
50 }
51
52
53 static int prinmalloc SCM_P ((SCM exp, SCM port, scm_print_state *pstate));
54
55 static int
56 prinmalloc (exp, port, pstate)
57 SCM exp;
58 SCM port;
59 scm_print_state *pstate;
60 {
61 scm_puts("#<malloc ", port);
62 scm_intprint(SCM_CDR(exp), 16, port);
63 scm_putc('>', port);
64 return 1;
65 }
66
67 \f
68 int scm_tc16_malloc;
69 static scm_smobfuns mallocsmob = {scm_mark0, fmalloc, prinmalloc, 0};
70
71 \f
72
73
74 SCM
75 scm_malloc_obj (n)
76 scm_sizet n;
77 {
78 SCM answer;
79 SCM mem;
80
81 SCM_NEWCELL (answer);
82 SCM_DEFER_INTS;
83 mem = (n
84 ? (SCM)malloc (n)
85 : 0);
86 if (n && !mem)
87 {
88 SCM_ALLOW_INTS;
89 return SCM_BOOL_F;
90 }
91 SCM_SETCDR (answer, mem);
92 SCM_SETCAR (answer, scm_tc16_malloc);
93 SCM_ALLOW_INTS;
94 return answer;
95 }
96
97
98 \f
99
100 void
101 scm_init_mallocs ()
102 {
103 scm_tc16_malloc = scm_newsmob (&mallocsmob);
104 }
105