summaryrefslogtreecommitdiff
path: root/lib/libc/src/stdlib/malloc.c
blob: 1c9e8cab2ca249d4669ba704d3f32cbacd6b06d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of Hyra nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/cdefs.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdbool.h>

#define HEAP_SIZE   0x1001A8
#define HEAP_MAGIC  0x05306A    /* "OSMORA" :~) */
#define HEAP_ALIGN  4
#define HEAP_PROT PROT_READ | PROT_WRITE
#define BYTE_PTR(PTR) ((char *)(PTR))
#define HEAP_NEXT(BLOCKP, SIZE) \
    PTR_OFFSET((BLOCKP), sizeof(struct mem_block) + (SIZE))

struct __aligned(HEAP_ALIGN) mem_block {
    uint32_t magic;
    uint32_t size;
    uint8_t allocated : 1;
    struct mem_block *next;
};

static struct mem_block *mem_head;
static struct mem_block *mem_tail;

/*
 * The size of the heap including data on the heap
 * as well as the sizes of their respective block
 * headers.
 */
static ssize_t heap_len = 0;
static off_t heap_pos = 0;

/*
 * During the initial state of malloc() when the C library
 * first starts up. We can assume that there is zero fragmentation
 * in our heap pool. This allows us to initially allocate memory
 * by bumping a pointer which is O(1). During this state, even after
 * any calls to free(), we can assume that there is more memory ahead
 * of us that is free (due to the initial zero-fragmentation). However,
 * once we've reached the end of the pool, if any memory has been previously
 * freed (indicated by heap_len > 0), we can wrap the tail and start allocating
 * in a best-fit fashion as we can assume that the heap is now fragmented.
 */
static bool wrap = false;

void __malloc_mem_init(void);

/*
 * Terminate program abnormally due to any heap
 * errors.
 *
 * TODO: Raise SIGABRT instead of using _Exit()
 */
__dead static void
__heap_abort(const char *str)
{
    printf(str);
    _Exit(1);
    __builtin_unreachable();
}

/*
 * Find a free block
 *
 * TODO: This is currently a first-fit style
 *       routine. This should be best-fit instead
 *       as it doesn't waste memory.
 */
static struct mem_block *
malloc_find_free(size_t size)
{
    struct mem_block *cur = mem_head;

    while (cur != NULL) {
        if (cur->size >= size) {
            return cur;
        }

        cur = cur->next;
    }

    return NULL;
}

void *
malloc(size_t size)
{
    struct mem_block *next_block;
    struct mem_block *tail;
    size_t inc_len = 0;

    size = ALIGN_UP(size, HEAP_ALIGN);
    inc_len = sizeof(*next_block) + size;

    if (heap_len < 0) {
        heap_len = 0;
    }

    if (heap_len < 0) {
        heap_len = 0;
        return NULL;
    }

    /* Any memory left to allocate? */
    if ((heap_len + inc_len) >= HEAP_SIZE) {
        return NULL;
    }

    if (heap_pos >= HEAP_SIZE - inc_len) {
        wrap = true;
        mem_tail = mem_head;
    }

    tail = wrap ? malloc_find_free(size) : mem_tail;
    if (tail == NULL) {
        return NULL;
    }

    next_block = mem_tail;
    mem_tail = HEAP_NEXT(mem_tail, size);
    mem_tail->next = NULL;

    next_block->next = mem_tail;
    next_block->size = size;
    next_block->allocated = 1;
    next_block->magic = HEAP_MAGIC;

    heap_len += inc_len;
    heap_pos += inc_len;
    return PTR_OFFSET(next_block, sizeof(*next_block));
}

void
free(void *ptr)
{
    struct mem_block *blk;

    blk = PTR_NOFFSET(ptr, sizeof(*blk));
    if (blk->magic != HEAP_MAGIC) {
        __heap_abort("free: bad free block detected\n");
    }
    if (!blk->allocated) {
        __heap_abort("free: double free detected\n");
    }

    blk->allocated = 0;
    heap_len -= (blk->size + sizeof(*blk));
    if (heap_len < 0) {
        heap_len = 0;
    }
}

void
__malloc_mem_init(void)
{
    mem_head = mmap(NULL, HEAP_SIZE, HEAP_PROT, MAP_ANON, 0, 0);
    if (mem_head == NULL) {
        __heap_abort("__malloc_mem_init: mem_head is NULL, out of memory\n");
    }

    mem_head->size = 0;
    mem_head->next = NULL;
    mem_head->allocated = 0;
    mem_tail = mem_head;
}