buddy_alloc.c 1.2025.9 → 1.2025.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +12 -0
- package/README.md +56 -6
- package/buddy_alloc.h +2232 -2226
- package/package.json +19 -7
- package/CMakeLists.txt +0 -21
- package/CODE_OF_CONDUCT.md +0 -128
- package/CONTRIBUTING.md +0 -3
- package/LICENSE.md +0 -5
- package/Makefile +0 -59
- package/SECURITY.md +0 -5
- package/compile_flags.txt +0 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
Copyright (C) 2021 by Stanislav Paskalev <spaskalev@protonmail.com>
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
4
|
+
with or without fee is hereby granted.
|
|
5
|
+
|
|
6
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
7
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
8
|
+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
9
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
10
|
+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
11
|
+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
12
|
+
THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
# buddy_alloc
|
|
2
|
-
A buddy memory allocator for C, by [Stanislav Paskalev](https://github.com/spaskalev).
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
A buddy memory allocator for C, by [Stanislav Paskalev](https://github.com/spaskalev). [[Latest release](https://github.com/spaskalev/buddy_alloc/releases/latest)]
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
[](https://github.com/spaskalev/buddy_alloc/actions/workflows/main.yml) [](https://github.com/spaskalev/buddy_alloc/actions/workflows/codeql.yml)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
<br>
|
|
8
9
|
|
|
9
10
|
## Licensing
|
|
10
11
|
|
|
11
12
|
This project is licensed under the 0BSD license. See the LICENSE.md file for details.
|
|
12
13
|
|
|
14
|
+
<br>
|
|
15
|
+
|
|
13
16
|
## Overview
|
|
14
17
|
|
|
15
18
|
This is a memory allocator suitable for use in applications that require predictable allocation and deallocation behavior. The allocator's metadata is kept separate from the arena and its size is a function of the arena and minimum allocations sizes.
|
|
16
19
|
|
|
20
|
+
<br>
|
|
21
|
+
|
|
17
22
|
## Features
|
|
18
23
|
|
|
19
24
|
- Bounded allocation and deallocation cost
|
|
@@ -24,18 +29,52 @@ This is a memory allocator suitable for use in applications that require predict
|
|
|
24
29
|
- Endian-agnostic, works on both LE and BE
|
|
25
30
|
- Compiles with GCC, Clang, MSVC and Pelles C
|
|
26
31
|
|
|
32
|
+
<br>
|
|
33
|
+
|
|
27
34
|
## Installation
|
|
28
35
|
|
|
29
36
|
Run:
|
|
30
|
-
|
|
37
|
+
|
|
38
|
+
```sh
|
|
31
39
|
$ npm i buddy_alloc.c
|
|
32
40
|
```
|
|
33
41
|
|
|
34
42
|
And then include `buddy_alloc.h` as follows:
|
|
43
|
+
|
|
35
44
|
```c
|
|
45
|
+
// main.c
|
|
46
|
+
#define BUDDY_ALLOC_IMPLEMENTATION
|
|
36
47
|
#include "node_modules/buddy_alloc.c/buddy_alloc.h"
|
|
48
|
+
|
|
49
|
+
int main() { /* ... */ }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
And then compile with `clang` or `gcc` as usual.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
$ clang main.c # or, use gcc
|
|
56
|
+
$ gcc main.c
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
You may also use a simpler approach:
|
|
60
|
+
|
|
61
|
+
```c
|
|
62
|
+
// main.c
|
|
63
|
+
#define BUDDY_ALLOC_IMPLEMENTATION
|
|
64
|
+
#include <buddy_alloc.h>
|
|
65
|
+
|
|
66
|
+
int main() { /* ... */ }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
If you add the path `node_modules/buddy_alloc.c` to your compiler's include paths.
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
$ clang -I./node_modules/buddy_alloc.c main.c # or, use gcc
|
|
73
|
+
$ gcc -I./node_modules/buddy_alloc.c main.c
|
|
37
74
|
```
|
|
38
75
|
|
|
76
|
+
<br>
|
|
77
|
+
|
|
39
78
|
## Usage
|
|
40
79
|
|
|
41
80
|
Initializing and using the buddy allocator with metadata external to the arena is done using the `buddy_init` function.
|
|
@@ -72,6 +111,8 @@ buddy_free(buddy, data);
|
|
|
72
111
|
free(buddy_arena);
|
|
73
112
|
```
|
|
74
113
|
|
|
114
|
+
<br>
|
|
115
|
+
|
|
75
116
|
## Metadata sizing
|
|
76
117
|
|
|
77
118
|
The following table documents the allocator metadata space requirements according to desired arena (8MB to 1024GB) and alignment/minimum allocation (64B to 8KB) sizes. The resulting values are rounded up to the nearest unit.
|
|
@@ -99,6 +140,8 @@ The following table documents the allocator metadata space requirements accordin
|
|
|
99
140
|
1024 GB | 8193MB | 4097MB | 2049MB | 1025MB | 513MB | 257MB | 129MB | 65MB |
|
|
100
141
|
```
|
|
101
142
|
|
|
143
|
+
<br>
|
|
144
|
+
|
|
102
145
|
## Design
|
|
103
146
|
|
|
104
147
|
The allocator was designed with the following requirements in mind.
|
|
@@ -113,6 +156,8 @@ The following were not design goals
|
|
|
113
156
|
- To be used by multiple threads at the same time without additional locking.
|
|
114
157
|
- To be a general purpose malloc() replacement.
|
|
115
158
|
|
|
159
|
+
<br>
|
|
160
|
+
|
|
116
161
|
## Rationale
|
|
117
162
|
|
|
118
163
|
### Why use a custom allocator (like buddy_alloc) ?
|
|
@@ -127,6 +172,8 @@ An application developer may also need object allocation that is relocatable. Us
|
|
|
127
172
|
|
|
128
173
|
With the introduction of the `buddy_walk` function the allocator can be used to iterate all the allocated slots with its arena. This can be used for example for a space-bounded mailbox where a failure to allocate means the mailbox is full and the walk can be used to process its content. This can also form the basis of a managed heap for garbage collection.
|
|
129
174
|
|
|
175
|
+
<br>
|
|
176
|
+
|
|
130
177
|
## Implementation
|
|
131
178
|
|
|
132
179
|
```
|
|
@@ -169,7 +216,7 @@ The allocator uses a bitset-backed perfect binary tree to track allocations. The
|
|
|
169
216
|
|
|
170
217
|
### Allocation and deallocation
|
|
171
218
|
|
|
172
|
-
The binary tree nodes are labeled with the largest allocation slot available under them. This allows allocation to happen with a limited number of operations. Allocations that cannot be satisfied are fast to fail. Once a free node of the desired size is found it is marked as used and the nodes leading to root of the tree are updated to account for any difference in the largest available size. Deallocation works in a similar way - the allocated block size for the given address is found, marked as free and the same node update as with allocation is used to update the tree upwards.
|
|
219
|
+
The binary tree nodes are labeled with the largest allocation slot available under them. This allows allocation to happen with a limited number of operations. Allocations that cannot be satisfied are fast to fail. Once a free node of the desired size is found it is marked as used and the nodes leading to root of the tree are updated to account for any difference in the largest available size. Deallocation works in a similar way - the allocated block size for the given address is found, marked as free and the same node update as with allocation is used to update the tree upwards.
|
|
173
220
|
|
|
174
221
|
#### Fragmentation
|
|
175
222
|
|
|
@@ -187,6 +234,8 @@ The perfect binary tree always tracks an arena which size is a power-of-two. Whe
|
|
|
187
234
|
|
|
188
235
|
Resizing is available for both split and embedded allocator modes and supports both growing the arena and shrinking it. Checks are present that prevent shrinking the arena when memory that is to be reduced is still allocated.
|
|
189
236
|
|
|
237
|
+
<br>
|
|
238
|
+
|
|
190
239
|
## Users
|
|
191
240
|
|
|
192
241
|
If you are using buddy_alloc in your project and you would like project to be featured here please send a PR or file an issue. If you like buddy_alloc please star it on GitHub so that more users can learn of it. Thanks!
|
|
@@ -201,5 +250,6 @@ If you are using buddy_alloc in your project and you would like project to be fe
|
|
|
201
250
|
<br>
|
|
202
251
|
|
|
203
252
|
|
|
253
|
+
[](https://github.com/spaskalev/buddy_alloc)
|
|
204
254
|
[](https://nodef.github.io)
|
|
205
255
|

|