Skip to content

Commit

Permalink
brutal-alloc: Use the platform allocator when targeting the host.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Jul 16, 2022
1 parent d844b46 commit 4f7a72d
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 66 deletions.
1 change: 0 additions & 1 deletion sources/apps/demo/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <brutal-alloc>
#include <brutal-io>
#include <brutal-ui>

Expand Down
3 changes: 2 additions & 1 deletion sources/kernel/init.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "kernel/init.h"
#include <brutal-alloc>
#include <brutal-debug>

#include "kernel/heap.h"
#include "kernel/init.h"
#include "kernel/kernel.h"
#include "kernel/mmap.h"
#include "kernel/sched.h"
Expand Down
32 changes: 9 additions & 23 deletions sources/libs/brutal-alloc/global.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,29 @@

#include "heap.h"

static Alloc *heap(void)
{
static HeapAlloc heap;
static bool init = false;

if (!init)
{
heap_alloc_init(&heap, NODE_DEFAULT);
init = true;
}

return (Alloc *)&heap;
}

void *alloc_global_acquire(MAYBE_UNUSED Alloc *alloc, size_t size)
{
embed_mem_lock();
void *result = alloc_acquire(heap(), size);
embed_mem_unlock();
embed_heap_lock();
void *result = embed_heap_acquire(size);
embed_heap_unlock();

return result;
}

void *alloc_global_resize(MAYBE_UNUSED Alloc *alloc, void *ptr, size_t new_size)
{
embed_mem_lock();
void *result = alloc_resize(heap(), ptr, new_size);
embed_mem_unlock();
embed_heap_lock();
void *result = embed_heap_resize(ptr, new_size);
embed_heap_unlock();

return result;
}

void alloc_global_release(MAYBE_UNUSED Alloc *alloc, void *ptr)
{
embed_mem_lock();
alloc_release(heap(), ptr);
embed_mem_unlock();
embed_heap_lock();
embed_heap_release(ptr);
embed_heap_unlock();
}

Alloc *alloc_global(void)
Expand Down
20 changes: 10 additions & 10 deletions sources/libs/brutal-alloc/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,16 @@ void *heap_alloc_acquire(HeapAlloc *alloc, size_t req_size)
// CASE 2: It's a brand new block.
if (maj->first == nullptr)
{
maj->first = (HeapMinor *)((uintptr_t)maj + MAJOR_BLOCK_HEADER_SIZE);

maj->first->magic = ALLOC_HEAP_MAGIC;
maj->first->prev = nullptr;
maj->first->next = nullptr;
maj->first->block = maj;
maj->first->size = size;
maj->first->req_size = req_size;
HeapMinor *min = (HeapMinor *)((uintptr_t)maj + MAJOR_BLOCK_HEADER_SIZE);

min->magic = ALLOC_HEAP_MAGIC;
min->prev = nullptr;
min->next = nullptr;
min->block = maj;
min->size = size;
min->req_size = req_size;

maj->first = min;
maj->usage += size + MINOR_BLOCK_HEADER_SIZE;

return (void *)((uintptr_t)(maj->first) + MINOR_BLOCK_HEADER_SIZE);
Expand All @@ -182,7 +184,6 @@ void *heap_alloc_acquire(HeapAlloc *alloc, size_t req_size)
maj->first->prev = (HeapMinor *)((uintptr_t)maj + MAJOR_BLOCK_HEADER_SIZE);
maj->first->prev->next = maj->first;
maj->first = maj->first->prev;

maj->first->magic = ALLOC_HEAP_MAGIC;
maj->first->prev = nullptr;
maj->first->block = maj;
Expand Down Expand Up @@ -306,7 +307,6 @@ void heap_alloc_release(HeapAlloc *alloc, void *ptr)
}

HeapMajor *maj = min->block;

maj->usage -= (min->size + MINOR_BLOCK_HEADER_SIZE);
min->magic = ALLOC_HEAP_DEAD;

Expand Down
5 changes: 5 additions & 0 deletions sources/libs/brutal-fmt/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ IoResult fmt_unsigned(Fmt self, IoWriter writer, uint64_t value)
buf[i++] = '0';
}

if (self.prefix_plus)
{
buf[i++] = '+';
}

while (i < self.width)
{
buf[i++] = self.fill;
Expand Down
4 changes: 2 additions & 2 deletions sources/libs/brutal-gfx/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
struct _Gfx;

/**
```
<- line_ascend
_____ _ _ <- ascend
/ ____| | | | | <- captop
Expand All @@ -19,7 +19,7 @@ struct _Gfx;
|___/ <- descend
| ---- | ...advance
<- line_descend
```
*/
typedef struct
{
Expand Down
1 change: 1 addition & 0 deletions sources/libs/brutal-test/hook-alloc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <brutal-alloc>
#include <brutal-debug>

#include "runner.h"

Expand Down
48 changes: 38 additions & 10 deletions sources/libs/embed/brutal/mem.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
#include <bal/abi.h>
#include <brutal-alloc>
#include <brutal-sync>
#include <embed/mem.h>

void embed_mem_lock(void)
{
// NO-OP
}

void embed_mem_unlock(void)
{
// NO-OP
}

Error embed_mem_acquire(size_t size, void **out_result, MAYBE_UNUSED enum embed_mem_flag flags)
{
// Create the memory object
Expand Down Expand Up @@ -63,3 +55,39 @@ Error embed_mem_release(void *addr, size_t size)

return br_result_to_error(result);
}

static HeapAlloc _heap;

static Alloc *ensure_heap(void)
{
static bool init = false;
if (!init)
{
heap_alloc_init(&_heap, NODE_DEFAULT);
init = true;
}
return (Alloc *)&_heap;
}

void embed_heap_lock(void)
{
}

void embed_heap_unlock(void)
{
}

void *embed_heap_acquire(size_t size)
{
return alloc_acquire(ensure_heap(), size);
}

void *embed_heap_resize(void *ptr, size_t size)
{
return alloc_resize(ensure_heap(), ptr, size);
}

void embed_heap_release(void *ptr)
{
alloc_release(ensure_heap(), ptr);
}
36 changes: 33 additions & 3 deletions sources/libs/embed/efi/mem.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <brutal-alloc>
#include <brutal-debug>
#include <brutal-sync>
#include <efi/lib.h>
#include <efi/srvs/bs.h>
#include <embed/mem.h>
#include <brutal-debug>

Error embed_mem_acquire(size_t size, void **out_result, MAYBE_UNUSED enum embed_mem_flag flags)
{
Expand All @@ -23,10 +25,38 @@ Error embed_mem_release(void *addr, MAYBE_UNUSED size_t size)
return ERR_SUCCESS;
}

void embed_mem_lock(void)
static HeapAlloc _heap;

static Alloc *ensure_heap(void)
{
static bool init = false;
if (!init)
{
heap_alloc_init(&_heap, NODE_DEFAULT);
init = true;
}
return (Alloc *)&_heap;
}

void embed_heap_lock(void)
{
}

void embed_heap_unlock(void)
{
}

void *embed_heap_acquire(size_t size)
{
return alloc_acquire(ensure_heap(), size);
}

void *embed_heap_resize(void *ptr, size_t size)
{
return alloc_resize(ensure_heap(), ptr, size);
}

void embed_mem_unlock(void)
void embed_heap_release(void *ptr)
{
alloc_release(ensure_heap(), ptr);
}
52 changes: 41 additions & 11 deletions sources/libs/embed/kernel/mem.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
#include <embed/mem.h>
#include <brutal-debug>
#include <brutal-sync>
#include <embed/mem.h>

#include "kernel/heap.h"

static Lock _lock;

void embed_mem_lock(void)
{
lock_acquire(&_lock);
}

void embed_mem_unlock(void)
{
lock_release(&_lock);
}

Error embed_mem_acquire(size_t size, void **out_result, MAYBE_UNUSED enum embed_mem_flag flags)
{
HeapResult alloc_result = heap_alloc(size);
Expand All @@ -35,3 +26,42 @@ Error embed_mem_release(void *addr, size_t size)
heap_free((HeapRange){(uintptr_t)addr, size});
return ERR_SUCCESS;
}

static Lock _lock;
static HeapAlloc _heap;

static Alloc *ensure_heap(void)
{
static bool init = false;
if (!init)
{
heap_alloc_init(&_heap, NODE_DEFAULT);
init = true;
}
return (Alloc *)&_heap;
}

void embed_heap_lock(void)
{
lock_acquire(&_lock);
}

void embed_heap_unlock(void)
{
lock_release(&_lock);
}

void *embed_heap_acquire(size_t size)
{
return alloc_acquire(ensure_heap(), size);
}

void *embed_heap_resize(void *ptr, size_t size)
{
return alloc_resize(ensure_heap(), ptr, size);
}

void embed_heap_release(void *ptr)
{
alloc_release(ensure_heap(), ptr);
}
14 changes: 10 additions & 4 deletions sources/libs/embed/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ enum embed_mem_flag
EMBED_MEM_NONE,
};

void embed_mem_lock(void);

void embed_mem_unlock(void);

Error embed_mem_acquire(size_t size, void **out_result, enum embed_mem_flag flags);

Error embed_mem_release(void *addr, size_t size);

void *embed_mem_set(void *d, uint8_t s, size_t c);

void *embed_mem_copy(void *to, void const *from, size_t size);

void embed_heap_lock(void);

void embed_heap_unlock(void);

void *embed_heap_acquire(size_t size);

void *embed_heap_resize(void *ptr, size_t size);

void embed_heap_release(void *ptr);
28 changes: 27 additions & 1 deletion sources/libs/embed/posix/mem.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <brutal-debug>
#include <embed/mem.h>
#include <embed/posix/err.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <brutal-debug>

void embed_mem_lock(void)
{
Expand Down Expand Up @@ -35,3 +36,28 @@ Error embed_mem_release(void *addr, size_t size)

return ERR_SUCCESS;
}

void embed_heap_lock(void)
{
// no-op
}

void embed_heap_unlock(void)
{
// no-op
}

void *embed_heap_acquire(size_t size)
{
return calloc(1, size);
}

void *embed_heap_resize(void *ptr, size_t size)
{
return realloc(ptr, size);
}

void embed_heap_release(void *ptr)
{
free(ptr);
}
2 changes: 2 additions & 0 deletions sources/utils/test/brutal/fmt/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ test$(fmt_printf)
TEST_CASE("00012", "%05d", 12);
TEST_CASE("00123", "%05d", 123);
TEST_CASE("0x123", "%#05x", 0x123);
TEST_CASE("0x123", "%#05x", 0x123);
TEST_CASE("0x123", "%#010x", 0x123);
}

0 comments on commit 4f7a72d

Please sign in to comment.