Debian Codegraph @

vendor/Heap-Layers/heaps/threads/threadheap.h

raw symbols SCIP
1 /* -*- C++ -*- */
2
3 #ifndef HL_THREADHEAP_H
4 #define HL_THREADHEAP_H
5
6 /*
7
8 Heap Layers: An Extensible Memory Allocation Infrastructure
9
10 Copyright (C) 2000-2020 by Emery Berger
11 http://www.emeryberger.com
12 [email protected]
13
14 Heap Layers is distributed under the terms of the Apache 2.0 license.
15
16 You may obtain a copy of the License at
17 http://www.apache.org/licenses/LICENSE-2.0
18
19 */
20
21 #include <assert.h>
22 #include <new>
23
24 #include "threads/cpuinfo.h"
25
26 #if !defined(_WIN32)
27 #include <pthread.h>
28 #endif
29
30 /*
31
32 A ThreadHeap comprises NumHeaps "per-thread" heaps.
33
34 To pick a per-thread heap, the current thread id is hashed (mod NumHeaps).
35
36 malloc gets memory from its hashed per-thread heap.
37 free returns memory to its hashed per-thread heap.
38
39 (This allows the per-thread heap to determine the return
40 policy -- 'pure private heaps', 'private heaps with ownership',
41 etc.)
42
43 NB: We assume that the thread heaps are 'locked' as needed. */
44
45 namespace HL {
46
47 template <int NumHeaps, class PerThreadHeap>
48 class ThreadHeap : public PerThreadHeap {
49 public:
50
51 enum { Alignment = PerThreadHeap::Alignment };
52
53 inline void * malloc (size_t sz) {
54 auto tid = Modulo<NumHeaps>::mod (CPUInfo::getThreadId());
55 assert (tid >= 0);
56 assert (tid < NumHeaps);
57 return getHeap(tid)->malloc (sz);
58 }
59
60 inline void free (void * ptr) {
61 auto tid = Modulo<NumHeaps>::mod (CPUInfo::getThreadId());
62 assert (tid >= 0);
63 assert (tid < NumHeaps);
64 getHeap(tid)->free (ptr);
65 }
66
67 inline size_t getSize (void * ptr) {
68 auto tid = Modulo<NumHeaps>::mod (CPUInfo::getThreadId());
69 assert (tid >= 0);
70 assert (tid < NumHeaps);
71 return getHeap(tid)->getSize (ptr);
72 }
73
74
75 private:
76
77 // Access the given heap within the buffer.
78 inline PerThreadHeap * getHeap (unsigned int index) {
79 int ind = (int) index;
80 assert (ind >= 0);
81 assert (ind < NumHeaps);
82 return &ptHeaps[ind];
83 }
84
85 PerThreadHeap ptHeaps[NumHeaps];
86
87 };
88
89 }
90
91
92 #endif