-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3428df0
commit ae0c537
Showing
10 changed files
with
221 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.