]>
Commit | Line | Data |
---|---|---|
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 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with Quagga; see the file COPYING. If not, write to the Free | |
22 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
23 | * 02111-1307, USA. | |
24 | */ | |
25 | ||
26 | /* | |
fc5300bd | 27 | * Header file for Quagga/FRR protobuf memory management code. |
dad253b4 AS |
28 | */ |
29 | ||
30 | #ifndef _QPB_ALLOCATOR_H_ | |
31 | #define _QPB_ALLOCATOR_H_ | |
32 | ||
33 | #include <google/protobuf-c/protobuf-c.h> | |
34 | ||
35 | struct linear_allocator_t_; | |
36 | ||
37 | /* | |
38 | * Alias for ProtobufCAllocator that is easier on the fingers. | |
39 | */ | |
40 | typedef ProtobufCAllocator qpb_allocator_t; | |
41 | ||
42 | /* | |
43 | * qpb_alloc | |
44 | */ | |
ac4d0be5 | 45 | static inline void *qpb_alloc(qpb_allocator_t *allocator, size_t size) |
dad253b4 | 46 | { |
ac4d0be5 | 47 | return allocator->alloc(allocator->allocator_data, size); |
dad253b4 AS |
48 | } |
49 | ||
50 | /* | |
51 | * qpb_alloc_ptr_array | |
52 | * | |
53 | * Allocate space for the specified number of pointers. | |
54 | */ | |
ac4d0be5 | 55 | static inline void *qpb_alloc_ptr_array(qpb_allocator_t *allocator, |
56 | size_t num_ptrs) | |
dad253b4 | 57 | { |
ac4d0be5 | 58 | return qpb_alloc(allocator, num_ptrs * sizeof(void *)); |
dad253b4 AS |
59 | } |
60 | ||
61 | /* | |
62 | * qpb_free | |
63 | */ | |
ac4d0be5 | 64 | static inline void qpb_free(qpb_allocator_t *allocator, void *ptr) |
dad253b4 | 65 | { |
ac4d0be5 | 66 | allocator->free(allocator->allocator_data, ptr); |
dad253b4 AS |
67 | } |
68 | ||
69 | /* | |
70 | * QPB_ALLOC | |
71 | * | |
72 | * Convenience macro to reduce the probability of allocating memory of | |
73 | * incorrect size. It returns enough memory to store the given type, | |
74 | * and evaluates to an appropriately typed pointer. | |
75 | */ | |
ac4d0be5 | 76 | #define QPB_ALLOC(allocator, type) (type *)qpb_alloc(allocator, sizeof(type)) |
dad253b4 AS |
77 | |
78 | ||
79 | /* | |
80 | * Externs. | |
81 | */ | |
ac4d0be5 | 82 | extern void qpb_allocator_init_linear(qpb_allocator_t *, |
83 | struct linear_allocator_t_ *); | |
dad253b4 AS |
84 | |
85 | /* | |
86 | * The following macros are for the common case where a qpb allocator | |
87 | * is being used alongside a linear allocator that allocates memory | |
88 | * off of the stack. | |
89 | */ | |
ac4d0be5 | 90 | #define QPB_DECLARE_STACK_ALLOCATOR(allocator, size) \ |
91 | qpb_allocator_t allocator; \ | |
92 | linear_allocator_t lin_##allocator; \ | |
93 | char lin_##allocator##_buf[size] | |
dad253b4 | 94 | |
ac4d0be5 | 95 | #define QPB_INIT_STACK_ALLOCATOR(allocator) \ |
96 | do { \ | |
97 | linear_allocator_init(&(lin_##allocator), \ | |
98 | lin_##allocator##_buf, \ | |
99 | sizeof(lin_##allocator##_buf)); \ | |
100 | qpb_allocator_init_linear(&allocator, &(lin_##allocator)); \ | |
101 | } while (0) | |
dad253b4 | 102 | |
ac4d0be5 | 103 | #define QPB_RESET_STACK_ALLOCATOR(allocator) \ |
104 | do { \ | |
105 | linear_allocator_reset(&(lin_##allocator)); \ | |
106 | } while (0) | |
dad253b4 AS |
107 | |
108 | #endif /* _QPB_ALLOCATOR_H_ */ |