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

Wmem new memory allocator #213

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions GNUmakefile.os4
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ WARNINGS := \
-Wundef -Wmissing-declarations -Wunused -Wwrite-strings -Wno-unused-value -Wno-comment -Wno-missing-braces \
-Wno-deprecated-declarations -Wno-sign-compare -Wno-unused-variable -Wno-parentheses -Wno-missing-prototypes \
-Wstrict-aliasing -Wno-shadow -Wno-discarded-qualifiers -Wno-unused-function -Wno-unused-parameter -Wno-strict-aliasing \
-Wno-type-limits -Wno-cast-function-type -Wno-frame-address -Werror \
-Wno-type-limits -Wno-cast-function-type -Wno-frame-address -Wno-error=unused-but-set-variable -Werror \
# -Wbad-function-cast -Wconversion -Wformat

PIC := -fPIC -DPIC
Expand Down Expand Up @@ -106,7 +106,8 @@ INCLUDES := -I$(LIB_DIR)/include \
-I$(LIB_DIR)/termios \
-I$(LIB_DIR)/time \
-I$(LIB_DIR)/unistd \
-I$(LIB_DIR)/wchar
-I$(LIB_DIR)/wchar \
-I$(LIB_DIR)/wmem

SHARED := $(if $(SHARED),$(SHARED),yes)
STATIC := $(if $(STATIC),$(STATIC),yes)
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,19 @@ have problems running existent software!

### New memory allocator

Clib4 now use `Wheel Of Fortune` allocator that is faster than previous one, and it seems more robust and with a cleaner
and portable code
Clib4 now use `wmem` allocator from WireShark that is faster than previous one, and it seems more robust and with a cleaner
and portable code.
You can choose at runtime (for test purpose) which kind of allocators want to use setting `CLIB4_MEMORY_ALLOCATOR` env variable.
At moment you can use:

| Value | Allocator |
|-------|---------------------------|
| 1 | WMEM_ALLOCATOR_SIMPLE |
| 2 | WMEM_ALLOCATOR_BLOCK |
| 3 | WMEM_ALLOCATOR_STRICT |
| 4 | WMEM_ALLOCATOR_BLOCK_FAST |

The default one is `WMEM_ALLOCATOR_BLOCK_STRICT`. `WMEM_ALLOCATOR_BLOCK_FAST` can crash on some situation. Please refer to `wmem/wmem_core.h` for all details.

### Optimized AMCC functions

Expand Down
23 changes: 20 additions & 3 deletions libc.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,7 @@ C_STDLIB := \
stdlib/udivsi4.o \
stdlib/umodsi3.o \
stdlib/unsetenv.o \
stdlib/valloc.o \
stdlib/wof_allocator.o
stdlib/valloc.o

C_STRING := \
string/strings_ffs.o \
Expand Down Expand Up @@ -923,6 +922,23 @@ C_WCHAR := \
wchar/wctrans.o \
wchar/wctype.o

C_WMEM := \
wmem/wmem_allocator_block.o \
wmem/wmem_allocator_block_fast.o \
wmem/wmem_allocator_simple.o \
wmem/wmem_allocator_strict.o \
wmem/wmem_array.o \
wmem/wmem_core.o \
wmem/wmem_interval_tree.o \
wmem/wmem_list.o \
wmem/wmem_map.o \
wmem/wmem_miscutl.o \
wmem/wmem_multimap.o \
wmem/wmem_stack.o \
wmem/wmem_strutl.o \
wmem/wmem_tree.o \
wmem/wmem_user_cb.o

ifdef SPE
$(info Adding SPE objects)
C_STRING := $(C_STRING) \
Expand Down Expand Up @@ -969,7 +985,8 @@ C_LIBRARY := \
$(C_TIME) \
$(C_UNISTD) \
$(C_USERGROUP) \
$(C_WCHAR)
$(C_WCHAR) \
$(C_WMEM)

C_LIB := \
c.lib_rev.o \
Expand Down
6 changes: 3 additions & 3 deletions library/c.lib_rev.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define REVISION 0
#define SUBREVISION 0

#define DATE "25.09.2024"
#define DATE "23.10.2024"
#define VERS "clib4.library 1.0.0"
#define VSTRING "clib4.library 1.0.0 (25.09.2024)\r\n"
#define VERSTAG "\0$VER: clib4.library 1.0.0 (25.09.2024)"
#define VSTRING "clib4.library 1.0.0 (23.10.2024)\r\n"
#define VERSTAG "\0$VER: clib4.library 1.0.0 (23.10.2024)"
7 changes: 5 additions & 2 deletions library/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#include "include/wchar.h"
#include "include/setjmp.h"
#include "include/resolv.h"

#include "wmem_allocator.h"

#include <exec/types.h>
#include <exec/lists.h>
#include <exec/semaphores.h>
Expand Down Expand Up @@ -210,7 +213,7 @@ struct _clib4 {
BOOL __fully_initialized;
int32_t __pipenum;
void *__pipe_semaphore;
BOOL unused3;
short __wof_mem_allocator_type;

/* This is used with the dlopen(), dlclose() and dlsym() functions. */
void *__dl_root_handle; //Elf32_Handle
Expand Down Expand Up @@ -293,7 +296,7 @@ struct _clib4 {
struct SignalSemaphore *stdio_lock;

/* Wof Allocator main pointer */
wof_allocator_t *__wof_allocator;
wmem_allocator_t *__wmem_allocator;
void *unused6;

/* Names of files and directories to delete when shutting down. */
Expand Down
1 change: 1 addition & 0 deletions library/libc_init_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ reent_init(struct _clib4 *__clib4, BOOL fallback) {
.__children = 1,
.term_entry = NULL,
.__was_sig = -1,
.__wof_mem_allocator_type = WMEM_ALLOCATOR_BLOCK,
};

if (!__clib4->__random_lock || !__clib4->__pipe_semaphore) {
Expand Down
2 changes: 1 addition & 1 deletion library/stdlib/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ __free_r(struct _clib4 *__clib4, void *ptr) {
ItemPoolFree(__clib4->__memalign_pool, e);
e = NULL;
} else {
wof_free(__clib4->__wof_allocator, ptr);
wmem_free(__clib4->__wmem_allocator, ptr);
}

__memory_unlock(__clib4);
Expand Down
19 changes: 19 additions & 0 deletions library/stdlib/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,13 @@ _main(
struct _clib4 *__clib4 = NULL;
uint32 pid = GetPID(0, GPID_PROCESS);
struct Clib4Resource *res = (APTR) OpenResource(RESOURCE_NAME);
char envbuf[256 + 1];
LONG len;

DECLARE_UTILITYBASE();

ClearMem(envbuf, 257);

/* Pick up the Workbench startup message, if available. */
me = (struct Process *) FindTask(NULL);
if (!me->pr_CLI) {
Expand Down Expand Up @@ -292,6 +296,21 @@ _main(

__clib4->__WBenchMsg = sms;

/* Check if user has choosen a different memory allocator and this needs to be called before constructors
* sice malloc constructor will use __wof_mem_allocator_type field
*/
if ((len = GetVar("CLIB4_MEMORY_ALLOCATOR", envbuf, sizeof(envbuf), 0)) >= 0) {
if (!Stricmp(envbuf, "1"))
__clib4->__wof_mem_allocator_type = WMEM_ALLOCATOR_SIMPLE;
else if (!Stricmp(envbuf, "2"))
__clib4->__wof_mem_allocator_type = WMEM_ALLOCATOR_BLOCK;
else if (!Stricmp(envbuf, "3"))
__clib4->__wof_mem_allocator_type = WMEM_ALLOCATOR_STRICT;
else if (!Stricmp(envbuf, "4"))
__clib4->__wof_mem_allocator_type = WMEM_ALLOCATOR_BLOCK_FAST; // WARNING - At moment this is crashing
// else leave the default one
}

/* After reent structure we can call clib4 constructors */
SHOWMSG("Calling clib4 ctors");
_start_ctors(__CTOR_LIST__);
Expand Down
12 changes: 6 additions & 6 deletions library/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ __malloc_r(struct _clib4 *__clib4, size_t size) {

__memory_lock(__clib4);

result = wof_alloc(__clib4->__wof_allocator, size);
result = wmem_alloc(__clib4->__wmem_allocator, size);

if (!result)
__set_errno_r(__clib4, ENOMEM);
Expand All @@ -54,9 +54,9 @@ STDLIB_DESTRUCTOR(stdlib_memory_exit) {

__memory_lock(__clib4);

if (__clib4->__wof_allocator != NULL) {
wof_allocator_destroy(__clib4->__wof_allocator);
__clib4->__wof_allocator = NULL;
if (__clib4->__wmem_allocator != NULL) {
wmem_destroy_allocator(__clib4->__wmem_allocator);
__clib4->__wmem_allocator = NULL;
}

__memory_unlock(__clib4);
Expand All @@ -80,8 +80,8 @@ STDLIB_CONSTRUCTOR(stdlib_memory_init) {
if (__clib4->memory_semaphore == NULL)
goto out;

__clib4->__wof_allocator = wof_allocator_new();
if (__clib4->__wof_allocator == NULL) {
__clib4->__wmem_allocator = wmem_allocator_new(__clib4->__wof_mem_allocator_type); // make this dynamic
if (__clib4->__wmem_allocator == NULL) {
__delete_semaphore(__clib4->memory_semaphore);
__clib4->memory_semaphore = NULL;
goto out;
Expand Down
2 changes: 1 addition & 1 deletion library/stdlib/realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ realloc(void *ptr, size_t size) {

__memory_lock(__clib4);

result = wof_realloc(__clib4->__wof_allocator, ptr, size);
result = wmem_realloc(__clib4->__wmem_allocator, ptr, size);
if (result == NULL) {
SHOWMSG("could not reallocate memory");
}
Expand Down
3 changes: 2 additions & 1 deletion library/stdlib/stdlib_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

#include <stddef.h>

#include "wof_allocator.h"
#include "wmem_core.h"
#include "wmem_allocator.h"

/* We shuffle things around a bit for the debug code. This works by joining
related code which shares the same name. The debug code symbols also have
Expand Down
Loading