1 /*
2  * Copyright (c) 2020 Andrew Kelley
3  *
4  * This file is part of zig, which is MIT licensed.
5  * See http://opensource.org/licenses/MIT
6  */
7 
8 #ifndef ZIG_HEAP_HPP
9 #define ZIG_HEAP_HPP
10 
11 #include "util_base.hpp"
12 #include "mem.hpp"
13 
14 namespace heap {
15 
16 struct BootstrapAllocator final : mem::Allocator {
17     void init(const char *name);
18     void deinit();
destructheap::BootstrapAllocator19     void destruct(Allocator *allocator) {}
20 
21 private:
22     ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate(const mem::TypeInfo &info, size_t count) final;
23     ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) final;
24     void *internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
25     void *internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
26     void internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) final;
27 };
28 
29 struct CAllocator final : mem::Allocator {
30     void init(const char *name);
31     void deinit();
32 
33     static CAllocator *construct(mem::Allocator *allocator, const char *name);
34     void destruct(mem::Allocator *allocator) final;
35 
36 
37 private:
38     ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate(const mem::TypeInfo &info, size_t count) final;
39     ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) final;
40     void *internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
41     void *internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
42     void internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) final;
43 
44 };
45 
46 //
47 // arena allocator
48 //
49 // - allocations are backed by the underlying allocator's memory
50 // - allocations are N:1 relationship to underlying allocations
51 // - dellocations are noops
52 // - deinit() releases all underlying memory
53 //
54 struct ArenaAllocator final : mem::Allocator {
55     void init(Allocator *backing, const char *name);
56     void deinit();
57 
58     static ArenaAllocator *construct(mem::Allocator *allocator, mem::Allocator *backing, const char *name);
59     void destruct(mem::Allocator *allocator) final;
60 
61 
62 private:
63     ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate(const mem::TypeInfo &info, size_t count) final;
64     ATTRIBUTE_RETURNS_NOALIAS void *internal_allocate_nonzero(const mem::TypeInfo &info, size_t count) final;
65     void *internal_reallocate(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
66     void *internal_reallocate_nonzero(const mem::TypeInfo &info, void *old_ptr, size_t old_count, size_t new_count) final;
67     void internal_deallocate(const mem::TypeInfo &info, void *ptr, size_t count) final;
68 
69     struct Impl;
70     Impl *impl;
71 };
72 
73 extern BootstrapAllocator bootstrap_allocator_state;
74 extern mem::Allocator &bootstrap_allocator;
75 
76 extern CAllocator c_allocator_state;
77 extern mem::Allocator &c_allocator;
78 
79 } // namespace heap
80 
81 #endif
82