diff options
author | Christian Cunningham <cc@localhost> | 2022-02-04 23:55:05 -0700 |
---|---|---|
committer | Christian Cunningham <cc@localhost> | 2022-02-04 23:55:05 -0700 |
commit | e3eb5a92ae3991cdfbb5a5cf553eac476bc5822f (patch) | |
tree | b6f8b98b8e9b970dc08c218c07b1351b0eaf2944 /src/lib | |
parent | 1b14e08247393af5f4e8f2a3c31406ce750ca2f0 (diff) |
Removed old mem
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/ll.c | 57 | ||||
-rw-r--r-- | src/lib/mem.c | 340 | ||||
-rw-r--r-- | src/lib/q.c | 54 |
3 files changed, 0 insertions, 451 deletions
diff --git a/src/lib/ll.c b/src/lib/ll.c deleted file mode 100644 index 4918194..0000000 --- a/src/lib/ll.c +++ /dev/null @@ -1,57 +0,0 @@ -#include <lib/ll.h> -#include <lib/mem.h> - -struct LL* new_ll(void* val) -{ - struct LL* ll = (struct LL*)malloc(sizeof(struct LL)); - ll->prev = ll; - ll->next = ll; - ll->data = val; - return ll; -} - -void push_ll(struct LL* l, void* val) -{ - struct LL* ll = (struct LL*)malloc(sizeof(struct LL)); - l->prev->next = ll; - ll->prev = l->prev; - ll->next = l; - l->prev = ll; - ll->data = val; -} - -void pop_ll(struct LL* l) -{ - if ((l->prev == l->next) && (l->prev == l)) - l->data = 0; - else { - l->prev->next = l->next; - l->next->prev = l->prev; - free(l); - } -} - -void remove_ll(struct LL* l, unsigned long idx) -{ - struct LL* t = l; - for(unsigned long i = 0; i < idx; i++) { - t = t->next; - } - t->prev->next = t->next; - t->next->prev = t->prev; - free(t); -} - -unsigned long length_ll(struct LL* l) -{ - struct LL* t = l; - unsigned long len = 0; - while (1) { - if (t->next == l) { - return len; - } - len++; - t = t->next; - } - return len; -} diff --git a/src/lib/mem.c b/src/lib/mem.c deleted file mode 100644 index 2f50d3d..0000000 --- a/src/lib/mem.c +++ /dev/null @@ -1,340 +0,0 @@ -#include <drivers/uart.h> -#include <globals.h> -#include <graphics/lfb.h> -#include <lib/mem.h> -#include <sys/schedule.h> - -void memcpyrot(unsigned char* src, struct RotBuffer* rb, unsigned int n) -{ - if (n > rb->size) - return; - unsigned char* base = rb->base; - unsigned int offset = rb->woffset; - for (unsigned int i = 0; i < n; i++) { - if (offset >= rb->size) - offset = 0; - base[offset] = src[i]; - offset++; - } - rb->woffset = offset; -} - -void memshow(unsigned char* addr, unsigned int n) -{ - unsigned char temp; - for(unsigned int i = 0; i < n; i++) { - temp = addr[i] >> 4; - temp += 0x30; - if (temp > 0x39) - temp += 7; - uart_char(temp); - temp = addr[i]; - temp += 0x30; - if (temp > 0x39) - temp += 7; - uart_char(temp); - if (i+1 != n) - uart_char(0x20); - } - uart_char(0x0a); -} - -void memshow32(unsigned long* addr, unsigned int n) -{ - for(unsigned int i = 0; i < n; i++) { - uart_hex(addr[i]); - if (i+1 != n) - uart_char(0x20); - } - 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++) { - dest[i] = src[i]; - } -} - -unsigned char memcmp(unsigned char* a, unsigned char* b, unsigned int n) -{ - for(unsigned int i = 0; i < n; i++) { - if (a[i] != b[i]) - return 0; - } - 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++) { - dest[i] = src[i]; - } -} - -unsigned char memcmp32(unsigned long* a, unsigned long* b, unsigned int n) -{ - for(unsigned int i = 0; i < n; i++) { - if (a[i] != b[i]) - return 0; - } - return 1; -} - - -void* malloc(unsigned char size) -{ - unsigned char* mem = (unsigned char*)rpi_heap; - unsigned long i = 0; - // TODO: Use Null PID - while (((void*)(mem + i) < rpi_heap_top) && !(mem[i + MEM_SIZE_OFFSET] == size && mem[i + MEM_USE_OFFSET]==0)) { - i += mem[i] + MEM_META_SIZE; - } - if (mem + i >= (unsigned char*)(rpi_heap_top + MAX_MM)) - return NULL; - // Update top of heap - if (mem[i] == 0) - rpi_heap_top = (void*)&mem[i + MEM_META_SIZE + size]; - mem[i] = size; - mem[i + MEM_BASE_SIZE + size] = size; - // Use allocator's PID - mem[i + MEM_USE_OFFSET] = 1; - return (void*)&mem[i + MEM_BASE_SIZE]; -} - -void* calloc(unsigned char size) -{ - void* addr = malloc(size); - if (addr != NULL) { - memset(addr, 0, size); - return addr; - } - return NULL; -} -void* realloc(void* old, unsigned char size) -{ - unsigned char osize = *(unsigned char*)(old - MEM_BASE_SIZE); - if (size <= osize) - return old; - void* new = malloc(size); - if (new != NULL) { - free(old); - return new; - } - return NULL; -} - -void* malloca(unsigned char size, unsigned char amnt) -{ - // Return malloc if alignment size is 0 or 1 - trivial alignment - if(amnt == 0 || amnt == 1) - return malloc(size); - unsigned char* mem = (unsigned char*)rpi_heap; - unsigned long i = 0; - // TODO: Use Null PID - while(mem + i < rpi_heap + MAX_MM) { - unsigned long diff = (unsigned long)mem + i + MEM_BASE_SIZE; - diff %= amnt; - diff = amnt - diff; - diff %= amnt; - if((mem[i + MEM_SIZE_OFFSET] == size) && mem[i + MEM_USE_OFFSET]==0) { - if(diff == 0) { - mem[i + MEM_SIZE_OFFSET] = size; - mem[i + MEM_BASE_SIZE + size] = size; - mem[i + MEM_USE_OFFSET] = 1; - return (void*)&mem[i + MEM_BASE_SIZE]; - } - } else if (mem[i] == 0) { - if(diff == 0) { - mem[i + MEM_SIZE_OFFSET] = size; - mem[i + MEM_BASE_SIZE + size] = size; - mem[i + MEM_USE_OFFSET] = 1; - rpi_heap_top = (void*)&mem[i + MEM_META_SIZE + size]; - return (void*)&mem[i + MEM_BASE_SIZE]; - } else { - while (diff <= MEM_BASE_SIZE) { - diff += amnt; - } - unsigned long empty_size = diff - MEM_META_SIZE; - if(empty_size == 0) { - empty_size += amnt; - diff += amnt; - } - mem[i + MEM_SIZE_OFFSET] = empty_size; - mem[i + MEM_BASE_SIZE + empty_size] = empty_size; - i += diff; - mem[i + MEM_SIZE_OFFSET] = size; - mem[i + MEM_BASE_SIZE + size] = size; - mem[i + MEM_USE_OFFSET] = 1; - rpi_heap_top = (void*)&mem[i + MEM_META_SIZE + size]; - return (void*)&mem[i + MEM_BASE_SIZE]; - } - } - - i += mem[i] + MEM_META_SIZE; - } - return NULL; -} - -void* calloca(unsigned char size, unsigned char amnt) -{ - void* addr = malloca(size, amnt); - if (addr != NULL) { - memset(addr, 0, size); - return addr; - } - return NULL; -} - -void* realloca(void* old, unsigned char size, unsigned char amnt) -{ - unsigned char osize = *(unsigned char*)(old - MEM_BASE_SIZE); - if (size <= osize) - return old; - void* new = malloca(size, amnt); - if (new != NULL) { - free(old); - return new; - } - return NULL; -} - -void free(void* memloc) -{ - // Don't try to free memory outside of heap - if(!(((void*)rpi_heap <= memloc) && (memloc < rpi_heap_top))) - return; - unsigned char* base = memloc - MEM_BASE_SIZE; - unsigned char size = base[MEM_SIZE_OFFSET]; - // TODO: Use Null PID - base[MEM_USE_OFFSET] = 0; - // Clear out old memory - for(unsigned int i = 0; i < size; i++) { - base[i + MEM_BASE_SIZE] = 0; - } - // If it is the last entry, clear it and move the heap top down - if (base + size + MEM_META_SIZE == rpi_heap_top) { - while(base[MEM_USE_OFFSET] == 0 && base >= rpi_heap) { - base[MEM_SIZE_OFFSET] = 0; - base[MEM_BASE_SIZE + size] = 0; - rpi_heap_top = base; - unsigned long psize = *(base - 1); - base -= psize + MEM_META_SIZE; - } - } -} - -void* heap_base(void) -{ - return (void*)rpi_heap; -} - -void* heap_top(void) -{ - return rpi_heap_top; -} - -void vheap_info(void) -{ - unsigned char* base = rpi_heap; - unsigned long ioff = STACK_DRAW_WIDTH*STACK_DRAW_SIZE; - unsigned long yoff = STACK_DRAW_YOFF; - draw_cbox(ioff, yoff, STACK_DRAW_SIZE*STACK_DRAW_WIDTH, STACK_DRAW_SIZE * 8, 0x0); - while ((void*)base < rpi_heap_top) { - unsigned char size = base[MEM_SIZE_OFFSET]; - if(base[MEM_USE_OFFSET] == 0) { - draw_cbox(ioff, yoff, STACK_DRAW_SIZE, STACK_DRAW_SIZE, 0x00FF00); - } else { - draw_cbox(ioff, yoff, STACK_DRAW_SIZE, STACK_DRAW_SIZE, 0xFF0000); - } - ioff += STACK_DRAW_SIZE; - if(ioff % STACK_DRAW_WIDTH == 0) { - yoff += STACK_DRAW_SIZE; - ioff = STACK_DRAW_WIDTH*STACK_DRAW_SIZE; - } - base += size + MEM_META_SIZE; - } -} - -void heap_info(void) -{ - unsigned char* base = rpi_heap; - while ((void*)base < rpi_heap_top) { - unsigned char size = base[MEM_SIZE_OFFSET]; - if(base[MEM_USE_OFFSET] == 0) { - uart_char('F'); - uart_char(' '); - } else { - } - uart_hex((unsigned long)(base + MEM_BASE_SIZE)); - uart_string(" Size: "); - uart_10(size); - uart_string("\n"); - static char* data = "00 \0"; - static unsigned char temp = 0; - for(unsigned int i = 0; i < size; i++) { - temp = (base[MEM_BASE_SIZE + i]>>4)&0xF; - if(temp > 9) - temp += 7; - temp += 0x30; - data[0] = temp; - temp = (base[MEM_BASE_SIZE + i])&0xF; - if(temp > 9) - temp += 7; - temp += 0x30; - data[1] = temp; - uart_string(data); - } - uart_char('\n'); - base += size + MEM_META_SIZE; - } - uart_char('\n'); -} - -void heap_info_u(void) -{ - unsigned char* base = rpi_heap; - while ((void*)base < rpi_heap_top) { - unsigned char size = base[MEM_SIZE_OFFSET]; - if(base[MEM_USE_OFFSET] == 0) { - base += size + MEM_META_SIZE; - continue; - } - uart_hex((unsigned long)(base + MEM_BASE_SIZE)); - uart_string(" Size: "); - uart_10(size); - uart_string("\n"); - static char* data = "00 \0"; - static unsigned char temp = 0; - for(unsigned int i = 0; i < size; i++) { - temp = (base[MEM_BASE_SIZE + i]>>4)&0xF; - if(temp > 9) - temp += 7; - temp += 0x30; - data[0] = temp; - temp = (base[MEM_BASE_SIZE + i])&0xF; - if(temp > 9) - temp += 7; - temp += 0x30; - data[1] = temp; - uart_string(data); - } - uart_char('\n'); - base += size + MEM_META_SIZE; - } - uart_char('\n'); -} diff --git a/src/lib/q.c b/src/lib/q.c deleted file mode 100644 index 2ade143..0000000 --- a/src/lib/q.c +++ /dev/null @@ -1,54 +0,0 @@ -#include <lib/q.h> -#include <lib/mem.h> - -struct Q_base* new_q() -{ - struct Q_base* q = (struct Q_base*)malloc(sizeof(struct Q_base)); - q->next = 0; - q->last = 0; - return q; -} - -void push_q(struct Q_base* qb, void* val) -{ - struct Q* n = (struct Q*)malloc(sizeof(struct Q)); - n->next = 0; - n->data = val; - if (qb->last != 0) - qb->last->next = n; - else { - qb->next = n; - } - qb->last = n; -} - -void pop_q(struct Q_base* qb) -{ - if (qb->next == 0) - return; - if (qb->next == qb->last) { - free(qb->next->data); - free(qb->next); - qb->next = 0; - qb->last = 0; - return; - } - struct Q* t = qb->next; - qb->next = qb->next->next; - free(t->data); - free(t); -} - -unsigned long length_q(struct Q_base* qb) -{ - unsigned long length = 0; - if(qb->next == 0) - return length; - length++; - struct Q* q = qb->next; - while (q != qb->last) { - length++; - q = q->next; - } - return length; -} |