Skip to content

Commit

Permalink
Cleaner interface for kernel memory allocator.
Browse files Browse the repository at this point in the history
 * Split <libkern.h> into <memory.h> and <debug.h>
 * Rename klog into Log.
  • Loading branch information
cahirwpz committed Apr 13, 2021
1 parent b8f4e46 commit 33ca073
Show file tree
Hide file tree
Showing 30 changed files with 111 additions and 131 deletions.
6 changes: 0 additions & 6 deletions FreeRTOSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,4 @@ void vPortTrapHandler(struct TrapFrame *);
void vPortDefineMemoryRegions(struct MemRegion *);
void vPortSetupExceptionVector(struct BootData *);

/* Allocate chip memory, should be freed with vPortFree. */
void *pvPortMallocChip(size_t size);

/* Check consistency of heap allocator structures. */
void vPortMemCheck(int verbose);

#endif /* FREERTOS_CONFIG_H */
5 changes: 2 additions & 3 deletions drivers/bitmap.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <bitmap.h>
#include <copper.h>
#include <cpu.h>
#include <string.h>
#include <libkern.h>
#include <memory.h>

void CopSetupScreen(CopList_t *list, const Bitmap_t *bm, uint16_t mode,
uint16_t xs, uint16_t ys) {
Expand Down Expand Up @@ -32,7 +31,7 @@ void BitmapInit(Bitmap_t *bm, uint16_t width, uint16_t height, uint16_t depth,
uint16_t bytesPerRow = ((width + 15) & ~15) / 8;
int bplSize = muls16(bytesPerRow, height);
long size = muls16(bplSize, depth);
void *planes = kmalloc_chip(size);
void *planes = MemAlloc(size, MF_CHIP);
memset(planes, 0, size);

bm->width = width;
Expand Down
6 changes: 3 additions & 3 deletions drivers/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <driver.h>
#include <devfile.h>
#include <ioreq.h>
#include <libkern.h>
#include <memory.h>
#include <string.h>
#include <sys/errno.h>

Expand Down Expand Up @@ -102,8 +102,8 @@ int ConsoleAttach(Driver_t *drv) {
BitmapInit(&cons->bm, WIDTH, HEIGHT + FONT_H, DEPTH, 0);

cons->rowsize = muls16(FONT_H, cons->bm.bytesPerRow);
cons->rowptr = kmalloc(sizeof(void *) * (NROW + 1));
cons->rowins = kmalloc(sizeof(CopIns_t *) * NROW);
cons->rowptr = MemAlloc(sizeof(void *) * (NROW + 1), 0);
cons->rowins = MemAlloc(sizeof(CopIns_t *) * NROW, 0);

for (short i = 0; i < NROW + 1; i++)
cons->rowptr[i] = cons->bm.planes[0] + muls16(i, cons->rowsize);
Expand Down
7 changes: 3 additions & 4 deletions drivers/copper.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <cpu.h>
#include <copper.h>
#include <libkern.h>
#include <memory.h>

void CopListInit(CopList_t *list, uint16_t length) {
list->curr = list->list = kmalloc_chip(muls16(length, sizeof(CopIns_t)));
list->curr = list->list = MemAlloc(muls16(length, sizeof(CopIns_t)), MF_CHIP);
}

void CopListKill(CopList_t *list) {
kfree(list->list);
MemFree(list->list);
}

void CopListActivate(CopList_t *list) {
Expand Down
13 changes: 7 additions & 6 deletions drivers/driver.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#include <driver.h>
#include <libkern.h>
#include <debug.h>
#include <memory.h>
#include <sys/errno.h>

int DeviceAttach(Driver_t *drv) {
drv->state = kcalloc(1, drv->size);
drv->state = MemAlloc(drv->size, MF_ZERO);
if (!drv->state)
return ENOMEM;
klog("Attaching '%s' driver.\n", drv->name);
Log("Attaching '%s' driver.\n", drv->name);
int error = drv->attach(drv);
if (!error)
return 0;
kfree(drv->state);
MemFree(drv->state);
return error;
}

int DeviceDetach(Driver_t *drv) {
klog("Detaching '%s' driver.\n", drv->name);
Log("Detaching '%s' driver.\n", drv->name);
int error = drv->detach(drv);
if (error)
return error;
kfree(drv->state);
MemFree(drv->state);
return 0;
}
4 changes: 2 additions & 2 deletions drivers/floppy.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

#include <driver.h>
#include <string.h>
#include <libkern.h>
#include <devfile.h>
#include <msgport.h>
#include <notify.h>
#include <ioreq.h>
#include <memory.h>
#include <sys/errno.h>

#define __FLOPPY_DRIVER
Expand Down Expand Up @@ -70,7 +70,7 @@ static int FloppyAttach(Driver_t *drv) {
DASSERT(flp->ioTask != NULL);

flp->ioPort = MsgPortCreate(flp->ioTask);
flp->diskTrack = pvPortMallocChip(DISK_TRACK_SIZE);
flp->diskTrack = MemAlloc(DISK_TRACK_SIZE, MF_CHIP);
flp->track = -1;
DASSERT(flp->diskTrack != NULL);

Expand Down
1 change: 0 additions & 1 deletion drivers/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <ioreq.h>
#include <driver.h>
#include <keysym.h>
#include <libkern.h>
#include <sys/errno.h>

#define DEBUG 0
Expand Down
1 change: 0 additions & 1 deletion drivers/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <ioreq.h>
#include <driver.h>
#include <strings.h>
#include <libkern.h>
#include <sys/errno.h>

#define DEBUG 0
Expand Down
1 change: 0 additions & 1 deletion drivers/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <custom.h>
#include <interrupt.h>
#include <libkern.h>
#include <driver.h>
#include <ring.h>
#include <event.h>
Expand Down
4 changes: 2 additions & 2 deletions drivers/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <event.h>
#include <notify.h>
#include <ioreq.h>
#include <libkern.h>
#include <memory.h>
#include <string.h>
#include <sys/errno.h>

Expand Down Expand Up @@ -79,7 +79,7 @@ int AddTtyDevFile(const char *name, File_t *cons) {
TtyState_t *tty;
int error;

if (!(tty = kmalloc(sizeof(TtyState_t))))
if (!(tty = MemAlloc(sizeof(TtyState_t), 0)))
return ENOMEM;
tty->cons = cons;
tty->cons->nonblock = 1;
Expand Down
4 changes: 2 additions & 2 deletions examples/filesys/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <FreeRTOS/task.h>

#include <stdlib.h>
#include <libkern.h>
#include <memory.h>

#include "filesys.h"

Expand Down Expand Up @@ -30,7 +30,7 @@ static void vReaderTask(__unused void *data) {
FsMount();

unsigned seed = (intptr_t)xTaskGetCurrentTaskHandle();
uint8_t *buf = pvPortMalloc(BUFSIZE);
uint8_t *buf = MemAlloc(BUFSIZE, 0);
int n = DirectorySize();

for (;;) {
Expand Down
5 changes: 3 additions & 2 deletions examples/floppy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <FreeRTOS/semphr.h>

#include <driver.h>
#include <memory.h>
#include <floppy.h>
#include <interrupt.h>
#include <file.h>
Expand Down Expand Up @@ -42,7 +43,7 @@ static uint32_t FastRand(void) {
extern uint32_t crc32(const uint8_t *frame, size_t frame_len);

static void vReaderTask(File_t *fd) {
void *data = pvPortMalloc(SECTOR_SIZE);
void *data = MemAlloc(SECTOR_SIZE, 0);

for (;;) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
Expand Down Expand Up @@ -72,7 +73,7 @@ static void vReaderTask(File_t *fd) {
}

static void vWriterTask(File_t *fd) {
uint32_t *data = pvPortMalloc(SECTOR_SIZE);
uint32_t *data = MemAlloc(SECTOR_SIZE, 0);
DASSERT(data != NULL);

for (;;) {
Expand Down
19 changes: 10 additions & 9 deletions examples/preemption/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <FreeRTOS/task.h>

#include <interrupt.h>
#include <libkern.h>
#include <debug.h>
#include <memory.h>
#include <stdlib.h>

#define RED_TASK_PRIORITY 2
Expand All @@ -25,8 +26,8 @@ static void RandMalloc(void) {
return;

int sz = 1 + (rand() % MAXBLKSZ);
void *p = kmalloc(sz);
klog("malloc(%d) = %x\n", sz, p);
void *p = MemAlloc(sz, 0);
Log("malloc(%d) = %x\n", sz, p);
Slot[FreeSlot++] = p;
}

Expand All @@ -36,8 +37,8 @@ static void RandFree(void) {

int n = rand() % FreeSlot;
void *p = Slot[n];
kfree(p);
klog("free(%p)\n", p);
MemFree(p);
Log("free(%p)\n", p);
Slot[n] = NULL;

int last = --FreeSlot;
Expand All @@ -52,15 +53,15 @@ static void RandRealloc(void) {
int n = rand() % FreeSlot;
void *p = Slot[n];
int sz = 1 + (rand() % MAXBLKSZ);
void *q = krealloc(p, sz);
klog("realloc(%p, %d) = %p\n", p, sz, q);
void *q = MemRealloc(p, sz);
Log("realloc(%p, %d) = %p\n", p, sz, q);
Slot[n] = q;
}

static void vTestHeapTask(__unused void *data) {
for (short i = 0; i < NSLOTS * 3 / 4; i++)
RandMalloc();
kmcheck(0);
MemCheck(0);

for (;;) {
int c = rand() % 4;
Expand All @@ -70,7 +71,7 @@ static void vTestHeapTask(__unused void *data) {
RandFree();
else
RandRealloc();
kmcheck(0);
MemCheck(0);
vTaskDelay(1);
}
}
Expand Down
13 changes: 6 additions & 7 deletions examples/startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
#include <cpu.h>
#include <cia.h>
#include <boot.h>
#include <libkern.h>

#define DEBUG 0
#include <debug.h>

extern int main(void);

void _start(BootData_t *aBootData) {
klog("FreeRTOS running on Amiga!\n");
klog("VBR at $%08x\n", aBootData->bd_vbr);
klog("CPU model $%02x\n", aBootData->bd_cpumodel);
klog("Entry point at $%08x\n", aBootData->bd_entry);
Log("FreeRTOS running on Amiga!\n");
Log("VBR at $%08x\n", aBootData->bd_vbr);
Log("CPU model $%02x\n", aBootData->bd_cpumodel);
Log("Entry point at $%08x\n", aBootData->bd_entry);

for (int i = 0; i < aBootData->bd_nregions; i++)
klog("MEM[%d]: %08x - %08x\n", i, aBootData->bd_region[i].mr_lower,
aBootData->bd_region[i].mr_upper);
Log("MEM[%d]: %08x - %08x\n", i, aBootData->bd_region[i].mr_lower,
aBootData->bd_region[i].mr_upper);

DASSERT(custom.intenar == 0);
DASSERT((custom.dmaconr & DMAF_ALL) == 0);
Expand Down
4 changes: 2 additions & 2 deletions examples/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <file.h>
#include <filedesc.h>
#include <proc.h>
#include <libkern.h>
#include <debug.h>
#include <tty.h>

static File_t *FloppyOpen(const char *path) {
Expand All @@ -27,7 +27,7 @@ static void vMainTask(__unused void *data) {
if (ProcLoadImage(&p, init)) {
ProcSetArgv(&p, (char *[]){"init", NULL});
ProcEnter(&p);
klog("Program returned: %d\n", p.exitcode);
Log("Program returned: %d\n", p.exitcode);
}
ProcFini(&p);

Expand Down
2 changes: 1 addition & 1 deletion kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ SOURCES = amigahunk.c \
event.c \
intr.S \
intsrv.c \
malloc.c \
memory.c \
msgport.c \
notify.c \
pipe.c \
Expand Down
10 changes: 5 additions & 5 deletions kernel/amigahunk.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <FreeRTOS/FreeRTOS.h>
#include <amigahunk.h>
#include <strings.h>
#include <stdlib.h>
#include <libkern.h>
#include <memory.h>

#define DEBUG 0
#include <debug.h>

#define HUNK_CODE 1001
#define HUNK_DATA 1002
Expand Down Expand Up @@ -39,8 +39,8 @@ static bool AllocHunks(File_t *fh, Hunk_t **hunkArray, short hunkCount) {
/* size specifiers including memory attribute flags */
uint32_t n = ReadLong(fh);

Hunk_t *hunk = ((n & HUNKF_CHIP) ? pvPortMallocChip : pvPortMalloc)(
sizeof(Hunk_t) + n * sizeof(int));
Hunk_t *hunk = MemAlloc(sizeof(Hunk_t) + n * sizeof(int),
(n & HUNKF_CHIP) ? MF_CHIP : 0);
*hunkArray++ = hunk;

if (!hunk)
Expand Down Expand Up @@ -153,7 +153,7 @@ Hunk_t *LoadHunkList(File_t *fh) {
void FreeHunkList(Hunk_t *hunk) {
do {
Hunk_t *next = hunk->next;
vPortFree(hunk);
MemFree(hunk);
hunk = next;
} while (hunk);
}
9 changes: 5 additions & 4 deletions kernel/devfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <FreeRTOS/atomic.h>

#include <file.h>
#include <libkern.h>
#include <debug.h>
#include <memory.h>
#include <string.h>
#include <devfile.h>
#include <event.h>
Expand Down Expand Up @@ -50,7 +51,7 @@ int AddDevFile(const char *name, DevFileOps_t *ops, DevFile_t **devp) {
goto leave;
}

if (!(dev = kmalloc(sizeof(DevFile_t)))) {
if (!(dev = MemAlloc(sizeof(DevFile_t), 0))) {
error = ENOMEM;
goto leave;
}
Expand All @@ -63,7 +64,7 @@ int AddDevFile(const char *name, DevFileOps_t *ops, DevFile_t **devp) {
if (devp)
*devp = dev;

klog("Registered \'%s\' device file.\n", name);
Log("Registered \'%s\' device file.\n", name);

leave:
xTaskResumeAll();
Expand All @@ -82,7 +83,7 @@ int OpenDevFile(const char *name, File_t **fp) {
goto leave;
}

if (!(f = kcalloc(1, sizeof(File_t)))) {
if (!(f = MemAlloc(sizeof(File_t), MF_ZERO))) {
error = ENOMEM;
goto leave;
}
Expand Down
Loading

0 comments on commit 33ca073

Please sign in to comment.