]> git.proxmox.com Git - mirror_frr.git/blame - qpb/qpb_allocator.h
Merge pull request #836 from dwalton76/zebra-debug-packet-send-detail
[mirror_frr.git] / qpb / qpb_allocator.h
CommitLineData
dad253b4
AS
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 *
896014f4
DL
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
dad253b4
AS
23 */
24
25/*
fc5300bd 26 * Header file for Quagga/FRR protobuf memory management code.
dad253b4
AS
27 */
28
29#ifndef _QPB_ALLOCATOR_H_
30#define _QPB_ALLOCATOR_H_
31
32#include <google/protobuf-c/protobuf-c.h>
33
34struct linear_allocator_t_;
35
36/*
37 * Alias for ProtobufCAllocator that is easier on the fingers.
38 */
39typedef ProtobufCAllocator qpb_allocator_t;
40
41/*
42 * qpb_alloc
43 */
d62a17ae 44static inline void *qpb_alloc(qpb_allocator_t *allocator, size_t size)
dad253b4 45{
d62a17ae 46 return allocator->alloc(allocator->allocator_data, size);
dad253b4
AS
47}
48
49/*
50 * qpb_alloc_ptr_array
51 *
52 * Allocate space for the specified number of pointers.
53 */
d62a17ae 54static inline void *qpb_alloc_ptr_array(qpb_allocator_t *allocator,
55 size_t num_ptrs)
dad253b4 56{
d62a17ae 57 return qpb_alloc(allocator, num_ptrs * sizeof(void *));
dad253b4
AS
58}
59
60/*
61 * qpb_free
62 */
d62a17ae 63static inline void qpb_free(qpb_allocator_t *allocator, void *ptr)
dad253b4 64{
d62a17ae 65 allocator->free(allocator->allocator_data, ptr);
dad253b4
AS
66}
67
68/*
69 * QPB_ALLOC
70 *
71 * Convenience macro to reduce the probability of allocating memory of
72 * incorrect size. It returns enough memory to store the given type,
73 * and evaluates to an appropriately typed pointer.
74 */
d62a17ae 75#define QPB_ALLOC(allocator, type) (type *)qpb_alloc(allocator, sizeof(type))
dad253b4 76
9d303b37 77
dad253b4
AS
78/*
79 * Externs.
80 */
d62a17ae 81extern void qpb_allocator_init_linear(qpb_allocator_t *,
82 struct linear_allocator_t_ *);
dad253b4
AS
83
84/*
85 * The following macros are for the common case where a qpb allocator
86 * is being used alongside a linear allocator that allocates memory
87 * off of the stack.
88 */
d62a17ae 89#define QPB_DECLARE_STACK_ALLOCATOR(allocator, size) \
90 qpb_allocator_t allocator; \
91 linear_allocator_t lin_##allocator; \
92 char lin_##allocator##_buf[size]
dad253b4 93
d62a17ae 94#define QPB_INIT_STACK_ALLOCATOR(allocator) \
95 do { \
96 linear_allocator_init(&(lin_##allocator), \
97 lin_##allocator##_buf, \
98 sizeof(lin_##allocator##_buf)); \
99 qpb_allocator_init_linear(&allocator, &(lin_##allocator)); \
100 } while (0)
dad253b4 101
d62a17ae 102#define QPB_RESET_STACK_ALLOCATOR(allocator) \
103 do { \
104 linear_allocator_reset(&(lin_##allocator)); \
105 } while (0)
dad253b4
AS
106
107#endif /* _QPB_ALLOCATOR_H_ */