xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/r600/sfn/sfn_memorypool.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /* -*- mesa-c++  -*-
2  * Copyright 2022 Collabora LTD
3  * Author: Gert Wollny <[email protected]>
4  * SPDX-License-Identifier: MIT
5  */
6 
7 #ifndef MEMORYPOOL_H
8 #define MEMORYPOOL_H
9 
10 #include <cstdlib>
11 #include <memory>
12 #include <stack>
13 
14 #define R600_POINTER_TYPE(X) X *
15 
16 namespace r600 {
17 
18 void
19 init_pool();
20 void
21 release_pool();
22 
23 class Allocate {
24 public:
25    void *operator new(size_t size);
26    void operator delete(void *p, size_t size);
27 };
28 
29 class MemoryPool {
30 public:
31    static MemoryPool& instance();
32    static void release_all();
33 
34    void free();
35    void initialize();
36 
37    void *allocate(size_t size);
38    void *allocate(size_t size, size_t align);
39 
40 private:
41    MemoryPool() noexcept;
42 
43    struct MemoryPoolImpl *impl;
44 };
45 
46 template <typename T> struct Allocator {
47    using value_type = T;
48 
49    Allocator() = default;
50    Allocator(const Allocator& other) = default;
51 
AllocatorAllocator52    template <typename U> Allocator(const Allocator<U>& other) { (void)other; }
53 
allocateAllocator54    T *allocate(size_t n)
55    {
56       return (T *)MemoryPool::instance().allocate(n * sizeof(T), alignof(T));
57    }
58 
deallocateAllocator59    void deallocate(void *p, size_t n)
60    {
61       (void)p;
62       (void)n;
63       // MemoryPool::instance().deallocate(p, n * sizeof(T), alignof(T));
64    }
65 
66    friend bool operator==(const Allocator<T>& lhs, const Allocator<T>& rhs)
67    {
68       (void)lhs;
69       (void)rhs;
70       return true;
71    }
72 };
73 
74 } // namespace r600
75 
76 #endif // MEMORYPOOL_H
77