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 | #include <algorithm> |
19 | #include <cstddef> |
20 | #include <cstdint> |
21 | #include <limits> |
22 | |
23 | #include <gtest/gtest.h> |
24 | |
25 | #include "arrow/memory_pool.h" |
26 | #include "arrow/status.h" |
27 | #include "arrow/test-util.h" |
28 | |
29 | namespace arrow { |
30 | |
31 | class TestMemoryPoolBase : public ::testing::Test { |
32 | public: |
33 | virtual ::arrow::MemoryPool* memory_pool() = 0; |
34 | |
35 | void TestMemoryTracking() { |
36 | auto pool = memory_pool(); |
37 | |
38 | uint8_t* data; |
39 | ASSERT_OK(pool->Allocate(100, &data)); |
40 | EXPECT_EQ(static_cast<uint64_t>(0), reinterpret_cast<uint64_t>(data) % 64); |
41 | ASSERT_EQ(100, pool->bytes_allocated()); |
42 | |
43 | uint8_t* data2; |
44 | ASSERT_OK(pool->Allocate(27, &data2)); |
45 | EXPECT_EQ(static_cast<uint64_t>(0), reinterpret_cast<uint64_t>(data2) % 64); |
46 | ASSERT_EQ(127, pool->bytes_allocated()); |
47 | |
48 | pool->Free(data, 100); |
49 | ASSERT_EQ(27, pool->bytes_allocated()); |
50 | pool->Free(data2, 27); |
51 | ASSERT_EQ(0, pool->bytes_allocated()); |
52 | } |
53 | |
54 | void TestOOM() { |
55 | auto pool = memory_pool(); |
56 | |
57 | uint8_t* data; |
58 | int64_t to_alloc = std::min<uint64_t>(std::numeric_limits<int64_t>::max(), |
59 | std::numeric_limits<size_t>::max()); |
60 | // subtract 63 to prevent overflow after the size is aligned |
61 | to_alloc -= 63; |
62 | ASSERT_RAISES(OutOfMemory, pool->Allocate(to_alloc, &data)); |
63 | } |
64 | |
65 | void TestReallocate() { |
66 | auto pool = memory_pool(); |
67 | |
68 | uint8_t* data; |
69 | ASSERT_OK(pool->Allocate(10, &data)); |
70 | ASSERT_EQ(10, pool->bytes_allocated()); |
71 | data[0] = 35; |
72 | data[9] = 12; |
73 | |
74 | // Expand |
75 | ASSERT_OK(pool->Reallocate(10, 20, &data)); |
76 | ASSERT_EQ(data[9], 12); |
77 | ASSERT_EQ(20, pool->bytes_allocated()); |
78 | |
79 | // Shrink |
80 | ASSERT_OK(pool->Reallocate(20, 5, &data)); |
81 | ASSERT_EQ(data[0], 35); |
82 | ASSERT_EQ(5, pool->bytes_allocated()); |
83 | |
84 | // Free |
85 | pool->Free(data, 5); |
86 | ASSERT_EQ(0, pool->bytes_allocated()); |
87 | } |
88 | }; |
89 | |
90 | } // namespace arrow |
91 | |