SHOGUN  v2.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 
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include <new>
20 
21 /* wrappers for malloc, free, realloc, calloc */
22 #ifdef TRACE_MEMORY_ALLOCS
23 #define SG_MALLOC(type, len) (type*) sg_malloc(sizeof(type)*size_t(len), __FILE__, __LINE__)
24 #define SG_MALLOC(type, len) (type*) sg_malloc(sizeof(type)*size_t(len), __FILE__, __LINE__)
25 #define SG_CALLOC(type, len) (type*) sg_calloc(size_t(len), sizeof(type), __FILE__, __LINE__)
26 #define SG_REALLOC(type, ptr, len) (type*) sg_realloc(ptr, sizeof(type)*size_t(len), __FILE__, __LINE__)
27 #define SG_FREE(ptr) sg_free(ptr)
28 
29 void* sg_malloc(size_t size, const char* file, int line);
30 void sg_free(void* ptr);
31 void* sg_realloc(void* ptr, size_t size, const char* file, int line);
32 void* sg_calloc(size_t num, size_t size, const char* file, int line);
33 #else //TRACE_MEMORY_ALLOCS
34 
35 #define SG_MALLOC(type, len) (type*) sg_malloc(sizeof(type)*size_t(len))
36 #define SG_MALLOC(type, len) (type*) sg_malloc(sizeof(type)*size_t(len))
37 #define SG_CALLOC(type, len) (type*) sg_calloc(size_t(len), sizeof(type))
38 #define SG_REALLOC(type, ptr, len) (type*) sg_realloc(ptr, sizeof(type)*size_t(len))
39 #define SG_FREE(ptr) sg_free(ptr)
40 
41 void* sg_malloc(size_t size);
42 void sg_free(void* ptr);
43 void* sg_realloc(void* ptr, size_t size);
44 void* sg_calloc(size_t num, size_t size);
45 #endif //TRACE_MEMORY_ALLOCS
46 
47 /* overload new() / delete */
48 void* operator new(size_t size) throw (std::bad_alloc);
49 void operator delete(void *p) throw();
50 
51 /* overload new[] / delete[] */
52 void* operator new[](size_t size) throw(std::bad_alloc);
53 void operator delete[](void *p) throw();
54 
55 
56 #ifdef TRACE_MEMORY_ALLOCS
57 namespace shogun
58 {
60 class MemoryBlock
61 {
62  public:
65  MemoryBlock();
69  MemoryBlock(void* p);
76  MemoryBlock(void* p, size_t sz, const char* fname=NULL, int linenr=-1);
80  MemoryBlock(const MemoryBlock &b);
81 
85  bool operator==(const MemoryBlock &b) const;
87  void display();
89  void set_sgobject();
90 
91  protected:
92  void* ptr;
93  size_t size;
94  const char* file;
95  int line;
96  bool is_sgobject;
97 };
98 }
99 
100 void list_memory_allocs();
101 #endif
102 #endif // __MEMORY_H__

SHOGUN Machine Learning Toolbox - Documentation