1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef TRACKING_MEM_RES_H
10#define TRACKING_MEM_RES_H
11
12#include <cstddef>
13#include <memory_resource>
14
15class TrackingMemRes : public std::pmr::memory_resource {
16public:
17 TrackingMemRes(std::size_t* last_size, size_t* last_alignment)
18 : last_size_(last_size), last_alignment_(last_alignment) {}
19
20private:
21 std::size_t* last_size_;
22 std::size_t* last_alignment_;
23 void* do_allocate(std::size_t size, size_t alignment) override {
24 *last_size_ = size;
25 *last_alignment_ = alignment;
26
27 return std::pmr::new_delete_resource()->allocate(bytes: size, alignment: alignment);
28 }
29
30 void do_deallocate(void* ptr, std::size_t size, size_t alignment) override {
31 *last_size_ = size;
32 *last_alignment_ = alignment;
33 std::pmr::new_delete_resource()->deallocate(p: ptr, bytes: size, alignment: alignment);
34 }
35
36 bool do_is_equal(const memory_resource& ptr) const noexcept override { return &ptr == this; }
37};
38
39#endif // TRACKING_MEM_RES_H
40

source code of libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/tracking_mem_res.h