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

SHOGUN Machine Learning Toolbox - Documentation