Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
dynarray.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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/status.h"
31
32#include <cstddef>
33
34/**
35 * provides a memory range that can be expanded but doesn't waste
36 * physical memory or relocate itself.
37 *
38 * works by preallocating address space and committing as needed.
39 * used as a building block for other allocators.
40 **/
42{
43 std::uint8_t* base;
44 size_t max_size_pa; /// reserved
45 size_t cur_size; /// committed
47
48 size_t pos;
49};
50
51
52/**
53 * ready the DynArray object for use.
54 *
55 * no virtual memory is actually committed until calls to da_set_size.
56 *
57 * @param da DynArray.
58 * @param max_size size [bytes] of address space to reserve (*);
59 * the DynArray can never expand beyond this.
60 * (* rounded up to next page size multiple)
61 * @return Status.
62 **/
63Status da_alloc(DynArray* da, size_t max_size);
64
65/**
66 * free all memory (address space + physical) that constitutes the
67 * given array.
68 *
69 * use-after-free is impossible because the memory is unmapped.
70 *
71 * @param da DynArray* zeroed afterwards.
72 * @return Status
73 **/
75
76/**
77 * expand or shrink the array: changes the amount of currently committed
78 * (i.e. usable) memory pages.
79 *
80 * @param da DynArray.
81 * @param new_size target size (rounded up to next page multiple).
82 * pages are added/removed until this is met.
83 * @return Status.
84 **/
85Status da_set_size(DynArray* da, size_t new_size);
86
87/**
88 * Make sure at least <size> bytes starting at da->pos are committed and
89 * ready for use.
90 *
91 * @param da DynArray*
92 * @param size Minimum amount to guarantee [bytes]
93 * @return Status
94 **/
95Status da_reserve(DynArray* da, size_t size);
96
97/**
98 * "write" to array, i.e. copy from the given buffer.
99 *
100 * starts at offset DynArray.pos and advances this.
101 *
102 * @param da DynArray.
103 * @param data_src source memory
104 * @param size [bytes] to copy
105 * @return Status.
106 **/
107Status da_append(DynArray* da, const void* data_src, size_t size);
108
109#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:145
Status da_alloc(DynArray *da, size_t max_size)
ready the DynArray object for use.
Definition: dynarray.cpp:67
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:99
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:136
Status da_free(DynArray *da)
free all memory (address space + physical) that constitutes the given array.
Definition: dynarray.cpp:86
std::int64_t Status
Error handling system.
Definition: status.h:174
provides a memory range that can be expanded but doesn't waste physical memory or relocate itself.
Definition: dynarray.h:42
size_t cur_size_pa
committed
Definition: dynarray.h:46
size_t max_size_pa
Definition: dynarray.h:44
size_t pos
Definition: dynarray.h:48
size_t cur_size
reserved
Definition: dynarray.h:45
std::uint8_t * base
Definition: dynarray.h:43