1 // Copyright 2015 The Gemmlowp Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "test.h"
16 #include "../internal/allocator.h"
17
18 namespace gemmlowp {
19
test_allocator(Allocator * a,int max_array_size)20 void test_allocator(Allocator* a, int max_array_size) {
21 const std::size_t int32_array_size = Random() % max_array_size;
22 auto handle_to_int32_array = a->Reserve<std::int32_t>(int32_array_size);
23 const std::size_t int8_array_size = Random() % max_array_size;
24 auto handle_to_int8_array = a->Reserve<std::int8_t>(int8_array_size);
25 a->Commit();
26 std::int32_t* int32_array =
27 a->GetPointer<std::int32_t>(handle_to_int32_array);
28 std::int8_t* int8_array = a->GetPointer<std::int8_t>(handle_to_int8_array);
29 Check(int32_array == a->GetPointer<std::int32_t>(handle_to_int32_array));
30 Check(int8_array == a->GetPointer<std::int8_t>(handle_to_int8_array));
31 Check(
32 !(reinterpret_cast<std::uintptr_t>(int32_array) % Allocator::kAlignment));
33 Check(
34 !(reinterpret_cast<std::uintptr_t>(int8_array) % Allocator::kAlignment));
35 Check(reinterpret_cast<std::uintptr_t>(int8_array) >=
36 reinterpret_cast<std::uintptr_t>(int32_array + int32_array_size));
37 memset(int32_array, 0, sizeof(*int32_array) * int32_array_size);
38 memset(int8_array, 0, sizeof(*int8_array) * int8_array_size);
39 a->Decommit();
40 }
41
test_allocator()42 void test_allocator() {
43 Allocator allocator;
44
45 // Test allocating increasingly large sizes on the same allocator,
46 // starting with size 0.
47 for (int i = 1; i < 1000; i += 10) {
48 test_allocator(&allocator, i);
49 }
50 }
51
52 } // namespace gemmlowp
53
main()54 int main() { gemmlowp::test_allocator(); }
55