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 <cstdint>
19
20#include <gtest/gtest.h>
21
22#include "arrow/memory_pool-test.h"
23#include "arrow/memory_pool.h"
24#include "arrow/status.h"
25#include "arrow/test-util.h"
26
27namespace arrow {
28
29class TestDefaultMemoryPool : public ::arrow::TestMemoryPoolBase {
30 public:
31 ::arrow::MemoryPool* memory_pool() override { return ::arrow::default_memory_pool(); }
32};
33
34TEST_F(TestDefaultMemoryPool, MemoryTracking) { this->TestMemoryTracking(); }
35
36TEST_F(TestDefaultMemoryPool, OOM) {
37#ifndef ADDRESS_SANITIZER
38 this->TestOOM();
39#endif
40}
41
42TEST_F(TestDefaultMemoryPool, Reallocate) { this->TestReallocate(); }
43
44// Death tests and valgrind are known to not play well 100% of the time. See
45// googletest documentation
46#if !(defined(ARROW_VALGRIND) || defined(ADDRESS_SANITIZER))
47
48TEST(DefaultMemoryPoolDeathTest, MaxMemory) {
49 MemoryPool* pool = default_memory_pool();
50 uint8_t* data1;
51 uint8_t* data2;
52
53 ASSERT_OK(pool->Allocate(100, &data1));
54 ASSERT_OK(pool->Allocate(50, &data2));
55 pool->Free(data2, 50);
56 ASSERT_OK(pool->Allocate(100, &data2));
57 pool->Free(data1, 100);
58 pool->Free(data2, 100);
59
60 ASSERT_EQ(200, pool->max_memory());
61}
62
63#endif // ARROW_VALGRIND
64
65TEST(LoggingMemoryPool, Logging) {
66 MemoryPool* pool = default_memory_pool();
67
68 LoggingMemoryPool lp(pool);
69
70 uint8_t* data;
71 ASSERT_OK(pool->Allocate(100, &data));
72
73 uint8_t* data2;
74 ASSERT_OK(pool->Allocate(100, &data2));
75
76 pool->Free(data, 100);
77 pool->Free(data2, 100);
78
79 ASSERT_EQ(200, pool->max_memory());
80}
81
82TEST(ProxyMemoryPool, Logging) {
83 MemoryPool* pool = default_memory_pool();
84
85 ProxyMemoryPool pp(pool);
86
87 uint8_t* data;
88 ASSERT_OK(pool->Allocate(100, &data));
89
90 uint8_t* data2;
91 ASSERT_OK(pp.Allocate(300, &data2));
92
93 ASSERT_EQ(400, pool->bytes_allocated());
94 ASSERT_EQ(300, pp.bytes_allocated());
95
96 pool->Free(data, 100);
97 pp.Free(data2, 300);
98
99 ASSERT_EQ(0, pool->bytes_allocated());
100 ASSERT_EQ(0, pp.bytes_allocated());
101}
102} // namespace arrow
103