Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / apps / JAWS3 / jaws3 / Cached_Allocator_T.cpp
blobbcd714d7a8114ba59f0ea791ae0f56969341fb2c
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)
9 char *next = 0;
10 char *next_indirect = pool + (this->pool_size_);
11 ACE_OS::memcpy (&next, next_indirect, sizeof (char *));
12 return next;
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 ()
25 char *new_pool = 0;
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;
37 else
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))
46 , pool_head_ (0)
47 , pool_tail_ (0)
48 , free_list_ (ACE_PURE_FREE_LIST)
50 this->extend_pool ();
53 template <class T, class ACE_LOCK>
54 JAWS_Cached_Allocator<T, ACE_LOCK>::~JAWS_Cached_Allocator ()
56 char *curr = this->pool_head_;
58 while (curr)
60 char *next = this->get_next_pool (curr);
61 delete [] curr;
62 curr = next;
66 template <class T, class ACE_LOCK> void *
67 JAWS_Cached_Allocator<T, ACE_LOCK>::malloc (size_t nbytes)
69 if (nbytes > sizeof (T))
70 return 0;
72 ACE_Cached_Mem_Pool_Node<T> *node = 0;
73 node = this->free_list_.remove ();
75 if (node == 0)
77 this->extend_pool ();
78 node = this->free_list_.remove ();
79 // ASSERT node != 0
82 // addr() call is really not absolutely necessary because of the way
83 // ACE_Cached_Mem_Pool_Node's internal structure arranged.
84 return node->addr ();
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.
103 if (ts_obj == 0)
105 ACE_NEW_RETURN (ts_obj,
106 JAWS_CACHED_ALLOCATOR(T) (this->n_chunks_),
108 this->ts_allocator_.ts_object (ts_obj);
111 return ts_obj;
114 template <class T>
115 JAWS_TSS_Cached_Allocator<T>::JAWS_TSS_Cached_Allocator (size_t n_chunks)
116 : n_chunks_ (n_chunks)
120 template <class T>
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 */