1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _PHOThreadHeap_H_ |
---|
4 | #define _PHOThreadHeap_H_ |
---|
5 | |
---|
6 | #include <assert.h> |
---|
7 | |
---|
8 | static volatile int getThreadId (void); |
---|
9 | |
---|
10 | template <int NumHeaps, class super> |
---|
11 | class MarkThreadHeap : public super { |
---|
12 | public: |
---|
13 | |
---|
14 | inline void * malloc (size_t sz) { |
---|
15 | int tid = getThreadId() % NumHeaps; |
---|
16 | void * ptr = super::malloc (sz); |
---|
17 | if (ptr != NULL) { |
---|
18 | super::setHeap(ptr, tid); |
---|
19 | super::setPrevHeap(super::getNext(ptr), tid); |
---|
20 | } |
---|
21 | return ptr; |
---|
22 | } |
---|
23 | }; |
---|
24 | |
---|
25 | |
---|
26 | template <int NumHeaps, class super> |
---|
27 | class CheckThreadHeap : public super { |
---|
28 | public: |
---|
29 | |
---|
30 | inline void * malloc (size_t sz) { |
---|
31 | int tid = getThreadId() % NumHeaps; |
---|
32 | void * ptr = super::malloc (sz); |
---|
33 | if (ptr != NULL) |
---|
34 | assert (super::getHeap(ptr) == tid); |
---|
35 | return ptr; |
---|
36 | } |
---|
37 | |
---|
38 | inline void free (void * ptr) { |
---|
39 | super::free (ptr); |
---|
40 | } |
---|
41 | }; |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | /* |
---|
46 | |
---|
47 | A PHOThreadHeap comprises NumHeaps "per-thread" heaps. |
---|
48 | |
---|
49 | To pick a per-thread heap, the current thread id is hashed (mod NumHeaps). |
---|
50 | |
---|
51 | malloc gets memory from its hashed per-thread heap. |
---|
52 | free returns memory to its originating heap. |
---|
53 | |
---|
54 | NB: We assume that the thread heaps are 'locked' as needed. */ |
---|
55 | |
---|
56 | |
---|
57 | template <int NumHeaps, class super> |
---|
58 | class PHOThreadHeap { // : public MarkThreadHeap<NumHeaps, super> { |
---|
59 | public: |
---|
60 | |
---|
61 | inline void * malloc (size_t sz) { |
---|
62 | int tid = getThreadId() % NumHeaps; |
---|
63 | void * ptr = selectHeap(tid)->malloc (sz); |
---|
64 | return ptr; |
---|
65 | } |
---|
66 | |
---|
67 | inline void free (void * ptr) { |
---|
68 | int tid = super::getHeap(ptr); |
---|
69 | selectHeap(tid)->free (ptr); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | inline int remove (void * ptr); |
---|
74 | #if 0 |
---|
75 | { |
---|
76 | int tid = super::getHeap(ptr); |
---|
77 | selectHeap(tid)->remove (ptr); |
---|
78 | } |
---|
79 | #endif |
---|
80 | |
---|
81 | private: |
---|
82 | |
---|
83 | // Access the given heap within the buffer. |
---|
84 | MarkThreadHeap<NumHeaps, super> * selectHeap (int index) { |
---|
85 | assert (index >= 0); |
---|
86 | assert (index < NumHeaps); |
---|
87 | return &ptHeaps[index]; |
---|
88 | } |
---|
89 | |
---|
90 | MarkThreadHeap<NumHeaps, super> ptHeaps[NumHeaps]; |
---|
91 | |
---|
92 | }; |
---|
93 | |
---|
94 | |
---|
95 | // A platform-dependent way to get a thread id. |
---|
96 | |
---|
97 | // Include the necessary platform-dependent crud. |
---|
98 | #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) |
---|
99 | #ifndef WIN32 |
---|
100 | #define WIN32 1 |
---|
101 | #endif |
---|
102 | #include <windows.h> |
---|
103 | #include <process.h> |
---|
104 | #endif |
---|
105 | |
---|
106 | #if defined(__SVR4) |
---|
107 | extern "C" unsigned int lwp_self (void); |
---|
108 | #endif |
---|
109 | |
---|
110 | |
---|
111 | static volatile int getThreadId (void) { |
---|
112 | #if defined(WIN32) |
---|
113 | // It looks like thread id's are always multiples of 4, so... |
---|
114 | int tid = GetCurrentThreadId() >> 2; |
---|
115 | // Now hash in some of the first bits. |
---|
116 | // return tid; |
---|
117 | return (tid & ~(1024-1)) ^ tid; |
---|
118 | #endif |
---|
119 | #if defined(__BEOS__) |
---|
120 | return find_thread(0); |
---|
121 | #endif |
---|
122 | #if defined(__linux) |
---|
123 | // Consecutive thread id's in Linux are 1024 apart; |
---|
124 | // dividing off the 1024 gives us an appropriate thread id. |
---|
125 | return (int) pthread_self() >> 10; // (>> 10 = / 1024) |
---|
126 | #endif |
---|
127 | #if defined(__SVR4) |
---|
128 | return (int) lwp_self(); |
---|
129 | #endif |
---|
130 | #if defined(POSIX) |
---|
131 | return (int) pthread_self(); |
---|
132 | #endif |
---|
133 | #if USE_SPROC |
---|
134 | // This hairiness has the same effect as calling getpid(), |
---|
135 | // but it's MUCH faster since it avoids making a system call |
---|
136 | // and just accesses the sproc-local data directly. |
---|
137 | int pid = (int) PRDA->sys_prda.prda_sys.t_pid; |
---|
138 | return pid; |
---|
139 | #endif |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | #endif |
---|