diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index d9ae0e1..2edc23d 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -8,16 +8,6 @@ #define PAGE_ID(address) (U32(address) >> 12) #define PAGE_OFFSET(address) (U32(address) & 0xFFF) -typedef struct AllocationBlock { - uint8_t data[3948]; - uint32_t allocatedFine[32]; - uint32_t allocatedCoarse; - uint32_t blockSize; - struct AllocationBlock *next; - struct AllocationBlock *previous; - uint32_t magic; -} AllocationBlock; - typedef struct { uint32_t present : 1; uint32_t writable : 1; diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index d9ae0e1..2edc23d 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -8,16 +8,6 @@ #define PAGE_ID(address) (U32(address) >> 12) #define PAGE_OFFSET(address) (U32(address) & 0xFFF) -typedef struct AllocationBlock { - uint8_t data[3948]; - uint32_t allocatedFine[32]; - uint32_t allocatedCoarse; - uint32_t blockSize; - struct AllocationBlock *next; - struct AllocationBlock *previous; - uint32_t magic; -} AllocationBlock; - typedef struct { uint32_t present : 1; uint32_t writable : 1; diff --git a/src/kernel/main.c b/src/kernel/main.c index b5f5085..d2dcdf4 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,6 @@ +#define ALLOC_MAIN +#include "memory/malloc.h" + #include #include #include diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index d9ae0e1..2edc23d 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -8,16 +8,6 @@ #define PAGE_ID(address) (U32(address) >> 12) #define PAGE_OFFSET(address) (U32(address) & 0xFFF) -typedef struct AllocationBlock { - uint8_t data[3948]; - uint32_t allocatedFine[32]; - uint32_t allocatedCoarse; - uint32_t blockSize; - struct AllocationBlock *next; - struct AllocationBlock *previous; - uint32_t magic; -} AllocationBlock; - typedef struct { uint32_t present : 1; uint32_t writable : 1; diff --git a/src/kernel/main.c b/src/kernel/main.c index b5f5085..d2dcdf4 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,6 @@ +#define ALLOC_MAIN +#include "memory/malloc.h" + #include #include #include diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index 8c1a07e..66efaf7 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,10 +1,4 @@ #include "malloc.h" -#include -#include - -#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) - -AllocationData allocationData; void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { block->allocatedFine[coarse] |= 1 << fine; @@ -22,12 +16,14 @@ return result; } -void *malloc(uint32_t size) { +#define NULL 0 + +void *_malloc(AllocationData *allocationData, uint32_t size) { uint32_t sizeBit = LOG2(size) + 1; if (sizeBit > 10) { return getPagesCount(((size - 1) >> 12) + 1); } - AllocationBlock *block = allocationData[sizeBit], *last = 0; + AllocationBlock *block = (*allocationData)[sizeBit], *last = 0; while (1) { if (!block) { block = getPage(); @@ -37,7 +33,7 @@ block->previous = last; last->next = block; } else { - allocationData[sizeBit] = block; + (*allocationData)[sizeBit] = block; block->previous = NULL; } block->magic = ALLOCATION_MAGIC; diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index d9ae0e1..2edc23d 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -8,16 +8,6 @@ #define PAGE_ID(address) (U32(address) >> 12) #define PAGE_OFFSET(address) (U32(address) & 0xFFF) -typedef struct AllocationBlock { - uint8_t data[3948]; - uint32_t allocatedFine[32]; - uint32_t allocatedCoarse; - uint32_t blockSize; - struct AllocationBlock *next; - struct AllocationBlock *previous; - uint32_t magic; -} AllocationBlock; - typedef struct { uint32_t present : 1; uint32_t writable : 1; diff --git a/src/kernel/main.c b/src/kernel/main.c index b5f5085..d2dcdf4 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,6 @@ +#define ALLOC_MAIN +#include "memory/malloc.h" + #include #include #include diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index 8c1a07e..66efaf7 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,10 +1,4 @@ #include "malloc.h" -#include -#include - -#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) - -AllocationData allocationData; void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { block->allocatedFine[coarse] |= 1 << fine; @@ -22,12 +16,14 @@ return result; } -void *malloc(uint32_t size) { +#define NULL 0 + +void *_malloc(AllocationData *allocationData, uint32_t size) { uint32_t sizeBit = LOG2(size) + 1; if (sizeBit > 10) { return getPagesCount(((size - 1) >> 12) + 1); } - AllocationBlock *block = allocationData[sizeBit], *last = 0; + AllocationBlock *block = (*allocationData)[sizeBit], *last = 0; while (1) { if (!block) { block = getPage(); @@ -37,7 +33,7 @@ block->previous = last; last->next = block; } else { - allocationData[sizeBit] = block; + (*allocationData)[sizeBit] = block; block->previous = NULL; } block->magic = ALLOCATION_MAGIC; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index b9f010a..4f1524b 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -1,11 +1,41 @@ #ifndef ALLOC_H #define ALLOC_H -#include #include -#define ALLOCATION_MAGIC 0x44BB33DD +#include +extern void *getPage(); +extern void *getPagesCount(uint32_t); +extern void freePage(void *); +extern void memset(void *, uint8_t, uint32_t); + +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +#define ALLOCATION_MAGIC 0xB105F00D // == biosfood + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; typedef AllocationBlock *AllocationData[12]; +extern void free(void *); + +#ifdef ALLOC_MAIN +#undef ALLOC_MAIN + +AllocationData allocationData; + +extern void *_malloc(void *, uintptr_t); + +void *malloc(uint32_t size) { _malloc(&allocationData, size); } + +#endif + #endif diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index d9ae0e1..2edc23d 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -8,16 +8,6 @@ #define PAGE_ID(address) (U32(address) >> 12) #define PAGE_OFFSET(address) (U32(address) & 0xFFF) -typedef struct AllocationBlock { - uint8_t data[3948]; - uint32_t allocatedFine[32]; - uint32_t allocatedCoarse; - uint32_t blockSize; - struct AllocationBlock *next; - struct AllocationBlock *previous; - uint32_t magic; -} AllocationBlock; - typedef struct { uint32_t present : 1; uint32_t writable : 1; diff --git a/src/kernel/main.c b/src/kernel/main.c index b5f5085..d2dcdf4 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,6 @@ +#define ALLOC_MAIN +#include "memory/malloc.h" + #include #include #include diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index 8c1a07e..66efaf7 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,10 +1,4 @@ #include "malloc.h" -#include -#include - -#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) - -AllocationData allocationData; void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { block->allocatedFine[coarse] |= 1 << fine; @@ -22,12 +16,14 @@ return result; } -void *malloc(uint32_t size) { +#define NULL 0 + +void *_malloc(AllocationData *allocationData, uint32_t size) { uint32_t sizeBit = LOG2(size) + 1; if (sizeBit > 10) { return getPagesCount(((size - 1) >> 12) + 1); } - AllocationBlock *block = allocationData[sizeBit], *last = 0; + AllocationBlock *block = (*allocationData)[sizeBit], *last = 0; while (1) { if (!block) { block = getPage(); @@ -37,7 +33,7 @@ block->previous = last; last->next = block; } else { - allocationData[sizeBit] = block; + (*allocationData)[sizeBit] = block; block->previous = NULL; } block->magic = ALLOCATION_MAGIC; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index b9f010a..4f1524b 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -1,11 +1,41 @@ #ifndef ALLOC_H #define ALLOC_H -#include #include -#define ALLOCATION_MAGIC 0x44BB33DD +#include +extern void *getPage(); +extern void *getPagesCount(uint32_t); +extern void freePage(void *); +extern void memset(void *, uint8_t, uint32_t); + +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +#define ALLOCATION_MAGIC 0xB105F00D // == biosfood + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; typedef AllocationBlock *AllocationData[12]; +extern void free(void *); + +#ifdef ALLOC_MAIN +#undef ALLOC_MAIN + +AllocationData allocationData; + +extern void *_malloc(void *, uintptr_t); + +void *malloc(uint32_t size) { _malloc(&allocationData, size); } + +#endif + #endif diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index 2cb743e..d0fe4be 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -51,3 +51,17 @@ return PTR(syscall(SYS_REQUEST_MEMORY, pageCount, U32(targetAddress), U32(physicalAddress), 0)); } + +void *getPage() { return requestMemory(1, NULL, NULL); } + +void *getPagesCount(uint32_t count) { return requestMemory(count, NULL, NULL); } + +void freePage(void *location) {} + +void memset(void *_target, uint8_t byte, uint32_t size) { + uint8_t *target = _target; + for (uint32_t i = 0; i < size; i++) { + *target = byte; + target++; + } +} diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index d9ae0e1..2edc23d 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -8,16 +8,6 @@ #define PAGE_ID(address) (U32(address) >> 12) #define PAGE_OFFSET(address) (U32(address) & 0xFFF) -typedef struct AllocationBlock { - uint8_t data[3948]; - uint32_t allocatedFine[32]; - uint32_t allocatedCoarse; - uint32_t blockSize; - struct AllocationBlock *next; - struct AllocationBlock *previous; - uint32_t magic; -} AllocationBlock; - typedef struct { uint32_t present : 1; uint32_t writable : 1; diff --git a/src/kernel/main.c b/src/kernel/main.c index b5f5085..d2dcdf4 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,6 @@ +#define ALLOC_MAIN +#include "memory/malloc.h" + #include #include #include diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index 8c1a07e..66efaf7 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,10 +1,4 @@ #include "malloc.h" -#include -#include - -#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) - -AllocationData allocationData; void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { block->allocatedFine[coarse] |= 1 << fine; @@ -22,12 +16,14 @@ return result; } -void *malloc(uint32_t size) { +#define NULL 0 + +void *_malloc(AllocationData *allocationData, uint32_t size) { uint32_t sizeBit = LOG2(size) + 1; if (sizeBit > 10) { return getPagesCount(((size - 1) >> 12) + 1); } - AllocationBlock *block = allocationData[sizeBit], *last = 0; + AllocationBlock *block = (*allocationData)[sizeBit], *last = 0; while (1) { if (!block) { block = getPage(); @@ -37,7 +33,7 @@ block->previous = last; last->next = block; } else { - allocationData[sizeBit] = block; + (*allocationData)[sizeBit] = block; block->previous = NULL; } block->magic = ALLOCATION_MAGIC; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index b9f010a..4f1524b 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -1,11 +1,41 @@ #ifndef ALLOC_H #define ALLOC_H -#include #include -#define ALLOCATION_MAGIC 0x44BB33DD +#include +extern void *getPage(); +extern void *getPagesCount(uint32_t); +extern void freePage(void *); +extern void memset(void *, uint8_t, uint32_t); + +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +#define ALLOCATION_MAGIC 0xB105F00D // == biosfood + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; typedef AllocationBlock *AllocationData[12]; +extern void free(void *); + +#ifdef ALLOC_MAIN +#undef ALLOC_MAIN + +AllocationData allocationData; + +extern void *_malloc(void *, uintptr_t); + +void *malloc(uint32_t size) { _malloc(&allocationData, size); } + +#endif + #endif diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index 2cb743e..d0fe4be 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -51,3 +51,17 @@ return PTR(syscall(SYS_REQUEST_MEMORY, pageCount, U32(targetAddress), U32(physicalAddress), 0)); } + +void *getPage() { return requestMemory(1, NULL, NULL); } + +void *getPagesCount(uint32_t count) { return requestMemory(count, NULL, NULL); } + +void freePage(void *location) {} + +void memset(void *_target, uint8_t byte, uint32_t size) { + uint8_t *target = _target; + for (uint32_t i = 0; i < size; i++) { + *target = byte; + target++; + } +} diff --git a/src/userland/hlib/malloc.c b/src/userland/hlib/malloc.c new file mode 120000 index 0000000..022d82b --- /dev/null +++ b/src/userland/hlib/malloc.c @@ -0,0 +1 @@ +../../kernel/memory/malloc.c \ No newline at end of file diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index d9ae0e1..2edc23d 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -8,16 +8,6 @@ #define PAGE_ID(address) (U32(address) >> 12) #define PAGE_OFFSET(address) (U32(address) & 0xFFF) -typedef struct AllocationBlock { - uint8_t data[3948]; - uint32_t allocatedFine[32]; - uint32_t allocatedCoarse; - uint32_t blockSize; - struct AllocationBlock *next; - struct AllocationBlock *previous; - uint32_t magic; -} AllocationBlock; - typedef struct { uint32_t present : 1; uint32_t writable : 1; diff --git a/src/kernel/main.c b/src/kernel/main.c index b5f5085..d2dcdf4 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,6 @@ +#define ALLOC_MAIN +#include "memory/malloc.h" + #include #include #include diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index 8c1a07e..66efaf7 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,10 +1,4 @@ #include "malloc.h" -#include -#include - -#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) - -AllocationData allocationData; void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { block->allocatedFine[coarse] |= 1 << fine; @@ -22,12 +16,14 @@ return result; } -void *malloc(uint32_t size) { +#define NULL 0 + +void *_malloc(AllocationData *allocationData, uint32_t size) { uint32_t sizeBit = LOG2(size) + 1; if (sizeBit > 10) { return getPagesCount(((size - 1) >> 12) + 1); } - AllocationBlock *block = allocationData[sizeBit], *last = 0; + AllocationBlock *block = (*allocationData)[sizeBit], *last = 0; while (1) { if (!block) { block = getPage(); @@ -37,7 +33,7 @@ block->previous = last; last->next = block; } else { - allocationData[sizeBit] = block; + (*allocationData)[sizeBit] = block; block->previous = NULL; } block->magic = ALLOCATION_MAGIC; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index b9f010a..4f1524b 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -1,11 +1,41 @@ #ifndef ALLOC_H #define ALLOC_H -#include #include -#define ALLOCATION_MAGIC 0x44BB33DD +#include +extern void *getPage(); +extern void *getPagesCount(uint32_t); +extern void freePage(void *); +extern void memset(void *, uint8_t, uint32_t); + +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +#define ALLOCATION_MAGIC 0xB105F00D // == biosfood + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; typedef AllocationBlock *AllocationData[12]; +extern void free(void *); + +#ifdef ALLOC_MAIN +#undef ALLOC_MAIN + +AllocationData allocationData; + +extern void *_malloc(void *, uintptr_t); + +void *malloc(uint32_t size) { _malloc(&allocationData, size); } + +#endif + #endif diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index 2cb743e..d0fe4be 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -51,3 +51,17 @@ return PTR(syscall(SYS_REQUEST_MEMORY, pageCount, U32(targetAddress), U32(physicalAddress), 0)); } + +void *getPage() { return requestMemory(1, NULL, NULL); } + +void *getPagesCount(uint32_t count) { return requestMemory(count, NULL, NULL); } + +void freePage(void *location) {} + +void memset(void *_target, uint8_t byte, uint32_t size) { + uint8_t *target = _target; + for (uint32_t i = 0; i < size; i++) { + *target = byte; + target++; + } +} diff --git a/src/userland/hlib/malloc.c b/src/userland/hlib/malloc.c new file mode 120000 index 0000000..022d82b --- /dev/null +++ b/src/userland/hlib/malloc.c @@ -0,0 +1 @@ +../../kernel/memory/malloc.c \ No newline at end of file diff --git a/src/userland/hlib/malloc.h b/src/userland/hlib/malloc.h new file mode 120000 index 0000000..8a56b0a --- /dev/null +++ b/src/userland/hlib/malloc.h @@ -0,0 +1 @@ +../../kernel/memory/malloc.h \ No newline at end of file diff --git a/src/include/hlib.h b/src/include/hlib.h index d77a938..c0e1579 100644 --- a/src/include/hlib.h +++ b/src/include/hlib.h @@ -4,9 +4,10 @@ #include #include +#include "../userland/hlib/malloc.h" + #define PTR(x) ((void *)(uintptr_t)(x)) #define U32(x) ((uint32_t)(uintptr_t)(x)) -#define NULL PTR(0) extern uint32_t installServiceProvider(char *name, int32_t(provider)(void *, uint32_t)); diff --git a/src/kernel/include/memory.h b/src/kernel/include/memory.h index d9ae0e1..2edc23d 100644 --- a/src/kernel/include/memory.h +++ b/src/kernel/include/memory.h @@ -8,16 +8,6 @@ #define PAGE_ID(address) (U32(address) >> 12) #define PAGE_OFFSET(address) (U32(address) & 0xFFF) -typedef struct AllocationBlock { - uint8_t data[3948]; - uint32_t allocatedFine[32]; - uint32_t allocatedCoarse; - uint32_t blockSize; - struct AllocationBlock *next; - struct AllocationBlock *previous; - uint32_t magic; -} AllocationBlock; - typedef struct { uint32_t present : 1; uint32_t writable : 1; diff --git a/src/kernel/main.c b/src/kernel/main.c index b5f5085..d2dcdf4 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -1,3 +1,6 @@ +#define ALLOC_MAIN +#include "memory/malloc.h" + #include #include #include diff --git a/src/kernel/memory/malloc.c b/src/kernel/memory/malloc.c index 8c1a07e..66efaf7 100644 --- a/src/kernel/memory/malloc.c +++ b/src/kernel/memory/malloc.c @@ -1,10 +1,4 @@ #include "malloc.h" -#include -#include - -#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) - -AllocationData allocationData; void *reserveBlock(AllocationBlock *block, uint8_t coarse, uint8_t fine) { block->allocatedFine[coarse] |= 1 << fine; @@ -22,12 +16,14 @@ return result; } -void *malloc(uint32_t size) { +#define NULL 0 + +void *_malloc(AllocationData *allocationData, uint32_t size) { uint32_t sizeBit = LOG2(size) + 1; if (sizeBit > 10) { return getPagesCount(((size - 1) >> 12) + 1); } - AllocationBlock *block = allocationData[sizeBit], *last = 0; + AllocationBlock *block = (*allocationData)[sizeBit], *last = 0; while (1) { if (!block) { block = getPage(); @@ -37,7 +33,7 @@ block->previous = last; last->next = block; } else { - allocationData[sizeBit] = block; + (*allocationData)[sizeBit] = block; block->previous = NULL; } block->magic = ALLOCATION_MAGIC; diff --git a/src/kernel/memory/malloc.h b/src/kernel/memory/malloc.h index b9f010a..4f1524b 100644 --- a/src/kernel/memory/malloc.h +++ b/src/kernel/memory/malloc.h @@ -1,11 +1,41 @@ #ifndef ALLOC_H #define ALLOC_H -#include #include -#define ALLOCATION_MAGIC 0x44BB33DD +#include +extern void *getPage(); +extern void *getPagesCount(uint32_t); +extern void freePage(void *); +extern void memset(void *, uint8_t, uint32_t); + +#define LOG2(X) ((unsigned)(64 - __builtin_clzll((X)) - 1)) + +#define ALLOCATION_MAGIC 0xB105F00D // == biosfood + +typedef struct AllocationBlock { + uint8_t data[3948]; + uint32_t allocatedFine[32]; + uint32_t allocatedCoarse; + uint32_t blockSize; + struct AllocationBlock *next; + struct AllocationBlock *previous; + uint32_t magic; +} AllocationBlock; typedef AllocationBlock *AllocationData[12]; +extern void free(void *); + +#ifdef ALLOC_MAIN +#undef ALLOC_MAIN + +AllocationData allocationData; + +extern void *_malloc(void *, uintptr_t); + +void *malloc(uint32_t size) { _malloc(&allocationData, size); } + +#endif + #endif diff --git a/src/userland/hlib/main.c b/src/userland/hlib/main.c index 2cb743e..d0fe4be 100644 --- a/src/userland/hlib/main.c +++ b/src/userland/hlib/main.c @@ -51,3 +51,17 @@ return PTR(syscall(SYS_REQUEST_MEMORY, pageCount, U32(targetAddress), U32(physicalAddress), 0)); } + +void *getPage() { return requestMemory(1, NULL, NULL); } + +void *getPagesCount(uint32_t count) { return requestMemory(count, NULL, NULL); } + +void freePage(void *location) {} + +void memset(void *_target, uint8_t byte, uint32_t size) { + uint8_t *target = _target; + for (uint32_t i = 0; i < size; i++) { + *target = byte; + target++; + } +} diff --git a/src/userland/hlib/malloc.c b/src/userland/hlib/malloc.c new file mode 120000 index 0000000..022d82b --- /dev/null +++ b/src/userland/hlib/malloc.c @@ -0,0 +1 @@ +../../kernel/memory/malloc.c \ No newline at end of file diff --git a/src/userland/hlib/malloc.h b/src/userland/hlib/malloc.h new file mode 120000 index 0000000..8a56b0a --- /dev/null +++ b/src/userland/hlib/malloc.h @@ -0,0 +1 @@ +../../kernel/memory/malloc.h \ No newline at end of file diff --git a/src/userland/loader/main.c b/src/userland/loader/main.c index ac1f221..6e947fd 100644 --- a/src/userland/loader/main.c +++ b/src/userland/loader/main.c @@ -1,3 +1,5 @@ +#define ALLOC_MAIN + #include #include @@ -9,6 +11,7 @@ log("honey os is alive :)"); loadFromInitrd("pic"); loadFromInitrd("keyboard"); + malloc(10); log("finished loading essential modules"); return 0; }