Skip to content

Commit 7b9b237

Browse files
add different allocator support
1 parent cc7e8d5 commit 7b9b237

File tree

10 files changed

+220
-253
lines changed

10 files changed

+220
-253
lines changed

src/libc/allocator.src

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
assume adl=1
2+
3+
section .text
4+
public _malloc, _calloc, _free, _realloc
5+
6+
public _calloc
7+
_calloc:
8+
pop de
9+
pop bc
10+
ex (sp),hl
11+
push bc
12+
push de
13+
call __imulu
14+
push hl
15+
call _malloc
16+
add hl,de
17+
xor a,a
18+
sbc hl,de
19+
ld e,a
20+
push de
21+
push hl
22+
call nz,_memset
23+
pop hl
24+
pop de
25+
pop bc
26+
ret
27+
28+
if defined ALLOCATOR_SIMPLE
29+
30+
_malloc := __simple_malloc
31+
_free := __simple_free
32+
_realloc := __simple_realloc
33+
34+
end if
35+
36+
if defined ALLOCATOR_STANDARD
37+
38+
_malloc := __standard_malloc
39+
_free := __standard_free
40+
_realloc := __standard_realloc
41+
42+
end if
43+
44+
extern __simple_free
45+
extern __simple_malloc
46+
extern __simple_realloc
47+
extern __standard_malloc
48+
extern __standard_free
49+
extern __standard_realloc
50+
extern __imulu
51+
extern _memset

src/libc/allocator_simple.src

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
assume adl = 1
2+
3+
section .text
4+
5+
public __simple_malloc
6+
__simple_malloc:
7+
pop bc
8+
ex (sp),de
9+
push bc
10+
ld bc,(_heap_ptr)
11+
push bc
12+
pop iy
13+
add iy,de
14+
lea hl,iy
15+
or a,a
16+
sbc hl,bc
17+
jr c,.null
18+
ld de,___heaptop
19+
lea hl,iy
20+
or a,a
21+
sbc hl,de
22+
jr nc,.null
23+
ld (_heap_ptr),iy
24+
push bc
25+
pop hl
26+
ret
27+
.null:
28+
or a,a
29+
sbc hl,hl
30+
ret
31+
32+
public __simple_free
33+
__simple_free:
34+
ret
35+
36+
public __simple_realloc
37+
__simple_realloc:
38+
or a,a
39+
sbc hl,hl
40+
ret
41+
42+
section .data
43+
private _heap_ptr
44+
_heap_ptr:
45+
dl ___heapbot
46+
47+
extern ___heaptop
48+
extern ___heapbot

src/libc/allocator_standard.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <stddef.h>
2+
#include <stdlib.h>
3+
#include <stdint.h>
4+
#include <string.h>
5+
6+
typedef struct __attribute__((packed)) block
7+
{
8+
struct block *ptr;
9+
size_t size;
10+
} __attribute__((packed)) block_t;
11+
12+
extern uint8_t __heapbot[];
13+
extern uint8_t __heaptop[];
14+
static uintptr_t heap_ptr = (uintptr_t)__heapbot;
15+
static block_t _alloc_base;
16+
17+
void *_standard_malloc(size_t alloc_size)
18+
{
19+
block_t *q;
20+
block_t *r;
21+
22+
/* add size of block header to real size */
23+
const size_t size = alloc_size + sizeof(block_t);
24+
if (size < alloc_size)
25+
{
26+
return NULL;
27+
}
28+
29+
for (block_t *p = &_alloc_base; (q = p->ptr); p = q)
30+
{
31+
if (q->size >= size)
32+
{
33+
if (q->size <= size + sizeof(block_t))
34+
{
35+
p->ptr = q->ptr;
36+
}
37+
else
38+
{
39+
q->size -= size;
40+
q = (block_t*)(((uint8_t*)q) + q->size);
41+
q->size = size;
42+
}
43+
44+
return q + 1;
45+
}
46+
}
47+
48+
/* compute next heap pointer */
49+
if (heap_ptr + size < heap_ptr || heap_ptr + size >= (uintptr_t)__heaptop)
50+
{
51+
return NULL;
52+
}
53+
54+
r = (block_t*)heap_ptr;
55+
r->size = size;
56+
57+
heap_ptr = heap_ptr + size;
58+
59+
return r + 1;
60+
}
61+
62+
void _standard_free(void *ptr)
63+
{
64+
if (ptr != NULL)
65+
{
66+
block_t *p;
67+
block_t *q;
68+
69+
q = (block_t*)ptr - 1;
70+
71+
for (p = &_alloc_base; p->ptr && p->ptr < q; p = p->ptr);
72+
73+
if ((uint8_t*)p->ptr == ((uint8_t*)q) + q->size)
74+
{
75+
q->size += p->ptr->size;
76+
q->ptr = p->ptr->ptr;
77+
}
78+
else
79+
{
80+
q->ptr = p->ptr;
81+
}
82+
83+
if (((uint8_t*)p) + p->size == (uint8_t*)q)
84+
{
85+
p->size += q->size;
86+
p->ptr = q->ptr;
87+
}
88+
else
89+
{
90+
p->ptr = q;
91+
}
92+
}
93+
}
94+
95+
void *_standard_realloc(void *ptr, size_t size)
96+
{
97+
block_t *h;
98+
void *p;
99+
100+
if (ptr == NULL)
101+
{
102+
return malloc(size);
103+
}
104+
105+
h = (block_t*)((uint8_t*)ptr - sizeof(block_t));
106+
107+
if (h->size >= (size + sizeof(block_t)))
108+
{
109+
return ptr;
110+
}
111+
112+
if ((p = malloc(size)))
113+
{
114+
memcpy(p, ptr, size);
115+
free(ptr);
116+
}
117+
118+
return p;
119+
}

src/libc/calloc.src

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/libc/free.c

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/libc/include/stdlib.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,12 @@ typedef struct {
1818
long long quot;
1919
} lldiv_t;
2020

21-
typedef char __align;
22-
union header {
23-
struct {
24-
union header *ptr;
25-
unsigned int size;
26-
} s;
27-
__align x;
28-
};
29-
typedef union header _HEADER;
30-
3121
#define EXIT_SUCCESS 0
3222
#define EXIT_FAILURE 1
3323

3424
#define RAND_MAX 8388607
3525

36-
#define HEADER _HEADER
3726
#define allocp _allocp
38-
#define NALLOC 50
3927

4028
__BEGIN_DECLS
4129

0 commit comments

Comments
 (0)