]> git.proxmox.com Git - mirror_frr.git/blob - qpb/qpb_allocator.h
Merge pull request #12797 from jvidalallende/ubi8_minimal_dockerfile
[mirror_frr.git] / qpb / qpb_allocator.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * qpb_allocator.h
4 *
5 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
6 *
7 * @author Avneesh Sachdev <avneesh@sproute.com>
8 */
9
10 /*
11 * Header file for Quagga/FRR protobuf memory management code.
12 */
13
14 #ifndef _QPB_ALLOCATOR_H_
15 #define _QPB_ALLOCATOR_H_
16
17 #include <google/protobuf-c/protobuf-c.h>
18
19 struct linear_allocator_t_;
20
21 /*
22 * Alias for ProtobufCAllocator that is easier on the fingers.
23 */
24 typedef ProtobufCAllocator qpb_allocator_t;
25
26 /*
27 * qpb_alloc
28 */
29 static inline void *qpb_alloc(qpb_allocator_t *allocator, size_t size)
30 {
31 return allocator->alloc(allocator->allocator_data, size);
32 }
33
34 /*
35 * qpb_alloc_ptr_array
36 *
37 * Allocate space for the specified number of pointers.
38 */
39 static inline void *qpb_alloc_ptr_array(qpb_allocator_t *allocator,
40 size_t num_ptrs)
41 {
42 return qpb_alloc(allocator, num_ptrs * sizeof(void *));
43 }
44
45 /*
46 * qpb_free
47 */
48 static inline void qpb_free(qpb_allocator_t *allocator, void *ptr)
49 {
50 allocator->free(allocator->allocator_data, ptr);
51 }
52
53 /*
54 * QPB_ALLOC
55 *
56 * Convenience macro to reduce the probability of allocating memory of
57 * incorrect size. It returns enough memory to store the given type,
58 * and evaluates to an appropriately typed pointer.
59 */
60 #define QPB_ALLOC(allocator, type) (type *)qpb_alloc(allocator, sizeof(type))
61
62 /*
63 * Externs.
64 */
65 extern void qpb_allocator_init_linear(qpb_allocator_t *,
66 struct linear_allocator_t_ *);
67
68 /*
69 * The following macros are for the common case where a qpb allocator
70 * is being used alongside a linear allocator that allocates memory
71 * off of the stack.
72 */
73 #define QPB_DECLARE_STACK_ALLOCATOR(allocator, size) \
74 qpb_allocator_t allocator; \
75 linear_allocator_t lin_##allocator; \
76 char lin_##allocator##_buf[size]
77
78 #define QPB_INIT_STACK_ALLOCATOR(allocator) \
79 do { \
80 linear_allocator_init(&(lin_##allocator), \
81 lin_##allocator##_buf, \
82 sizeof(lin_##allocator##_buf)); \
83 qpb_allocator_init_linear(&allocator, &(lin_##allocator)); \
84 } while (0)
85
86 #define QPB_RESET_STACK_ALLOCATOR(allocator) \
87 do { \
88 linear_allocator_reset(&(lin_##allocator)); \
89 } while (0)
90
91 #endif /* _QPB_ALLOCATOR_H_ */