]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership. The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, | |
12 | // software distributed under the License is distributed on an | |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | // KIND, either express or implied. See the License for the | |
15 | // specific language governing permissions and limitations | |
16 | // under the License. | |
17 | ||
18 | #pragma once | |
19 | ||
20 | #include <algorithm> | |
21 | #include <cstddef> | |
22 | #include <cstdint> | |
23 | #include <limits> | |
24 | ||
25 | #include <gtest/gtest.h> | |
26 | ||
27 | #include "arrow/memory_pool.h" | |
28 | #include "arrow/status.h" | |
29 | #include "arrow/testing/gtest_util.h" | |
30 | ||
31 | namespace arrow { | |
32 | ||
33 | class TestMemoryPoolBase : public ::testing::Test { | |
34 | public: | |
35 | virtual ::arrow::MemoryPool* memory_pool() = 0; | |
36 | ||
37 | void TestMemoryTracking() { | |
38 | auto pool = memory_pool(); | |
39 | ||
40 | uint8_t* data; | |
41 | ASSERT_OK(pool->Allocate(100, &data)); | |
42 | EXPECT_EQ(static_cast<uint64_t>(0), reinterpret_cast<uint64_t>(data) % 64); | |
43 | ASSERT_EQ(100, pool->bytes_allocated()); | |
44 | ||
45 | uint8_t* data2; | |
46 | ASSERT_OK(pool->Allocate(27, &data2)); | |
47 | EXPECT_EQ(static_cast<uint64_t>(0), reinterpret_cast<uint64_t>(data2) % 64); | |
48 | ASSERT_EQ(127, pool->bytes_allocated()); | |
49 | ||
50 | pool->Free(data, 100); | |
51 | ASSERT_EQ(27, pool->bytes_allocated()); | |
52 | pool->Free(data2, 27); | |
53 | ASSERT_EQ(0, pool->bytes_allocated()); | |
54 | } | |
55 | ||
56 | void TestOOM() { | |
57 | auto pool = memory_pool(); | |
58 | ||
59 | uint8_t* data; | |
60 | int64_t to_alloc = std::min<uint64_t>(std::numeric_limits<int64_t>::max(), | |
61 | std::numeric_limits<size_t>::max()); | |
62 | // subtract 63 to prevent overflow after the size is aligned | |
63 | to_alloc -= 63; | |
64 | ASSERT_RAISES(OutOfMemory, pool->Allocate(to_alloc, &data)); | |
65 | } | |
66 | ||
67 | void TestReallocate() { | |
68 | auto pool = memory_pool(); | |
69 | ||
70 | uint8_t* data; | |
71 | ASSERT_OK(pool->Allocate(10, &data)); | |
72 | ASSERT_EQ(10, pool->bytes_allocated()); | |
73 | data[0] = 35; | |
74 | data[9] = 12; | |
75 | ||
76 | // Expand | |
77 | ASSERT_OK(pool->Reallocate(10, 20, &data)); | |
78 | ASSERT_EQ(data[9], 12); | |
79 | ASSERT_EQ(20, pool->bytes_allocated()); | |
80 | ||
81 | // Shrink | |
82 | ASSERT_OK(pool->Reallocate(20, 5, &data)); | |
83 | ASSERT_EQ(data[0], 35); | |
84 | ASSERT_EQ(5, pool->bytes_allocated()); | |
85 | ||
86 | // Free | |
87 | pool->Free(data, 5); | |
88 | ASSERT_EQ(0, pool->bytes_allocated()); | |
89 | } | |
90 | }; | |
91 | ||
92 | } // namespace arrow |