KASKADE 7 development version
kalloc.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2013-2018 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef KALLOC_HH
14#define KALLOC_HH
15
16#include <map>
17#include <mutex>
18#include <vector>
19
20
21
22
23namespace Kaskade
24{
25
39 class Kalloc
40 {
41 // Meta-data for chunks of memory managed.
42 // Currently this just contains a void pointer. Do we need this for future flexibility,
43 // i.e. do we want to store some more metadata of chunks? Linked list of free chunks instead
44 // of currently relying on system new for managing chunk pointers?
45 // If not, we could remove this and simply work with void* internally.
46 class Chunk
47 {
48 public:
49 Chunk(void* mem_);
50
51 bool operator==(Chunk const& c) const;
52
53 void* mem; // pointer to memory chunk
54 };
55
56 public:
66 Kalloc(int node_, int align_=64, size_t blocksize_=2*1024*1024, bool checking=
67#if defined(NDEBUG)
68 false
69#else
70 true
71#endif
72 );
73
77 Kalloc(Kalloc&& kalloc);
78
83
94 void* alloc(size_t n);
95
104 void free(void* p, size_t n);
105
114 void reserve(size_t n, size_t k);
115
119 size_t alignment() const { return align; }
120
124 std::ostream& print(std::ostream& out) const;
125
126 protected:
133 void* allocUnlocked(size_t n);
134
141 void freeUnlocked(void* p, size_t n);
142
143 private:
144 // Conceptually constant parameters
145 int node; // NUMA node on which to allocate
146 int align; // memory alignment
147 size_t blocksize; // NUMA memory block size
148 mutable std::mutex mutex; // mutex for protection against concurrent access
149
150 size_t large; // maximum size of ranges stored in lists
151 size_t mid; // minimum size of mid ranges
152
153
154 // The free memory ranges we manage
155 std::vector<std::vector<Chunk>> freeChunks; // buckets of free memory chunks
156
157 // Statistic data for reporting and on-line performance optimization, e.g., range coalescence
158 size_t nBlocks; // number of memory blocks obtained from the system
159 size_t nRangeBlocks; // number of memory blocks used for smaller ranges
160 std::vector<size_t> nLentRanges; // the number of lent memory ranges from that bucket
161 std::map<void*,size_t> systemBlocks; // keeps track of all memory blocks obtained from the system
162
163 void* extract(int bucket);
164 void insert(void* range, int bucket);
165 size_t bucketsize(int bucket) const;
166 int bucket(size_t n) const;
167
168 size_t freeRangedMemory() const; // computes the amount of free memory available in range lists
169 void coalesce(); // finds adjacent free ranges and merges them
170 template <class Iter>
171 void merge(Iter first, Iter last); // merges consecutive ranges
172 void distribute(void* mem, size_t n); // subidivides the given memory into chunks of suitable size and sort into buckets
173 std::pair<void*,size_t> findFreePage(int b); // find a bucket (at least b) with a free page, allocating one from system if needed
174
175 size_t totalAllocations, globalAllocations, freedLocal, freedGlobal, bucketMiss; // statistics
176
177 bool checkLent; // if true, enables keeping track of all memory ranges handed out
178 std::map<void*,size_t> lent; // for debugging only: can keep track of all memory ranges handed out
179 };
180
186 class KallocUnlocked: public Kalloc
187 {
188 public:
196 KallocUnlocked(int node_, int align_=64, size_t blocksize_=2*1024*1024, bool checking=true): Kalloc(node_,align_,blocksize_,checking) {}
197
208 void* alloc(size_t n) { return allocUnlocked(n); }
209
218 void free(void* p, size_t n) { return freeUnlocked(p,n); }
219 };
220}
221
222#endif
223
A simple memory manager for NUMA systems.
Definition: kalloc.hh:40
void * allocUnlocked(size_t n)
Allocates n bytes of memory.
void free(void *p, size_t n)
Releases memory range.
Kalloc(Kalloc &&kalloc)
Move Constructor.
~Kalloc()
Destructor.
void reserve(size_t n, size_t k)
Tells the allocator that subsequently several blocks of the same size will be requested.
size_t alignment() const
Reports the alignment size.
Definition: kalloc.hh:119
std::ostream & print(std::ostream &out) const
Prints memory management statistics to the given stream.
void freeUnlocked(void *p, size_t n)
Releases memory range.
void * alloc(size_t n)
Allocates n bytes of memory.
Kalloc(int node_, int align_=64, size_t blocksize_=2 *1024 *1024, bool checking=true)
Constructor.
A class for local memory management.
Definition: kalloc.hh:187
void * alloc(size_t n)
Allocates n bytes of memory.
Definition: kalloc.hh:208
KallocUnlocked(int node_, int align_=64, size_t blocksize_=2 *1024 *1024, bool checking=true)
Constructor.
Definition: kalloc.hh:196
void free(void *p, size_t n)
Releases memory range.
Definition: kalloc.hh:218