]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
dad253b4 AS |
2 | /* |
3 | * qpb_allocator.h | |
4 | * | |
5 | * @copyright Copyright (C) 2016 Sproute Networks, Inc. | |
6 | * | |
7 | * @author Avneesh Sachdev <avneesh@sproute.com> | |
dad253b4 AS |
8 | */ |
9 | ||
10 | /* | |
fc5300bd | 11 | * Header file for Quagga/FRR protobuf memory management code. |
dad253b4 AS |
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 | */ | |
d62a17ae | 29 | static inline void *qpb_alloc(qpb_allocator_t *allocator, size_t size) |
dad253b4 | 30 | { |
d62a17ae | 31 | return allocator->alloc(allocator->allocator_data, size); |
dad253b4 AS |
32 | } |
33 | ||
34 | /* | |
35 | * qpb_alloc_ptr_array | |
36 | * | |
37 | * Allocate space for the specified number of pointers. | |
38 | */ | |
d62a17ae | 39 | static inline void *qpb_alloc_ptr_array(qpb_allocator_t *allocator, |
40 | size_t num_ptrs) | |
dad253b4 | 41 | { |
d62a17ae | 42 | return qpb_alloc(allocator, num_ptrs * sizeof(void *)); |
dad253b4 AS |
43 | } |
44 | ||
45 | /* | |
46 | * qpb_free | |
47 | */ | |
d62a17ae | 48 | static inline void qpb_free(qpb_allocator_t *allocator, void *ptr) |
dad253b4 | 49 | { |
d62a17ae | 50 | allocator->free(allocator->allocator_data, ptr); |
dad253b4 AS |
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 | */ | |
d62a17ae | 60 | #define QPB_ALLOC(allocator, type) (type *)qpb_alloc(allocator, sizeof(type)) |
dad253b4 | 61 | |
dad253b4 AS |
62 | /* |
63 | * Externs. | |
64 | */ | |
d62a17ae | 65 | extern void qpb_allocator_init_linear(qpb_allocator_t *, |
66 | struct linear_allocator_t_ *); | |
dad253b4 AS |
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 | */ | |
d62a17ae | 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] | |
dad253b4 | 77 | |
d62a17ae | 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) | |
dad253b4 | 85 | |
d62a17ae | 86 | #define QPB_RESET_STACK_ALLOCATOR(allocator) \ |
87 | do { \ | |
88 | linear_allocator_reset(&(lin_##allocator)); \ | |
89 | } while (0) | |
dad253b4 AS |
90 | |
91 | #endif /* _QPB_ALLOCATOR_H_ */ |