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

SHOGUN Machine Learning Toolbox - Documentation