ds 0.10.0
A C library to safely yet efficiently work with UTF-8–encoded, growable dynamic strings.
alloc.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef DS_ALLOC_H
4#define DS_ALLOC_H 1
5
6//! @file ds/alloc.h
7//! @author Avinash Maddikonda (svasssakavi@gmail.com)
8//! @brief Declaration of the @ref ds_allocator_t "Allocator" type, related
9//! constants and functions.
10//! @since 0.8.0
11//! @date 2023-08-10
12
13#include "ds/usize.h"
14
15#ifdef __cplusplus
16extern "C"
17{
18#endif /* __cplusplus */
19
20 /// @brief An implementation of @ref ds_allocator_t "Allocator" can allocate,
21 /// reallocate (grow, shrink), and deallocate arbitrary blocks of data.
22 ///
23 /// Zero-sized allocations are allowed in @ref ds_allocator_t "Allocator". If
24 /// an underlying allocator does not support this (like `jemalloc`) or return
25 /// a `NULL` pointer (such as `malloc`), this must be caught by the
26 /// implementation.
28 {
29 /// @brief Attempts to allocate a block of memory.
30 ///
31 /// The returned block may have a larger size than specified by @p size,
32 /// and may or may not have its contents initialized.
33 ///
34 /// @param size The size of the block of memory to allocate.
35 /// @return A non-`NULL` pointer to the block of memory meeting the size
36 /// and alignment guarantees on success.
37 /// @return `NULL` on failure.
38 void *(*allocate) (ds_usize_t size);
39
40 /// @brief Attempts to reallocate (extend, shrink) the memory block.
41 ///
42 /// The returned block may have a larger size than specified by @p
43 /// new_size, and may or may not have its contents initialized.
44 ///
45 /// If this returns a non-`NULL` pointer, then ownership of the memory
46 /// block referenced by @p ptr has been transferred to this allocator. Any
47 /// access to the old @p ptr is Undefined Behavior, even if the allocation
48 /// was grown in-place. The newly returned pointer is the only valid
49 /// pointer for accessing this memory now.
50 ///
51 /// If this method returns `NULL`, then ownership of the memory block has
52 /// not been transferred to this allocator, and the contents of the memory
53 /// block are unaltered.
54 ///
55 /// @param ptr A reference to the memory block to reallocate.
56 /// @param new_size The new size of the block of memory to reallocate.
57 ///
58 /// @return A non-`NULL` pointer to the block of memory meeting the size
59 /// and alignment guarantees on success.
60 /// @return `NULL` on failure.
61 ///
62 /// ### Safety
63 ///
64 /// @p ptr must denote a block of memory *currently allocated* via this
65 /// allocator.
66 void *(*reallocate) (void *ptr, ds_usize_t new_size);
67
68 /// @brief Deallocates the memory referenced by @p ptr.
69 /// @param[out] ptr A reference to the memory to deallocate.
70 ///
71 /// ### Safety
72 ///
73 /// @p ptr must denote a block of memory *currently allocated* via this
74 /// allocator.
75 void (*deallocate) (void *ptr);
76 };
77
78 typedef struct ds_allocator_t ds_allocator_t;
79
80 /// @brief The global memory allocator.
81 ///
82 /// By default, uses the C stdlib `malloc`, `realloc`, and `free` functions,
83 /// but can be defined to use a custom allocator.
85
86/// @brief The size of @ref ds_allocator_t "Allocator" in bytes.
87///
88/// ### Examples
89///
90/// Basic usage:
91///
92/// ```c
93/// assert (DS_ALLOCATOR_BYTES == sizeof (ds_allocator_t));
94/// ```
95#define DS_ALLOCATOR_BYTES ((ds_usize_t)sizeof (ds_allocator_t))
96
97#ifdef __cplusplus
98}
99#endif /* __cplusplus */
100
101#endif /* DS_ALLOC_H */
ds_allocator_t ds_allocator_global
The global memory allocator.
Definition: alloc.c:12
An implementation of Allocator can allocate, reallocate (grow, shrink), and deallocate arbitrary bloc...
Definition: alloc.h:28
void(* deallocate)(void *ptr)
Deallocates the memory referenced by ptr.
Definition: alloc.h:75
Declaration of the usize type, related constants and functions.
size_t ds_usize_t
The pointer-sized unsigned integer type.
Definition: usize.h:26