OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SchemeSmobGC.cc
Go to the documentation of this file.
1 /*
2  * SchemeSmobMisc.c
3  *
4  * Scheme small objects (SMOBS) garbage-collection methods
5  *
6  * Copyright (c) 2008,2009,2014 Linas Vepstas <linas@linas.org>
7  */
8 
9 #ifdef HAVE_GUILE
10 
11 #include <cstddef>
12 #include <libguile.h>
13 
17 
18 using namespace opencog;
19 
20 SCM SchemeSmob::mark_misc(SCM misc_smob)
21 {
22  scm_t_bits misctype = SCM_SMOB_FLAGS(misc_smob);
23 
24  switch (misctype)
25  {
26  case COG_HANDLE: // Nothing to do here ...
27  case COG_TV: // Nothing to do here ...
28  case COG_AS: // Nothing to do here ...
29  case COG_AV: // Nothing to do here ...
30  case COG_EXTEND: // Nothing to do here ...
31  return SCM_BOOL_F;
32 
33  // I don't get it .. started seeing these recently. I'm just
34  // going to silently ignore thse, for now, don't know what
35  // they mean. XXX TODO figure out and fix if needed. Or document.
36  case 0:
37  return SCM_BOOL_F;
38 
39  default:
40  fprintf(stderr, "Error: opencog-guile: "
41  "don't know how to mark this type: %d\n",
42  (int) misctype);
43  break;
44  }
45 
46  return SCM_BOOL_F;
47 }
48 
57 size_t SchemeSmob::free_misc(SCM node)
58 {
59  scm_t_bits misctype = SCM_SMOB_FLAGS(node);
60 
61  switch (misctype)
62  {
63  case COG_AS:
64  {
65  AtomSpace *as = (AtomSpace *) SCM_SMOB_DATA(node);
66  std::lock_guard<std::mutex> lck(as_mtx);
67  auto has = deleteable_as.find(as);
68  if (deleteable_as.end() != has and 0 == deleteable_as[as])
69  {
70  deleteable_as.erase(has);
71  scm_gc_unregister_collectable_memory (as,
72  sizeof(*as), "opencog atomspace");
73  delete as;
74  }
75  scm_remember_upto_here_1(node);
76  return 0;
77  }
78  case COG_AV:
80  av = (AttentionValue *) SCM_SMOB_DATA(node);
81  scm_gc_unregister_collectable_memory (av,
82  sizeof(*av), "opencog av");
83  delete av;
84  scm_remember_upto_here_1(node);
85  return 0;
86 
87  case COG_HANDLE:
88  Handle* hp;
89  hp = (Handle*) SCM_SMOB_DATA(node);
90  scm_gc_unregister_collectable_memory (hp,
91  sizeof(*hp), "opencog handle");
92  delete hp;
93  scm_remember_upto_here_1(node);
94  return 0;
95 
96  case COG_TV:
97  TruthValue *tv;
98  tv = (TruthValue *) SCM_SMOB_DATA(node);
99  scm_gc_unregister_collectable_memory (tv,
100  sizeof(*tv), "opencog tv");
101  delete tv;
102  scm_remember_upto_here_1(node);
103  return 0;
104 
105  case COG_EXTEND:
106  PrimitiveEnviron *pe;
107  pe = (PrimitiveEnviron *) SCM_SMOB_DATA(node);
108  scm_gc_unregister_collectable_memory (pe,
109  pe->get_size(), "opencog primitive environ");
110  delete pe;
111  scm_remember_upto_here_1(node);
112  return 0;
113 
114  default:
115  fprintf(stderr, "Error: opencog-guile: "
116  "don't know how to free this type: %d\n",
117  (int) misctype);
118  break;
119  }
120  return 0;
121 }
122 
123 /* ============================================================== */
124 
125 std::string SchemeSmob::misc_to_string(SCM node)
126 {
127  scm_t_bits misctype = SCM_SMOB_FLAGS(node);
128  switch (misctype)
129  {
130  case COG_AS:
131  {
132  std::string str(as_to_string((AtomSpace *) SCM_SMOB_DATA(node)));
133  scm_remember_upto_here_1(node);
134  return str;
135  }
136  case COG_AV:
137  {
138  std::string str(av_to_string((AttentionValue *) SCM_SMOB_DATA(node)));
139  scm_remember_upto_here_1(node);
140  return str;
141  }
142  case COG_HANDLE:
143  return handle_to_string(node);
144 
145  case COG_TV:
146  {
147  std::string str(tv_to_string((TruthValue *) SCM_SMOB_DATA(node)));
148  scm_remember_upto_here_1(node);
149  return str;
150  }
151  case COG_EXTEND:
152  {
153  // return "#<opencog extension>\n";
154  // Hmm. Is this really the right thing to return ?? I'm not sure ..
155  PrimitiveEnviron * pe = (PrimitiveEnviron *) SCM_SMOB_DATA(node);
156  std::string str(pe->get_name());
157  scm_remember_upto_here_1(node);
158  return str;
159  }
160  default:
161  return "#<unknown opencog type>\n";
162  }
163  return "";
164 }
165 
166 int SchemeSmob::print_misc(SCM node, SCM port, scm_print_state * ps)
167 {
168  std::string str = misc_to_string(node);
169  scm_puts (str.c_str(), port);
170  return 1; //non-zero means success
171 }
172 
173 /* ============================================================== */
174 
175 #endif /* HAVE_GUILE */
176 /* ===================== END OF FILE ============================ */
static std::string av_to_string(const AttentionValue *)
Definition: SchemeSmobAV.cc:50
static int print_misc(SCM, SCM, scm_print_state *)
static std::string tv_to_string(const TruthValue *)
static std::string handle_to_string(SCM)
static SCM mark_misc(SCM)
Definition: SchemeSmobGC.cc:20
static std::string as_to_string(const AtomSpace *)
Definition: SchemeSmobAS.cc:30
virtual const char * get_name(void)=0
static std::map< AtomSpace *, int > deleteable_as
Definition: SchemeSmob.h:141
virtual size_t get_size(void)=0
static std::string misc_to_string(SCM)
static size_t free_misc(SCM)
Definition: SchemeSmobGC.cc:57
static std::mutex as_mtx
Definition: SchemeSmob.h:140