Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef _I_ProxyAllocator_h_
00032 #define _I_ProxyAllocator_h_
00033
00034 #include "libts.h"
00035
00036 class EThread;
00037
00038 extern int thread_freelist_size;
00039
00040 struct ProxyAllocator
00041 {
00042 int allocated;
00043 void *freelist;
00044
00045 ProxyAllocator():allocated(0), freelist(0) { }
00046 };
00047
00048 template<class C> inline C * thread_alloc(ClassAllocator<C> &a, ProxyAllocator & l)
00049 {
00050 #if TS_USE_FREELIST && !TS_USE_RECLAIMABLE_FREELIST
00051 if (l.freelist) {
00052 C *v = (C *) l.freelist;
00053 l.freelist = *(C **) l.freelist;
00054 l.allocated--;
00055 *(void **) v = *(void **) &a.proto.typeObject;
00056 return v;
00057 }
00058 #else
00059 (void)l;
00060 #endif
00061 return a.alloc();
00062 }
00063
00064 template<class C> inline C * thread_alloc_init(ClassAllocator<C> &a, ProxyAllocator & l)
00065 {
00066 #if TS_USE_FREELIST && !TS_USE_RECLAIMABLE_FREELIST
00067 if (l.freelist) {
00068 C *v = (C *) l.freelist;
00069 l.freelist = *(C **) l.freelist;
00070 l.allocated--;
00071 memcpy((void *) v, (void *) &a.proto.typeObject, sizeof(C));
00072 return v;
00073 }
00074 #else
00075 (void)l;
00076 #endif
00077 return a.alloc();
00078 }
00079
00080 template<class C> inline void
00081 thread_free(ClassAllocator<C> &a, C *p)
00082 {
00083 a.free(p);
00084 }
00085
00086 static inline void
00087 thread_free(Allocator &a, void *p)
00088 {
00089 a.free_void(p);
00090 }
00091
00092 template<class C> inline void
00093 thread_freeup(ClassAllocator<C> &a, ProxyAllocator & l)
00094 {
00095 while (l.freelist) {
00096 C *v = (C *) l.freelist;
00097 l.freelist = *(C **) l.freelist;
00098 l.allocated--;
00099 a.free(v);
00100 }
00101 ink_assert(!l.allocated);
00102 }
00103
00104 void* thread_alloc(Allocator &a, ProxyAllocator &l);
00105 void thread_freeup(Allocator &a, ProxyAllocator &l);
00106
00107 #define THREAD_ALLOC(_a, _t) thread_alloc(::_a, _t->_a)
00108 #define THREAD_ALLOC_INIT(_a, _t) thread_alloc_init(::_a, _t->_a)
00109 #if TS_USE_FREELIST && !TS_USE_RECLAIMABLE_FREELIST
00110 #define THREAD_FREE(_p, _a, _t) do { \
00111 *(char **)_p = (char*)_t->_a.freelist; \
00112 _t->_a.freelist = _p; \
00113 _t->_a.allocated++; \
00114 if (_t->_a.allocated > thread_freelist_size) \
00115 thread_freeup(::_a, _t->_a); \
00116 } while (0)
00117 #else
00118 #define THREAD_FREE(_p, _a, _t) do { \
00119 (void)_t; \
00120 thread_free(::_a, _p); \
00121 } while (0)
00122 #endif
00123
00124 #endif