1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _THREADHEAP_H_ |
---|
4 | #define _THREADHEAP_H_ |
---|
5 | |
---|
6 | #include <assert.h> |
---|
7 | #include <new.h> |
---|
8 | |
---|
9 | /* |
---|
10 | |
---|
11 | A ThreadHeap comprises NumHeaps "per-thread" heaps. |
---|
12 | |
---|
13 | To pick a per-thread heap, the current thread id is hashed (mod NumHeaps). |
---|
14 | |
---|
15 | malloc gets memory from its hashed per-thread heap. |
---|
16 | free returns memory to its hashed per-thread heap. |
---|
17 | |
---|
18 | (This allows the per-thread heap to determine the return |
---|
19 | policy -- 'pure private heaps', 'private heaps with ownership', |
---|
20 | etc.) |
---|
21 | |
---|
22 | NB: We assume that the thread heaps are 'locked' as needed. */ |
---|
23 | |
---|
24 | |
---|
25 | template <int NumHeaps, class PerThreadHeap> |
---|
26 | class ThreadHeap : public PerThreadHeap { |
---|
27 | public: |
---|
28 | |
---|
29 | ThreadHeap (void) |
---|
30 | { |
---|
31 | } |
---|
32 | |
---|
33 | inline void * malloc (size_t sz) { |
---|
34 | int tid = getThreadId() % NumHeaps; |
---|
35 | return getHeap(tid)->malloc (sz); |
---|
36 | } |
---|
37 | |
---|
38 | inline void free (void * ptr) { |
---|
39 | int tid = getThreadId() % NumHeaps; |
---|
40 | getHeap(tid)->free (ptr); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | private: |
---|
45 | |
---|
46 | // Access the given heap within the buffer. |
---|
47 | PerThreadHeap * getHeap (int index) { |
---|
48 | assert (index >= 0); |
---|
49 | assert (index < NumHeaps); |
---|
50 | return &ptHeaps[index]; |
---|
51 | } |
---|
52 | |
---|
53 | // Get a suitable thread id number. |
---|
54 | inline static volatile int getThreadId (void); |
---|
55 | |
---|
56 | PerThreadHeap ptHeaps[NumHeaps]; |
---|
57 | |
---|
58 | }; |
---|
59 | |
---|
60 | |
---|
61 | // A platform-dependent way to get a thread id. |
---|
62 | |
---|
63 | // Include the necessary platform-dependent crud. |
---|
64 | #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) |
---|
65 | #ifndef WIN32 |
---|
66 | #define WIN32 1 |
---|
67 | #endif |
---|
68 | #include <windows.h> |
---|
69 | #include <process.h> |
---|
70 | #endif |
---|
71 | |
---|
72 | template <int NumHeaps, class PerThreadHeap> |
---|
73 | inline volatile int |
---|
74 | ThreadHeap<NumHeaps, PerThreadHeap>::getThreadId (void) { |
---|
75 | #if WIN32 |
---|
76 | return GetCurrentThreadId(); |
---|
77 | #endif |
---|
78 | #if defined(__BEOS__) |
---|
79 | return find_thread(0); |
---|
80 | #endif |
---|
81 | #if defined(__linux) |
---|
82 | // Consecutive thread id's in Linux are 1024 apart; |
---|
83 | // dividing off the 1024 gives us an appropriate thread id. |
---|
84 | return (int) pthread_self() >> 10; // (>> 10 = / 1024) |
---|
85 | #endif |
---|
86 | #if defined(__SVR4) |
---|
87 | return (int) lwp_self(); |
---|
88 | #endif |
---|
89 | #if defined(POSIX) |
---|
90 | return (int) pthread_self(); |
---|
91 | #endif |
---|
92 | #if USE_SPROC |
---|
93 | // This hairiness has the same effect as calling getpid(), |
---|
94 | // but it's MUCH faster since it avoids making a system call |
---|
95 | // and just accesses the sproc-local data directly. |
---|
96 | int pid = (int) PRDA->sys_prda.prda_sys.t_pid; |
---|
97 | return pid; |
---|
98 | #endif |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | #endif |
---|