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 the linear allocator.
13 * An allocator that allocates memory by walking down towards the end
14 * of a buffer. No attempt is made to reuse blocks that are freed
15 * subsequently. The assumption is that the buffer is big enough to
16 * cover allocations for a given purpose.
24 * Alignment for block allocated by the allocator. Must be a power of 2.
26 #define LINEAR_ALLOCATOR_ALIGNMENT 8
28 #define LINEAR_ALLOCATOR_ALIGN(value) \
29 (((value) + LINEAR_ALLOCATOR_ALIGNMENT - 1) \
30 & ~(LINEAR_ALLOCATOR_ALIGNMENT - 1));
33 * linear_allocator_align_ptr
35 static inline char *linear_allocator_align_ptr(char *ptr
)
37 return (char *)LINEAR_ALLOCATOR_ALIGN((intptr_t)ptr
);
40 typedef struct linear_allocator_t_
{
44 * Current location in the buffer.
54 * Version number of the allocator, this is bumped up when the allocator
55 * is reset and helps identifies bad frees.
60 * The number of blocks that are currently allocated.
66 * linear_allocator_block_t
68 * Header structure at the begining of each block.
70 typedef struct linear_allocator_block_t_
{
74 * The version of the allocator when this block was allocated.
78 } linear_allocator_block_t
;
80 #define LINEAR_ALLOCATOR_BLOCK_IN_USE 0x01
82 #define LINEAR_ALLOCATOR_HDR_SIZE (sizeof(linear_allocator_block_t))
85 * linear_allocator_block_size
87 * The total amount of space a block will take in the buffer,
88 * including the size of the header.
90 static inline size_t linear_allocator_block_size(size_t user_size
)
92 return LINEAR_ALLOCATOR_ALIGN(LINEAR_ALLOCATOR_HDR_SIZE
+ user_size
);
96 * linear_allocator_ptr_to_block
98 static inline linear_allocator_block_t
*linear_allocator_ptr_to_block(void *ptr
)
101 block_ptr
= ((char *)ptr
) - offsetof(linear_allocator_block_t
, data
);
106 * linear_allocator_init
108 static inline void linear_allocator_init(linear_allocator_t
*allocator
,
109 char *buf
, size_t buf_len
)
111 memset(allocator
, 0, sizeof(*allocator
));
113 assert(linear_allocator_align_ptr(buf
) == buf
);
114 allocator
->buf
= buf
;
115 allocator
->cur
= buf
;
116 allocator
->end
= buf
+ buf_len
;
120 * linear_allocator_reset
122 * Prepare an allocator for reuse.
124 * *** NOTE ** This implicitly frees all the blocks in the allocator.
126 static inline void linear_allocator_reset(linear_allocator_t
*allocator
)
128 allocator
->num_allocated
= 0;
129 allocator
->version
++;
130 allocator
->cur
= allocator
->buf
;
134 * linear_allocator_alloc
136 static inline void *linear_allocator_alloc(linear_allocator_t
*allocator
,
140 linear_allocator_block_t
*block
;
142 block_size
= linear_allocator_block_size(user_size
);
144 if (allocator
->cur
+ block_size
> allocator
->end
) {
148 block
= (linear_allocator_block_t
*)allocator
->cur
;
149 allocator
->cur
+= block_size
;
151 block
->flags
= LINEAR_ALLOCATOR_BLOCK_IN_USE
;
152 block
->version
= allocator
->version
;
153 allocator
->num_allocated
++;
158 * linear_allocator_free
160 static inline void linear_allocator_free(linear_allocator_t
*allocator
,
163 linear_allocator_block_t
*block
;
165 if (((char *)ptr
) < allocator
->buf
|| ((char *)ptr
) >= allocator
->end
) {
170 block
= linear_allocator_ptr_to_block(ptr
);
171 if (block
->version
!= allocator
->version
) {
176 block
->flags
= block
->flags
& ~LINEAR_ALLOCATOR_BLOCK_IN_USE
;
178 if (--allocator
->num_allocated
< 0) {