Skip to content
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
7 changes: 6 additions & 1 deletion GNUmakefile.os4
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,19 @@ DLIBS :=
ifndef DEBUG
OPTIMIZE += $(STABS) -DNDEBUG
else
OPTIMIZE += -gstabs -DDEBUG
OPTIMIZE += -gstabs -DDEBUG -DMEMORY_DEBUG
DLIBS += $(BUILD_DIR)/lib/libdebug.a
endif


CFLAGS := $(WARNINGS) $(OPTIMIZE) $(OPTIONS) $(INCLUDES) -D__USE_INLINE__
CFLAGS_N := $(WARNINGS) $(OPTIMIZE) $(OPTIONS) $(INCLUDES)
AFLAGS := -Wa,-mregnames

ifdef USE_TEMPFILES
CFLAGS += -DUSE_TEMPFILES
endif

ifdef SPE
CC := ppc-amigaos-gcc-6.4.0
AS := ppc-amigaos-as-6.4.0
Expand Down
4 changes: 3 additions & 1 deletion library/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ struct _clib4 {
struct StackSwapStruct stack_swap_struct;

/* Wof Allocator memory semaphore */
struct SignalSemaphore *memory_semaphore;
// struct SignalSemaphore *memory_semaphore;
APTR memory_mutex;

int pipenum;

/* gettext */
Expand Down
1 change: 1 addition & 0 deletions library/getclib4.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ __getClib4(void) {
if (!r) {
struct Clib4Resource *res = (APTR) OpenResource(RESOURCE_NAME);
if (res) {
DebugPrintF("[__getClib4 :] FALLBACK.\n");
return res->fallbackClib;
}
}
Expand Down
89 changes: 49 additions & 40 deletions library/posix/memalign.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,66 @@
#include <stdint.h>
#include <malloc.h>

static inline BOOL
isPowerOfTwo(size_t alignment) {
return (alignment != 0) && ((alignment & (alignment - 1)) == 0);
}
// static inline BOOL
// isPowerOfTwo(size_t alignment) {
// return (alignment != 0) && ((alignment & (alignment - 1)) == 0);
// }

void *
memalign(size_t alignment, size_t size) {
uintptr_t aligned_addr = 0;
struct _clib4 *__clib4 = __CLIB4;

ENTER();
// Alignment must be powers of two
if (alignment == 0 || (alignment & -alignment) != alignment) {
__set_errno(EINVAL);
return NULL;
}

SHOWVALUE(alignment);
SHOWVALUE(size);
return __malloc_aligned_r(__CLIB4, size, alignment);

// Validate alignment
if (!isPowerOfTwo(alignment)) {
__set_errno_r(__clib4, EINVAL);
goto out;
}
// uintptr_t aligned_addr = 0;
// struct _clib4 *__clib4 = __CLIB4;

// Architecture-specific minimum alignment
const size_t min_alignment = sizeof(void*) * 2; // 8 on 32-bit, 16 on 64-bit
if (alignment < min_alignment)
alignment = min_alignment;
// ENTER();

const size_t header_size = sizeof(AlignedHeader);
const size_t padding = alignment - 1;
// SHOWVALUE(alignment);
// SHOWVALUE(size);

// Prevent overflow
if (size > SIZE_MAX - (header_size + padding)) {
__set_errno_r(__clib4, EOVERFLOW);
goto out;
}
// // Validate alignment
// if (!isPowerOfTwo(alignment)) {
// __set_errno_r(__clib4, EINVAL);
// goto out;
// }

void* original_ptr = __malloc_r(__clib4, size + header_size + padding);
if (!original_ptr) {
__set_errno_r(__clib4, ENOMEM);
goto out;
}
// // Architecture-specific minimum alignment
// const size_t min_alignment = sizeof(void*) * 2; // 8 on 32-bit, 16 on 64-bit
// if (alignment < min_alignment)
// alignment = min_alignment;

// const size_t header_size = sizeof(AlignedHeader);
// const size_t padding = alignment - 1;

// // Prevent overflow
// if (size > SIZE_MAX - (header_size + padding)) {
// __set_errno_r(__clib4, EOVERFLOW);
// goto out;
// }

// void* original_ptr = __malloc_r(__clib4, size + header_size + padding);
// if (!original_ptr) {
// __set_errno_r(__clib4, ENOMEM);
// goto out;
// }

// Calculate aligned address
uintptr_t base = (uintptr_t)original_ptr;
aligned_addr = (base + header_size + alignment - 1) & ~(alignment - 1);
// // Calculate aligned address
// uintptr_t base = (uintptr_t)original_ptr;
// aligned_addr = (base + header_size + alignment - 1) & ~(alignment - 1);

// Store metadata
AlignedHeader* header = (AlignedHeader*)(aligned_addr - header_size);
header->magic = ALIGNED_BLOCK_MAGIC;
header->original_ptr = original_ptr;
// // Store metadata
// AlignedHeader* header = (AlignedHeader*)(aligned_addr - header_size);
// header->magic = ALIGNED_BLOCK_MAGIC;
// header->original_ptr = original_ptr;

out:
RETURN(aligned_addr);
return (void *) aligned_addr;
// out:
// RETURN(aligned_addr);
// return (void *) aligned_addr;
}
4 changes: 4 additions & 0 deletions library/shared_library/clib4.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ BPTR libClose(struct LibraryManagerInterface *Self) {
SHOWMSG("Closing randfd[1]");
close(__clib4->randfd[1]);
}

SHOWMSG("Hej");
struct Task *t = IExec->FindTask(NULL);
IExec->DebugPrintF("[__getclib4 :] ln_Type == %ld, pr_UID == %ld\n", t->tc_Node.ln_Type, ((struct Process *)t)->pr_UID);

SHOWMSG("Calling clib4 dtors");
_end_ctors(__DTOR_LIST__);
Expand Down
6 changes: 5 additions & 1 deletion library/stdio/fdhookentry.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "stdio_headers.h"
#endif /* _STDIO_HEADERS_H */

// #ifndef _STDIO_PROTOS_H
#include "stdio_protos.h"
// #endif /* _STDIO_PROTOS_H */

#ifndef _UNISTD_HEADERS_H
#include "unistd_headers.h"
#endif /* _UNISTD_HEADERS_H */
Expand Down Expand Up @@ -36,7 +40,7 @@ int64_t __fd_hook_entry(struct _clib4 *__clib4, struct fd *fd, struct file_actio
ENTER();

assert(fam != NULL && fd != NULL);
assert(__is_valid_fd(__clib4, fd));
// assert(__is_valid_fd(__clib4, fd));

/* Careful: file_action_close has to monkey with the file descriptor
table and therefore needs to obtain the stdio lock before
Expand Down
1 change: 0 additions & 1 deletion library/stdlib/aligned_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ void *
aligned_alloc(size_t align, size_t size) {
ENTER();

/* align must be a power of 2 */
/* size must be a multiple of align */
if ((align & -align) != align) {
__set_errno(EINVAL);
Expand Down
12 changes: 6 additions & 6 deletions library/stdlib/free.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ __free_r(struct _clib4 *__clib4, void *ptr) {

__memory_lock(__clib4);

AlignedHeader* header = (AlignedHeader*) ((uintptr_t)ptr - sizeof(AlignedHeader));
// AlignedHeader* header = (AlignedHeader*) ((uintptr_t)ptr - sizeof(AlignedHeader));

// Validate with endian-independent integer comparison
if (header->magic == ALIGNED_BLOCK_MAGIC) {
wmem_free(__clib4->__wmem_allocator, header->original_ptr);
} else {
wmem_free(__clib4->__wmem_allocator,ptr);
}
// if (header->magic == ALIGNED_BLOCK_MAGIC) {
// wmem_free(__clib4->__wmem_allocator, header->original_ptr);
// } else {
wmem_free(__clib4->__wmem_allocator, ptr);
// }

__memory_unlock(__clib4);
}
Expand Down
53 changes: 36 additions & 17 deletions library/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ malloc(size_t size) {

void *
__malloc_r(struct _clib4 *__clib4, size_t size) {
return __malloc_aligned_r(__clib4, size, 16);
}

void *
__malloc_aligned_r(struct _clib4 *__clib4, size_t size, int32_t alignment) {
ENTER();
void *result = NULL;

if(size == 0) size = 4;

// Prevent overflow
if (size > SIZE_MAX) {
__set_errno_r(__clib4, EOVERFLOW);
Expand All @@ -32,7 +39,7 @@ __malloc_r(struct _clib4 *__clib4, size_t size) {

__memory_lock(__clib4);

result = wmem_alloc(__clib4->__wmem_allocator, size);
result = wmem_alloc_aligned(__clib4->__wmem_allocator, size, alignment);

if (!result)
__set_errno_r(__clib4, ENOMEM);
Expand All @@ -45,13 +52,21 @@ __malloc_r(struct _clib4 *__clib4, size_t size) {
}

void __memory_lock(struct _clib4 *__clib4) {
if (__clib4->memory_semaphore != NULL)
ObtainSemaphore(__clib4->memory_semaphore);
// if (__clib4->memory_semaphore != NULL)
// ObtainSemaphore(__clib4->memory_semaphore);
// DebugPrintF("[__memory_lock :] <LOCKING...>\n");
if(__clib4->memory_mutex)
MutexObtain(__clib4->memory_mutex);
// DebugPrintF("[__memory_lock :] <LOCKED.>\n");
}

void __memory_unlock(struct _clib4 *__clib4) {
if (__clib4->memory_semaphore != NULL)
ReleaseSemaphore(__clib4->memory_semaphore);
// if (__clib4->memory_semaphore != NULL)
// ReleaseSemaphore(__clib4->memory_semaphore);
// DebugPrintF("[__memory_lock :] <UNLOCKING...>\n");
if(__clib4->memory_mutex)
MutexRelease(__clib4->memory_mutex);
// DebugPrintF("[__memory_lock :] <UNLOCKED.>\n");
}

STDLIB_DESTRUCTOR(stdlib_memory_exit) {
Expand All @@ -63,20 +78,19 @@ STDLIB_DESTRUCTOR(stdlib_memory_exit) {
if (__clib4->__wmem_allocator != NULL) {
SHOWMSG("Destroying Memory Allocator");
wmem_destroy_allocator(__clib4->__wmem_allocator);
#if MEMORY_DEBUG
if (__clib4->allocated_memory_by_malloc > 0) {
Printf("WARNING: There are %ld unfreed malloc!\n", __clib4->allocated_memory_by_malloc);
}
#endif

SHOWMSG("Done");
__clib4->__wmem_allocator = NULL;
}

__memory_unlock(__clib4);

if (__clib4->memory_semaphore != NULL) {
__delete_semaphore(__clib4->memory_semaphore);
__clib4->memory_semaphore = NULL;
// if (__clib4->memory_semaphore != NULL) {
if (__clib4->memory_mutex) {
// __delete_semaphore(__clib4->memory_semaphore);
// __clib4->memory_semaphore = NULL;
__delete_mutex(__clib4->memory_mutex);
__clib4->memory_mutex = NULL;
}

LEAVE();
Expand All @@ -89,14 +103,19 @@ STDLIB_CONSTRUCTOR(stdlib_memory_init) {

ENTER();

__clib4->memory_semaphore = __create_semaphore();
if (__clib4->memory_semaphore == NULL)
// __clib4->memory_semaphore = __create_semaphore();
// if (__clib4->memory_semaphore == NULL)
// goto out;
__clib4->memory_mutex = __create_mutex();
if (__clib4->memory_mutex == NULL)
goto out;

__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;
// __delete_semaphore(__clib4->memory_semaphore);
// __clib4->memory_semaphore = NULL;
__delete_mutex(__clib4->memory_mutex);
__clib4->memory_mutex = NULL;
goto out;
}

Expand Down
3 changes: 2 additions & 1 deletion library/stdlib/posix_memalign.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ posix_memalign(void **res, size_t align, size_t len) {

void *mem = aligned_alloc(align, len);
if (!mem) {
return errno;
if(errno)
return errno;
}
*res = mem;

Expand Down
2 changes: 2 additions & 0 deletions library/stdlib/realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ realloc(void *ptr, size_t size) {

assert((int) size >= 0);

if (size == 0) size = 4;

__memory_lock(__clib4);

result = wmem_realloc(__clib4->__wmem_allocator, ptr, size);
Expand Down
1 change: 1 addition & 0 deletions library/stdlib/stdlib_protos.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ extern void loadstate(uint32_t *state);

extern void __srandom(unsigned seed);
extern void *__malloc_r(struct _clib4 *__clib4, size_t size);
extern void *__malloc_aligned_r(struct _clib4 *__clib4, size_t size, int32_t alignment);
extern void *__calloc_r(struct _clib4 *__clib4, size_t num_elements, size_t element_size);

extern uint32_t lcg31(uint32_t x);
Expand Down
4 changes: 2 additions & 2 deletions library/wmem/wmem_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ struct _wmem_user_cb_container_t;
* on this structure */
struct _wmem_allocator_t {
/* Consumer functions */
void *(*walloc)(void *private_data, const size_t size);
void *(*walloc)(void *private_data, const size_t size, int32_t alignment);
void (*wfree)(void *private_data, void *ptr);
void *(*wrealloc)(void *private_data, void *ptr, const size_t size);
void *(*wrealloc)(void *private_data, void *ptr, const size_t size, int32_t alignment);

/* Producer/Manager functions */
void (*free_all)(void *private_data);
Expand Down
Loading