Skip to content

Commit 1168258

Browse files
committed
Changed memory allocator to wmem from WireShark project
1 parent 932dd1f commit 1168258

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+6964
-962
lines changed

GNUmakefile.os4

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ WARNINGS := \
7272
-Wundef -Wmissing-declarations -Wunused -Wwrite-strings -Wno-unused-value -Wno-comment -Wno-missing-braces \
7373
-Wno-deprecated-declarations -Wno-sign-compare -Wno-unused-variable -Wno-parentheses -Wno-missing-prototypes \
7474
-Wstrict-aliasing -Wno-shadow -Wno-discarded-qualifiers -Wno-unused-function -Wno-unused-parameter -Wno-strict-aliasing \
75-
-Wno-type-limits -Wno-cast-function-type -Wno-frame-address -Werror \
75+
-Wno-type-limits -Wno-cast-function-type -Wno-frame-address -Wno-error=unused-but-set-variable -Werror \
7676
# -Wbad-function-cast -Wconversion -Wformat
7777

7878
PIC := -fPIC -DPIC
@@ -106,7 +106,8 @@ INCLUDES := -I$(LIB_DIR)/include \
106106
-I$(LIB_DIR)/termios \
107107
-I$(LIB_DIR)/time \
108108
-I$(LIB_DIR)/unistd \
109-
-I$(LIB_DIR)/wchar
109+
-I$(LIB_DIR)/wchar \
110+
-I$(LIB_DIR)/wmem
110111

111112
SHARED := $(if $(SHARED),$(SHARED),yes)
112113
STATIC := $(if $(STATIC),$(STATIC),yes)

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,19 @@ have problems running existent software!
5353

5454
### New memory allocator
5555

56-
Clib4 now use `Wheel Of Fortune` allocator that is faster than previous one, and it seems more robust and with a cleaner
57-
and portable code
56+
Clib4 now use `wmem` allocator from WireShark that is faster than previous one, and it seems more robust and with a cleaner
57+
and portable code.
58+
You can choose at runtime (for test purpose) which kind of allocators want to use setting `CLIB4_MEMORY_ALLOCATOR` env variable.
59+
At moment you can use:
60+
61+
| Value | Allocator |
62+
|-------|---------------------------|
63+
| 1 | WMEM_ALLOCATOR_SIMPLE |
64+
| 2 | WMEM_ALLOCATOR_BLOCK |
65+
| 3 | WMEM_ALLOCATOR_STRICT |
66+
| 4 | WMEM_ALLOCATOR_BLOCK_FAST |
67+
68+
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.
5869

5970
### Optimized AMCC functions
6071

libc.gmk

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,7 @@ C_STDLIB := \
549549
stdlib/udivsi4.o \
550550
stdlib/umodsi3.o \
551551
stdlib/unsetenv.o \
552-
stdlib/valloc.o \
553-
stdlib/wof_allocator.o
552+
stdlib/valloc.o
554553

555554
C_STRING := \
556555
string/strings_ffs.o \
@@ -923,6 +922,23 @@ C_WCHAR := \
923922
wchar/wctrans.o \
924923
wchar/wctype.o
925924

925+
C_WMEM := \
926+
wmem/wmem_allocator_block.o \
927+
wmem/wmem_allocator_block_fast.o \
928+
wmem/wmem_allocator_simple.o \
929+
wmem/wmem_allocator_strict.o \
930+
wmem/wmem_array.o \
931+
wmem/wmem_core.o \
932+
wmem/wmem_interval_tree.o \
933+
wmem/wmem_list.o \
934+
wmem/wmem_map.o \
935+
wmem/wmem_miscutl.o \
936+
wmem/wmem_multimap.o \
937+
wmem/wmem_stack.o \
938+
wmem/wmem_strutl.o \
939+
wmem/wmem_tree.o \
940+
wmem/wmem_user_cb.o
941+
926942
ifdef SPE
927943
$(info Adding SPE objects)
928944
C_STRING := $(C_STRING) \
@@ -969,7 +985,8 @@ C_LIBRARY := \
969985
$(C_TIME) \
970986
$(C_UNISTD) \
971987
$(C_USERGROUP) \
972-
$(C_WCHAR)
988+
$(C_WCHAR) \
989+
$(C_WMEM)
973990

974991
C_LIB := \
975992
c.lib_rev.o \

library/c.lib_rev.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define REVISION 0
33
#define SUBREVISION 0
44

5-
#define DATE "25.09.2024"
5+
#define DATE "23.10.2024"
66
#define VERS "clib4.library 1.0.0"
7-
#define VSTRING "clib4.library 1.0.0 (25.09.2024)\r\n"
8-
#define VERSTAG "\0$VER: clib4.library 1.0.0 (25.09.2024)"
7+
#define VSTRING "clib4.library 1.0.0 (23.10.2024)\r\n"
8+
#define VERSTAG "\0$VER: clib4.library 1.0.0 (23.10.2024)"

library/dos.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "include/wchar.h"
1616
#include "include/setjmp.h"
1717
#include "include/resolv.h"
18+
19+
#include "wmem_allocator.h"
20+
1821
#include <exec/types.h>
1922
#include <exec/lists.h>
2023
#include <exec/semaphores.h>
@@ -210,7 +213,7 @@ struct _clib4 {
210213
BOOL __fully_initialized;
211214
int32_t __pipenum;
212215
void *__pipe_semaphore;
213-
BOOL unused3;
216+
short __wof_mem_allocator_type;
214217

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

295298
/* Wof Allocator main pointer */
296-
wof_allocator_t *__wof_allocator;
299+
wmem_allocator_t *__wmem_allocator;
297300
void *unused6;
298301

299302
/* Names of files and directories to delete when shutting down. */

library/libc_init_global.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ reent_init(struct _clib4 *__clib4, BOOL fallback) {
296296
.__children = 1,
297297
.term_entry = NULL,
298298
.__was_sig = -1,
299+
.__wof_mem_allocator_type = WMEM_ALLOCATOR_BLOCK,
299300
};
300301

301302
if (!__clib4->__random_lock || !__clib4->__pipe_semaphore) {

library/stdlib/free.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ __free_r(struct _clib4 *__clib4, void *ptr) {
3737
ItemPoolFree(__clib4->__memalign_pool, e);
3838
e = NULL;
3939
} else {
40-
wof_free(__clib4->__wof_allocator, ptr);
40+
wmem_free(__clib4->__wmem_allocator, ptr);
4141
}
4242

4343
__memory_unlock(__clib4);

library/stdlib/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,13 @@ _main(
241241
struct _clib4 *__clib4 = NULL;
242242
uint32 pid = GetPID(0, GPID_PROCESS);
243243
struct Clib4Resource *res = (APTR) OpenResource(RESOURCE_NAME);
244+
char envbuf[256 + 1];
245+
LONG len;
244246

245247
DECLARE_UTILITYBASE();
246248

249+
ClearMem(envbuf, 257);
250+
247251
/* Pick up the Workbench startup message, if available. */
248252
me = (struct Process *) FindTask(NULL);
249253
if (!me->pr_CLI) {
@@ -292,6 +296,21 @@ _main(
292296

293297
__clib4->__WBenchMsg = sms;
294298

299+
/* Check if user has choosen a different memory allocator and this needs to be called before constructors
300+
* sice malloc constructor will use __wof_mem_allocator_type field
301+
*/
302+
if ((len = GetVar("CLIB4_MEMORY_ALLOCATOR", envbuf, sizeof(envbuf), 0)) >= 0) {
303+
if (!Stricmp(envbuf, "1"))
304+
__clib4->__wof_mem_allocator_type = WMEM_ALLOCATOR_SIMPLE;
305+
else if (!Stricmp(envbuf, "2"))
306+
__clib4->__wof_mem_allocator_type = WMEM_ALLOCATOR_BLOCK;
307+
else if (!Stricmp(envbuf, "3"))
308+
__clib4->__wof_mem_allocator_type = WMEM_ALLOCATOR_STRICT;
309+
else if (!Stricmp(envbuf, "4"))
310+
__clib4->__wof_mem_allocator_type = WMEM_ALLOCATOR_BLOCK_FAST; // WARNING - At moment this is crashing
311+
// else leave the default one
312+
}
313+
295314
/* After reent structure we can call clib4 constructors */
296315
SHOWMSG("Calling clib4 ctors");
297316
_start_ctors(__CTOR_LIST__);

library/stdlib/malloc.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ __malloc_r(struct _clib4 *__clib4, size_t size) {
2727

2828
__memory_lock(__clib4);
2929

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

3232
if (!result)
3333
__set_errno_r(__clib4, ENOMEM);
@@ -54,9 +54,9 @@ STDLIB_DESTRUCTOR(stdlib_memory_exit) {
5454

5555
__memory_lock(__clib4);
5656

57-
if (__clib4->__wof_allocator != NULL) {
58-
wof_allocator_destroy(__clib4->__wof_allocator);
59-
__clib4->__wof_allocator = NULL;
57+
if (__clib4->__wmem_allocator != NULL) {
58+
wmem_destroy_allocator(__clib4->__wmem_allocator);
59+
__clib4->__wmem_allocator = NULL;
6060
}
6161

6262
__memory_unlock(__clib4);
@@ -80,8 +80,8 @@ STDLIB_CONSTRUCTOR(stdlib_memory_init) {
8080
if (__clib4->memory_semaphore == NULL)
8181
goto out;
8282

83-
__clib4->__wof_allocator = wof_allocator_new();
84-
if (__clib4->__wof_allocator == NULL) {
83+
__clib4->__wmem_allocator = wmem_allocator_new(__clib4->__wof_mem_allocator_type); // make this dynamic
84+
if (__clib4->__wmem_allocator == NULL) {
8585
__delete_semaphore(__clib4->memory_semaphore);
8686
__clib4->memory_semaphore = NULL;
8787
goto out;

library/stdlib/realloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ realloc(void *ptr, size_t size) {
2121

2222
__memory_lock(__clib4);
2323

24-
result = wof_realloc(__clib4->__wof_allocator, ptr, size);
24+
result = wmem_realloc(__clib4->__wmem_allocator, ptr, size);
2525
if (result == NULL) {
2626
SHOWMSG("could not reallocate memory");
2727
}

0 commit comments

Comments
 (0)