Skip to content

Commit

Permalink
add different allocator support
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoconlechuga committed Sep 18, 2023
1 parent 3428df0 commit ae0c537
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 253 deletions.
51 changes: 51 additions & 0 deletions src/libc/allocator.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
assume adl=1

section .text
public _malloc, _calloc, _free, _realloc

public _calloc
_calloc:
pop de
pop bc
ex (sp),hl
push bc
push de
call __imulu
push hl
call _malloc
add hl,de
xor a,a
sbc hl,de
ld e,a
push de
push hl
call nz,_memset
pop hl
pop de
pop bc
ret

if defined ALLOCATOR_SIMPLE

_malloc := __simple_malloc
_free := __simple_free
_realloc := __simple_realloc

end if

if defined ALLOCATOR_STANDARD

_malloc := __standard_malloc
_free := __standard_free
_realloc := __standard_realloc

end if

extern __simple_free
extern __simple_malloc
extern __simple_realloc
extern __standard_malloc
extern __standard_free
extern __standard_realloc
extern __imulu
extern _memset
39 changes: 39 additions & 0 deletions src/libc/allocator_simple.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
assume adl = 1

section .text
public __malloc_simple
__simple_malloc:
pop bc
ex (sp),de
push bc
ld bc,___heapbot
ld iy,(_heap_size)
add iy,de
lea hl,iy
or a,a
sbc hl,bc
jr nc,.full
ld (_heap_size),iy
ld hl,___heapbot
add hl,de
ret
.full:
or a,a
sbc hl,hl
ret

public __simple_free
__simple_free:
ret

public __simple_realloc
__simple_realloc:
or a,a
sbc hl,hl
ret

section .bss
private _heap_size
_heap_size:
rb 3

129 changes: 129 additions & 0 deletions src/libc/allocator_standard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

typedef struct __attribute__((packed)) block
{
struct block *ptr;
size_t size;
} __attribute__((packed)) block_t;

extern uint8_t ___heapbot[];
extern uint8_t ___heaptop[];
static uintptr_t heap_ptr = (uintptr_t)___heapbot;
static block_t _alloc_base;

void *_standard_malloc(size_t alloc_size)
{
uintptr_t heap_ptr_new;
block_t *p;
block_t *q;
block_t *r;
size_t size;

/* add size of block header to real size */
size = alloc_size + sizeof(block_t);
if (size < alloc_size)
{
return NULL;
}

for (p = &_alloc_base; (q = p->ptr); p = q)
{
if (q->size >= size)
{
if (q->size <= size + sizeof(block_t))
{
p->ptr = q->ptr;
}
else
{
q->size -= size;
q = (block_t*)(((uint8_t*)q) + q->size);
q->size = size;
}

return q + 1;
}
}

/* compute next heap pointer */
heap_ptr_new = heap_ptr + size;
if (heap_ptr_new < heap_ptr)
{
return NULL;
}

/* ensure there's enough room in the heap */
if (heap_ptr_new >= (uintptr_t)___heaptop)
{
return NULL;
}

r = (block_t*)heap_ptr;
r->size = size;

heap_ptr = heap_ptr_new;

return r + 1;
}

void _standard_free(void *ptr)
{
if (ptr != NULL)
{
block_t *p;
block_t *q;

q = (block_t*)ptr - 1;

for (p = &_alloc_base; p->ptr && p->ptr < q; p = p->ptr);

if ((uint8_t*)p->ptr == ((uint8_t*)q) + q->size)
{
q->size += p->ptr->size;
q->ptr = p->ptr->ptr;
}
else
{
q->ptr = p->ptr;
}

if (((uint8_t*)p) + p->size == (uint8_t*)q)
{
p->size += q->size;
p->ptr = q->ptr;
}
else
{
p->ptr = q;
}
}
}

void *_standard_realloc(void *ptr, size_t size)
{
block_t *h;
void *p;

if (ptr == NULL)
{
return malloc(size);
}

h = (block_t*)((uint8_t*)ptr - sizeof(block_t));

if (h->size >= (size + sizeof(block_t)))
{
return ptr;
}

if ((p = malloc(size)))
{
memcpy(p, ptr, size);
free(ptr);
}

return p;
}
29 changes: 0 additions & 29 deletions src/libc/calloc.src

This file was deleted.

51 changes: 0 additions & 51 deletions src/libc/free.c

This file was deleted.

12 changes: 0 additions & 12 deletions src/libc/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,12 @@ typedef struct {
long long quot;
} lldiv_t;

typedef char __align;
union header {
struct {
union header *ptr;
unsigned int size;
} s;
__align x;
};
typedef union header _HEADER;

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

#define RAND_MAX 8388607

#define HEADER _HEADER
#define allocp _allocp
#define NALLOC 50

__BEGIN_DECLS

Expand Down
Loading

0 comments on commit ae0c537

Please sign in to comment.