1#pragma once
 2
 3#include <stddef.h>
 4
 5#ifdef __cplusplus
 6extern "C" {
 7#endif
 8
 9[[nodiscard]] void* mmarray_allocate(unsigned max_capacity, unsigned length, unsigned item_size);
10/*
11 * Allocate array of desired `length` and `item_size` using mmap.
12 * Return pointer to the array.
13 * Abort the program if mmap fails.
14 *
15 * If max_capacity is zero, mmarray_grow reallocates memory and may return different address.
16 * If max_capacity is nonzero, the array is capped. The address space is reserved for entire
17 * `max_capacity` items and only necessary pages are committed for `length` items.
18 */
19
20[[nodiscard]] void* mmarray_grow(void* array, unsigned increment);
21/*
22 * Reallocate array if necessary using mremap.
23 * Return new pointer.
24 * Abort the program if mremap fails.
25 */
26
27void mmarray_reset(void* array, unsigned item_size);
28/*
29 * Reset array length, set new item size and recalculate capacity.
30 */
31
32void mmarray_free(void* array);
33/*
34 * Free array.
35 */
36
37[[nodiscard]] void* mmarray_append_item(void* array, void* item);
38/*
39 * Append new item to the array.
40 * Return new pointer.
41 * Abort the program if array_grow fails.
42 */
43
44[[nodiscard]] unsigned mmarray_length(void* array);
45[[nodiscard]] unsigned mmarray_capacity(void* array);
46
47#ifdef __cplusplus
48}
49#endif