rcutils  master
C API providing common utilities and data structures.
allocator.h
Go to the documentation of this file.
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 
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"
31 
33 
47 typedef struct rcutils_allocator_t
48 {
50 
51  void * (*allocate)(size_t size, void * state);
53 
54  void (* deallocate)(void * pointer, void * state);
56 
66  void * (*reallocate)(void * pointer, size_t size, void * state);
68 
69  void * (*zero_allocate)(size_t number_of_elements, size_t size_of_element, void * state);
71 
76  void * state;
78 
80 
87 
89 
110 
112 
118 bool
120 
122 #define RCUTILS_CHECK_ALLOCATOR(allocator, fail_statement) \
123  if (!rcutils_allocator_is_valid(allocator)) { \
124  fail_statement; \
125  }
126 
128 #define RCUTILS_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement) \
129  if (!rcutils_allocator_is_valid(allocator)) { \
130  RCUTILS_SET_ERROR_MSG(msg); \
131  fail_statement; \
132  }
133 
135 
144 void *
145 rcutils_reallocf(void * pointer, size_t size, rcutils_allocator_t * allocator);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif // RCUTILS__ALLOCATOR_H_
rcutils_get_default_allocator
rcutils_allocator_t rcutils_get_default_allocator(void)
Return a properly initialized rcutils_allocator_t with default values.
macros.h
rcutils_allocator_is_valid
bool rcutils_allocator_is_valid(const rcutils_allocator_t *allocator)
Return true if the given allocator has non-null function pointers.
rcutils_allocator_t
struct rcutils_allocator_t rcutils_allocator_t
Encapsulation of an allocator.
rcutils_reallocf
void * rcutils_reallocf(void *pointer, size_t size, rcutils_allocator_t *allocator)
Emulate the behavior of reallocf.
RCUTILS_WARN_UNUSED
#define RCUTILS_WARN_UNUSED
A macro to make the compiler warn when the return value of a function is not used.
Definition: macros.h:27
rcutils_ret.h
visibility_control.h
rcutils_get_zero_initialized_allocator
rcutils_allocator_t rcutils_get_zero_initialized_allocator(void)
Return a zero initialized allocator.
rcutils_allocator_t
Encapsulation of an allocator.
Definition: allocator.h:47
RCUTILS_PUBLIC
#define RCUTILS_PUBLIC
Definition: visibility_control.h:23
rcutils_allocator_t::state
void * state
Implementation defined state storage.
Definition: allocator.h:76
rcutils_allocator_t::deallocate
void(* deallocate)(void *pointer, void *state)
Deallocate previously allocated memory, mimicking free().
Definition: allocator.h:54