SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2008-2009 Soeren Sonnenburg 00008 * Copyright (C) 2008-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00009 */ 00010 00011 #ifndef __MEMORY_H__ 00012 #define __MEMORY_H__ 00013 00014 #include <shogun/lib/config.h> 00015 #include <shogun/lib/common.h> 00016 00017 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00018 #include <stdio.h> 00019 #include <stdlib.h> 00020 00021 #include <new> 00022 00023 /* wrappers for malloc, free, realloc, calloc */ 00024 00025 /* overload new() / delete */ 00026 void* operator new(size_t size) throw (std::bad_alloc); 00027 void operator delete(void *p) throw(); 00028 00029 /* overload new[] / delete[] */ 00030 void* operator new[](size_t size) throw(std::bad_alloc); 00031 void operator delete[](void *p) throw(); 00032 00033 #ifdef TRACE_MEMORY_ALLOCS 00034 #define SG_MALLOC(type, len) sg_generic_malloc<type>(size_t(len), __FILE__, __LINE__) 00035 #define SG_CALLOC(type, len) sg_generic_calloc<type>(size_t(len), __FILE__, __LINE__) 00036 #define SG_REALLOC(type, ptr, old_len, len) sg_generic_realloc<type>(ptr, size_t(old_len), size_t(len), __FILE__, __LINE__) 00037 #define SG_FREE(ptr) sg_generic_free(ptr) 00038 #else //TRACE_MEMORY_ALLOCS 00039 00040 #define SG_MALLOC(type, len) sg_generic_malloc<type>(size_t(len)) 00041 #define SG_CALLOC(type, len) sg_generic_calloc<type>(size_t(len)) 00042 #define SG_REALLOC(type, ptr, old_len, len) sg_generic_realloc<type>(ptr, size_t(old_len), size_t(len)) 00043 #define SG_FREE(ptr) sg_generic_free(ptr) 00044 #endif //TRACE_MEMORY_ALLOCS 00045 00046 namespace shogun 00047 { 00048 template <class T> class SGVector; 00049 template <class T> class SGSparseVector; 00050 template <class T> class SGMatrix; 00051 00052 #ifdef TRACE_MEMORY_ALLOCS 00053 void* sg_malloc(size_t size, const char* file, int line); 00054 template <class T> T* sg_generic_malloc(size_t len, const char* file, int line) 00055 { 00056 return (T*) sg_malloc(sizeof(T)*len, file, line); 00057 } 00058 00059 void* sg_calloc(size_t num, size_t size, const char* file, int line); 00060 template <class T> T* sg_generic_calloc(size_t len, const char* file, int line) 00061 { 00062 return (T*) sg_calloc(len, sizeof(T), file, line); 00063 } 00064 00065 void* sg_realloc(void* ptr, size_t size, const char* file, int line); 00066 template <class T> T* sg_generic_realloc(T* ptr, size_t old_len, size_t len, const char* file, int line) 00067 { 00068 return (T*) sg_realloc(ptr, sizeof(T)*len, file, line); 00069 } 00070 00071 void sg_free(void* ptr); 00072 template <class T> void sg_generic_free(T* ptr) 00073 { 00074 sg_free((void*) ptr); 00075 } 00076 #else //TRACE_MEMORY_ALLOCS 00077 void* sg_malloc(size_t size); 00078 template <class T> T* sg_generic_malloc(size_t len) 00079 { 00080 return (T*) sg_malloc(sizeof(T)*len); 00081 } 00082 00083 void* sg_realloc(void* ptr, size_t size); 00084 template <class T> T* sg_generic_realloc(T* ptr, size_t old_len, size_t len) 00085 { 00086 return (T*) sg_realloc(ptr, sizeof(T)*len); 00087 } 00088 00089 void* sg_calloc(size_t num, size_t size); 00090 template <class T> T* sg_generic_calloc(size_t len) 00091 { 00092 return (T*) sg_calloc(len, sizeof(T)); 00093 } 00094 00095 void sg_free(void* ptr); 00096 template <class T> void sg_generic_free(T* ptr) 00097 { 00098 sg_free(ptr); 00099 } 00100 #endif //TRACE_MEMORY_ALLOCS 00101 #ifdef TRACE_MEMORY_ALLOCS 00102 00103 class MemoryBlock 00104 { 00105 public: 00108 MemoryBlock(); 00112 MemoryBlock(void* p); 00119 MemoryBlock(void* p, size_t sz, const char* fname=NULL, int linenr=-1); 00123 MemoryBlock(const MemoryBlock &b); 00124 00128 bool operator==(const MemoryBlock &b) const; 00130 void display(); 00132 void set_sgobject(); 00133 00134 protected: 00135 void* ptr; 00136 size_t size; 00137 const char* file; 00138 int line; 00139 bool is_sgobject; 00140 }; 00141 void list_memory_allocs(); 00142 #endif 00143 00144 #ifdef TRACE_MEMORY_ALLOCS 00145 #define SG_SPECIALIZED_MALLOC(type) \ 00146 template<> type* sg_generic_malloc<type >(size_t len, const char* file, int line); \ 00147 template<> type* sg_generic_calloc<type >(size_t len, const char* file, int line); \ 00148 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len, const char* file, int line); \ 00149 template<> void sg_generic_free<type >(type* ptr); 00150 #else // TRACE_MEMORY_ALLOCS 00151 #define SG_SPECIALIZED_MALLOC(type) \ 00152 template<> type* sg_generic_malloc<type >(size_t len); \ 00153 template<> type* sg_generic_calloc<type >(size_t len); \ 00154 template<> type* sg_generic_realloc<type >(type* ptr, size_t old_len, size_t len); \ 00155 template<> void sg_generic_free<type >(type* ptr); 00156 #endif // TRACE_MEMORY_ALLOCS 00157 00158 SG_SPECIALIZED_MALLOC(SGVector<bool>) 00159 SG_SPECIALIZED_MALLOC(SGVector<char>) 00160 SG_SPECIALIZED_MALLOC(SGVector<int8_t>) 00161 SG_SPECIALIZED_MALLOC(SGVector<uint8_t>) 00162 SG_SPECIALIZED_MALLOC(SGVector<int16_t>) 00163 SG_SPECIALIZED_MALLOC(SGVector<uint16_t>) 00164 SG_SPECIALIZED_MALLOC(SGVector<int32_t>) 00165 SG_SPECIALIZED_MALLOC(SGVector<uint32_t>) 00166 SG_SPECIALIZED_MALLOC(SGVector<int64_t>) 00167 SG_SPECIALIZED_MALLOC(SGVector<uint64_t>) 00168 SG_SPECIALIZED_MALLOC(SGVector<float32_t>) 00169 SG_SPECIALIZED_MALLOC(SGVector<float64_t>) 00170 SG_SPECIALIZED_MALLOC(SGVector<floatmax_t>) 00171 SG_SPECIALIZED_MALLOC(SGVector<complex128_t>) 00172 00173 SG_SPECIALIZED_MALLOC(SGSparseVector<bool>) 00174 SG_SPECIALIZED_MALLOC(SGSparseVector<char>) 00175 SG_SPECIALIZED_MALLOC(SGSparseVector<int8_t>) 00176 SG_SPECIALIZED_MALLOC(SGSparseVector<uint8_t>) 00177 SG_SPECIALIZED_MALLOC(SGSparseVector<int16_t>) 00178 SG_SPECIALIZED_MALLOC(SGSparseVector<uint16_t>) 00179 SG_SPECIALIZED_MALLOC(SGSparseVector<int32_t>) 00180 SG_SPECIALIZED_MALLOC(SGSparseVector<uint32_t>) 00181 SG_SPECIALIZED_MALLOC(SGSparseVector<int64_t>) 00182 SG_SPECIALIZED_MALLOC(SGSparseVector<uint64_t>) 00183 SG_SPECIALIZED_MALLOC(SGSparseVector<float32_t>) 00184 SG_SPECIALIZED_MALLOC(SGSparseVector<float64_t>) 00185 SG_SPECIALIZED_MALLOC(SGSparseVector<floatmax_t>) 00186 SG_SPECIALIZED_MALLOC(SGSparseVector<complex128_t>) 00187 00188 SG_SPECIALIZED_MALLOC(SGMatrix<bool>) 00189 SG_SPECIALIZED_MALLOC(SGMatrix<char>) 00190 SG_SPECIALIZED_MALLOC(SGMatrix<int8_t>) 00191 SG_SPECIALIZED_MALLOC(SGMatrix<uint8_t>) 00192 SG_SPECIALIZED_MALLOC(SGMatrix<int16_t>) 00193 SG_SPECIALIZED_MALLOC(SGMatrix<uint16_t>) 00194 SG_SPECIALIZED_MALLOC(SGMatrix<int32_t>) 00195 SG_SPECIALIZED_MALLOC(SGMatrix<uint32_t>) 00196 SG_SPECIALIZED_MALLOC(SGMatrix<int64_t>) 00197 SG_SPECIALIZED_MALLOC(SGMatrix<uint64_t>) 00198 SG_SPECIALIZED_MALLOC(SGMatrix<float32_t>) 00199 SG_SPECIALIZED_MALLOC(SGMatrix<float64_t>) 00200 SG_SPECIALIZED_MALLOC(SGMatrix<floatmax_t>) 00201 SG_SPECIALIZED_MALLOC(SGMatrix<complex128_t>) 00202 #undef SG_SPECIALIZED_MALLOC 00203 00204 void* get_copy(void* src, size_t len); 00205 char* get_strdup(const char* str); 00206 } 00207 00208 #endif // DOXYGEN_SHOULD_SKIP_THIS 00209 00210 #endif // __MEMORY_H__