SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
memory.h
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 
11 #ifndef __MEMORY_H__
12 #define __MEMORY_H__
13 
14 #include <shogun/lib/config.h>
15 #include <shogun/lib/common.h>
16 
17 #ifndef DOXYGEN_SHOULD_SKIP_THIS
18 
19 #include <new>
20 
21 /* wrappers for malloc, free, realloc, calloc */
22 
23 /* overload new() / delete */
24 void* operator new(size_t size) throw (std::bad_alloc);
25 void operator delete(void *p) throw();
26 
27 /* overload new[] / delete[] */
28 void* operator new[](size_t size) throw(std::bad_alloc);
29 void operator delete[](void *p) throw();
30 
31 #ifdef TRACE_MEMORY_ALLOCS
32 #define SG_MALLOC(type, len) sg_generic_malloc<type>(size_t(len), __FILE__, __LINE__)
33 #define SG_CALLOC(type, len) sg_generic_calloc<type>(size_t(len), __FILE__, __LINE__)
34 #define SG_REALLOC(type, ptr, old_len, len) sg_generic_realloc<type>(ptr, size_t(old_len), size_t(len), __FILE__, __LINE__)
35 #define SG_FREE(ptr) sg_generic_free(ptr)
36 #else //TRACE_MEMORY_ALLOCS
37 
38 #define SG_MALLOC(type, len) sg_generic_malloc<type>(size_t(len))
39 #define SG_CALLOC(type, len) sg_generic_calloc<type>(size_t(len))
40 #define SG_REALLOC(type, ptr, old_len, len) sg_generic_realloc<type>(ptr, size_t(old_len), size_t(len))
41 #define SG_FREE(ptr) sg_generic_free(ptr)
42 #endif //TRACE_MEMORY_ALLOCS
43 
44 namespace shogun
45 {
46  template <class T> class SGVector;
47  template <class T> class SGSparseVector;
48  template <class T> class SGMatrix;
49 
50 #ifdef TRACE_MEMORY_ALLOCS
51 void* sg_malloc(size_t size, const char* file, int line);
52 template <class T> T* sg_generic_malloc(size_t len, const char* file, int line)
53 {
54  return (T*) sg_malloc(sizeof(T)*len, file, line);
55 }
56 
57 void* sg_calloc(size_t num, size_t size, const char* file, int line);
58 template <class T> T* sg_generic_calloc(size_t len, const char* file, int line)
59 {
60  return (T*) sg_calloc(len, sizeof(T), file, line);
61 }
62 
63 void* sg_realloc(void* ptr, size_t size, const char* file, int line);
64 template <class T> T* sg_generic_realloc(T* ptr, size_t old_len, size_t len, const char* file, int line)
65 {
66  return (T*) sg_realloc(ptr, sizeof(T)*len, file, line);
67 }
68 
69 void sg_free(void* ptr);
70 template <class T> void sg_generic_free(T* ptr)
71 {
72  sg_free((void*) ptr);
73 }
74 #else //TRACE_MEMORY_ALLOCS
75 void* sg_malloc(size_t size);
76 template <class T> T* sg_generic_malloc(size_t len)
77 {
78  return (T*) sg_malloc(sizeof(T)*len);
79 }
80 
81 void* sg_realloc(void* ptr, size_t size);
82 template <class T> T* sg_generic_realloc(T* ptr, size_t old_len, size_t len)
83 {
84  return (T*) sg_realloc(ptr, sizeof(T)*len);
85 }
86 
87 void* sg_calloc(size_t num, size_t size);
88 template <class T> T* sg_generic_calloc(size_t len)
89 {
90  return (T*) sg_calloc(len, sizeof(T));
91 }
92 
93 void sg_free(void* ptr);
94 template <class T> void sg_generic_free(T* ptr)
95 {
96  sg_free(ptr);
97 }
98 #endif //TRACE_MEMORY_ALLOCS
99 #ifdef TRACE_MEMORY_ALLOCS
100 
101 class MemoryBlock
102 {
103  public:
106  MemoryBlock();
110  MemoryBlock(void* p);
117  MemoryBlock(void* p, size_t sz, const char* fname=NULL, int linenr=-1);
121  MemoryBlock(const MemoryBlock &b);
122 
126  bool operator==(const MemoryBlock &b) const;
128  void display();
130  void set_sgobject();
131 
132  protected:
133  void* ptr;
134  size_t size;
135  const char* file;
136  int line;
137  bool is_sgobject;
138 };
139 void list_memory_allocs();
140 #endif
141 
142 #ifdef TRACE_MEMORY_ALLOCS
143 #define SG_SPECIALIZED_MALLOC(type) \
144 template<> type* sg_generic_malloc<type >(size_t len, const char* file, int line); \
145 template<> type* sg_generic_calloc<type >(size_t len, const char* file, int line); \
146 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len, const char* file, int line); \
147 template<> void sg_generic_free<type >(type* ptr);
148 #else // TRACE_MEMORY_ALLOCS
149 #define SG_SPECIALIZED_MALLOC(type) \
150 template<> type* sg_generic_malloc<type >(size_t len); \
151 template<> type* sg_generic_calloc<type >(size_t len); \
152 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len); \
153 template<> void sg_generic_free<type >(type* ptr);
154 #endif // TRACE_MEMORY_ALLOCS
155 
156 SG_SPECIALIZED_MALLOC(SGVector<bool>)
157 SG_SPECIALIZED_MALLOC(SGVector<char>)
158 SG_SPECIALIZED_MALLOC(SGVector<int8_t>)
159 SG_SPECIALIZED_MALLOC(SGVector<uint8_t>)
160 SG_SPECIALIZED_MALLOC(SGVector<int16_t>)
161 SG_SPECIALIZED_MALLOC(SGVector<uint16_t>)
162 SG_SPECIALIZED_MALLOC(SGVector<int32_t>)
163 SG_SPECIALIZED_MALLOC(SGVector<uint32_t>)
164 SG_SPECIALIZED_MALLOC(SGVector<int64_t>)
165 SG_SPECIALIZED_MALLOC(SGVector<uint64_t>)
170 
171 SG_SPECIALIZED_MALLOC(SGSparseVector<bool>)
172 SG_SPECIALIZED_MALLOC(SGSparseVector<char>)
173 SG_SPECIALIZED_MALLOC(SGSparseVector<int8_t>)
174 SG_SPECIALIZED_MALLOC(SGSparseVector<uint8_t>)
175 SG_SPECIALIZED_MALLOC(SGSparseVector<int16_t>)
176 SG_SPECIALIZED_MALLOC(SGSparseVector<uint16_t>)
177 SG_SPECIALIZED_MALLOC(SGSparseVector<int32_t>)
178 SG_SPECIALIZED_MALLOC(SGSparseVector<uint32_t>)
179 SG_SPECIALIZED_MALLOC(SGSparseVector<int64_t>)
180 SG_SPECIALIZED_MALLOC(SGSparseVector<uint64_t>)
181 SG_SPECIALIZED_MALLOC(SGSparseVector<float32_t>)
182 SG_SPECIALIZED_MALLOC(SGSparseVector<float64_t>)
183 SG_SPECIALIZED_MALLOC(SGSparseVector<floatmax_t>)
184 SG_SPECIALIZED_MALLOC(SGSparseVector<complex128_t>)
185 
186 SG_SPECIALIZED_MALLOC(SGMatrix<bool>)
187 SG_SPECIALIZED_MALLOC(SGMatrix<char>)
188 SG_SPECIALIZED_MALLOC(SGMatrix<int8_t>)
189 SG_SPECIALIZED_MALLOC(SGMatrix<uint8_t>)
190 SG_SPECIALIZED_MALLOC(SGMatrix<int16_t>)
191 SG_SPECIALIZED_MALLOC(SGMatrix<uint16_t>)
192 SG_SPECIALIZED_MALLOC(SGMatrix<int32_t>)
193 SG_SPECIALIZED_MALLOC(SGMatrix<uint32_t>)
194 SG_SPECIALIZED_MALLOC(SGMatrix<int64_t>)
195 SG_SPECIALIZED_MALLOC(SGMatrix<uint64_t>)
200 #undef SG_SPECIALIZED_MALLOC
201 
202 void* get_copy(void* src, size_t len);
203 char* get_strdup(const char* str);
204 }
205 
206 #endif // DOXYGEN_SHOULD_SKIP_THIS
207 
208 #endif // __MEMORY_H__

SHOGUN Machine Learning Toolbox - Documentation