Pyrogenesis  trunk
dynarray.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 Wildfire Games.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * dynamic (expandable) array
25  */
26 
27 #ifndef INCLUDED_ALLOCATORS_DYNARRAY
28 #define INCLUDED_ALLOCATORS_DYNARRAY
29 
30 #include "lib/posix/posix_mman.h" // PROT_*
31 
32 /**
33  * provides a memory range that can be expanded but doesn't waste
34  * physical memory or relocate itself.
35  *
36  * works by preallocating address space and committing as needed.
37  * used as a building block for other allocators.
38  **/
39 struct DynArray
40 {
41  u8* base;
42  size_t max_size_pa; /// reserved
43  size_t cur_size; /// committed
44  size_t cur_size_pa;
45 
46  size_t pos;
47 };
48 
49 
50 /**
51  * ready the DynArray object for use.
52  *
53  * no virtual memory is actually committed until calls to da_set_size.
54  *
55  * @param da DynArray.
56  * @param max_size size [bytes] of address space to reserve (*);
57  * the DynArray can never expand beyond this.
58  * (* rounded up to next page size multiple)
59  * @return Status.
60  **/
61 Status da_alloc(DynArray* da, size_t max_size);
62 
63 /**
64  * free all memory (address space + physical) that constitutes the
65  * given array.
66  *
67  * use-after-free is impossible because the memory is unmapped.
68  *
69  * @param da DynArray* zeroed afterwards.
70  * @return Status
71  **/
73 
74 /**
75  * expand or shrink the array: changes the amount of currently committed
76  * (i.e. usable) memory pages.
77  *
78  * @param da DynArray.
79  * @param new_size target size (rounded up to next page multiple).
80  * pages are added/removed until this is met.
81  * @return Status.
82  **/
83 Status da_set_size(DynArray* da, size_t new_size);
84 
85 /**
86  * Make sure at least <size> bytes starting at da->pos are committed and
87  * ready for use.
88  *
89  * @param da DynArray*
90  * @param size Minimum amount to guarantee [bytes]
91  * @return Status
92  **/
93 Status da_reserve(DynArray* da, size_t size);
94 
95 /**
96  * "write" to array, i.e. copy from the given buffer.
97  *
98  * starts at offset DynArray.pos and advances this.
99  *
100  * @param da DynArray.
101  * @param data_src source memory
102  * @param size [bytes] to copy
103  * @return Status.
104  **/
105 Status da_append(DynArray* da, const void* data_src, size_t size);
106 
107 #endif // #ifndef INCLUDED_ALLOCATORS_DYNARRAY
Status da_append(DynArray *da, const void *data_src, size_t size)
"write" to array, i.e.
Definition: dynarray.cpp:141
Status da_alloc(DynArray *da, size_t max_size)
ready the DynArray object for use.
Definition: dynarray.cpp:63
provides a memory range that can be expanded but doesn&#39;t waste physical memory or relocate itself...
Definition: dynarray.h:39
Status da_reserve(DynArray *da, size_t size)
Make sure at least <size> bytes starting at da->pos are committed and ready for use.
Definition: dynarray.cpp:132
uint8_t u8
Definition: types.h:37
Status da_set_size(DynArray *da, size_t new_size)
expand or shrink the array: changes the amount of currently committed (i.e.
Definition: dynarray.cpp:95
u8 * base
Definition: dynarray.h:41
Status da_free(DynArray *da)
free all memory (address space + physical) that constitutes the given array.
Definition: dynarray.cpp:82
i64 Status
Error handling system.
Definition: status.h:169
size_t pos
Definition: dynarray.h:46
size_t cur_size_pa
committed
Definition: dynarray.h:44
size_t max_size_pa
Definition: dynarray.h:42
size_t cur_size
reserved
Definition: dynarray.h:43