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

[libc] Add arena-based memory allocator for OpenWatcom large model #2138

Merged
merged 7 commits into from
Dec 17, 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
4 changes: 2 additions & 2 deletions elkscmd/ash/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ OBJS= builtins.o cd.o dirent.o error.o eval.o exec.o expand.o input.o \
output.o var.o init.o \
linenoise_elks.o autocomplete.o

# heap debugging
#OBJS += ../../libc/malloc/v7malloc.o
# heap debugging using v7 debug malloc
#OBJS += v7stub.o

# builtins must be manually added
OBJS += bltin/echo.o
Expand Down
17 changes: 17 additions & 0 deletions elkscmd/ash/v7stub.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* stub to force inclusion of debug malloc (v7) */
#include <stdlib.h>

void *malloc(size_t size)
{
return __dmalloc(size);
}

void free(void *ptr)
{
__dfree(ptr);
}

void *realloc(void *ptr, size_t size)
{
return __drealloc(ptr, size);
}
31 changes: 12 additions & 19 deletions libc/include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,23 @@

#include <sys/types.h>

/* default malloc (dev86) */
void *malloc(size_t);
void free(void *);
void *realloc(void *, size_t);
void *calloc(size_t elm, size_t sz);

#ifdef __LIBC__
/*
* Mini malloc allows you to use a less efficient but smaller malloc the
* cost is about 100 bytes of code in free but malloc (700bytes) doesn't
* have to be linked. Unfortunatly memory can only be reused if everything
* above it has been freed
*
*/
void free(void *);

/* remove __MINI_MALLOC__ and always use real malloc for libc routines */
//#define __MINI_MALLOC__
/* debug malloc (v7 malloc) */
void *__dmalloc(size_t);
void *__drealloc(void *, size_t);
void __dfree(void *);

void __wcnear *__mini_malloc(size_t size);
#endif
/* arena malloc (64k near/unlimited far heap) */
void *__amalloc(size_t);
int __amalloc_add_heap(char __far *start, size_t size);
void *__arealloc(void *, size_t); /* NYI */
void __afree(void *);

#ifdef __MINI_MALLOC__
extern void __wcnear *(*__alloca_alloc)(size_t);
#define malloc(x) ((*__alloca_alloc)(x))
#endif
void *calloc(size_t elm, size_t sz);

/* alloc/free from main memory */
void __far *fmemalloc(unsigned long size);
Expand Down
41 changes: 29 additions & 12 deletions libc/malloc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,49 @@ COMPILER ?= ia16
LIB ?= out.a

include $(TOPDIR)/libc/$(COMPILER).inc

# options for default malloc (dev86) only:
# allocations smaller than MCHUNK words (not bytes) are rounded up,
# larger requests are allocated from heap as is.
CFLAGS += -DMCHUNK=16
#CFLAGS += -DVERBOSE=1
#CFLAGS += -DMINALLOC
#CFLAGS += -DLAZY_FREE
CFLAGS += -DVERBOSE=1
#CFLAGS += -DL_alloca

# use V7 malloc for heap integrity checking
#OBJS = v7malloc.o calloc.o sbrk.o brk.o

OBJS = \
# default malloc (dev86)
DEFAULT_MALLOC_OBJS = \
malloc.o \
free.o \
realloc.o \
__mini_malloc.o \
__alloca_alloc.o \
__freed_list.o \
__mini_malloc.o \
noise.o \
alloca.o \
brk.o \

# debug malloc (v7)
DEBUG_MALLOC_OBJS = v7malloc.o

# arena malloc
ARENA_MALLOC_OBJS = amalloc.o

# these objects work with any malloc
OBJS = \
calloc.o \
free.o \
malloc.o \
noise.o \
dprintf.o \
realloc.o \
brk.o \
sbrk.o \
fmemalloc.o \
fmemfree.o \
dprintf.o \

# default and debug mallocs available for ia16 and OWC
OBJS += $(DEFAULT_MALLOC_OBJS) $(DEBUG_MALLOC_OBJS)

# arena malloc for OWC only for now
ifeq "$(COMPILER)" "watcom"
OBJS += $(ARENA_MALLOC_OBJS)
endif

IA16OBJS = \
stackcheck.o \
Expand Down
32 changes: 24 additions & 8 deletions libc/malloc/_malloc.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#ifndef _MALLOC_H
#define _MALLOC_H
#define _MALLOC_H

/*
* Internal types for default malloc (dev86)
*/
#include <sys/types.h>

typedef union mem_cell
{
union mem_cell __wcnear *next; /* A pointer to the next mem */
unsigned int size; /* An int >= sizeof pointer */
char __wcnear *depth; /* For the alloca hack */
union mem_cell __wcnear *next; /* A pointer to the next mem */
unsigned int size; /* An int >= sizeof pointer */
char __wcnear *depth; /* For the alloca hack */
} mem;

void __wcnear *__mini_malloc(size_t size);
void __noise(char *y, mem __wcnear *x);
int __dprintf(const char *fmt, ...);
extern int __debug_level;
extern mem __wcnear *__freed_list;

#if !VERBOSE
#define dprintf(...)
Expand All @@ -25,10 +30,21 @@ extern int __debug_level;
#define debug(str,ptr) __noise(str,ptr)
#endif

#define m_deep(p) ((p) [0].depth) /* For alloca */
#define m_next(p) ((p) [1].next) /* For malloc and alloca */
#define m_size(p) ((p) [0].size) /* For malloc */
#define m_deep(p) ((p) [0].depth) /* For alloca */
#define m_next(p) ((p) [1].next) /* For malloc and alloca */
#define m_size(p) ((p) [0].size) /* For malloc */

extern mem __wcnear *__freed_list;
/*
* Mini malloc allows you to use a less efficient but smaller malloc the
* cost is about 100 bytes of code in free but malloc (700bytes) doesn't
* have to be linked. Unfortunatly memory can only be reused if everything
* above it has been freed
*/
/* remove __MINI_MALLOC__ and always use real malloc for libc routines */
/*#define __MINI_MALLOC__*/
#ifdef __MINI_MALLOC__
extern void __wcnear *(*__alloca_alloc)(size_t);
#define malloc(x) ((*__alloca_alloc)(x)) /* NOTE won't work anymore */
#endif

#endif
Loading
Loading