1#pragma once
2
3#include "include/pw_interfaces.h"
4#include "include/pw_status.h"
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10/*
11 * Memory allocation helpers.
12 */
13
14[[nodiscard]] static inline void* _pw_alloc(uint16_t type_id, unsigned memsize, bool clean)
15{
16 void* result = _pw_types[type_id]->allocator->allocate(memsize, clean);
17 if (!result) {
18 pw_set_status(PwStatus(PW_ERROR_OOM));
19 }
20 return result;
21}
22
23[[nodiscard]] static inline bool _pw_realloc(uint16_t type_id, void** memblock, unsigned old_memsize, unsigned new_memsize, bool clean)
24{
25 if (_pw_types[type_id]->allocator->reallocate(memblock, old_memsize, new_memsize, clean, nullptr)) {
26 return true;
27 } else {
28 pw_set_status(PwStatus(PW_ERROR_OOM));
29 return false;
30 }
31}
32
33static inline void _pw_free(uint16_t type_id, void** memblock, unsigned memsize)
34{
35 _pw_types[type_id]->allocator->release(memblock, memsize);
36}
37
38/*
39 * Arena for various "static" data like type structures.
40 */
41
42void* _pw_arena_alloc_bytes(unsigned size, unsigned alignment);
43
44#define _pw_arena_alloc(num_elements, element_type) \
45 _pw_arena_alloc_bytes((num_elements) * sizeof(element_type), alignof(element_type))
46
47#ifdef __cplusplus
48}
49#endif