aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/test/memory_allocator_observer.cpp
blob: ff12cc745a1098db97ee4ac49bee6dad9160a9b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "memory_allocator_observer.h"
#include <iostream>

namespace vespalib::alloc::test {

std::ostream&
operator<<(std::ostream &os, const MemoryAllocatorObserver::Stats &stats)
{
    os << "{alloc_cnt=" << stats.alloc_cnt << ", free_cnt=" << stats.free_cnt << "}";
    return os;
}

MemoryAllocatorObserver::MemoryAllocatorObserver(Stats &stats)
    : MemoryAllocator(),
      _stats(stats),
      _backing_allocator(alloc::MemoryAllocator::select_allocator(HUGEPAGE_SIZE, 0))
{
}
MemoryAllocatorObserver::~MemoryAllocatorObserver() = default;

PtrAndSize
MemoryAllocatorObserver::alloc(size_t sz) const
{
    ++_stats.alloc_cnt;
    return _backing_allocator->alloc(sz);
}


void
MemoryAllocatorObserver::free(PtrAndSize alloc) const noexcept
{
    ++_stats.free_cnt;
    _backing_allocator->free(alloc);
}

size_t
MemoryAllocatorObserver::resize_inplace(PtrAndSize current, size_t newSize) const
{
    return _backing_allocator->resize_inplace(current, newSize);
}

}