1 #ifndef JAWS_CACHED_ALLOCATOR_T_CPP
2 #define JAWS_CACHED_ALLOCATOR_T_CPP
4 #include "jaws3/Cached_Allocator_T.h"
6 template <class T
, class ACE_LOCK
> char *
7 JAWS_Cached_Allocator
<T
, ACE_LOCK
>::get_next_pool (char *pool
)
10 char *next_indirect
= pool
+ (this->pool_size_
);
11 ACE_OS::memcpy (&next
, next_indirect
, sizeof (char *));
15 template <class T
, class ACE_LOCK
> void
16 JAWS_Cached_Allocator
<T
, ACE_LOCK
>::set_next_pool (char *pool
, char *next_pool
)
18 char *next_indirect
= pool
+ (this->pool_size_
);
19 ACE_OS::memcpy (next_indirect
, &next_pool
, sizeof (char *));
22 template <class T
, class ACE_LOCK
> void
23 JAWS_Cached_Allocator
<T
, ACE_LOCK
>::extend_pool ()
26 ACE_NEW (new_pool
, char[this->pool_size_
+ sizeof (char *)]);
28 for (size_t c
= 0; c
< (this->pool_size_
/ sizeof (T
)); c
++)
30 void* placement
= new_pool
+ c
* sizeof(T
);
31 this->free_list_
.add (new (placement
) ACE_Cached_Mem_Pool_Node
<T
>);
32 this->set_next_pool (new_pool
, 0);
35 if (this->pool_head_
== 0)
36 this->pool_head_
= this->pool_tail_
= new_pool
;
38 this->set_next_pool (this->pool_tail_
, new_pool
);
40 this->pool_tail_
= new_pool
;
43 template <class T
, class ACE_LOCK
>
44 JAWS_Cached_Allocator
<T
, ACE_LOCK
>::JAWS_Cached_Allocator (size_t n_chunks
)
45 : pool_size_ (n_chunks
* sizeof (T
))
48 , free_list_ (ACE_PURE_FREE_LIST
)
53 template <class T
, class ACE_LOCK
>
54 JAWS_Cached_Allocator
<T
, ACE_LOCK
>::~JAWS_Cached_Allocator ()
56 char *curr
= this->pool_head_
;
60 char *next
= this->get_next_pool (curr
);
66 template <class T
, class ACE_LOCK
> void *
67 JAWS_Cached_Allocator
<T
, ACE_LOCK
>::malloc (size_t nbytes
)
69 if (nbytes
> sizeof (T
))
72 ACE_Cached_Mem_Pool_Node
<T
> *node
= 0;
73 node
= this->free_list_
.remove ();
78 node
= this->free_list_
.remove ();
82 // addr() call is really not absolutely necessary because of the way
83 // ACE_Cached_Mem_Pool_Node's internal structure arranged.
87 template <class T
, class ACE_LOCK
> void
88 JAWS_Cached_Allocator
<T
, ACE_LOCK
>::free (void *ptr
)
90 this->free_list_
.add ((ACE_Cached_Mem_Pool_Node
<T
> *) ptr
);
94 template <class T
> JAWS_Cached_Allocator
<T
, ACE_SYNCH_NULL_MUTEX
> *
95 JAWS_TSS_Cached_Allocator
<T
>::ts_allocator ()
97 JAWS_Cached_Allocator
<T
, ACE_SYNCH_NULL_MUTEX
> *ts_obj
= 0;
99 ts_obj
= this->ts_allocator_
.ts_object ();
101 // Don't need double-check locking since this value is
102 // obtained from a thread specific context.
105 ACE_NEW_RETURN (ts_obj
,
106 JAWS_CACHED_ALLOCATOR(T
) (this->n_chunks_
),
108 this->ts_allocator_
.ts_object (ts_obj
);
115 JAWS_TSS_Cached_Allocator
<T
>::JAWS_TSS_Cached_Allocator (size_t n_chunks
)
116 : n_chunks_ (n_chunks
)
121 JAWS_TSS_Cached_Allocator
<T
>::~JAWS_TSS_Cached_Allocator ()
125 template <class T
> void *
126 JAWS_TSS_Cached_Allocator
<T
>::malloc (size_t nbytes
)
128 return this->ts_allocator ()->malloc (nbytes
);
131 template <class T
> void
132 JAWS_TSS_Cached_Allocator
<T
>::free (void *ptr
)
134 this->ts_allocator ()->free (ptr
);
138 #endif /* JAWS_CACHED_ALLOCATOR_T_CPP */