1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _SIZETHREADHEAP_H_ |
---|
4 | #define _SIZETHREADHEAP_H_ |
---|
5 | |
---|
6 | #include <assert.h> |
---|
7 | |
---|
8 | template <class Super> |
---|
9 | class SizeThreadHeap : public Super { |
---|
10 | public: |
---|
11 | |
---|
12 | inline void * malloc (size_t sz) { |
---|
13 | // Add room for a size field & a thread field. |
---|
14 | // Both of these must fit in a double. |
---|
15 | assert (sizeof(st) <= sizeof(double)); |
---|
16 | st * ptr = (st *) Super::malloc (sz + sizeof(double)); |
---|
17 | // Store the requested size. |
---|
18 | ptr->size = sz; |
---|
19 | assert (getOrigPtr(ptr + 1) == ptr); |
---|
20 | return (void *) (ptr + 1); |
---|
21 | } |
---|
22 | |
---|
23 | inline void free (void * ptr) { |
---|
24 | void * origPtr = (void *) getOrigPtr(ptr); |
---|
25 | Super::free (origPtr); |
---|
26 | } |
---|
27 | |
---|
28 | static inline volatile int getThreadId (void); |
---|
29 | |
---|
30 | static inline size_t& size (void * ptr) { |
---|
31 | return getOrigPtr(ptr)->size; |
---|
32 | } |
---|
33 | |
---|
34 | static inline int& thread (void * ptr) { |
---|
35 | return getOrigPtr(ptr)->tid; |
---|
36 | } |
---|
37 | |
---|
38 | private: |
---|
39 | |
---|
40 | typedef struct _st { |
---|
41 | size_t size; |
---|
42 | int tid; |
---|
43 | } st; |
---|
44 | |
---|
45 | static inline st * getOrigPtr (void * ptr) { |
---|
46 | return (st *) ((double *) ptr - 1); |
---|
47 | } |
---|
48 | |
---|
49 | }; |
---|
50 | |
---|
51 | |
---|
52 | |
---|
53 | // A platform-dependent way to get a thread id. |
---|
54 | |
---|
55 | // Include the necessary platform-dependent crud. |
---|
56 | #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) |
---|
57 | #ifndef WIN32 |
---|
58 | #define WIN32 1 |
---|
59 | #endif |
---|
60 | #include <windows.h> |
---|
61 | #include <process.h> |
---|
62 | #endif |
---|
63 | |
---|
64 | template <class SuperHeap> |
---|
65 | inline volatile int |
---|
66 | SizeThreadHeap<SuperHeap>::getThreadId (void) { |
---|
67 | #if defined(WIN32) |
---|
68 | return GetCurrentThreadId(); |
---|
69 | #endif |
---|
70 | #if defined(__BEOS__) |
---|
71 | return find_thread(0); |
---|
72 | #endif |
---|
73 | #if defined(__linux) |
---|
74 | // Consecutive thread id's in Linux are 1024 apart; |
---|
75 | // dividing off the 1024 gives us an appropriate thread id. |
---|
76 | return (int) pthread_self() >> 10; // (>> 10 = / 1024) |
---|
77 | #endif |
---|
78 | #if defined(__SVR4) |
---|
79 | return (int) lwp_self(); |
---|
80 | #endif |
---|
81 | #if defined(POSIX) // FIX ME?? |
---|
82 | return (int) pthread_self(); |
---|
83 | #endif |
---|
84 | #if USE_SPROC // FIX ME |
---|
85 | // This hairiness has the same effect as calling getpid(), |
---|
86 | // but it's MUCH faster since it avoids making a system call |
---|
87 | // and just accesses the sproc-local data directly. |
---|
88 | int pid = (int) PRDA->sys_prda.prda_sys.t_pid; |
---|
89 | return pid; |
---|
90 | #endif |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | #endif |
---|