|
| 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 "gtest/gtest.h" |
| 19 | + |
| 20 | +#include "arrow/memory_pool.h" |
| 21 | +#include "arrow/test-util.h" |
| 22 | + |
| 23 | +namespace arrow { |
| 24 | + |
| 25 | +namespace test { |
| 26 | + |
| 27 | +class TestMemoryPoolBase : public ::testing::Test { |
| 28 | + public: |
| 29 | + virtual ::arrow::MemoryPool* memory_pool() = 0; |
| 30 | + |
| 31 | + void TestMemoryTracking() { |
| 32 | + auto pool = memory_pool(); |
| 33 | + |
| 34 | + uint8_t* data; |
| 35 | + ASSERT_OK(pool->Allocate(100, &data)); |
| 36 | + EXPECT_EQ(static_cast<uint64_t>(0), reinterpret_cast<uint64_t>(data) % 64); |
| 37 | + ASSERT_EQ(100, pool->bytes_allocated()); |
| 38 | + |
| 39 | + pool->Free(data, 100); |
| 40 | + ASSERT_EQ(0, pool->bytes_allocated()); |
| 41 | + } |
| 42 | + |
| 43 | + void TestOOM() { |
| 44 | + auto pool = memory_pool(); |
| 45 | + |
| 46 | + uint8_t* data; |
| 47 | + int64_t to_alloc = std::numeric_limits<int64_t>::max(); |
| 48 | + ASSERT_RAISES(OutOfMemory, pool->Allocate(to_alloc, &data)); |
| 49 | + } |
| 50 | + |
| 51 | + void TestReallocate() { |
| 52 | + auto pool = memory_pool(); |
| 53 | + |
| 54 | + uint8_t* data; |
| 55 | + ASSERT_OK(pool->Allocate(10, &data)); |
| 56 | + ASSERT_EQ(10, pool->bytes_allocated()); |
| 57 | + data[0] = 35; |
| 58 | + data[9] = 12; |
| 59 | + |
| 60 | + // Expand |
| 61 | + ASSERT_OK(pool->Reallocate(10, 20, &data)); |
| 62 | + ASSERT_EQ(data[9], 12); |
| 63 | + ASSERT_EQ(20, pool->bytes_allocated()); |
| 64 | + |
| 65 | + // Shrink |
| 66 | + ASSERT_OK(pool->Reallocate(20, 5, &data)); |
| 67 | + ASSERT_EQ(data[0], 35); |
| 68 | + ASSERT_EQ(5, pool->bytes_allocated()); |
| 69 | + |
| 70 | + // Free |
| 71 | + pool->Free(data, 5); |
| 72 | + ASSERT_EQ(0, pool->bytes_allocated()); |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +} // namespace test |
| 77 | +} // namespace arrow |
0 commit comments