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 
15 #ifndef RCUTILS__ALLOCATOR_H_
16 #define RCUTILS__ALLOCATOR_H_
17 
18 #if __cplusplus
19 extern "C"
20 {
21 #endif
22 
23 #include <stdbool.h>
24 #include <stddef.h>
25 
26 #include "rcutils/macros.h"
29 
31 
43 typedef struct rcutils_allocator_t
44 {
46 
47  void * (*allocate)(size_t size, void * state);
49 
50  void (* deallocate)(void * pointer, void * state);
52 
62  void * (*reallocate)(void * pointer, size_t size, void * state);
64 
65  void * (*zero_allocate)(size_t number_of_elements, size_t size_of_element, void * state);
67 
68  void * state;
70 
72 
79 
81 
101 
103 
108 bool
110 
111 #define RCUTILS_CHECK_ALLOCATOR(allocator, fail_statement) \
112  if (!rcutils_allocator_is_valid(allocator)) { \
113  fail_statement; \
114  }
115 
116 #define RCUTILS_CHECK_ALLOCATOR_WITH_MSG(allocator, msg, fail_statement) \
117  if (!rcutils_allocator_is_valid(allocator)) { \
118  RCUTILS_SET_ERROR_MSG(msg, rcutils_get_default_allocator()) \
119  fail_statement; \
120  }
121 
123 
129 void *
130 rcutils_reallocf(void * pointer, size_t size, rcutils_allocator_t * allocator);
131 
132 #if __cplusplus
133 }
134 #endif
135 
136 #endif // RCUTILS__ALLOCATOR_H_
bool rcutils_allocator_is_valid(const rcutils_allocator_t *allocator)
Return true if the given allocator has non-null function pointers.
rcutils_allocator_t rcutils_get_zero_initialized_allocator(void)
Return a zero initialized allocator.
void * state
Implementation defined state storage.
Definition: allocator.h:68
void * rcutils_reallocf(void *pointer, size_t size, rcutils_allocator_t *allocator)
Emulate the behavior of reallocf.
Encapsulation of an allocator.
Definition: allocator.h:43
rcutils_allocator_t rcutils_get_default_allocator(void)
Return a properly initialized rcutils_allocator_t with default values.
#define RCUTILS_WARN_UNUSED
Definition: macros.h:24
#define RCUTILS_PUBLIC
Definition: visibility_control.h:48
struct rcutils_allocator_t rcutils_allocator_t
Encapsulation of an allocator.
void(* deallocate)(void *pointer, void *state)
Deallocate previously allocated memory, mimicking std::free().
Definition: allocator.h:50