Skip to content

Commit

Permalink
[libc] Add malloc_usable_size, write general-purpose realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Dec 17, 2024
1 parent 6757020 commit 3bb1b5d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 29 deletions.
33 changes: 19 additions & 14 deletions libc/include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
#include <sys/types.h>

/* default malloc (dev86) */
void *malloc(size_t);
void *realloc(void *, size_t);
void free(void *);
void *malloc(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 *);
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 *);
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 *calloc(size_t elm, size_t sz);
/* usable with all mallocs */
void *realloc(void *, size_t);
void *calloc(size_t elm, size_t sz);

/* alloc/free from main memory */
void __far *fmemalloc(unsigned long size);
int fmemfree(void __far *ptr);
int _fmemalloc(int paras, unsigned short *pseg);
int _fmemfree(unsigned short seg);
int fmemfree(void __far *ptr);

int _fmemalloc(int paras, unsigned short *pseg); /* syscall */
int _fmemfree(unsigned short seg); /* syscall */

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

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

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

if (p == NULL)
return 0;
ASSERT(FP_SEG(ptr)==allocseg);
ASSERT(p>clearbusy(allocs[allocsize-1].ptr)&&p<=alloct);
--p;
ASSERT(testbusy(next(p)));
return (clearbusy(next(p)) - clearbusy(p)) * sizeof(union store);
}

#if LATER
/* realloc(p, nbytes) reallocates a block obtained from malloc()
* and freed since last call of malloc()
Expand Down
7 changes: 7 additions & 0 deletions libc/malloc/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ __search_chunk(unsigned int mem_size)
return p1;
}

size_t malloc_usable_size(void *ptr)
{
if (ptr == 0)
return 0;
return (m_size(((mem *) ptr) - 1) - 1) * sizeof(mem);
}

void *
malloc(size_t size)
{
Expand Down
16 changes: 6 additions & 10 deletions libc/malloc/realloc.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
#include <malloc.h>
#include <string.h>

#include "_malloc.h"

#undef malloc

void *
realloc(void *ptr, size_t size)
/* this realloc usable with all malloc allocators */
void *realloc(void *ptr, size_t size)
{
void *nptr;
unsigned int osize;
size_t osize;

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

osize = (m_size(((mem *) ptr) - 1) - 1) * sizeof(mem);

osize = malloc_usable_size(ptr);
#if LATER
if (size <= osize)
return ptr;
#endif

nptr = malloc(size);

if (nptr == 0)
return 0;

Expand Down
20 changes: 16 additions & 4 deletions libc/malloc/v7malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ __dmalloc(size_t nbytes)
if (nbytes < MINALLOC)
nbytes = MINALLOC;

/* check INT overflow beyond 32762 (nbytes/WORD+WORD+WORD+(WORD-1) > 0xFFFF/WORD/WORD)*/
if (nbytes > ((unsigned)-1)/WORD-WORD-WORD-(WORD-1)) {
/* check INT overflow beyond 32762 (nbytes/WORD+2*WORD+(WORD-1) > 0xFFFF/WORD/WORD) */
if (nbytes > ((unsigned)-1)/WORD-2*WORD-(WORD-1)) {
debug(" (req too big) = NULL\n");
errno = ENOMEM;
return(NULL);
Expand All @@ -133,8 +133,8 @@ __dmalloc(size_t nbytes)
(next(q) - q) * sizeof(union store));
next(p) = next(q);
}
debug2("q %04x p %04x nw %d p+nw %04x ", (unsigned)q, (unsigned)p,
nw, (unsigned)(p+nw));
/*debug2("q %04x p %04x nw %d p+nw %04x ", (unsigned)q, (unsigned)p,
nw, (unsigned)(p+nw));*/
if(q>=p+nw && p+nw>=p)
goto found;
}
Expand Down Expand Up @@ -227,6 +227,18 @@ __dfree(void *ptr)
malloc_show_heap();
}

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

if (p == NULL)
return 0;
ASSERT(p>clearbusy(allocs[SIZE-1].ptr)&&p<=alloct);
--p;
ASSERT(testbusy(next(p)));
return (clearbusy(next(p)) - clearbusy(p)) * sizeof(union store);
}

/* realloc(p, nbytes) reallocates a block obtained from malloc()
* and freed since last call of malloc()
* to have new size nbytes, and old content
Expand Down

0 comments on commit 3bb1b5d

Please sign in to comment.