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

Expand Down
1 change: 0 additions & 1 deletion library/getclib4.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ __getClib4(void) {
if (!r) {
struct Clib4Resource *res = (APTR) OpenResource(RESOURCE_NAME);
if (res) {
DebugPrintF("[__getClib4 :] FALLBACK.\n");
return res->fallbackClib;
}
}
Expand Down
2 changes: 1 addition & 1 deletion library/libc_init_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ reent_init(struct _clib4 *__clib4, BOOL fallback) {
.__children = 1,
.term_entry = NULL,
.__was_sig = -1,
.__wof_mem_allocator_type = WMEM_ALLOCATOR_BLOCK,
.__wof_mem_allocator_type = WMEM_ALLOCATOR_SIMPLE,
.allocated_memory_by_malloc = 0,
.__environment_pool = NULL,
.__num_iob = 0,
Expand Down
52 changes: 0 additions & 52 deletions library/posix/memalign.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
#include <stdint.h>
#include <malloc.h>

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

void *
memalign(size_t alignment, size_t size) {

Expand All @@ -28,51 +23,4 @@ memalign(size_t alignment, size_t size) {
}

return __malloc_aligned_r(__CLIB4, size, alignment);

// uintptr_t aligned_addr = 0;
// struct _clib4 *__clib4 = __CLIB4;

// ENTER();

// SHOWVALUE(alignment);
// SHOWVALUE(size);

// // Validate alignment
// if (!isPowerOfTwo(alignment)) {
// __set_errno_r(__clib4, EINVAL);
// 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);

// // 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;
}
5 changes: 3 additions & 2 deletions library/pthread/pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ int __pthread_init_func(void) {

OpenDevice(TIMERNAME, UNIT_WAITUNTIL, (struct IORequest *) timedTimerIO, 0);

set_tls_register(inf);

/* Mark all threads as IDLE */
for (i = PTHREAD_FIRST_THREAD_ID; i < PTHREAD_THREADS_MAX; i++) {
inf = &threads[i];
inf->status = THREAD_STATE_IDLE;
}

set_tls_register(inf);

return TRUE;
}

Expand Down Expand Up @@ -339,6 +339,7 @@ void __pthread_exit_func(void) {

PTHREAD_CONSTRUCTOR(__pthread_init) {
ENTER();
DebugPrintF("[__pthread_init :] Pthread constructor called.\n");
_DOSBase = OpenLibrary("dos.library", MIN_OS_VERSION);
if (_DOSBase) {
_IDOS = (struct DOSIFace *) GetInterface((struct Library *) _DOSBase, "main", 1, NULL);
Expand Down
1 change: 0 additions & 1 deletion library/stdio/fclose.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ fclose(FILE *stream) {
/* Get rid of any custom file buffer allocated. */
if (file->iob_CustomBuffer != NULL) {
SHOWMSG("Delete allocated buffer");
DebugPrintF("Freeing pooled buffer: 0x%lx\n", file->iob_CustomBuffer);
FreeVecPooled(__clib4->_iob_pool, file->iob_CustomBuffer);
file->iob_CustomBuffer = NULL;
}
Expand Down
2 changes: 0 additions & 2 deletions library/stdio/file_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ FILE_CONSTRUCTOR(stdio_file_init) {
if (buffer == NULL)
goto out;

DebugPrintF("Allocating stdio buffer: %ld\n", BUFSIZ + (__clib4->__cache_line_size - 1));

ClearMem(buffer, BUFSIZ + (__clib4->__cache_line_size - 1));

/* Allocate memory for an arbitration mechanism, then initialize it. */
Expand Down
2 changes: 0 additions & 2 deletions library/stdio/openiob.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ __open_iob(struct _clib4 *__clib4, const char *filename, const char *mode, int f
goto out;
}

DebugPrintF("Allocated file io buffer: %ld\n", BUFSIZ + (__clib4->__cache_line_size - 1));

ClearMem(buffer, BUFSIZ + (__clib4->__cache_line_size - 1));

if (file_descriptor < 0) {
Expand Down
1 change: 0 additions & 1 deletion library/stdio/setvbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ setvbuf(FILE *stream, char *buf, int bufmode, size_t size) {
__set_errno_r(__clib4, ENOBUFS);
goto out;
}
DebugPrintF("Allocated vbuf: %ld\n", size + (__clib4->__cache_line_size - 1));
}
}

Expand Down
18 changes: 1 addition & 17 deletions library/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,13 @@ __malloc_aligned_r(struct _clib4 *__clib4, size_t size, int32_t alignment) {
}

void __memory_lock(struct _clib4 *__clib4) {
// 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);
// 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 @@ -85,10 +77,7 @@ STDLIB_DESTRUCTOR(stdlib_memory_exit) {

__memory_unlock(__clib4);

// if (__clib4->memory_semaphore != NULL) {
if (__clib4->memory_mutex) {
// __delete_semaphore(__clib4->memory_semaphore);
// __clib4->memory_semaphore = NULL;
if (__clib4->memory_mutex != NULL) {
__delete_mutex(__clib4->memory_mutex);
__clib4->memory_mutex = NULL;
}
Expand All @@ -103,17 +92,12 @@ STDLIB_CONSTRUCTOR(stdlib_memory_init) {

ENTER();

// __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_mutex(__clib4->memory_mutex);
__clib4->memory_mutex = NULL;
goto out;
Expand Down
4 changes: 2 additions & 2 deletions library/stdlib/stdlib_constructor.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
#define CLIB_CONSTRUCTOR(name) CONSTRUCTOR(name, 108)
#define CLIB_DESTRUCTOR(name) DESTRUCTOR(name, 108)

#define PTHREAD_CONSTRUCTOR(name) NSCONSTRUCTOR(name, 108)
#define PTHREAD_DESTRUCTOR(name) NSDESTRUCTOR(name, 108)
#define PTHREAD_CONSTRUCTOR(name) NSCONSTRUCTOR(name, 107)
#define PTHREAD_DESTRUCTOR(name) NSDESTRUCTOR(name, 107)

#define PROFILE_CONSTRUCTOR(name) CONSTRUCTOR(name, 109)
#define PROFILE_DESTRUCTOR(name) DESTRUCTOR(name, 109)
Expand Down
39 changes: 16 additions & 23 deletions library/wmem/wmem_allocator_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

// On amiga we will implement this with true flexible alignment.

#define WMEM_ALIGN_AMOUNT (2 * sizeof (size_t))
#define WMEM_ALIGN_AMOUNT (4 * sizeof (size_t))
#define WMEM_ALIGN_SIZE(SIZE) ((~(WMEM_ALIGN_AMOUNT-1)) & \
((SIZE) + (WMEM_ALIGN_AMOUNT-1)))

Expand Down Expand Up @@ -166,7 +166,6 @@ typedef struct _wmem_block_chunk_t {
uint32_t jumbo: 1;

uint32_t len: 29;
// void *data; // Unfortunately we need this for flixible alignment.
} wmem_block_chunk_t;

/* Handy macros for navigating the chunks in a block as if they were a
Expand All @@ -179,7 +178,7 @@ typedef struct _wmem_block_chunk_t {
? NULL \
: ((wmem_block_chunk_t*)(((uint8_t*)(CHUNK)) + (CHUNK)->len)))

#define WMEM_CHUNK_HEADER_SIZE /*WMEM_ALIGN_SIZE(*/sizeof(wmem_block_chunk_t)/*)*/
#define WMEM_CHUNK_HEADER_SIZE WMEM_ALIGN_SIZE(sizeof(wmem_block_chunk_t))

#define WMEM_BLOCK_MAX_ALLOC_SIZE (WMEM_BLOCK_SIZE - \
(WMEM_BLOCK_HEADER_SIZE + WMEM_CHUNK_HEADER_SIZE))
Expand All @@ -190,7 +189,7 @@ typedef struct _wmem_block_chunk_t {
#define WMEM_CHUNK_DATA_LEN(CHUNK) ((CHUNK)->len - WMEM_CHUNK_HEADER_SIZE)

/* some handy block macros */
#define WMEM_BLOCK_HEADER_SIZE /*WMEM_ALIGN_SIZE(*/sizeof(wmem_block_hdr_t)/*)*/
#define WMEM_BLOCK_HEADER_SIZE WMEM_ALIGN_SIZE(sizeof(wmem_block_hdr_t))
#define WMEM_BLOCK_TO_CHUNK(BLOCK) ((wmem_block_chunk_t*)((uint8_t*)(BLOCK) + WMEM_BLOCK_HEADER_SIZE))
#define WMEM_CHUNK_TO_BLOCK(CHUNK) ((wmem_block_hdr_t*)((uint8_t*)(CHUNK) - WMEM_BLOCK_HEADER_SIZE))

Expand Down Expand Up @@ -516,11 +515,10 @@ wmem_block_merge_free(wmem_block_allocator_t *allocator,
tmp->len += chunk->len;
tmp->last = chunk->last;
chunk = tmp;
// chunk->data = 0;
}

#ifdef MEMORY_DEBUG
// D(("[merge_free :] chunk : 0x%lx, left_free : 0x%lx, right_free : 0x%lx\n", chunk, left_free, right_free));
D(("[merge_free :] chunk : 0x%lx, left_free : 0x%lx, right_free : 0x%lx\n", chunk, left_free, right_free));
#endif

/* The length of our chunk may have changed. If we have a chunk following,
Expand Down Expand Up @@ -556,9 +554,6 @@ wmem_block_merge_free(wmem_block_allocator_t *allocator,
wmem_block_add_to_recycler(allocator, chunk);
}
}
#ifdef MEMORY_DEBUG
// D(("[merge_free :] Done.\n"));
#endif
}

/* Takes an unused chunk and a size, and splits it into two chunks if possible.
Expand Down Expand Up @@ -601,14 +596,13 @@ wmem_block_split_free_chunk(wmem_block_allocator_t *allocator,
/* set new values for chunk */
chunk->len = (uint32_t) aligned_size;
chunk->last = false;
// chunk->data = (void*)align_address((uintptr_t)chunk + WMEM_CHUNK_HEADER_SIZE, alignment); //we do this in alloc

/* with chunk's values set, we can use the standard macro to calculate
* the location and size of the new free chunk */
extra = WMEM_CHUNK_NEXT(chunk);

#ifdef MEMORY_DEBUG
// D(("[split_free :] chunk : 0x%lx, extra : 0x%lx, aligned_size : 0x%lx, available : 0x%lx, alignment : %ld\n", chunk, extra, aligned_size, available, alignment));
D(("[split_free :] chunk : 0x%lx, extra : 0x%lx, aligned_size : 0x%lx, available : 0x%lx, alignment : %ld\n", chunk, extra, aligned_size, available, alignment));
#endif

/* Now we move the free chunk's address without changing its location
Expand Down Expand Up @@ -653,7 +647,6 @@ wmem_block_split_free_chunk(wmem_block_allocator_t *allocator,
extra->prev = chunk->len;
extra->used = false;
extra->jumbo = false;
// extra->data = 0; // important

/* Correctly update the following chunk's back-pointer */
if (!last) {
Expand All @@ -674,13 +667,12 @@ wmem_block_split_used_chunk(wmem_block_allocator_t *allocator,
bool last;

#ifdef MEMORY_DEBUG
// D(("[split_used :] chunk : 0x%lx, chunk->data : 0x%lx\n", chunk, chunk->data));
D(("[split_used :] chunk : 0x%lx\n", chunk));
#endif

aligned_size = size;
aligned_size = WMEM_ALIGN_SIZE(aligned_size);
aligned_size = WMEM_ALIGN_SIZE(size) + WMEM_CHUNK_HEADER_SIZE;

if (aligned_size > chunk->len) {
if (aligned_size > WMEM_CHUNK_DATA_LEN(chunk)) {
/* in this case we don't have enough space to really split it, so
* it's basically a no-op */
return;
Expand All @@ -689,8 +681,8 @@ wmem_block_split_used_chunk(wmem_block_allocator_t *allocator,
* may still not be usefully large */

/* preserve a few values from chunk that we'll need to manipulate */
last = chunk->last;
available = chunk->len - aligned_size;
last = chunk->last;
available = chunk->len - aligned_size;

/* set new values for chunk */
chunk->len = (uint32_t) aligned_size;
Expand All @@ -706,7 +698,6 @@ wmem_block_split_used_chunk(wmem_block_allocator_t *allocator,
extra->prev = chunk->len;
extra->used = false;
extra->jumbo = false;
// extra->data = 0;

/* Correctly update the following chunk's back-pointer */
if (!last) {
Expand Down Expand Up @@ -764,7 +755,6 @@ wmem_block_init_block(wmem_block_allocator_t *allocator,
chunk->last = true;
chunk->prev = 0;
chunk->len = WMEM_BLOCK_SIZE - WMEM_BLOCK_HEADER_SIZE;
// chunk->data = 0;

/* now push that chunk onto the master list */
wmem_block_push_master(allocator, chunk);
Expand Down Expand Up @@ -855,6 +845,10 @@ wmem_block_alloc(void *private_data, const size_t size, int32_t alignment) {
wmem_block_allocator_t *allocator = (wmem_block_allocator_t *) private_data;
wmem_block_chunk_t *chunk;

#ifdef MEMORY_DEBUG
D(("[block_alloc :] size == %ld, alignment = %ld\n", size, alignment));
#endif

size_t needed = WMEM_CHUNK_HEADER_SIZE + alignment + sizeof(wmem_block_pre_t) + size;
needed = WMEM_ALIGN_SIZE(needed);

Expand Down Expand Up @@ -891,7 +885,7 @@ wmem_block_alloc(void *private_data, const size_t size, int32_t alignment) {
chunk->used = true;
void *data = (void*)align_address((uintptr_t)chunk + WMEM_CHUNK_HEADER_SIZE + sizeof(wmem_block_pre_t), alignment);

wmem_block_pre_t *pre = WMEM_DATA_TO_PRE(data); // (wmem_block_pre_t *)((uintptr_t)chunk->data - sizeof(wmem_block_pre_t));
wmem_block_pre_t *pre = WMEM_DATA_TO_PRE(data);
pre->chunk = chunk;

/* and return the data pointer */
Expand All @@ -903,9 +897,8 @@ wmem_block_free(void *private_data, void *ptr) {
wmem_block_allocator_t *allocator = (wmem_block_allocator_t *) private_data;
wmem_block_chunk_t *chunk;

// chunk = wmem_block_get_chunk(allocator, ptr);
chunk = WMEM_DATA_TO_CHUNK(ptr);

if (chunk->jumbo) {
wmem_block_free_jumbo(allocator, chunk);
return;
Expand Down
Loading