File: test_fixed_sized_memory_block_allocator.cpp

package info (click to toggle)
etlcpp 20.40.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 18,548 kB
  • sloc: cpp: 257,359; ansic: 10,566; sh: 1,730; asm: 301; python: 281; makefile: 24
file content (289 lines) | stat: -rw-r--r-- 11,984 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com

Copyright(c) 2020 John Wellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#include "unit_test_framework.h"

#include "etl/fixed_sized_memory_block_allocator.h"

namespace
{
  using Allocator8  = etl::fixed_sized_memory_block_allocator<sizeof(int8_t),  alignof(int8_t),  4>;
  using Allocator16 = etl::fixed_sized_memory_block_allocator<sizeof(int16_t), alignof(int16_t), 4>;
  using Allocator32 = etl::fixed_sized_memory_block_allocator<sizeof(int32_t), alignof(int32_t), 4>;

  class CustomAllocator8 : public Allocator8
  {
  public:

    bool allocate_block_called = false;
    bool release_block_called  = false;
    mutable bool is_owner_of_block_called = false;

  protected:

    //*************************************************************************
    /// The overridden virtual function to allocate a block.
    //*************************************************************************
    void* allocate_block(size_t required_size, size_t required_alignment) override
    {
      allocate_block_called = true;

      return Allocator8::allocate_block(required_size, required_alignment);
    }

    //*************************************************************************
    /// The overridden virtual function to release a block.
    //*************************************************************************
    bool release_block(const void* const pblock) override
    {
      release_block_called = true;

      return Allocator8::release_block(pblock);
    }

    //*************************************************************************
    /// Returns true if the allocator is the owner of the block.
    //*************************************************************************
    bool is_owner_of_block(const void* const pblock) const override
    {
      is_owner_of_block_called = true;

      return Allocator8::is_owner_of_block(pblock);
    }
  };

  SUITE(test_fixed_sized_memory_block_allocator)
  {
    //*************************************************************************
    TEST(test_allocator_no_successor_use_all_allocation)
    {
      Allocator16 allocator16;

      int16_t* p1 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(int16_t)));
      int16_t* p2 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(int16_t)));
      int16_t* p3 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(int16_t)));
      int16_t* p4 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(int16_t)));
      int16_t* p5 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(int16_t)));

      CHECK(p1 != nullptr);
      CHECK(p2 != nullptr);
      CHECK(p3 != nullptr);
      CHECK(p4 != nullptr);
      CHECK(p5 == nullptr);

      CHECK(allocator16.release(p1));
      CHECK(allocator16.release(p2));
      CHECK(allocator16.release(p3));
      CHECK(allocator16.release(p4));
      CHECK(!allocator16.release(p5));
    }

    //*************************************************************************
    TEST(test_allocator_has_successors)
    {
      Allocator16 allocator16;
      Allocator16 allocator16s;
      Allocator16 allocator16ss;

      allocator16.set_successor(allocator16s);
      allocator16s.set_successor(allocator16ss);

      CHECK(allocator16.has_successor()   == true);
      CHECK(allocator16s.has_successor()  == true);
      CHECK(allocator16ss.has_successor() == false);
    }

    //*************************************************************************
    TEST(test_allocator_with_successors_use_all_allocation)
    {          
      Allocator16 allocator16;
      Allocator16 allocator16s;
      Allocator16 allocator16ss;
      
      allocator16.set_successor(allocator16s);
      allocator16s.set_successor(allocator16ss);

      int16_t* p1 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p2 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p3 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p4 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p5 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p6 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p7 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p8 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p9 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p10 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p11 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p12 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));
      int16_t* p13 = static_cast<int16_t*>(allocator16.allocate(sizeof(int16_t), alignof(uint16_t)));

      CHECK(p1  != nullptr);
      CHECK(p2  != nullptr);
      CHECK(p3  != nullptr);
      CHECK(p4  != nullptr);
      CHECK(p5  != nullptr);
      CHECK(p6  != nullptr);
      CHECK(p7  != nullptr);
      CHECK(p8  != nullptr);
      CHECK(p9  != nullptr);
      CHECK(p10 != nullptr);
      CHECK(p11 != nullptr);
      CHECK(p12 != nullptr);
      CHECK(p13 == nullptr);

      CHECK(allocator16.release(p1));
      CHECK(allocator16.release(p2));
      CHECK(allocator16.release(p3));
      CHECK(allocator16.release(p4));
      CHECK(allocator16.release(p5));
      CHECK(allocator16.release(p6));
      CHECK(allocator16.release(p7));
      CHECK(allocator16.release(p8));
      CHECK(allocator16.release(p9));
      CHECK(allocator16.release(p10));
      CHECK(allocator16.release(p11));
      CHECK(allocator16.release(p12));
      CHECK(!allocator16.release(p13));
    }

    //*************************************************************************
    TEST(test_allocator_with_different_block_sized_successors)
    {
      Allocator8  allocator8;
      Allocator16 allocator16;
      Allocator32 allocator32;

      allocator8.set_successor(allocator16, allocator32);

      int8_t*  p1  = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t),   alignof(uint8_t)));  // Take from allocator8
      int16_t* p2  = static_cast<int16_t*>(allocator8.allocate(sizeof(int16_t), alignof(uint16_t))); // Take from allocator16
      int32_t* p3  = static_cast<int32_t*>(allocator8.allocate(sizeof(int32_t), alignof(uint32_t))); // Take from allocator32
      int64_t* p4  = static_cast<int64_t*>(allocator8.allocate(sizeof(int64_t), alignof(uint64_t))); // Unable to allocate
      int8_t*  p5  = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t),   alignof(uint8_t)));  // Take from allocator8
      int8_t*  p6  = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t),   alignof(uint8_t)));  // Take from allocator8
      int8_t*  p7  = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t),   alignof(uint8_t)));  // Take from allocator8. allocator8 is now full.
      int8_t*  p8  = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t),   alignof(uint8_t)));  // Take from allocator16
      int8_t*  p9  = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t),   alignof(uint8_t)));  // Take from allocator16
      int8_t*  p10 = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t),   alignof(uint8_t)));  // Take from allocator16. allocator16 now is full.
      int8_t*  p11 = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t),   alignof(uint8_t)));  // Take from allocator32

      CHECK(p1  != nullptr);
      CHECK(p2  != nullptr);
      CHECK(p3  != nullptr);
      CHECK(p4  == nullptr);
      CHECK(p5  != nullptr);
      CHECK(p6  != nullptr);
      CHECK(p7  != nullptr);
      CHECK(p8  != nullptr);
      CHECK(p9  != nullptr);
      CHECK(p10 != nullptr);
      CHECK(p11 != nullptr);

      // Take from allocator8
      CHECK(allocator8.is_owner_of(p1));
      CHECK(!allocator16.is_owner_of(p1));
      CHECK(!allocator32.is_owner_of(p1));

      // Take from allocator16
      CHECK(allocator8.is_owner_of(p2));
      CHECK(allocator16.is_owner_of(p2));
      CHECK(!allocator32.is_owner_of(p2));

      // Take from allocator32
      CHECK(allocator8.is_owner_of(p3));
      CHECK(allocator16.is_owner_of(p3));
      CHECK(allocator32.is_owner_of(p3));
            
      // Unable to allocate
      CHECK(!allocator8.is_owner_of(p4));
      CHECK(!allocator16.is_owner_of(p4));
      CHECK(!allocator32.is_owner_of(p4));

      // Take from allocator8
      CHECK(allocator8.is_owner_of(p5));
      CHECK(!allocator16.is_owner_of(p5));
      CHECK(!allocator32.is_owner_of(p5));

      // Take from allocator8
      CHECK(allocator8.is_owner_of(p6));
      CHECK(!allocator16.is_owner_of(p6));
      CHECK(!allocator32.is_owner_of(p6));

      // Take from allocator8.
      CHECK(allocator8.is_owner_of(p7));
      CHECK(!allocator16.is_owner_of(p7));
      CHECK(!allocator32.is_owner_of(p7));

      // Take from allocator16
      CHECK(allocator8.is_owner_of(p8));
      CHECK(allocator16.is_owner_of(p8));
      CHECK(!allocator32.is_owner_of(p8));

      // Take from allocator16
      CHECK(allocator8.is_owner_of(p9));
      CHECK(allocator16.is_owner_of(p9));
      CHECK(!allocator32.is_owner_of(p9));

      // Take from allocator16.
      CHECK(allocator8.is_owner_of(p10));
      CHECK(allocator16.is_owner_of(p10));
      CHECK(!allocator32.is_owner_of(p10));

      // Take from allocator32
      CHECK(allocator8.is_owner_of(p11));
      CHECK(allocator16.is_owner_of(p11));
      CHECK(allocator32.is_owner_of(p11));

      CHECK(allocator8.release(p1));
      CHECK(allocator8.release(p2));
      CHECK(allocator8.release(p3));
      CHECK(!allocator8.release(p4));
      CHECK(allocator8.release(p5));
      CHECK(allocator8.release(p6));
      CHECK(allocator8.release(p7));
      CHECK(allocator8.release(p8));
      CHECK(allocator8.release(p9));
      CHECK(allocator8.release(p10));
      CHECK(allocator8.release(p11));
    }

    //*************************************************************************
    TEST(test_custom_allocator)
    {
      CustomAllocator8 allocator8;

      int8_t* p1 = static_cast<int8_t*>(allocator8.allocate(sizeof(int8_t), alignof(int8_t)));
      CHECK(allocator8.allocate_block_called);
      CHECK(p1 != nullptr);
      CHECK(allocator8.is_owner_of(p1));
      CHECK(allocator8.is_owner_of_block_called);
      CHECK(allocator8.release(p1));
      CHECK(allocator8.release_block_called);
    }
  }
}