aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-01-20 18:33:29 -0700
committerChristian Cunningham <cc@localhost>2022-01-20 18:33:29 -0700
commit9ca0f8a8bdbfe543a23ff2e75502744a7263a5b7 (patch)
tree3670a0f0aaa70f03c0176e9e1a0d292139f0fa04
parent022dfd0f42962da6c60fbeb5604e1455a07eaaa6 (diff)
Added memory functions
-rw-r--r--include/lib/mem.h4
-rw-r--r--src/lib/mem.c28
2 files changed, 32 insertions, 0 deletions
diff --git a/include/lib/mem.h b/include/lib/mem.h
index 166dfdc..eff6445 100644
--- a/include/lib/mem.h
+++ b/include/lib/mem.h
@@ -12,15 +12,19 @@ struct MemTab {
void* data;
} __attribute__((packed));
+void memset(unsigned char* dest, unsigned char value, unsigned int n);
void memcpy(unsigned char* src, unsigned char* dest, unsigned int n);
unsigned char memcmp(unsigned char* a, unsigned char* b, unsigned int n);
void memshow32(unsigned long* addr, unsigned int n);
+void memset32(unsigned long* dest, unsigned long value, unsigned int n);
void memcpy32(unsigned long* src, unsigned long* dest, unsigned int n);
unsigned char memcmp32(unsigned long* a, unsigned long* b, unsigned int n);
void* malloc(unsigned char size);
+void* calloc(unsigned char size);
void* malloca(unsigned char size, unsigned char amnt);
+void* calloca(unsigned char size, unsigned char amnt);
void free(void* memloc);
void* heap_base(void);
void* heap_top(void);
diff --git a/src/lib/mem.c b/src/lib/mem.c
index ac5db4d..91fed94 100644
--- a/src/lib/mem.c
+++ b/src/lib/mem.c
@@ -11,6 +11,13 @@ void memshow32(unsigned long* addr, unsigned int n)
uart_char(0x0a);
}
+void memset(unsigned char* dest, unsigned char value, unsigned int n)
+{
+ for(unsigned int i = 0; i < n; i++) {
+ dest[i] = value;
+ }
+}
+
void memcpy(unsigned char* src, unsigned char* dest, unsigned int n)
{
for(unsigned int i = 0; i < n; i++) {
@@ -27,6 +34,13 @@ unsigned char memcmp(unsigned char* a, unsigned char* b, unsigned int n)
return 1;
}
+void memset32(unsigned long* dest, unsigned long value, unsigned int n)
+{
+ for(unsigned int i = 0; i < n; i++) {
+ dest[i] = value;
+ }
+}
+
void memcpy32(unsigned long* src, unsigned long* dest, unsigned int n)
{
for(unsigned int i = 0; i < n; i++) {
@@ -66,6 +80,13 @@ void* malloc(unsigned char size)
return (void*)&mem[i + MEM_BASE_SIZE];
}
+void* calloc(unsigned char size)
+{
+ void* addr = malloc(size);
+ memset(addr, 0, size);
+ return addr;
+}
+
void* malloca(unsigned char size, unsigned char amnt)
{
// Return malloc if alignment size is 0 or 1 - trivial alignment
@@ -113,6 +134,13 @@ void* malloca(unsigned char size, unsigned char amnt)
}
}
+void* calloca(unsigned char size, unsigned char amnt)
+{
+ void* addr = malloca(size, amnt);
+ memset(addr, 0, size);
+ return addr;
+}
+
void free(void* memloc)
{
// Don't try to free memory outside of heap