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] More memory allocation changes for 8086 toolchain #2140

Merged
merged 1 commit 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
2 changes: 1 addition & 1 deletion elkscmd/tui/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PRGS = fm matrix cons ttyinfo sl ttyclock ttypong ttytetris invaders
#PRGS_HOST =

TUILIB = tty.o runes.o unikey.o
MALLOC=../../libc/malloc/v7malloc.o
MALLOC=v7stub.o

all: $(PRGS) $(PRGS_HOST)

Expand Down
17 changes: 17 additions & 0 deletions elkscmd/tui/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: 17 additions & 14 deletions libc/include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
#include <sys/types.h>

/* default malloc (dev86) */
void *malloc(size_t);
void free(void *);
size_t malloc_usable_size(void *);
void *malloc(size_t);
void *realloc(void *, size_t);
void free(void *);
size_t malloc_usable_size(void *);

/* debug malloc (v7 malloc) */
void *__dmalloc(size_t);
void *__drealloc(void *, size_t);
void __dfree(void *);
size_t __dmalloc_usable_size(void *);
void *__dmalloc(size_t);
void *__drealloc(void *, size_t);
void __dfree(void *);
size_t __dmalloc_usable_size(void *);

/* 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 *);
size_t __dmalloc_usable_size(void *);
void *__amalloc(size_t);
int __amalloc_add_heap(char __far *start, size_t size);
void *__arealloc(void *, size_t); /* not implemented */
void __afree(void *);
size_t __amalloc_usable_size(void *);

/* usable with all mallocs */
void *realloc(void *, size_t);
void *calloc(size_t elm, size_t sz);
void *calloc(size_t elm, size_t sz);

/* alloc/free from main memory */
void __far *fmemalloc(unsigned long size);
Expand All @@ -32,4 +32,7 @@ int fmemfree(void __far *ptr);
int _fmemalloc(int paras, unsigned short *pseg); /* syscall */
int _fmemfree(unsigned short seg); /* syscall */

extern unsigned int malloc_arena_size;
extern unsigned int malloc_arena_thresh;

#endif
2 changes: 1 addition & 1 deletion libc/malloc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CFLAGS += -DMCHUNK=16
# default malloc (dev86)
DEFAULT_MALLOC_OBJS = \
malloc.o \
realloc.o \
free.o \
__mini_malloc.o \
__alloca_alloc.o \
Expand All @@ -32,7 +33,6 @@ ARENA_MALLOC_OBJS = amalloc.o

# these objects work with any malloc
OBJS = \
realloc.o \
calloc.o \
brk.o \
sbrk.o \
Expand Down
13 changes: 7 additions & 6 deletions libc/malloc/amalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ __afree(void *ptr)

if (p == NULL)
return;
debug("(%d) free(%d) = %04x\n", getpid(), (unsigned)(p[-1].ptr - p) << 1, p-1);
debug("(%d) free(%d) = %04x\n", getpid(),
(unsigned)(next(p-1) - p) * sizeof(union store), p-1);
ASSERT(FP_SEG(ptr)==allocseg);
ASSERT(p>clearbusy(allocs[allocsize-1].ptr)&&p<=alloct);
ASSERT(malloc_check_heap());
Expand All @@ -253,7 +254,7 @@ size_t __amalloc_usable_size(void *ptr)
{
NPTR p = (NPTR)ptr;

if (p == NULL)
if (p == NULL) /* NOTE this allows fmemalloc pointers to return 0 here */
return 0;
ASSERT(FP_SEG(ptr)==allocseg);
ASSERT(p>clearbusy(allocs[allocsize-1].ptr)&&p<=alloct);
Expand All @@ -280,10 +281,10 @@ __arealloc(void *ptr, size_t nbytes)
return __amalloc(nbytes);
debug("(%d)realloc(%04x,%u) ", getpid(), (unsigned)(p-1), nbytes);

ASSERT(testbusy(p[-1].ptr));
if(testbusy(p[-1].ptr))
free(p);
onw = p[-1].ptr - p;
ASSERT(testbusy(next(p-1)));
if(testbusy(next(p-1)))
__afree(p);
onw = next(p-1) - p;
q = (NPTR)__amalloc(nbytes); // FIXME and also use memcpy
if(q==NULL || q==p)
return((void *)q);
Expand Down
9 changes: 5 additions & 4 deletions libc/malloc/realloc.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <malloc.h>
#include <string.h>

/* this realloc usable with all malloc allocators */
void *realloc(void *ptr, size_t size)
{
void *nptr;
Expand All @@ -11,15 +10,17 @@ void *realloc(void *ptr, size_t size)
return malloc(size);

osize = malloc_usable_size(ptr);
#if LATER
#if 0
if (size <= osize)
return ptr;
return ptr; /* don't reallocate, do nothing */
#else
if (size <= osize)
osize = size; /* copy less bytes in memcpy below */
#endif

nptr = malloc(size);
if (nptr == 0)
return 0;

memcpy(nptr, ptr, osize);
free(ptr);

Expand Down
11 changes: 6 additions & 5 deletions libc/malloc/v7malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ __dfree(void *ptr)

if (p == NULL)
return;
debug("(%d) free(%d) = %04x\n", getpid(), (unsigned)(p[-1].ptr - p) << 1, p-1);
debug("(%d) free(%d) = %04x\n", getpid(),
(unsigned)(next(p-1) - p) * sizeof(union store), p-1);
ASSERT(p>clearbusy(allocs[SIZE-1].ptr)&&p<=alloct);
ASSERT(malloc_check_heap());
allocp = --p;
Expand Down Expand Up @@ -256,10 +257,10 @@ __drealloc(void *ptr, size_t nbytes)
return __dmalloc(nbytes);
debug("(%d)realloc(%04x,%u) ", getpid(), (unsigned)(p-1), nbytes);

ASSERT(testbusy(p[-1].ptr));
if(testbusy(p[-1].ptr))
free(p);
onw = p[-1].ptr - p;
ASSERT(testbusy(next(p-1)));
if(testbusy(next(p-1)))
__dfree(p);
onw = next(p-1) - p;
q = (NPTR)__dmalloc(nbytes); // FIXME and also use memcpy
if(q==NULL || q==p)
return((void *)q);
Expand Down
Loading