Line | |
---|
1 | #ifndef _MULTIHEAP_H_ |
---|
2 | #define _MULTIHEAP_H_ |
---|
3 | |
---|
4 | #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) |
---|
5 | #ifndef WIN32 |
---|
6 | #define WIN32 1 |
---|
7 | #endif |
---|
8 | #include <windows.h> |
---|
9 | #include <process.h> |
---|
10 | #endif |
---|
11 | |
---|
12 | template <int NumHeaps, class Super> |
---|
13 | class MultiHeap : public Super { |
---|
14 | public: |
---|
15 | |
---|
16 | inline void * malloc (size_t sz) { |
---|
17 | // Hash the thread id. |
---|
18 | // We assume that it's impossible for two threads to collide. |
---|
19 | int tid = getThreadId() % NumHeaps; |
---|
20 | void * ptr = mySuper[tid].malloc (sz + sizeof(double)); |
---|
21 | *((int *) ptr) = tid; |
---|
22 | void * newPtr = (void *) ((double *) ptr + 1); |
---|
23 | return newPtr; |
---|
24 | } |
---|
25 | |
---|
26 | inline void free (void * ptr) { |
---|
27 | // Return the object to its own heap. |
---|
28 | void * originalPtr = (void *) ((double *) ptr - 1); |
---|
29 | int heapIndex = *((int *) originalPtr); |
---|
30 | // FIX ME! A 'cache' would be nice here... |
---|
31 | // FIX ME! We need a lock here. |
---|
32 | mySuper[heapIndex].free (originalPtr); |
---|
33 | } |
---|
34 | |
---|
35 | private: |
---|
36 | |
---|
37 | Super mySuper[NumHeaps]; |
---|
38 | |
---|
39 | int getThreadId (void) { |
---|
40 | #if WIN32 |
---|
41 | return GetCurrentThreadId(); |
---|
42 | #endif |
---|
43 | } |
---|
44 | |
---|
45 | }; |
---|
46 | |
---|
47 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.