SHOGUN
v2.0.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 00016 #include <stdio.h> 00017 #include <stdlib.h> 00018 00019 #include <new> 00020 00021 /* wrappers for malloc, free, realloc, calloc */ 00022 #ifdef TRACE_MEMORY_ALLOCS 00023 #define SG_MALLOC(type, len) (type*) sg_malloc(sizeof(type)*size_t(len), __FILE__, __LINE__) 00024 #define SG_MALLOC(type, len) (type*) sg_malloc(sizeof(type)*size_t(len), __FILE__, __LINE__) 00025 #define SG_CALLOC(type, len) (type*) sg_calloc(size_t(len), sizeof(type), __FILE__, __LINE__) 00026 #define SG_REALLOC(type, ptr, len) (type*) sg_realloc(ptr, sizeof(type)*size_t(len), __FILE__, __LINE__) 00027 #define SG_FREE(ptr) sg_free(ptr) 00028 00029 void* sg_malloc(size_t size, const char* file, int line); 00030 void sg_free(void* ptr); 00031 void* sg_realloc(void* ptr, size_t size, const char* file, int line); 00032 void* sg_calloc(size_t num, size_t size, const char* file, int line); 00033 #else //TRACE_MEMORY_ALLOCS 00034 00035 #define SG_MALLOC(type, len) (type*) sg_malloc(sizeof(type)*size_t(len)) 00036 #define SG_MALLOC(type, len) (type*) sg_malloc(sizeof(type)*size_t(len)) 00037 #define SG_CALLOC(type, len) (type*) sg_calloc(size_t(len), sizeof(type)) 00038 #define SG_REALLOC(type, ptr, len) (type*) sg_realloc(ptr, sizeof(type)*size_t(len)) 00039 #define SG_FREE(ptr) sg_free(ptr) 00040 00041 void* sg_malloc(size_t size); 00042 void sg_free(void* ptr); 00043 void* sg_realloc(void* ptr, size_t size); 00044 void* sg_calloc(size_t num, size_t size); 00045 #endif //TRACE_MEMORY_ALLOCS 00046 00047 /* overload new() / delete */ 00048 void* operator new(size_t size) throw (std::bad_alloc); 00049 void operator delete(void *p) throw(); 00050 00051 /* overload new[] / delete[] */ 00052 void* operator new[](size_t size) throw(std::bad_alloc); 00053 void operator delete[](void *p) throw(); 00054 00055 00056 #ifdef TRACE_MEMORY_ALLOCS 00057 namespace shogun 00058 { 00060 class MemoryBlock 00061 { 00062 public: 00065 MemoryBlock(); 00069 MemoryBlock(void* p); 00076 MemoryBlock(void* p, size_t sz, const char* fname=NULL, int linenr=-1); 00080 MemoryBlock(const MemoryBlock &b); 00081 00085 bool operator==(const MemoryBlock &b) const; 00087 void display(); 00089 void set_sgobject(); 00090 00091 protected: 00092 void* ptr; 00093 size_t size; 00094 const char* file; 00095 int line; 00096 bool is_sgobject; 00097 }; 00098 } 00099 00100 void list_memory_allocs(); 00101 #endif 00102 #endif // __MEMORY_H__