SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 #include <cstring>
21 
22 /* memcpy wrapper to enable clean moves to different memcpy backends */
23 namespace shogun
24 {
25 
26 template <class InputIt, class OutputIt>
27 SG_FORCED_INLINE void* sg_memcpy(InputIt dest, OutputIt src, size_t count)
28 {
29  return std::memcpy(static_cast<void*>(dest), static_cast<const void*>(src), count);
30 }
31 
32 } // namespace shogun
33 
34 /* wrappers for malloc, free, realloc, calloc */
35 
36 /* overload new() / delete */
37 void* operator new(size_t size) throw (std::bad_alloc);
38 void operator delete(void *p) throw();
39 
40 /* overload new[] / delete[] */
41 void* operator new[](size_t size) throw(std::bad_alloc);
42 void operator delete[](void *p) throw();
43 
44 #ifdef TRACE_MEMORY_ALLOCS
45 #define SG_MALLOC(type, len) sg_generic_malloc<type>(size_t(len), __FILE__, __LINE__)
46 #define SG_CALLOC(type, len) sg_generic_calloc<type>(size_t(len), __FILE__, __LINE__)
47 #define SG_REALLOC(type, ptr, old_len, len) sg_generic_realloc<type>(ptr, size_t(old_len), size_t(len), __FILE__, __LINE__)
48 #define SG_FREE(ptr) sg_generic_free(ptr)
49 #else //TRACE_MEMORY_ALLOCS
50 
51 #define SG_MALLOC(type, len) sg_generic_malloc<type>(size_t(len))
52 #define SG_CALLOC(type, len) sg_generic_calloc<type>(size_t(len))
53 #define SG_REALLOC(type, ptr, old_len, len) sg_generic_realloc<type>(ptr, size_t(old_len), size_t(len))
54 #define SG_FREE(ptr) sg_generic_free(ptr)
55 #endif //TRACE_MEMORY_ALLOCS
56 
57 namespace shogun
58 {
59  template <class T> class SGVector;
60  template <class T> class SGSparseVector;
61  template <class T> class SGMatrix;
62 
63 #ifdef TRACE_MEMORY_ALLOCS
64 void* sg_malloc(size_t size, const char* file, int line);
65 template <class T> T* sg_generic_malloc(size_t len, const char* file, int line)
66 {
67  return (T*) sg_malloc(sizeof(T)*len, file, line);
68 }
69 
70 void* sg_calloc(size_t num, size_t size, const char* file, int line);
71 template <class T> T* sg_generic_calloc(size_t len, const char* file, int line)
72 {
73  return (T*) sg_calloc(len, sizeof(T), file, line);
74 }
75 
76 void* sg_realloc(void* ptr, size_t size, const char* file, int line);
77 template <class T> T* sg_generic_realloc(T* ptr, size_t old_len, size_t len, const char* file, int line)
78 {
79  return (T*) sg_realloc(ptr, sizeof(T)*len, file, line);
80 }
81 
82 void sg_free(void* ptr);
83 template <class T> void sg_generic_free(T* ptr)
84 {
85  sg_free((void*) ptr);
86 }
87 #else //TRACE_MEMORY_ALLOCS
88 void* sg_malloc(size_t size);
89 template <class T> T* sg_generic_malloc(size_t len)
90 {
91  return (T*) sg_malloc(sizeof(T)*len);
92 }
93 
94 void* sg_realloc(void* ptr, size_t size);
95 template <class T> T* sg_generic_realloc(T* ptr, size_t old_len, size_t len)
96 {
97  return (T*) sg_realloc(ptr, sizeof(T)*len);
98 }
99 
100 void* sg_calloc(size_t num, size_t size);
101 template <class T> T* sg_generic_calloc(size_t len)
102 {
103  return (T*) sg_calloc(len, sizeof(T));
104 }
105 
106 void sg_free(void* ptr);
107 template <class T> void sg_generic_free(T* ptr)
108 {
109  sg_free(ptr);
110 }
111 #endif //TRACE_MEMORY_ALLOCS
112 #ifdef TRACE_MEMORY_ALLOCS
113 
114 class MemoryBlock
115 {
116  public:
119  MemoryBlock();
123  MemoryBlock(void* p);
130  MemoryBlock(void* p, size_t sz, const char* fname=NULL, int linenr=-1);
134  MemoryBlock(const MemoryBlock &b);
135 
139  bool operator==(const MemoryBlock &b) const;
141  void display();
143  void set_sgobject();
144 
145  protected:
146  void* ptr;
147  size_t size;
148  const char* file;
149  int line;
150  bool is_sgobject;
151 };
152 void list_memory_allocs();
153 #endif
154 
155 #ifdef TRACE_MEMORY_ALLOCS
156 #define SG_SPECIALIZED_MALLOC(type) \
157 template<> type* sg_generic_malloc<type >(size_t len, const char* file, int line); \
158 template<> type* sg_generic_calloc<type >(size_t len, const char* file, int line); \
159 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len, const char* file, int line); \
160 template<> void sg_generic_free<type >(type* ptr);
161 #else // TRACE_MEMORY_ALLOCS
162 #define SG_SPECIALIZED_MALLOC(type) \
163 template<> type* sg_generic_malloc<type >(size_t len); \
164 template<> type* sg_generic_calloc<type >(size_t len); \
165 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len); \
166 template<> void sg_generic_free<type >(type* ptr);
167 #endif // TRACE_MEMORY_ALLOCS
168 
169 SG_SPECIALIZED_MALLOC(SGVector<bool>)
170 SG_SPECIALIZED_MALLOC(SGVector<char>)
171 SG_SPECIALIZED_MALLOC(SGVector<int8_t>)
172 SG_SPECIALIZED_MALLOC(SGVector<uint8_t>)
173 SG_SPECIALIZED_MALLOC(SGVector<int16_t>)
174 SG_SPECIALIZED_MALLOC(SGVector<uint16_t>)
175 SG_SPECIALIZED_MALLOC(SGVector<int32_t>)
176 SG_SPECIALIZED_MALLOC(SGVector<uint32_t>)
177 SG_SPECIALIZED_MALLOC(SGVector<int64_t>)
178 SG_SPECIALIZED_MALLOC(SGVector<uint64_t>)
183 
184 SG_SPECIALIZED_MALLOC(SGSparseVector<bool>)
185 SG_SPECIALIZED_MALLOC(SGSparseVector<char>)
186 SG_SPECIALIZED_MALLOC(SGSparseVector<int8_t>)
187 SG_SPECIALIZED_MALLOC(SGSparseVector<uint8_t>)
188 SG_SPECIALIZED_MALLOC(SGSparseVector<int16_t>)
189 SG_SPECIALIZED_MALLOC(SGSparseVector<uint16_t>)
190 SG_SPECIALIZED_MALLOC(SGSparseVector<int32_t>)
191 SG_SPECIALIZED_MALLOC(SGSparseVector<uint32_t>)
192 SG_SPECIALIZED_MALLOC(SGSparseVector<int64_t>)
193 SG_SPECIALIZED_MALLOC(SGSparseVector<uint64_t>)
194 SG_SPECIALIZED_MALLOC(SGSparseVector<float32_t>)
195 SG_SPECIALIZED_MALLOC(SGSparseVector<float64_t>)
196 SG_SPECIALIZED_MALLOC(SGSparseVector<floatmax_t>)
197 SG_SPECIALIZED_MALLOC(SGSparseVector<complex128_t>)
198 
199 SG_SPECIALIZED_MALLOC(SGMatrix<bool>)
200 SG_SPECIALIZED_MALLOC(SGMatrix<char>)
201 SG_SPECIALIZED_MALLOC(SGMatrix<int8_t>)
202 SG_SPECIALIZED_MALLOC(SGMatrix<uint8_t>)
203 SG_SPECIALIZED_MALLOC(SGMatrix<int16_t>)
204 SG_SPECIALIZED_MALLOC(SGMatrix<uint16_t>)
205 SG_SPECIALIZED_MALLOC(SGMatrix<int32_t>)
206 SG_SPECIALIZED_MALLOC(SGMatrix<uint32_t>)
207 SG_SPECIALIZED_MALLOC(SGMatrix<int64_t>)
208 SG_SPECIALIZED_MALLOC(SGMatrix<uint64_t>)
213 #undef SG_SPECIALIZED_MALLOC
214 
215 void* get_copy(void* src, size_t len);
216 char* get_strdup(const char* str);
217 }
218 
219 #endif // DOXYGEN_SHOULD_SKIP_THIS
220 
221 #endif // __MEMORY_H__
void * sg_calloc(size_t num, size_t size)
Definition: memory.cpp:227
std::complex< float64_t > complex128_t
Definition: common.h:77
bool operator==(const Any &lhs, const Any &rhs)
Definition: any.h:377
#define SG_FORCED_INLINE
Definition: common.h:91
void * sg_malloc(size_t size)
Definition: memory.cpp:194
double float64_t
Definition: common.h:60
long double floatmax_t
Definition: common.h:61
float float32_t
Definition: common.h:59
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SPECIALIZED_MALLOC(type)
Definition: memory.cpp:368
void sg_free(void *ptr)
Definition: memory.cpp:263
void * sg_realloc(void *ptr, size_t size)
Definition: memory.cpp:279

SHOGUN Machine Learning Toolbox - Documentation