1 /* Copyright (C) 2011 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 /*
24 * arena allocator (variable-size blocks, no deallocation).
25 */
26
27 #include "precompiled.h"
28 #include "lib/allocators/arena.h"
29
30 namespace Allocators {
31
32 template<class Storage>
33 struct BasicArenaTest
34 {
operator ()Allocators::BasicArenaTest35 void operator()() const
36 {
37 Arena<Storage> a(100);
38 const size_t initialSpace = a.RemainingBytes();
39 void* p = a.allocate(100);
40 ENSURE(p != 0);
41 ENSURE(a.Contains(uintptr_t(p)));
42 ENSURE(a.RemainingBytes() == initialSpace-100);
43 ENSURE(a.Contains(uintptr_t(p)+1));
44 ENSURE(a.Contains(uintptr_t(p)+99));
45 ENSURE(!a.Contains(uintptr_t(p)-1));
46 ENSURE(!a.Contains(uintptr_t(p)+100));
47 if(a.RemainingBytes() == 0)
48 ENSURE(a.allocate(1) == 0); // full
49 else
50 ENSURE(a.allocate(1) != 0); // can still expand
51 a.DeallocateAll();
52 ENSURE(!a.Contains(uintptr_t(p)));
53
54 p = a.allocate(36);
55 ENSURE(p != 0);
56 ENSURE(a.Contains(uintptr_t(p)));
57 ENSURE(a.RemainingBytes() == initialSpace-36);
58 void* p2 = a.allocate(64);
59 ENSURE(p2 != 0);
60 ENSURE(a.Contains(uintptr_t(p2)));
61 ENSURE(a.RemainingBytes() == initialSpace-36-64);
62 ENSURE(p2 == (void*)(uintptr_t(p)+36));
63 if(a.RemainingBytes() == 0)
64 ENSURE(a.allocate(1) == 0); // full
65 else
66 ENSURE(a.allocate(1) != 0); // can still expand
67 }
68 };
69
TestArena()70 void TestArena()
71 {
72 ForEachStorage<BasicArenaTest>();
73 }
74
75 } // namespace Allocators
76