1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_object_pool.cpp
28 #include "vogl_object_pool.h"
30 VOGL_NAMESPACE_BEGIN(vogl)
36 bool object_pool_test()
38 object_pool<uint, object_pool_spinlock_locking_policy> pool;
40 bool s = pool.check();
43 uint *q = pool.alloc();
58 vogl::vector<uint *> ptrs;
60 //for (uint t = 0; t < 10000; t++, z++)
61 for (uint t = 0; t < 1000000; t++, z++)
63 printf("%u %" PRIu64 " %" PRIu64 "\n", t, (uint64_t)pool.get_total_blocks(), (uint64_t)pool.get_total_heap_bytes());
65 uint n = rm.irand(0, 1000);
66 for (uint i = 0; i < n; i++)
68 ptrs.push_back(pool.alloc());
70 s = pool.is_valid_ptr(ptrs.back(), true);
73 *ptrs.back() = ptrs.size() - 1;
79 object_pool<uint, object_pool_spinlock_locking_policy> other_pool;
81 other_pool.swap(pool);
82 s = other_pool.check();
87 pool.swap(other_pool);
88 s = other_pool.check();
93 pool.swap(other_pool);
94 s = other_pool.check();
99 pool.swap(other_pool);
100 s = other_pool.check();
105 uint d = rm.irand(0, 1100); //(z < 400) ? 100 : 1200);
106 for (uint i = 0; i < d; i++)
111 uint k = rm.irand(0, ptrs.size());
114 VOGL_ASSERT(*p == k);
116 s = pool.is_valid_ptr(p);
121 s = pool.is_valid_ptr(p, false);
124 if (k != ptrs.size() - 1)
126 ptrs[k] = ptrs.back();
136 if (!rm.irand(0, 4000))
138 for (uint i = 0; i < ptrs.size(); i++)
139 pool.destroy(ptrs[i]);
145 size_t l = pool.free_unused_blocks();
146 printf("Freed %" PRIu64 " bytes of unused blocks\n", (uint64_t)l);
151 if (!rm.irand(0, 4000))
155 for (uint i = 0; i < ptrs.size(); i++)
156 pool.destroy(ptrs[i]);
165 if (!rm.irand(0, 4000))
181 VOGL_NAMESPACE_END(vogl)