]> git.proxmox.com Git - mirror_frr.git/blob - qpb/qpb_allocator.h
Merge remote-tracking branch 'origin/stable/3.0'
[mirror_frr.git] / qpb / qpb_allocator.h
1 /*
2 * qpb_allocator.h
3 *
4 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
5 *
6 * @author Avneesh Sachdev <avneesh@sproute.com>
7 *
8 * This file is part of Quagga.
9 *
10 * Quagga is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * Quagga is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /*
26 * Header file for Quagga/FRR protobuf memory management code.
27 */
28
29 #ifndef _QPB_ALLOCATOR_H_
30 #define _QPB_ALLOCATOR_H_
31
32 #include <google/protobuf-c/protobuf-c.h>
33
34 struct linear_allocator_t_;
35
36 /*
37 * Alias for ProtobufCAllocator that is easier on the fingers.
38 */
39 typedef ProtobufCAllocator qpb_allocator_t;
40
41 /*
42 * qpb_alloc
43 */
44 static inline void *
45 qpb_alloc (qpb_allocator_t *allocator, size_t size)
46 {
47 return allocator->alloc (allocator->allocator_data, size);
48 }
49
50 /*
51 * qpb_alloc_ptr_array
52 *
53 * Allocate space for the specified number of pointers.
54 */
55 static inline void *
56 qpb_alloc_ptr_array (qpb_allocator_t *allocator, size_t num_ptrs)
57 {
58 return qpb_alloc (allocator, num_ptrs * sizeof (void *));
59 }
60
61 /*
62 * qpb_free
63 */
64 static inline void
65 qpb_free (qpb_allocator_t *allocator, void *ptr)
66 {
67 allocator->free (allocator->allocator_data, ptr);
68 }
69
70 /*
71 * QPB_ALLOC
72 *
73 * Convenience macro to reduce the probability of allocating memory of
74 * incorrect size. It returns enough memory to store the given type,
75 * and evaluates to an appropriately typed pointer.
76 */
77 #define QPB_ALLOC(allocator, type) \
78 (type *) qpb_alloc(allocator, sizeof(type))
79
80
81 /*
82 * Externs.
83 */
84 extern void qpb_allocator_init_linear (qpb_allocator_t *,
85 struct linear_allocator_t_ *);
86
87 /*
88 * The following macros are for the common case where a qpb allocator
89 * is being used alongside a linear allocator that allocates memory
90 * off of the stack.
91 */
92 #define QPB_DECLARE_STACK_ALLOCATOR(allocator, size) \
93 qpb_allocator_t allocator; \
94 linear_allocator_t lin_ ## allocator; \
95 char lin_ ## allocator ## _buf[size]
96
97 #define QPB_INIT_STACK_ALLOCATOR(allocator) \
98 do \
99 { \
100 linear_allocator_init(&(lin_ ## allocator), \
101 lin_ ## allocator ## _buf, \
102 sizeof(lin_ ## allocator ## _buf)); \
103 qpb_allocator_init_linear(&allocator, &(lin_ ## allocator)); \
104 } while (0)
105
106 #define QPB_RESET_STACK_ALLOCATOR(allocator) \
107 do \
108 { \
109 linear_allocator_reset (&(lin_ ## allocator)); \
110 } while (0)
111
112 #endif /* _QPB_ALLOCATOR_H_ */