QDP++
qdp_default_allocator.h
Go to the documentation of this file.
1// -*- C++ -*-
2
7
8#ifndef QDP_DEFAULT_ALLOCATOR
9#define QDP_DEFAULT_ALLOCATOR
10
11#include "qdp_config.h"
12
13#if defined(QDP_DEBUG_MEMORY)
14#include <stack>
15#endif
16
17#include "qdp_allocator.h"
18#include "qdp_stdio.h"
19
20#include <string>
21#include <map>
22
23namespace QDP
24{
25 namespace Allocator
26 {
27
28#if defined(QDP_DEBUG_MEMORY)
29 // Struct to hold in map
30 struct MapVal {
31 MapVal(unsigned char* u, const std::string& f, int l, size_t b) :
32 unaligned(u), func(f), line(l), bytes(b) {}
33
34 unsigned char* unaligned;
35 std::string func;
36 int line;
37 size_t bytes;
38 };
39
40 // Convenience typedefs to save typing
41
42 // The type of the map to hold the aligned unaligned values
43 typedef std::map<unsigned char*, MapVal> MapT;
44
45 // Func info
46 struct FuncInfo_t {
47 FuncInfo_t(const char* f, int l) : func(f), line(l) {}
48
49 std::string func;
50 int line;
51 };
52#else
53 typedef std::map<unsigned char*, unsigned char *> MapT;
54#endif
55
56
57 // Specialise allocator to the default case
58 class QDPDefaultAllocator {
59 private:
60
61#if defined(QDP_DEBUG_MEMORY)
62 std::stack<FuncInfo_t> infostack;
63#endif
64
65 MapT the_alignment_map;
66
67 // Disallow Copies
68 QDPDefaultAllocator(const QDPDefaultAllocator& c) {}
69
70 // Disallow assignments (copies by another name)
71 void operator=(const QDPDefaultAllocator& c) {}
72
73 // Disallow creation / destruction by anyone except
74 // the singleton CreateUsingNew policy which is a "friend"
75 // I don't like friends but this follows Alexandrescu's advice
76 // on p154 of Modern C++ Design (A. Alexandrescu)
77 QDPDefaultAllocator() {}
78 ~QDPDefaultAllocator() {}
79
81 friend class QDP::CreateStatic<QDP::Allocator::QDPDefaultAllocator>;
82 public:
83
84 void init(size_t PoolSizeinMB);
85
86 // Pusher
87 void pushFunc(const char* func, int line);
88
89 // Popper
90 void popFunc();
91
95 void*
96 allocate(size_t n_bytes,const MemoryPoolHint& mem_pool_hint);
97
99 void
100 free(void *mem);
101
103 void
104 dump();
105
106
107
108 };
109
110
111 } // namespace Allocator
112} // namespace QDP
113
114#endif
void free(void *mem)
Free an aligned pointer, which was allocated by us.
void pushFunc(const char *func, int line)
void * allocate(size_t n_bytes, const MemoryPoolHint &mem_pool_hint)
std::map< unsigned char *, unsigned char * > MapT
Yet another random number generator.
Catch-alls for all memory allocators.
Parallel version of stdio.