xref: /aosp_15_r20/external/skia/src/sksl/SkSLMemoryPool.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 Google LLC
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SKSL_MEMORYPOOL
9 #define SKSL_MEMORYPOOL
10 
11 #include <memory>
12 
13 #include "include/core/SkTypes.h"
14 #include "src/base/SkArenaAlloc.h"
15 
16 namespace SkSL {
17 
18 class MemoryPool {
19 public:
Make()20     static std::unique_ptr<MemoryPool> Make() {
21         return std::make_unique<MemoryPool>();
22     }
allocate(size_t size)23     void* allocate(size_t size) {
24         return fArena.makeBytesAlignedTo(size, kAlignment);
25     }
release(void *)26     void release(void*) {
27         // SkArenaAlloc does not ever attempt to reclaim space.
28     }
29 
30 private:
31 #ifdef SK_FORCE_8_BYTE_ALIGNMENT
32     // https://github.com/emscripten-core/emscripten/issues/10072
33     // Since Skia does not use "long double" (16 bytes), we should be ok to force it back to 8 bytes
34     // until emscripten is fixed.
35     static constexpr size_t kAlignment = 8;
36 #else
37     // Guaranteed alignment of pointer returned by allocate().
38     static constexpr size_t kAlignment = alignof(std::max_align_t);
39 #endif
40 
41     SkSTArenaAlloc<65536> fArena{/*firstHeapAllocation=*/32768};
42 };
43 
44 }  // namespace SkSL
45 
46 #endif // SKSL_MEMORYPOOL
47