diff options
Diffstat (limited to 'src/kernel/mem.c')
-rw-r--r-- | src/kernel/mem.c | 41 |
1 files changed, 41 insertions, 0 deletions
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 |