From 4c2f2fb867cfe64f217a246010a2e2722514dd90 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Tue, 17 Dec 2019 00:43:52 -0500 Subject: a good deal of process --- src/kernel/mem.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/kernel/mem.c (limited to 'src/kernel/mem.c') diff --git a/src/kernel/mem.c b/src/kernel/mem.c new file mode 100644 index 0000000..f12fbf7 --- /dev/null +++ b/src/kernel/mem.c @@ -0,0 +1,41 @@ +/* +Copyright 2019 Benji Dial + +Permission to use, copy, modify, and/or distribute this +software for any purpose with or without fee is hereby +granted, provided that the above copyright notice and this +permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS +ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER +RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE +OF THIS SOFTWARE. +*/ + +#include "mem.h" + +void *allocate_pages(uint16_t n_pages, uint16_t proc_n) { + for (uint16_t *i = MMAP, *l = MMAP + 0x1000 - proc_n; ++i; i <= l) + outer_for: + if (!*i) { + for (uint16_t j = 1; j < n_pages; ++j) + if (i[j]) { + i += j + 1; + goto outer_for; + } + for (uint16_t j = 0; j < n_pages; ++j) + i[j] = proc_n; + return (void *)((i - MMAP) * 0x1000); + } + return (void *)0; +} + +void deallocate_pages(void *from, uint16_t n_pages) { + for (uint16_t *mb = MMAP + ((uint32_t)from / 0x1000), *l = mb + n_pages; mb < l; ++mb) + *mb = 0; +} \ No newline at end of file -- cgit v1.2.3