Skip to content

Commit

Permalink
Merge pull request #2176 from ghaerr/fmalloc
Browse files Browse the repository at this point in the history
[libc] Rename __amalloc -> _fmalloc, __dmalloc -> _dmalloc
  • Loading branch information
ghaerr authored Jan 9, 2025
2 parents 4efb51d + 5404487 commit 9f26e1e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 104 deletions.
2 changes: 1 addition & 1 deletion elkscmd/ash/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ OBJS= builtins.o cd.o dirent.o error.o eval.o exec.o expand.o input.o \
linenoise_elks.o autocomplete.o

# heap debugging using v7 debug malloc
#OBJS += v7stub.o
#OBJS += dmalloc.o

# builtins must be manually added
OBJS += bltin/echo.o
Expand Down
6 changes: 3 additions & 3 deletions elkscmd/ash/v7stub.c → elkscmd/ash/dmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

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

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

void *realloc(void *ptr, size_t size)
{
return __drealloc(ptr, size);
return _drealloc(ptr, size);
}
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=v7stub.o
MALLOC=dmalloc.o

all: $(PRGS) $(PRGS_HOST)

Expand Down
6 changes: 3 additions & 3 deletions elkscmd/tui/v7stub.c → elkscmd/tui/dmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

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

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

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

#include <sys/types.h>

/* default malloc (dev86) */
/* default malloc (dev86 near heap) */
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 *);

/* 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); /* not implemented */
void __afree(void *);
size_t __amalloc_usable_size(void *);
extern unsigned int malloc_arena_size;
extern unsigned int malloc_arena_thresh;
/* debug malloc (v7 malloc near heap) */
void *_dmalloc(size_t);
void *_drealloc(void *, size_t);
void _dfree(void *);
size_t _dmalloc_usable_size(void *);

/* _fmalloc (single arena 64k far heap) */
void __far *_fmalloc(size_t);
int _fmalloc_add_heap(char __far *start, size_t size);
void __far *_frealloc(void *, size_t); /* not implemented */
void _ffree(void __far *);
size_t _fmalloc_usable_size(void __far *);
extern unsigned int malloc_arena_size; /* mem.c wrapper function */
extern unsigned int malloc_arena_thresh; /* mem.c wrapper function */

/* usable with all mallocs */
void *calloc(size_t elm, size_t sz);
Expand Down
10 changes: 5 additions & 5 deletions libc/malloc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ DEFAULT_MALLOC_OBJS = \
alloca.o \

# debug malloc (v7)
DEBUG_MALLOC_OBJS = v7malloc.o
DEBUG_MALLOC_OBJS = dmalloc.o

# arena malloc
ARENA_MALLOC_OBJS = amalloc.o
# far malloc (single arena)
FAR_MALLOC_OBJS = fmalloc.o

# these objects work with any malloc
OBJS = \
Expand All @@ -42,9 +42,9 @@ OBJS = \
# default and debug mallocs for all compilers
OBJS += $(DEFAULT_MALLOC_OBJS) $(DEBUG_MALLOC_OBJS)

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

IA16OBJS = \
Expand Down
32 changes: 10 additions & 22 deletions libc/malloc/v7malloc.c → libc/malloc/dmalloc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Small malloc/realloc/free with heap checking
* _dmalloc - Small malloc/realloc/free with heap checking
* Ported to ELKS from V7 malloc by Greg Haerr 20 Apr 2020
*
* Enhancements:
Expand Down Expand Up @@ -79,7 +79,7 @@ static int malloc_check_heap(void);
#endif

void *
__dmalloc(size_t nbytes)
_dmalloc(size_t nbytes)
{
NPTR p, q;
unsigned int nw, temp;
Expand All @@ -89,10 +89,6 @@ __dmalloc(size_t nbytes)
#endif
if (allocs[0].ptr == 0) { /*first time*/
allocs[0].ptr = setbusy(&allocs[1]);
#if SIZE > 2
allocs[1].ptr = (NPTR)&allocs[SIZE-2];
allocs[SIZE-2].ptr = setbusy(&allocs[SIZE-1]);
#endif
allocs[SIZE-1].ptr = setbusy(&allocs[0]);
alloct = (NPTR)&allocs[SIZE-1];
allocp = (NPTR)&allocs[0];
Expand Down Expand Up @@ -149,11 +145,6 @@ __dmalloc(size_t nbytes)
break;
}

#if SIZE > 2
debug("Out of fixed heap\n");
return NULL;
#else

/* extend break at least BLOCK bytes at a time, plus a word for top link */
if (nw < BLOCK/WORD)
temp = BLOCK/WORD + 1;
Expand Down Expand Up @@ -191,7 +182,6 @@ __dmalloc(size_t nbytes)
sizeof(union store) +
(clearbusy(alloct) - clearbusy(allocs[SIZE-1].ptr)) * sizeof(union store));
next(alloct) = setbusy(allocs);
#endif
}
found:
allocp = p + nw;
Expand All @@ -209,11 +199,11 @@ __dmalloc(size_t nbytes)
/* freeing strategy tuned for LIFO allocation
*/
void
__dfree(void *ptr)
_dfree(void *ptr)
{
NPTR p = (NPTR)ptr;

if (p == NULL)
if (ptr == NULL)
return;
debug("(%d) free(%5u) ", getpid(), (unsigned)(next(p-1) - p) * sizeof(union store));
debug2("= %04x\n", p-1);
Expand All @@ -226,7 +216,7 @@ __dfree(void *ptr)
malloc_show_heap();
}

size_t __dmalloc_usable_size(void *ptr)
size_t _dmalloc_usable_size(void *ptr)
{
NPTR p = (NPTR)ptr;

Expand All @@ -244,22 +234,22 @@ size_t __dmalloc_usable_size(void *ptr)
* returns new location, or 0 on failure
*/
void *
__drealloc(void *ptr, size_t nbytes)
_drealloc(void *ptr, size_t nbytes)
{
NPTR p = (NPTR)ptr;
NPTR q;
NPTR s, t;
unsigned int nw, onw;

if (p == 0)
return __dmalloc(nbytes);
return _dmalloc(nbytes);
debug("(%d)realloc(%04x,%u) ", getpid(), (unsigned)(p-1), nbytes);

ASSERT(testbusy(next(p-1)));
if(testbusy(next(p-1)))
__dfree(p);
_dfree(p);
onw = next(p-1) - p;
q = (NPTR)__dmalloc(nbytes); // FIXME and also use memcpy
q = (NPTR)_dmalloc(nbytes); // FIXME and also use memcpy
if(q==NULL || q==p)
return((void *)q);

Expand Down Expand Up @@ -312,7 +302,7 @@ malloc_show_heap(void)
unsigned int size, alloc = 0, free = 0;
static unsigned int maxalloc;

if (!debug_level) return;
if (debug_level < 2) return;
debug2("--- heap size ---\n");
malloc_check_heap();
for(p = (NPTR)&allocs[0]; clearbusy(next(p)) > p; p=clearbusy(next(p))) {
Expand All @@ -322,11 +312,9 @@ malloc_show_heap(void)
debug2(" (free)");
free += size;
} else {
#if SIZE == 2
if (n < 3) /* don't count ptr to first sbrk()*/
debug2(" (skipped)");
else
#endif
alloc += size;
}
n++;
Expand Down
66 changes: 12 additions & 54 deletions libc/malloc/amalloc.c → libc/malloc/fmalloc.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* __amalloc - Arena-based heap allocator - provides up to 64k local (far) heap
* Based on __dmalloc (v7 debug malloc).
* _fmalloc - Arena-based far heap allocator - provides up to 64k far heap
* Based on _dmalloc (v7 debug malloc).
* 16 Dec 2024 Greg Haerr
*
* Small malloc/realloc/free with heap checking
Expand Down Expand Up @@ -86,7 +86,7 @@ static int malloc_check_heap(void);
#endif

/* add size bytes to arena malloc heap, must be done before first malloc */
int __amalloc_add_heap(char __far *start, size_t size)
int _fmalloc_add_heap(char __far *start, size_t size)
{
ASSERT(start != NULL && size >= 16);
allocs = (FPTR)start;
Expand All @@ -103,8 +103,8 @@ int __amalloc_add_heap(char __far *start, size_t size)
return 1;
}

void *
__amalloc(size_t nbytes)
void __far *
_fmalloc(size_t nbytes)
{
NPTR p, q;
unsigned int nw, temp;
Expand Down Expand Up @@ -169,50 +169,8 @@ __amalloc(size_t nbytes)
} else if(++temp>1)
break;
}

#if 1 // SIZE > 2
debug("Out of fixed heap\n");
return NULL;
#else

/* extend break at least BLOCK bytes at a time, plus a word for top link */
if (nw < BLOCK/WORD)
temp = BLOCK/WORD + 1;
else
temp = nw + 1; /* NOTE always allocates full req w/o looking at free at top */

if (debug_level > 2) malloc_show_heap();
debug2("sbrk(%d) ", temp*WORD);
#if 0 /* not required and slow, initial break always even */
q = (NPTR)sbrk(0);
if((INT)q & (sizeof(union store) - 1))
sbrk(4 - ((INT)q & (sizeof(union store) - 1)));

/* check possible address wrap - performed in kernel */
if(q+temp+GRANULE < q) {
debug(" (no more address space) = NULL\n");
errno = ENOMEM;
return(NULL);
}
#endif
q = (NPTR)sbrk(temp*WORD);
if((INT)q == -1) {
debug(" (no more mem) = NULL\n");
malloc_show_heap();
errno = ENOMEM;
return(NULL);
}
ASSERT(!((INT)q & 1));
ASSERT(q>alloct);
next(alloct) = q;
if(q!=alloct+1) /* mark any gap as permanently allocated*/
next(alloct) = setbusy(next(alloct));
alloct = next(q) = q+temp-1;
debug2("(TOTAL %u) ",
sizeof(union store) +
(clearbusy(alloct) - clearbusy(allocs[allocsize-1].ptr)) * sizeof(union store));
next(alloct) = setbusy((NPTR)allocs);
#endif
}
found:
//__dprintf("n %d, nb %d, f %d\n", n, nb, f);
Expand All @@ -231,7 +189,7 @@ __amalloc(size_t nbytes)
/* freeing strategy tuned for LIFO allocation
*/
void
__afree(void *ptr)
_ffree(void __far *ptr)
{
NPTR p = (NPTR)ptr;

Expand All @@ -249,7 +207,7 @@ __afree(void *ptr)
malloc_show_heap();
}

size_t __amalloc_usable_size(void *ptr)
size_t _fmalloc_usable_size(void __far *ptr)
{
NPTR p = (NPTR)ptr;

Expand All @@ -269,22 +227,22 @@ size_t __amalloc_usable_size(void *ptr)
* returns new location, or 0 on failure
*/
void *
__arealloc(void *ptr, size_t nbytes)
_frealloc(void __far *ptr, size_t nbytes)
{
NPTR p = (NPTR)ptr;
NPTR q;
NPTR s, t;
unsigned int nw, onw;

if (p == 0)
return __amalloc(nbytes);
return _fmalloc(nbytes);
debug("(%d)realloc(%04x,%u) ", getpid(), (unsigned)(p-1), nbytes);

ASSERT(testbusy(next(p-1)));
if(testbusy(next(p-1)))
__afree(p);
_ffree(p);
onw = next(p-1) - p;
q = (NPTR)__amalloc(nbytes); // FIXME and also use memcpy
q = (NPTR)_fmalloc(nbytes); // FIXME and also use memcpy
if(q==NULL || q==p)
return((void *)q);

Expand All @@ -310,7 +268,7 @@ __arealloc(void *ptr, size_t nbytes)
#if DEBUG
static void malloc_assert_fail(char *s, int line)
{
__dprintf("amalloc assert fail: %s (line %d)\n", s, line);
__dprintf("fmalloc assert fail: %s (line %d)\n", s, line);
abort();
}

Expand Down

0 comments on commit 9f26e1e

Please sign in to comment.