Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple allocators, add TLSF allocator and create cmake switches #83

Draft
wants to merge 128 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
9b1cdba
Added logs
EngineersBox Jan 13, 2024
9d0bd9f
Added logged fix
EngineersBox Jan 13, 2024
56d7f53
Removed logs
EngineersBox Jan 13, 2024
9747287
Fixed size extend check in realloc
EngineersBox Jan 14, 2024
ada15b3
Fixed size usage overlapping BlockHeaders
EngineersBox Jan 14, 2024
738f90d
Fixed usage of sizing and sbrk invocations
EngineersBox Jan 14, 2024
66d1e78
Fixed condition for realloc resizing to contiguously available free mem
EngineersBox Jan 14, 2024
9ca3ccc
Optimisations to free implementation
EngineersBox Jan 14, 2024
3172c52
Added debug logs
EngineersBox Jan 15, 2024
4d814f0
Added debug logs
EngineersBox Jan 15, 2024
37faa71
Added debug logs
EngineersBox Jan 15, 2024
b91b1d0
Added debug logs
EngineersBox Jan 15, 2024
8c919d8
Added debug logs
EngineersBox Jan 15, 2024
e6f258f
Added debug logs
EngineersBox Jan 16, 2024
343d768
Readded free sbrk
EngineersBox Jan 17, 2024
8ebf6f8
Removed free changes
EngineersBox Jan 17, 2024
2eb6961
Removed free changes
EngineersBox Jan 17, 2024
4a7d0cb
Removed free changes
EngineersBox Jan 17, 2024
f794183
Removed free changes
EngineersBox Jan 17, 2024
14e8b88
Removed free changes
EngineersBox Jan 17, 2024
a7ad145
Fixed size shift in free
EngineersBox Jan 18, 2024
6701b74
Fixed size shift in free
EngineersBox Jan 18, 2024
3b24503
Migrated to TLSF
EngineersBox Jan 18, 2024
e0fde1d
Migrated to TLSF
EngineersBox Jan 18, 2024
6277b2c
Migrated to TLSF
EngineersBox Jan 18, 2024
f3da282
Migrated to TLSF
EngineersBox Jan 18, 2024
e9c66a9
Migrated to TLSF
EngineersBox Jan 18, 2024
de5f81e
Migrated to TLSF
EngineersBox Jan 18, 2024
65255f7
Migrated to TLSF
EngineersBox Jan 18, 2024
ab7ba50
Migrated to TLSF
EngineersBox Jan 18, 2024
b6ef6b9
Migrated to TLSF
EngineersBox Jan 18, 2024
b520402
Reverted from TLSF
EngineersBox Jan 18, 2024
91e080f
Reverted from TLSF
EngineersBox Jan 18, 2024
dd1fc33
Fixed prints
EngineersBox Jan 18, 2024
e265713
Fixed prints
EngineersBox Jan 18, 2024
7064335
Fixed prints
EngineersBox Jan 18, 2024
edcd9b2
Fixed prints
EngineersBox Jan 18, 2024
ff02ba5
Fixed prints
EngineersBox Jan 18, 2024
0fd2163
Fixed prints
EngineersBox Jan 18, 2024
6f28a22
Fixed prints
EngineersBox Jan 18, 2024
8ed1a11
Fixed prints
EngineersBox Jan 18, 2024
01abbbd
Added print statements
EngineersBox Jan 18, 2024
ab7c7e3
Added print statements
EngineersBox Jan 18, 2024
6930571
Added print statements
EngineersBox Jan 18, 2024
28a03aa
Added print statements
EngineersBox Jan 18, 2024
8f2907a
Added print statements
EngineersBox Jan 18, 2024
811add5
Reverted changes to indexing
EngineersBox Jan 20, 2024
1ad7cda
Fixed free at end size calculation
EngineersBox Jan 20, 2024
290c399
Fixed free at end size calculation
EngineersBox Jan 20, 2024
4be9c1e
Fixed free at end size calculation
EngineersBox Jan 20, 2024
6571318
Added logs
EngineersBox Jan 20, 2024
a29dd9d
Added logs
EngineersBox Jan 20, 2024
7eb71b7
Added logs
EngineersBox Jan 20, 2024
27fa532
Added logs
EngineersBox Jan 20, 2024
7c85dad
Added logs
EngineersBox Jan 20, 2024
c0a785a
Added logs
EngineersBox Jan 20, 2024
76df894
Added logs
EngineersBox Jan 20, 2024
238e2f2
Added logs
EngineersBox Jan 20, 2024
6022431
Made free heap traversal conditional on define
EngineersBox Jan 20, 2024
3540831
Commented out logs
EngineersBox Jan 20, 2024
1a3643b
Commented out logs
EngineersBox Jan 20, 2024
62e446f
Commented out logs
EngineersBox Jan 20, 2024
a38d944
Fixed indentation and removed comments
EngineersBox Jan 21, 2024
6434be5
Conformed formatting
EngineersBox Jan 21, 2024
fd6c2ed
Conformed formatting
EngineersBox Jan 21, 2024
d02e1e2
Conformed formatting
EngineersBox Jan 21, 2024
214a920
Conformed formatting
EngineersBox Jan 21, 2024
5502d08
Added SMD pimritive type/lighting/coloring macro definitions
EngineersBox Feb 3, 2024
9dd23c4
Fixed formatting
EngineersBox Feb 3, 2024
f40c838
Fixed formatting
EngineersBox Feb 3, 2024
bdb0384
Fixed formatting
EngineersBox Feb 3, 2024
282406b
Fixed formatting
EngineersBox Feb 3, 2024
eb0b1bb
Added pimitive defines
EngineersBox Feb 3, 2024
65c4f84
Added TLSF allocator
EngineersBox Feb 3, 2024
94cde3a
Added TLSF allocator
EngineersBox Feb 3, 2024
9f54387
Implemented allocator interface and AFF/TLSF handlers
EngineersBox Feb 4, 2024
5f37f48
Implemented allocator interface and AFF/TLSF handlers
EngineersBox Feb 4, 2024
e42b237
Implemented allocator interface and AFF/TLSF handlers
EngineersBox Feb 4, 2024
ecd8b28
Implemented allocator interface and AFF/TLSF handlers
EngineersBox Feb 4, 2024
2c281a7
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 5, 2024
d190b06
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 5, 2024
12dbd09
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 5, 2024
ebc1a2e
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 5, 2024
b6cf9cb
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 5, 2024
7fb1239
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
a425fce
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
91e385f
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
daf70e9
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
5500ad6
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
9ec9ea3
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
773eab9
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
3f3e7af
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
5fb2d0c
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
659a679
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
fd16f01
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
5190f18
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
a3f319f
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
ad72b30
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
68b6adb
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
342b5a6
Added Cmake definitions to allow specifying compiler
EngineersBox Feb 7, 2024
ccf4734
Fixed include guards for allocator implementations
EngineersBox Feb 7, 2024
23dbcfe
Fixed include guards for allocator implementations
EngineersBox Feb 7, 2024
20432e5
Fixed include guards for allocator implementations
EngineersBox Feb 7, 2024
0988e73
Fixed include guards for allocator implementations
EngineersBox Feb 7, 2024
bf64110
Fixed attributes for malloc/calloc/realloc/free
EngineersBox Feb 7, 2024
48e929a
Fixed attributes for malloc/calloc/realloc/free
EngineersBox Feb 7, 2024
3fec0c2
Fixed attributes for malloc/calloc/realloc/free
EngineersBox Feb 7, 2024
bd22a09
Fixed warnings for _sdk_assert_abort
EngineersBox Feb 7, 2024
1c804e4
Marked _assert_abort and abort with noreturn attribute
EngineersBox Apr 5, 2024
6194155
Added heap tracking to TLSF allocator
EngineersBox Jul 27, 2024
cef8032
Added heap tracking to TLSF allocator
EngineersBox Jul 27, 2024
dc91dcc
Added heap tracking to TLSF allocator
EngineersBox Jul 27, 2024
3f98423
Added heap tracking to TLSF allocator
EngineersBox Jul 27, 2024
fcab585
Remove malloc.h redefines of stdlib.h
EngineersBox Jul 29, 2024
7122f86
Remove malloc.h redefines of stdlib.h
EngineersBox Jul 29, 2024
ba94d81
Removed global definition spot for allocators and left delegation to …
EngineersBox Jul 29, 2024
248105e
Removed global definition spot for allocators and left delegation to …
EngineersBox Jul 29, 2024
69f3666
Removed global definition spot for allocators and left delegation to …
EngineersBox Jul 29, 2024
7e56c17
Refactored to source only
EngineersBox Jul 29, 2024
db7d59e
Refactored to source only
EngineersBox Jul 29, 2024
0536cdb
Removed empty header and added attributes
EngineersBox Jul 29, 2024
47509bd
Renamed MALLOC_IMPL to SDK_ALLOC_IMPL
EngineersBox Jul 29, 2024
34b50a0
Fixed formatting
EngineersBox Jul 29, 2024
58c932a
Removed cmake debugging logs
EngineersBox Jul 29, 2024
5f9adc6
Fixed formatting of TLSF code
EngineersBox Jul 30, 2024
35f15c4
Fixed formatting of TLSF code
EngineersBox Jul 30, 2024
572b690
Updated documentation
EngineersBox Jul 30, 2024
5261496
Updated documentation
EngineersBox Jul 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ set(
CACHE STRING "Git commit hash (used by CI)"
)

set(PSN00BSDK_LIBC_ALLOCATORS CUSTOM AFF TLSF)
set(
PSN00BSDK_LIBC_ALLOCATOR AFF
CACHE STRING "Allocator to use in libc implementation (one of ${PSN00BSDK_LIBC_ALLOCATORS})"
)

# Attempt to automatically select a suitable CMake generator to build libpsn00b
# and the examples. Only Ninja and makefile-based generators can be used, as
# other generators (VS and Xcode) do not allow custom toolchains to be used.
Expand All @@ -73,6 +79,12 @@ if(NOT LIBPSN00B_GENERATOR MATCHES ".*(Make|Makefiles|Ninja)( Multi-Config)?$")
endif()
endif()

list(FIND PSN00BSDK_LIBC_ALLOCATORS ${PSN00BSDK_LIBC_ALLOCATOR} index)
if (index EQUAL -1)
message(FATAL_ERROR "Invalid allocator: ${PSN00BSDK_LIBC_ALLOCATOR} (must be CUSTOM, AFF or TLSF)")
endif()
set(PSN00BSDK_LIBC_ALLOCATOR ${index})

string(TIMESTAMP PSN00BSDK_BUILD_DATE UTC)

# Forward some important variables to mkpsxiso and to the subprojects (they are
Expand All @@ -89,6 +101,8 @@ set(
-DPSN00BSDK_BUILD_DATE:STRING=${PSN00BSDK_BUILD_DATE}
-DPSN00BSDK_GIT_TAG:STRING=${PSN00BSDK_GIT_TAG}
-DPSN00BSDK_GIT_COMMIT:STRING=${PSN00BSDK_GIT_COMMIT}
-DPSN00BSDK_LIBC_ALLOCATOR:STRING=${PSN00BSDK_LIBC_ALLOCATOR}
-DPSN00BSDK_LIBC_ALLOCATORS:STRING=${PSN00BSDK_LIBC_ALLOCATORS}
)
set(
_tools_args
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ the basics of the graphics and controller APIs.

* `libpsxpress`: Add an API for SPU-ADPCM audio encoding at runtime.

* `libc`: Improve the memory allocation framework with multiple allocators,
replace the string functions with optimized implementations and maybe add
* `libc`: Replace the string functions with optimized implementations and maybe add
helpers to manage swapping between main RAM and VRAM/SPU RAM.

* Add a full controller and memory card API that does not depend on the BIOS
Expand Down
41 changes: 41 additions & 0 deletions doc/allocator_implementation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Allocator Implementation

Supplying a custom allocator implementation can be done via the
`CUSTOM` value supplied to the `PSN00BSDK_SDK_ALLOCATOR` cmake
parameter.

## Functions

Using this variant requires the user to implement several calls
that are bound as weak functions (via `__attribute__((weak))`)
within the libc implementation. You are required to implement
the following to satisfy the `stdlib.h` header:

```c
void InitHeap(void* addr, size_t size);
void TrackHeapUsage(ptrdiff_t alloc_incr);
void GetHeapUsage(HeapUsage* usage);

void* malloc(size_t size);
void* calloc(size_t num, size_t size);
void* realloc(void* ptr, size_t size);
void free(void* ptr);
```

For reference implementations, see the [aff.c](../libpsn00b/libc/aff.c) and [tlsf.c](../libpsn00b/libc/tlsf.c)
sources within the SDK.

## Placeholder Details

Internally, the default placeholder implementations that are bound when
you provide the `CUSTOM` variant via cmake all contain logging statements
that will cause the application to terminate immediately upon invocation
of any allocator function calls. For example:

```c
void* malloc(size_t size) {
_sdk_log("[ERROR] Unimplemented custom allocator handle: void* malloc(size_t)\n");
abort();
return NULL;
}
```
17 changes: 17 additions & 0 deletions doc/cmake_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ rather than setting this variable.
`PSN00BSDK_TARGET` must be set regardless of whether or not `PSN00BSDK_TC` is
also set.

### `PSN00BSDK_LIBC_ALLOCATOR` (`STRING`)

Allocator implementation to use for the libc memory allocation API functions.
This MUST be one of the following:

1. `AFF`: Allocated first-fit, similar to that of the standard glibc allocator.
2. `TLSF`: Two-level segregated-fit designed for latency sensitive applications.
It has a bounded response time, performs faster than glibc-like allocators
and has a lower base overhead and per-allocation overhead.
3. `CUSTOM`: User-defined implementation of the allocation primitives.

For details on the specific functions required to be implemented as well as any
additional details, see the documentation on [allocator implementation](./allocator_implementation.md).

**IMPORTANT**: If an allocator is not specified when compliling the SDK, the `AFF` allocator will
be used by default.

## Internal settings

These settings are not stored in CMake's cache and can only be set from within
Expand Down
1 change: 1 addition & 0 deletions libpsn00b/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ foreach(_library IN LISTS PSN00BSDK_LIBRARIES)
)

target_compile_definitions(${_name} PRIVATE SDK_LIBRARY_NAME="${_library}")
target_compile_definitions(${_name} PRIVATE SDK_ALLOC_IMPL=${PSN00BSDK_LIBC_ALLOCATOR})
endforeach()
endforeach()

Expand Down
3 changes: 3 additions & 0 deletions libpsn00b/cmake/internal_setup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ if(TARGET psn00bsdk)
endif()
link_libraries(-lgcc)

# Guaranteed to not be -1 here
set(PSN00BSDK_LIBC_ALLOCATOR ${index})

# DON'T CHANGE THE ORDER or you'll break the libraries' internal dependencies.
set(
PSN00BSDK_LIBRARIES
Expand Down
8 changes: 7 additions & 1 deletion libpsn00b/include/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
extern "C" {
#endif

void _assert_abort(const char *file, int line, const char *expr);
__attribute__((noreturn)) void _assert_abort(const char *file, int line, const char *expr);

#ifdef __cplusplus
}
Expand All @@ -25,6 +25,7 @@ void _assert_abort(const char *file, int line, const char *expr);
#define assert(expr)
#define _sdk_log(fmt, ...)
#define _sdk_assert(expr, fmt, ...)
#define _sdk_assert_abort(expr, fmt, ...)
#define _sdk_validate_args_void(expr)
#define _sdk_validate_args(expr, ret)

Expand All @@ -46,6 +47,11 @@ void _assert_abort(const char *file, int line, const char *expr);
_sdk_log(fmt, __VA_ARGS__); \
return ret; \
}
#define _sdk_assert_abort(expr, fmt, ...) \
if (!(expr)) { \
_sdk_log(fmt, __VA_ARGS__); \
assert(expr); \
}
#define _sdk_validate_args_void(expr) \
if (!(expr)) { \
_sdk_log("invalid args to %s() (%s)\n", __func__, #expr); \
Expand Down
4 changes: 2 additions & 2 deletions libpsn00b/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C" {
extern int __argc;
extern const char **__argv;

void abort(void);
__attribute__((noreturn)) void abort(void);

int abs(int value);
int rand(void);
Expand All @@ -42,9 +42,9 @@ long long strtoll(const char *str, char **str_end, int base);
//double strtod(const char *str, char **str_end);
//long double strtold(const char *str, char **str_end);

void InitHeap(void *addr, size_t size);
void *sbrk(ptrdiff_t incr);

void InitHeap(void *addr, size_t size);
void TrackHeapUsage(ptrdiff_t alloc_incr);
void GetHeapUsage(HeapUsage *usage);

Expand Down
Loading