1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * @copyright Copyright (C) 2016 Sproute Networks, Inc.
7 * @author Avneesh Sachdev <avneesh@sproute.com>
11 * Header file for Quagga/FRR protobuf memory management code.
14 #ifndef _QPB_ALLOCATOR_H_
15 #define _QPB_ALLOCATOR_H_
17 #include <google/protobuf-c/protobuf-c.h>
19 struct linear_allocator_t_
;
22 * Alias for ProtobufCAllocator that is easier on the fingers.
24 typedef ProtobufCAllocator qpb_allocator_t
;
29 static inline void *qpb_alloc(qpb_allocator_t
*allocator
, size_t size
)
31 return allocator
->alloc(allocator
->allocator_data
, size
);
37 * Allocate space for the specified number of pointers.
39 static inline void *qpb_alloc_ptr_array(qpb_allocator_t
*allocator
,
42 return qpb_alloc(allocator
, num_ptrs
* sizeof(void *));
48 static inline void qpb_free(qpb_allocator_t
*allocator
, void *ptr
)
50 allocator
->free(allocator
->allocator_data
, ptr
);
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.
60 #define QPB_ALLOC(allocator, type) (type *)qpb_alloc(allocator, sizeof(type))
65 extern void qpb_allocator_init_linear(qpb_allocator_t
*,
66 struct linear_allocator_t_
*);
69 * The following macros are for the common case where a qpb allocator
70 * is being used alongside a linear allocator that allocates memory
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]
78 #define QPB_INIT_STACK_ALLOCATOR(allocator) \
80 linear_allocator_init(&(lin_##allocator), \
81 lin_##allocator##_buf, \
82 sizeof(lin_##allocator##_buf)); \
83 qpb_allocator_init_linear(&allocator, &(lin_##allocator)); \
86 #define QPB_RESET_STACK_ALLOCATOR(allocator) \
88 linear_allocator_reset(&(lin_##allocator)); \
91 #endif /* _QPB_ALLOCATOR_H_ */