1+ // ////////////////////////////////////////////////////////////////////////////////////////
2+ // A cross platform socket APIs, support ios & android & wp8 & window store
3+ // universal app
4+ // ////////////////////////////////////////////////////////////////////////////////////////
5+ /*
6+ The MIT License (MIT)
7+
8+ Copyright (c) 2012-2019 halx99
9+
10+ Permission is hereby granted, free of charge, to any person obtaining a copy
11+ of this software and associated documentation files (the "Software"), to deal
12+ in the Software without restriction, including without limitation the rights
13+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14+ copies of the Software, and to permit persons to whom the Software is
15+ furnished to do so, subject to the following conditions:
16+
17+ The above copyright notice and this permission notice shall be included in all
18+ copies or substantial portions of the Software.
19+
20+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+ SOFTWARE.
27+ */
128// object_pool.cpp: a simple & high-performance object pool implementation v1.3
229
3- #if !defined( YASIO__OBJECT_POOL_CPP)
4- # define YASIO__OBJECT_POOL_CPP
30+ #ifndef YASIO__OBJECT_POOL_CPP
31+ #define YASIO__OBJECT_POOL_CPP
532
6- # if !defined(YASIO_OBJECT_POOL_HEADER_ONLY)
7- # include " object_pool.h"
8- # endif
33+ #if !defined(YASIO_OBJECT_POOL_HEADER_ONLY)
34+ # include " object_pool.h"
35+ #endif
936
10- # define OBJECT_POOL_PREALLOCATE 1
37+ #define OBJECT_POOL_PREALLOCATE 1
1138
1239namespace yasio
1340{
@@ -17,19 +44,19 @@ namespace gc
1744namespace detail
1845{
1946
20- # define POOL_FL_BEGIN (chunk ) reinterpret_cast <free_link_node *>(chunk->data)
47+ #define POOL_FL_BEGIN (chunk ) reinterpret_cast <free_link_node *>(chunk->data)
2148
2249object_pool::object_pool (size_t element_size, size_t element_count)
2350 : free_link_(nullptr ), chunk_(nullptr ), element_size_(element_size),
2451 element_count_ (element_count)
25- # ifdef _DEBUG
52+ #ifdef _DEBUG
2653 ,
2754 allocated_count_ (0 )
28- # endif
55+ #endif
2956{
30- # if OBJECT_POOL_PREALLOCATE
57+ #if OBJECT_POOL_PREALLOCATE
3158 release (allocate_from_process_heap ()); // preallocate 1 chunk
32- # endif
59+ #endif
3360}
3461
3562object_pool::~object_pool (void ) { this ->purge (); }
@@ -55,9 +82,9 @@ void object_pool::cleanup(void)
5582
5683 this ->free_link_ = POOL_FL_BEGIN (this ->chunk_ );
5784
58- # if defined(_DEBUG)
85+ #if defined(_DEBUG)
5986 this ->allocated_count_ = 0 ;
60- # endif
87+ #endif
6188}
6289
6390void object_pool::purge (void )
@@ -74,9 +101,9 @@ void object_pool::purge(void)
74101
75102 free_link_ = nullptr ;
76103
77- # if defined(_DEBUG)
104+ #if defined(_DEBUG)
78105 allocated_count_ = 0 ;
79- # endif
106+ #endif
80107}
81108
82109void *object_pool::get (void )
@@ -95,18 +122,18 @@ void object_pool::release(void *_Ptr)
95122 ptr->next = this ->free_link_ ;
96123 this ->free_link_ = ptr;
97124
98- # if defined(_DEBUG)
125+ #if defined(_DEBUG)
99126 --this ->allocated_count_ ;
100- # endif
127+ #endif
101128}
102129
103130void *object_pool::allocate_from_process_heap (void )
104131{
105132 chunk_link new_chunk =
106133 (chunk_link)malloc (sizeof (chunk_link_node) + element_size_ * element_count_);
107- # ifdef _DEBUG
134+ #ifdef _DEBUG
108135 ::memset (new_chunk, 0x00 , sizeof (chunk_link_node));
109- # endif
136+ #endif
110137 tidy_chunk (new_chunk)->next = nullptr ;
111138
112139 // link the new_chunk
@@ -117,19 +144,19 @@ void *object_pool::allocate_from_process_heap(void)
117144 auto ptr = POOL_FL_BEGIN (new_chunk);
118145 this ->free_link_ = ptr->next ;
119146
120- # if defined(_DEBUG)
147+ #if defined(_DEBUG)
121148 ++this ->allocated_count_ ;
122- # endif
149+ #endif
123150 return reinterpret_cast <void *>(ptr);
124151}
125152
126153void *object_pool::allocate_from_chunk (void )
127154{
128155 free_link_node *ptr = this ->free_link_ ;
129156 this ->free_link_ = ptr->next ;
130- # if defined(_DEBUG)
157+ #if defined(_DEBUG)
131158 ++this ->allocated_count_ ;
132- # endif
159+ #endif
133160 return reinterpret_cast <void *>(ptr);
134161}
135162
@@ -151,8 +178,3 @@ object_pool::free_link_node *object_pool::tidy_chunk(chunk_link chunk)
151178} // namespace yasio
152179
153180#endif // YASIO__OBJECT_POOL_CPP
154- /*
155- * Copyright (c) 2012-2019 by HALX99, ALL RIGHTS RESERVED.
156- * Consult your license regarding permissions and restrictions.
157- * V1.3:2019
158- **/
0 commit comments