|
| 1 | +// Copyright 2015 Open Source Robotics Foundation, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/// \file |
| 16 | + |
| 17 | +#ifndef RCUTILS__ALLOCATOR_H_ |
| 18 | +#define RCUTILS__ALLOCATOR_H_ |
| 19 | + |
| 20 | +#ifdef __cplusplus |
| 21 | +extern "C" |
| 22 | +{ |
| 23 | +#endif |
| 24 | + |
| 25 | +#include <stdbool.h> |
| 26 | +#include <stddef.h> |
| 27 | + |
| 28 | +#include "rcutils/macros.h" |
| 29 | +#include "rcutils/types/rcutils_ret.h" |
| 30 | +#include "rcutils/visibility_control.h" |
| 31 | + |
| 32 | +/// Encapsulation of an allocator. |
| 33 | +/** |
| 34 | + * The default allocator uses malloc(), free(), calloc(), and realloc(). |
| 35 | + * It can be obtained using rcutils_get_default_allocator(). |
| 36 | + * |
| 37 | + * The allocator should be trivially copyable. |
| 38 | + * Meaning that the struct should continue to work after being assignment |
| 39 | + * copied into a new struct. |
| 40 | + * Specifically the object pointed to by the state pointer should remain valid |
| 41 | + * until all uses of the allocator have been made. |
| 42 | + * Particular care should be taken when giving an allocator to functions like |
| 43 | + * rcutils_*_init() where it is stored within another object and used later. |
| 44 | + * Developers should note that, while the fields of a const-qualified allocator |
| 45 | + * struct cannot be modified, the state of the allocator can be modified. |
| 46 | + */ |
| 47 | +typedef struct rcutils_allocator_s |
| 48 | +{ |
| 49 | + /// Allocate memory, given a size and the `state` pointer. |
| 50 | + /** An error should be indicated by returning `NULL`. */ |
| 51 | + void * (*allocate)(size_t size, void * state); |
| 52 | + /// Deallocate previously allocated memory, mimicking free(). |
| 53 | + /** Also takes the `state` pointer. */ |
| 54 | + void (* deallocate)(void * pointer, void * state); |
| 55 | + /// Reallocate if possible, otherwise it deallocates and allocates. |
| 56 | + /** |
| 57 | + * Also takes the `state` pointer. |
| 58 | + * |
| 59 | + * If unsupported then do deallocate and then allocate. |
| 60 | + * This should behave as realloc() does, as opposed to posix's |
| 61 | + * [reallocf](https://linux.die.net/man/3/reallocf), i.e. the memory given |
| 62 | + * by pointer will not be free'd automatically if realloc() fails. |
| 63 | + * For reallocf-like behavior use rcutils_reallocf(). |
| 64 | + * This function must be able to take an input pointer of `NULL` and succeed. |
| 65 | + */ |
| 66 | + void * (*reallocate)(void * pointer, size_t size, void * state); |
| 67 | + /// Allocate memory with all elements set to zero, given a number of elements and their size. |
| 68 | + /** An error should be indicated by returning `NULL`. */ |
| 69 | + void * (*zero_allocate)(size_t number_of_elements, size_t size_of_element, void * state); |
| 70 | + /// Implementation defined state storage. |
| 71 | + /** |
| 72 | + * This is passed as the final parameter to other allocator functions. |
| 73 | + * Note that the contents of the state can be modified even in const-qualified |
| 74 | + * allocator objects. |
| 75 | + */ |
| 76 | + void * state; |
| 77 | +} rcutils_allocator_t; |
| 78 | + |
| 79 | +/// Return a zero initialized allocator. |
| 80 | +/** |
| 81 | + * Note that this is an invalid allocator and should only be used as a placeholder. |
| 82 | + */ |
| 83 | +RCUTILS_PUBLIC |
| 84 | +RCUTILS_WARN_UNUSED |
| 85 | +rcutils_allocator_t |
| 86 | +rcutils_get_zero_initialized_allocator(void); |
| 87 | + |
| 88 | +/// Set rcutils default allocators. |
| 89 | +/** |
| 90 | + * <hr> |
| 91 | + * Attribute | Adherence |
| 92 | + * ------------------ | ------------- |
| 93 | + * Allocates Memory | No |
| 94 | + * Thread-Safe | Yes |
| 95 | + * Uses Atomics | No |
| 96 | + * Lock-Free | Yes |
| 97 | + */ |
| 98 | +RCUTILS_PUBLIC |
| 99 | +RCUTILS_WARN_UNUSED |
| 100 | +bool |
| 101 | +rcutils_set_default_allocator(rcutils_allocator_t * allocator); |
| 102 | + |
| 103 | +/// Return a properly initialized rcutils_allocator_t with default values. |
| 104 | +/** |
| 105 | + * This defaults to: |
| 106 | + * |
| 107 | + * - allocate = wraps malloc() |
| 108 | + * - deallocate = wraps free() |
| 109 | + * - reallocate = wraps realloc() |
| 110 | + * - zero_allocate = wraps calloc() |
| 111 | + * - state = `NULL` |
| 112 | + * |
| 113 | + * <hr> |
| 114 | + * Attribute | Adherence |
| 115 | + * ------------------ | ------------- |
| 116 | + * Allocates Memory | No |
| 117 | + * Thread-Safe | Yes |
| 118 | + * Uses Atomics | No |
| 119 | + * Lock-Free | Yes |
| 120 | + */ |
| 121 | +RCUTILS_PUBLIC |
| 122 | +RCUTILS_WARN_UNUSED |
| 123 | +rcutils_allocator_t |
| 124 | +rcutils_get_default_allocator(void); |
| 125 | + |
| 126 | +/// Return true if the given allocator has non-null function pointers. |
| 127 | +/** |
| 128 | + * \param[in] allocator to be checked by the function |
| 129 | + * \return `true` if the allocator is valid, `false` otherwise. |
| 130 | + */ |
| 131 | +RCUTILS_PUBLIC |
| 132 | +RCUTILS_WARN_UNUSED |
| 133 | +bool |
| 134 | +rcutils_allocator_is_valid(const rcutils_allocator_t * allocator); |
| 135 | + |
| 136 | +/// Check the given allocator and run fail_statement if it is not valid. |
| 137 | +#define RCUTILS_CHECK_ALLOCATOR(allocator, fail_statement) \ |
| 138 | + if (!rcutils_allocator_is_valid(allocator)) { \ |
| 139 | + fail_statement; \ |
| 140 | + } |
| 141 | + |
| 142 | +/// Check the given allocator, and set the message in msg and run fail_statement if it is not valid. |
| 143 | +#define RCUTILS_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement) \ |
| 144 | + if (!rcutils_allocator_is_valid(allocator)) { \ |
| 145 | + RCUTILS_SET_ERROR_MSG(msg); \ |
| 146 | + fail_statement; \ |
| 147 | + } |
| 148 | + |
| 149 | +/// Emulate the behavior of [reallocf](https://linux.die.net/man/3/reallocf). |
| 150 | +/** |
| 151 | + * This function will return `NULL` if the allocator is `NULL` or has `NULL` for |
| 152 | + * function pointer fields. |
| 153 | + * \param[inout] pointer to the memory which will be reallocated |
| 154 | + * \param[in] size in bytes |
| 155 | + * \param[in] allocator to be used to allocate and deallocate memory |
| 156 | + */ |
| 157 | +RCUTILS_PUBLIC |
| 158 | +RCUTILS_WARN_UNUSED |
| 159 | +void * |
| 160 | +rcutils_reallocf(void * pointer, size_t size, rcutils_allocator_t * allocator); |
| 161 | + |
| 162 | +#ifdef __cplusplus |
| 163 | +} |
| 164 | +#endif |
| 165 | + |
| 166 | +#endif // RCUTILS__ALLOCATOR_H_ |
0 commit comments