buddy_alloc.c 1.2025.9
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/CMakeLists.txt +21 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/CONTRIBUTING.md +3 -0
- package/LICENSE.md +5 -0
- package/Makefile +59 -0
- package/README.md +205 -0
- package/SECURITY.md +5 -0
- package/buddy_alloc.h +2226 -0
- package/compile_flags.txt +2 -0
- package/package.json +15 -0
package/CMakeLists.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.10)
|
|
2
|
+
project(buddy_alloc)
|
|
3
|
+
set(C_STANDARD C99)
|
|
4
|
+
|
|
5
|
+
# Compile C tests
|
|
6
|
+
project(buddy_tests)
|
|
7
|
+
set(C_STANDARD C99)
|
|
8
|
+
set(SOURCE_FILES tests.c)
|
|
9
|
+
add_executable(buddy_tests ${SOURCE_FILES})
|
|
10
|
+
|
|
11
|
+
# Compile CPP translation unit
|
|
12
|
+
project(buddy_cpp_translation_unit)
|
|
13
|
+
set(CXX_STANDARD C++11)
|
|
14
|
+
set(SOURCE_FILES testcxx.cpp)
|
|
15
|
+
add_executable(buddy_cpp_translation_unit ${SOURCE_FILES})
|
|
16
|
+
|
|
17
|
+
# Compile benchmark
|
|
18
|
+
project(buddy_bench)
|
|
19
|
+
set(C_STANDARD C99)
|
|
20
|
+
set(SOURCE_FILES bench.c)
|
|
21
|
+
add_executable(buddy_bench ${SOURCE_FILES})
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity
|
|
10
|
+
and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the
|
|
26
|
+
overall community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or
|
|
31
|
+
advances of any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email
|
|
35
|
+
address, without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official e-mail address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
https://github.com/spaskalev/buddy_alloc/discussions.
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series
|
|
86
|
+
of actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or
|
|
93
|
+
permanent ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within
|
|
113
|
+
the community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.0, available at
|
|
119
|
+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
|
122
|
+
enforcement ladder](https://github.com/mozilla/diversity).
|
|
123
|
+
|
|
124
|
+
[homepage]: https://www.contributor-covenant.org
|
|
125
|
+
|
|
126
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
127
|
+
https://www.contributor-covenant.org/faq. Translations are available at
|
|
128
|
+
https://www.contributor-covenant.org/translations.
|
package/CONTRIBUTING.md
ADDED
package/LICENSE.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
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 with or without fee is hereby granted.
|
|
4
|
+
|
|
5
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/Makefile
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright 2021 Stanislav Paskalev <spaskalev@protonmail.com>
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
# Disable default implicit rules
|
|
6
|
+
MAKEFLAGS += --no-builtin-rules
|
|
7
|
+
.SUFFIXES:
|
|
8
|
+
|
|
9
|
+
LLVM_VERSION?=11
|
|
10
|
+
CC?=clang-$(LLVM_VERSION)
|
|
11
|
+
CXX?=clang++-$(LLVM_VERSION)
|
|
12
|
+
CFLAGS?=-std=c99 --coverage -pg -no-pie -fno-builtin -g -O0 -Og -fstrict-aliasing -fstack-protector-all -fsanitize=undefined -fsanitize=bounds -pedantic -Wall -Wextra -Werror -Wfatal-errors -Wformat=2 -Wformat-security -Wconversion -Wcast-qual -Wnull-dereference -Wstack-protector -Warray-bounds -Warray-bounds-pointer-arithmetic -Wassign-enum -Wbad-function-cast -Wconditional-uninitialized -Wfloat-equal -Wformat-type-confusion -Widiomatic-parentheses -Wimplicit-fallthrough -Wloop-analysis -Wpointer-arith -Wshift-sign-overflow -Wshorten-64-to-32 -Wswitch-enum -Wtautological-constant-in-range-compare -Wunreachable-code-aggressive -Wthread-safety -Wthread-safety-beta -Wcomma -Wdeclaration-after-statement -D_FORTIFY_SOURCE=3
|
|
13
|
+
CXXFLAGS?=-std=c++11 --coverage -pg -no-pie -fno-builtin -g -O0 -Og -fstrict-aliasing -fstack-protector-all -fsanitize=undefined -fsanitize=bounds -pedantic -Wall -Wextra -Wformat=2 -Wformat-security -Wconversion -Wcast-qual -Wnull-dereference -Wstack-protector -Warray-bounds -Warray-bounds-pointer-arithmetic -Wassign-enum -Wbad-function-cast -Wconditional-uninitialized -Wfloat-equal -Wformat-type-confusion -Widiomatic-parentheses -Wimplicit-fallthrough -Wloop-analysis -Wpointer-arith -Wshift-sign-overflow -Wshorten-64-to-32 -Wswitch-enum -Wtautological-constant-in-range-compare -Wunreachable-code-aggressive -Wthread-safety -Wthread-safety-beta -Wcomma -Wdeclaration-after-statement -D_FORTIFY_SOURCE=3
|
|
14
|
+
LLVM_COV?=llvm-cov-$(LLVM_VERSION)
|
|
15
|
+
CPPCHECK?=cppcheck
|
|
16
|
+
|
|
17
|
+
TESTS_SRC=tests.c
|
|
18
|
+
TESTCXX_SRC=testcxx.cpp
|
|
19
|
+
LIB_SRC=buddy_alloc.h
|
|
20
|
+
BENCH_SRC=bench.c
|
|
21
|
+
BENCH_CFLAGS?=-O2
|
|
22
|
+
|
|
23
|
+
test: tests.out
|
|
24
|
+
rm -f *.gcda
|
|
25
|
+
./tests.out
|
|
26
|
+
$(LLVM_COV) gcov -b $(TESTS_SRC) | paste -s -d ',' | sed -e 's/,,/,\n/' | cut -d ',' -f 1,2,3
|
|
27
|
+
! grep '#####:' $(LIB_SRC).gcov
|
|
28
|
+
! grep -E '^branch\s*[0-9]? never executed$$' $(LIB_SRC).gcov
|
|
29
|
+
|
|
30
|
+
tests.out: $(TESTS_SRC) $(LIB_SRC) test-cppcheck check-recursion
|
|
31
|
+
$(CC) $(CFLAGS) $(TESTS_SRC) -o $@
|
|
32
|
+
|
|
33
|
+
test-cppcheck: $(TESTS_SRC)
|
|
34
|
+
$(CPPCHECK) --error-exitcode=1 --inline-suppr --quiet $^
|
|
35
|
+
|
|
36
|
+
test-cpp-translation-unit: $(TESTCXX_SRC)
|
|
37
|
+
$(CXX) $(CXXFLAGS) $(TESTCXX_SRC) -o $@
|
|
38
|
+
|
|
39
|
+
test-multiplatform: $(TESTS_SRC)
|
|
40
|
+
# 64-bit
|
|
41
|
+
powerpc64-linux-gnu-gcc -static $(TESTS_SRC) && ./a.out
|
|
42
|
+
aarch64-linux-gnu-gcc -static $(TESTS_SRC) && ./a.out
|
|
43
|
+
# 32-bit
|
|
44
|
+
i686-linux-gnu-gcc -static -g tests.c && ./a.out
|
|
45
|
+
powerpc-linux-gnu-gcc -static $(TESTS_SRC) && ./a.out
|
|
46
|
+
|
|
47
|
+
bench: $(BENCH_SRC) $(LIB_SRC)
|
|
48
|
+
$(CC) $(BENCH_CFLAGS) $(BENCH_SRC) -o $@
|
|
49
|
+
./$@
|
|
50
|
+
|
|
51
|
+
check-recursion: $(LIB_SRC)
|
|
52
|
+
[ $$( cflow --no-main $(LIB_SRC) | grep -c 'recursive:' ) -eq "0" ]
|
|
53
|
+
|
|
54
|
+
clean:
|
|
55
|
+
rm -f a.out *.gcda *.gcno *.gcov tests.out bench
|
|
56
|
+
|
|
57
|
+
.PHONY: test clean test-cppcheck
|
|
58
|
+
|
|
59
|
+
.PRECIOUS: tests.out testcxx.out
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# buddy_alloc
|
|
2
|
+
A buddy memory allocator for C, by [Stanislav Paskalev](https://github.com/spaskalev).
|
|
3
|
+
|
|
4
|
+
# Status
|
|
5
|
+
|
|
6
|
+
- [](https://github.com/spaskalev/buddy_alloc/actions/workflows/main.yml) [](https://github.com/spaskalev/buddy_alloc/actions/workflows/codeql.yml)
|
|
7
|
+
- [Latest release](https://github.com/spaskalev/buddy_alloc/releases/latest)
|
|
8
|
+
|
|
9
|
+
## Licensing
|
|
10
|
+
|
|
11
|
+
This project is licensed under the 0BSD license. See the LICENSE.md file for details.
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
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
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- Bounded allocation and deallocation cost
|
|
20
|
+
- Fixed call stack usage, no recursion, no global state
|
|
21
|
+
- C99-compatibility for code and tests
|
|
22
|
+
- 100% line and branch test coverage
|
|
23
|
+
- Supports 32-bit and 64-bit platforms
|
|
24
|
+
- Endian-agnostic, works on both LE and BE
|
|
25
|
+
- Compiles with GCC, Clang, MSVC and Pelles C
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
Run:
|
|
30
|
+
```bash
|
|
31
|
+
$ npm i buddy_alloc.c
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
And then include `buddy_alloc.h` as follows:
|
|
35
|
+
```c
|
|
36
|
+
#include "node_modules/buddy_alloc.c/buddy_alloc.h"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
Initializing and using the buddy allocator with metadata external to the arena is done using the `buddy_init` function.
|
|
42
|
+
|
|
43
|
+
```c
|
|
44
|
+
size_t arena_size = 65536;
|
|
45
|
+
/* You need space for the metadata and for the arena */
|
|
46
|
+
void *buddy_metadata = malloc(buddy_sizeof(arena_size));
|
|
47
|
+
void *buddy_arena = malloc(arena_size);
|
|
48
|
+
struct buddy *buddy = buddy_init(buddy_metadata, buddy_arena, arena_size);
|
|
49
|
+
|
|
50
|
+
/* Allocate using the buddy allocator */
|
|
51
|
+
void *data = buddy_malloc(buddy, 2048);
|
|
52
|
+
/* Free using the buddy allocator */
|
|
53
|
+
buddy_free(buddy, data);
|
|
54
|
+
|
|
55
|
+
free(buddy_metadata);
|
|
56
|
+
free(buddy_arena);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Initializing and using the buddy allocator with metadata internal to the arena is done using the `buddy_embed` function.
|
|
60
|
+
|
|
61
|
+
```c
|
|
62
|
+
size_t arena_size = 65536;
|
|
63
|
+
/* You need space for arena and builtin metadata */
|
|
64
|
+
void *buddy_arena = malloc(arena_size);
|
|
65
|
+
struct buddy *buddy = buddy_embed(buddy_arena, arena_size);
|
|
66
|
+
|
|
67
|
+
/* Allocate using the buddy allocator */
|
|
68
|
+
void *data = buddy_malloc(buddy, 2048);
|
|
69
|
+
/* Free using the buddy allocator */
|
|
70
|
+
buddy_free(buddy, data);
|
|
71
|
+
|
|
72
|
+
free(buddy_arena);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Metadata sizing
|
|
76
|
+
|
|
77
|
+
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.
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
| 64B | 128B | 256B | 512B | 1KB | 2KB | 4KB | 8KB |
|
|
81
|
+
---------+---------+--------+--------+--------+--------+--------+--------+--------+
|
|
82
|
+
8 MB | 65KB | 33KB | 17KB | 9KB | 5KB | 3KB | 2KB | 678B |
|
|
83
|
+
16 MB | 129KB | 65KB | 33KB | 17KB | 9KB | 5KB | 3KB | 2KB |
|
|
84
|
+
32 MB | 257KB | 129KB | 65KB | 33KB | 17KB | 9KB | 5KB | 3KB |
|
|
85
|
+
64 MB | 513KB | 257KB | 129KB | 65KB | 33KB | 17KB | 9KB | 5KB |
|
|
86
|
+
128 MB | 2MB | 513KB | 257KB | 129KB | 65KB | 33KB | 17KB | 9KB |
|
|
87
|
+
256 MB | 3MB | 2MB | 513KB | 257KB | 129KB | 65KB | 33KB | 17KB |
|
|
88
|
+
512 MB | 5MB | 3MB | 2MB | 513KB | 257KB | 129KB | 65KB | 33KB |
|
|
89
|
+
1 GB | 9MB | 5MB | 3MB | 2MB | 513KB | 257KB | 129KB | 65KB |
|
|
90
|
+
2 GB | 17MB | 9MB | 5MB | 3MB | 2MB | 513KB | 257KB | 129KB |
|
|
91
|
+
4 GB | 33MB | 17MB | 9MB | 5MB | 3MB | 2MB | 513KB | 257KB |
|
|
92
|
+
8 GB | 65MB | 33MB | 17MB | 9MB | 5MB | 3MB | 2MB | 513KB |
|
|
93
|
+
16 GB | 129MB | 65MB | 33MB | 17MB | 9MB | 5MB | 3MB | 2MB |
|
|
94
|
+
32 GB | 257MB | 129MB | 65MB | 33MB | 17MB | 9MB | 5MB | 3MB |
|
|
95
|
+
64 GB | 513MB | 257MB | 129MB | 65MB | 33MB | 17MB | 9MB | 5MB |
|
|
96
|
+
128 GB | 1025MB | 513MB | 257MB | 129MB | 65MB | 33MB | 17MB | 9MB |
|
|
97
|
+
256 GB | 2049MB | 1025MB | 513MB | 257MB | 129MB | 65MB | 33MB | 17MB |
|
|
98
|
+
512 GB | 4097MB | 2049MB | 1025MB | 513MB | 257MB | 129MB | 65MB | 33MB |
|
|
99
|
+
1024 GB | 8193MB | 4097MB | 2049MB | 1025MB | 513MB | 257MB | 129MB | 65MB |
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Design
|
|
103
|
+
|
|
104
|
+
The allocator was designed with the following requirements in mind.
|
|
105
|
+
|
|
106
|
+
- Allocation and deallocation operations should behave in a similar and predictable way regardless of the state of the allocator.
|
|
107
|
+
- The allocator's metadata size should be predictable based on the arena's size and not dependent on the state of the allocator.
|
|
108
|
+
- The allocator's metadata location should be external to the arena.
|
|
109
|
+
- Returned memory should be aligned to known and specified block size.
|
|
110
|
+
|
|
111
|
+
The following were not design goals
|
|
112
|
+
|
|
113
|
+
- To be used by multiple threads at the same time without additional locking.
|
|
114
|
+
- To be a general purpose malloc() replacement.
|
|
115
|
+
|
|
116
|
+
## Rationale
|
|
117
|
+
|
|
118
|
+
### Why use a custom allocator (like buddy_alloc) ?
|
|
119
|
+
|
|
120
|
+
A custom allocator is useful where there is no system allocator (e.g. on bare-metal) or when the system allocator does not meet some particular requirements, usually in terms of performance or features. The buddy_alloc custom allocator has bounded performance and bounded storage overhead for its metadata. The bounded performance is important in time-sensitive systems that must perform some action in a given amount of time. The bounded storage overhead is important for ensuring system reliability and allows for upfront system resource planing.
|
|
121
|
+
|
|
122
|
+
A common example of systems that require both bound performance and bounded storage overhead from their components are games and gaming consoles. Games are time-sensitive in multiple aspects - they have to render frames fast to ensure a smooth display and sample input regularly to account for player input. But just fast is not enough - if an allocator is fast on average but occasionally an operation happens to be an order of magnitude slower this will impact both the display of the game as well as the input and may frustrate the player. Games and game consoles are also sensitive to their storage requirements - game consoles usually ship with fixed hardware and game developers have to optimize their games to perform well on the given machines.
|
|
123
|
+
|
|
124
|
+
A custom allocator can supplement the system allocator where needed. A parser that is parsing some structured data (e.g. a json file) may need to allocate objects based on the input's structure. Using the system allocator for this is a risk as the parser may have a bug that causes it to allocate too much or the input may be crafted in such a way. Using a custom allocator with a fixed size for this sort of operations allows the operation to fail safely without impacting the application or the overall system stability.
|
|
125
|
+
|
|
126
|
+
An application developer may also need object allocation that is relocatable. Using memory representation as serialization output is a valid technique and it is used for persistence and replication. The buddy_alloc embedded mode is relocatable allowing it to be serialized and restored to a different memory location, a different process or a different machine altogether (provided matching architecture and binaries).
|
|
127
|
+
|
|
128
|
+
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
|
+
|
|
130
|
+
## Implementation
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
+-------------+ +----------------------------------------------------------+
|
|
134
|
+
| | | The allocator component works with 'struct buddy *' and |
|
|
135
|
+
| allocator +------------------+ is responsible for the allocator interface (malloc/free )|
|
|
136
|
+
| | | and for interfacing with the allocator tree. |
|
|
137
|
+
+------+------+ +----------------------------------------------------------+
|
|
138
|
+
|
|
|
139
|
+
|(uses)
|
|
140
|
+
|
|
|
141
|
+
+------v------+ +------------------------------------------------------+
|
|
142
|
+
| | | The allocator tree is the core internal component. |
|
|
143
|
+
| allocator +-------------------+ It provides the actual allocation and deallocation |
|
|
144
|
+
| tree | | algorithms and uses a binary tree to keep its state. |
|
|
145
|
+
| | +------------------------------------------------------+
|
|
146
|
+
+------+------+
|
|
147
|
+
|
|
|
148
|
+
|(uses)
|
|
149
|
+
|
|
|
150
|
+
+------v------+ +---------------------------------------------------+
|
|
151
|
+
| | | The bitset is the allocator tree backing store. |
|
|
152
|
+
| bitset +-------------------+ |
|
|
153
|
+
| | | The buddy_tree_internal_position_* functions map |
|
|
154
|
+
+-------------+ | a tree position to the bitset. |
|
|
155
|
+
| |
|
|
156
|
+
| The write_to and read_from (internal position) |
|
|
157
|
+
| functions encode and decode values in the bitset. |
|
|
158
|
+
| |
|
|
159
|
+
| Values are encoded in unary with no separators as |
|
|
160
|
+
| the struct internal_position lists their length. |
|
|
161
|
+
| The unary encoding is faster to encode and decode |
|
|
162
|
+
| on unaligned boundaries. |
|
|
163
|
+
+---------------------------------------------------+
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Metadata
|
|
167
|
+
|
|
168
|
+
The allocator uses a bitset-backed perfect binary tree to track allocations. The tree is fixed in size and remains outside of the main arena. This allows for better cache performance in the arena as the cache is not loading allocator metadata when processing application data.
|
|
169
|
+
|
|
170
|
+
### Allocation and deallocation
|
|
171
|
+
|
|
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.
|
|
173
|
+
|
|
174
|
+
#### Fragmentation
|
|
175
|
+
|
|
176
|
+
To minimize fragmentation the allocator will pick the more heavily-used branches when descending the tree to find a free slot. This ensures that larger continuous spans are kept available for larger-sized allocation requests. A minor benefit is that clumping allocations together can allow for better cache performance.
|
|
177
|
+
|
|
178
|
+
### Space requirements
|
|
179
|
+
|
|
180
|
+
The tree is stored in a bitset with each node using just enough bits to store the maximum allocation slot available under it. For leaf nodes this is a single bit. Other nodes sizes depend on the height of the tree.
|
|
181
|
+
|
|
182
|
+
### Non-power-of-two arena sizes
|
|
183
|
+
|
|
184
|
+
The perfect binary tree always tracks an arena which size is a power-of-two. When the allocator is initialized or resized with an arena that is not a perfect fit the binary tree is updated to mask out the virtual arena complement to next power-of-two.
|
|
185
|
+
|
|
186
|
+
### Resizing
|
|
187
|
+
|
|
188
|
+
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
|
+
|
|
190
|
+
## Users
|
|
191
|
+
|
|
192
|
+
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!
|
|
193
|
+
|
|
194
|
+
- Use in game development - [1](https://github.com/spaskalev/buddy_alloc/issues/13#issue-1088282333), [2](https://github.com/fruityloops1/bf-multiplayer/commit/0c2599390566c7a3f174afc6f3c97b6b3efbeb2c)
|
|
195
|
+
- Use in OS kernels - [1](https://github.com/Itay2805/pentagon/blob/1aa005a3f204f40b5869568bd78f4b3087e024a3/kernel/mem/phys.c#L28), [2](https://github.com/spaskalev/buddy_alloc/issues/76), [3](https://github.com/elydre/profanOS/commit/2d43930c36bdc4a5bead2312d7a629e36da4bd78)
|
|
196
|
+
- Use in OS research - [1](https://repositories.lib.utexas.edu/server/api/core/bitstreams/ce9f9383-809a-4cc3-ba0b-e8a5e0428ef4/content), [2](https://www.cs.utexas.edu/~witchel/pubs/zhu24dimes-lupin.pdf), [3](https://upcommons.upc.edu/bitstream/handle/2117/411096/main.pdf;jsessionid=8D64ABBCE67117F7F2BB29A51B72CCCF)
|
|
197
|
+
- Use in user-space software - [1](https://github.com/liulilittle/PPP-2/commit/6da093802ffa541ea4cf6f92b01ef783d493d706)
|
|
198
|
+
- Use in scientific software - [1](https://github.com/ecmwf-ifs/field_api)
|
|
199
|
+
|
|
200
|
+
<br>
|
|
201
|
+
<br>
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
[](https://nodef.github.io)
|
|
205
|
+

|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
Please report all issues via [GitHub's issues page](https://github.com/spaskalev/buddy_alloc/issues).
|
|
4
|
+
|
|
5
|
+
If your report contains confidential information you can use GitHub's private reporting of a security vulnerability as documented [here](https://docs.github.com/en/code-security/security-advisories/guidance-on-reporting-and-writing-information-about-vulnerabilities/privately-reporting-a-security-vulnerability).
|