Lazuli
Loading...
Searching...
No Matches
memory.c
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-3.0-only
3 * This file is part of Lazuli.
4 */
5
14#include <stdint.h>
15
16#include <Lazuli/common.h>
17#include <Lazuli/config.h>
18
20#include <Lazuli/sys/kernel.h>
21#include <Lazuli/sys/memory.h>
22
33static void *
34SetBreak(const unsigned int increment, AllocationMap * const map)
35{
36 ptrdiff_t newGap;
37 void *newBreak;
38 void *oldBreak;
39
40 if (NULL == map) {
41 return NULL;
42 }
43
44 if (0 == increment) {
45 return map->brk;
46 }
47
48 /* TODO: Check if this one is a good idea... */
49 /* TODO: Check for overflows */
50 oldBreak = map->brk;
51 newBreak = ALLOW_ARITHM(oldBreak) + increment;
52 newGap = ALLOW_ARITHM(SP) - ALLOW_ARITHM(newBreak);
53
54 if (newGap < LZ_CONFIG_BREAK_STACK_GAP) {
55 return NULL;
56 }
57
58 map->brk = newBreak;
59
60 return oldBreak;
61}
62
63/*
64 * For now, only incremental memory allocation is supported.
65 */
66void *
67KIncrementalMalloc(const size_t size)
68{
69 return SetBreak(size, &kernelAllocationMap);
70}
71
72void
73Memory_Copy(const void *source, void *destination, const size_t size)
74{
75 size_t i;
76 const uint8_t *sourceBytes = source;
77 uint8_t *destinationBytes = destination;
78
79 for (i = 0; i < size; ++i) {
80 destinationBytes[i] = sourceBytes[i];
81 }
82}
unsigned char uint8_t
Represents a unsigned integer type with width of exactly 8 bits.
Definition stdint.h:89
Basic type definitions and useful macros.
#define NULL
NULL pointer.
Definition common.h:70
#define ALLOW_ARITHM(X)
Allow arithmetic on a void pointer.
Definition common.h:118
int ptrdiff_t
Represents the difference between two pointers.
Definition common.h:75
Include appropriate config file.
const size_t LZ_CONFIG_BREAK_STACK_GAP
Size in bytes of the security gap between the break and the stack pointer.
AllocationMap kernelAllocationMap
Allocation map for the kernel.
Definition kernel.c:47
Kernel symbols definition.
static void * SetBreak(const unsigned int increment, AllocationMap *const map)
Set break position of a memory region.
Definition memory.c:34
void * KIncrementalMalloc(const size_t size)
Allocate memory for kernel objects using incremental method.
Definition memory.c:67
void Memory_Copy(const void *source, void *destination, const size_t size)
Copy bytes from one location to another in main memory.
Definition memory.c:73
Memory management API.
ATmega328P registers.
#define SP
Stack Pointer
Definition registers.h:64
Represents a map of the allocated memory regions and useful memory handlers for a task or the kernel.
Definition memory.h:25
void * brk
Break position, points to the first location beyond the current end of the heap.
Definition memory.h:35