SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
memory.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2008-2009 Soeren Sonnenburg
8  * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
12 #include <shogun/lib/memory.h>
13 #include <shogun/lib/common.h>
14 #include <shogun/lib/Set.h>
15 #include <shogun/base/SGObject.h>
16 
17 using namespace shogun;
18 
19 #ifdef TRACE_MEMORY_ALLOCS
20 extern CSet<shogun::MemoryBlock>* sg_mallocs;
21 
22 MemoryBlock::MemoryBlock(void* p) : ptr(p), size(0), file(NULL),
23  line(-1), is_sgobject(false)
24 {
25 }
26 
27 MemoryBlock::MemoryBlock(void* p, size_t sz, const char* fname, int linenr) :
28  ptr(p), size(sz), file(fname), line(linenr), is_sgobject(false)
29 {
30 }
31 
32 MemoryBlock::MemoryBlock(const MemoryBlock &b)
33 {
34  ptr=b.ptr;
35  size=b.size;
36  file=b.file;
37  line=b.line;
38  is_sgobject=b.is_sgobject;
39 }
40 
41 
42 bool MemoryBlock::operator==(const MemoryBlock &b) const
43 {
44  return ptr==b.ptr;
45 }
46 
47 void MemoryBlock::display()
48 {
49  if (line!=-1)
50  {
51  printf("Memory block at %p of size %lld bytes (allocated in %s line %d)\n",
52  ptr, (long long int) size, file, line);
53  }
54  else
55  {
56  if (is_sgobject)
57  {
58  CSGObject* obj=(CSGObject*) ptr;
59  printf("SGObject '%s' at %p of size %lld bytes with %d ref's\n",
60  obj->get_name(), obj, (long long int) size, obj->ref_count());
61  }
62  else
63  {
64  printf("Object at %p of size %lld bytes\n",
65  ptr, (long long int) size);
66  }
67  }
68 }
69 
70 void MemoryBlock::set_sgobject()
71 {
72  is_sgobject=true;
73 }
74 #endif
75 
76 void* operator new(size_t size) throw (std::bad_alloc)
77 {
78  void *p=malloc(size);
79 #ifdef TRACE_MEMORY_ALLOCS
80  if (sg_mallocs)
81  sg_mallocs->add(MemoryBlock(p,size));
82 #endif
83  if (!p)
84  {
85  const size_t buf_len=128;
86  char buf[buf_len];
87  size_t written=snprintf(buf, buf_len,
88  "Out of memory error, tried to allocate %lld bytes using new().\n", (long long int) size);
89  if (written<buf_len)
90  throw ShogunException(buf);
91  else
92  throw ShogunException("Out of memory error using new.\n");
93  }
94 
95  return p;
96 }
97 
98 void operator delete(void *p) throw()
99 {
100 #ifdef TRACE_MEMORY_ALLOCS
101  if (sg_mallocs)
102  sg_mallocs->remove(MemoryBlock(p, true));
103 #endif
104  free(p);
105 }
106 
107 void* operator new[](size_t size) throw(std::bad_alloc)
108 {
109  void *p=malloc(size);
110 #ifdef TRACE_MEMORY_ALLOCS
111  if (sg_mallocs)
112  sg_mallocs->add(MemoryBlock(p,size));
113 #endif
114 
115  if (!p)
116  {
117  const size_t buf_len=128;
118  char buf[buf_len];
119  size_t written=snprintf(buf, buf_len,
120  "Out of memory error, tried to allocate %lld bytes using new[].\n", (long long int) size);
121  if (written<buf_len)
122  throw ShogunException(buf);
123  else
124  throw ShogunException("Out of memory error using new[].\n");
125  }
126 
127  return p;
128 }
129 
130 void operator delete[](void *p) throw()
131 {
132 #ifdef TRACE_MEMORY_ALLOCS
133  if (sg_mallocs)
134  sg_mallocs->remove(MemoryBlock(p, false));
135 #endif
136  free(p);
137 }
138 
139 void* sg_malloc(size_t size
140 #ifdef TRACE_MEMORY_ALLOCS
141  , const char* file, int line
142 #endif
143 )
144 {
145  void* p=malloc(size);
146 #ifdef TRACE_MEMORY_ALLOCS
147  if (sg_mallocs)
148  sg_mallocs->add(MemoryBlock(p,size, file, line));
149 #endif
150 
151  if (!p)
152  {
153  const size_t buf_len=128;
154  char buf[buf_len];
155  size_t written=snprintf(buf, buf_len,
156  "Out of memory error, tried to allocate %lld bytes using malloc.\n", (long long int) size);
157  if (written<buf_len)
158  throw ShogunException(buf);
159  else
160  throw ShogunException("Out of memory error using malloc.\n");
161  }
162 
163  return p;
164 }
165 
166 void* sg_calloc(size_t num, size_t size
167 #ifdef TRACE_MEMORY_ALLOCS
168  , const char* file, int line
169 #endif
170 )
171 {
172  void* p=calloc(num, size);
173 #ifdef TRACE_MEMORY_ALLOCS
174  if (sg_mallocs)
175  sg_mallocs->add(MemoryBlock(p,size, file, line));
176 #endif
177 
178  if (!p)
179  {
180  const size_t buf_len=128;
181  char buf[buf_len];
182  size_t written=snprintf(buf, buf_len,
183  "Out of memory error, tried to allocate %lld bytes using calloc.\n",
184  (long long int) size);
185 
186  if (written<buf_len)
187  throw ShogunException(buf);
188  else
189  throw ShogunException("Out of memory error using calloc.\n");
190  }
191 
192  return p;
193 }
194 
195 void sg_free(void* ptr)
196 {
197 #ifdef TRACE_MEMORY_ALLOCS
198  if (sg_mallocs)
199  sg_mallocs->remove(MemoryBlock(ptr, false));
200 #endif
201  free(ptr);
202 }
203 
204 void* sg_realloc(void* ptr, size_t size
205 #ifdef TRACE_MEMORY_ALLOCS
206  , const char* file, int line
207 #endif
208 )
209 {
210  void* p=realloc(ptr, size);
211 
212 #ifdef TRACE_MEMORY_ALLOCS
213  if (sg_mallocs)
214  sg_mallocs->remove(MemoryBlock(ptr, false));
215 
216  if (sg_mallocs)
217  sg_mallocs->add(MemoryBlock(p,size, file, line));
218 #endif
219 
220  if (!p && (size || !ptr))
221  {
222  const size_t buf_len=128;
223  char buf[buf_len];
224  size_t written=snprintf(buf, buf_len,
225  "Out of memory error, tried to allocate %lld bytes using realloc.\n", (long long int) size);
226  if (written<buf_len)
227  throw ShogunException(buf);
228  else
229  throw ShogunException("Out of memory error using realloc.\n");
230  }
231 
232  return p;
233 }
234 
235 #ifdef TRACE_MEMORY_ALLOCS
236 void list_memory_allocs()
237 {
238  if (sg_mallocs)
239  {
240  int32_t num=sg_mallocs->get_num_elements();
241  printf("%d Blocks are allocated:\n", num);
242 
243 
244  for (int32_t i=0; i<num; i++)
245  sg_mallocs->get_element(i).display();
246  }
247 }
248 #endif

SHOGUN Machine Learning Toolbox - Documentation