From 69c770f7a1c5718abb3f939e60b11937e901a0f7 Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 08 2020 01:00:09 +0000 Subject: internals: added memory allocation functions. --- diff --git a/project/common.mk b/project/common.mk index ef5b62f..7ecc434 100644 --- a/project/common.mk +++ b/project/common.mk @@ -3,6 +3,7 @@ API_SRCS = \ INTERNAL_SRCS = \ src/internal/nolibc/toksvc_compiler.c \ src/internal/toksvc_dprintf_impl.c \ + src/internal/toksvc_memfn_impl.c \ src/internal/toksvc_nolibc_impl.c \ src/internal/toksvc_ntaio_impl.c \ diff --git a/project/headers.mk b/project/headers.mk index 80fde47..cdd0906 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -12,6 +12,7 @@ INTERNAL_HEADERS = \ $(SOURCE_DIR)/src/internal/nolibc/string.h \ $(SOURCE_DIR)/src/internal/nolibc/unistd.h \ $(SOURCE_DIR)/src/internal/toksvc_dprintf_impl.h \ + $(SOURCE_DIR)/src/internal/toksvc_memfn_impl.h \ $(SOURCE_DIR)/src/internal/toksvc_nolibc_impl.h \ ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS) diff --git a/src/internal/toksvc_memfn_impl.c b/src/internal/toksvc_memfn_impl.c new file mode 100644 index 0000000..1a13633 --- /dev/null +++ b/src/internal/toksvc_memfn_impl.c @@ -0,0 +1,92 @@ +/*********************************************************/ +/* toksvc: a framework-native token broker service */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TOKSVC. */ +/*********************************************************/ + +#include +#include +#include "toksvc_memfn_impl.h" +#include "toksvc_driver_impl.h" + +void * toks_calloc(size_t n, size_t size) +{ + void * addr; + size_t bsize; + struct toks_memory_block * block; + + if (!n || (size > (size_t)(-1) / n)) + return 0; + + size *= n; + size += 0xf; + size &= ~(size_t)0xf; + + addr = 0; + bsize = size + sizeof(*block); + + if (ntapi->zw_allocate_virtual_memory( + NT_CURRENT_PROCESS_HANDLE, + &addr, + 0, + &bsize, + NT_MEM_COMMIT, + NT_PAGE_READWRITE)) + return 0; + + block = (struct toks_memory_block *)addr; + block->addr = addr; + block->size = bsize; + block->used = size + offsetof(struct toks_memory_block,buffer); + block->avail = bsize - block->used; + + return (char *)addr + offsetof(struct toks_memory_block,buffer); +} + +void * toks_balloc( + struct toks_memory_block * cache, + struct toks_memory_block * block, + size_t n, size_t size) +{ + void * addr; + + if (!n || (size > (size_t)(-1) / n)) + return 0; + + if (!cache || !cache->addr) + return toks_calloc(n,size); + + size *= n; + size += 0xf; + size &= ~(size_t)0xf; + + if (size > block->avail) + return toks_calloc(1,size); + + /* cached */ + addr = &block->buffer[block->used / sizeof(size_t)]; + block->used += size; + block->avail -= size; + + return addr; +} + +void toks_free(void * addr) +{ + char * baddr; + size_t size; + struct toks_memory_block * block; + + baddr = addr; + baddr -= offsetof(struct toks_memory_block,buffer); + + block = (struct toks_memory_block *)baddr; + addr = block->addr; + size = block->size; + + ntapi->zw_free_virtual_memory( + NT_CURRENT_PROCESS_HANDLE, + &addr, + &size, + NT_MEM_RELEASE); +} diff --git a/src/internal/toksvc_memfn_impl.h b/src/internal/toksvc_memfn_impl.h new file mode 100644 index 0000000..71b0f66 --- /dev/null +++ b/src/internal/toksvc_memfn_impl.h @@ -0,0 +1,22 @@ +#ifndef TOKSVC_MEMFN_IMPL_H +#define TOKSVC_MEMFN_IMPL_H + +#include + +struct toks_memory_block { + void * addr; + size_t size; + size_t used; + size_t avail; + size_t buffer[]; +}; + +void * toks_calloc(size_t n, size_t size); +void toks_free(void * addr); + +void * toks_balloc( + struct toks_memory_block * cache, + struct toks_memory_block * block, + size_t n, size_t size); + +#endif