From 0cc9b20c15460213e488bf5e70963b941482f628 Mon Sep 17 00:00:00 2001 From: William Harrington Date: Tue, 14 Jan 2025 16:06:02 -0600 Subject: Add source. --- sdk/other/memreq.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 sdk/other/memreq.c (limited to 'sdk/other/memreq.c') diff --git a/sdk/other/memreq.c b/sdk/other/memreq.c new file mode 100644 index 0000000..16f8433 --- /dev/null +++ b/sdk/other/memreq.c @@ -0,0 +1,59 @@ +/* + * Copy strings and objects into a memory pool. + * $Id$ + * Copyright (c) 1997 by Tycho Softworks. + * For conditions of distribution and reuse see product license. + */ + +#include +#include + +static void *memrequest(MEMPOOL *mem, MEMPAGE *page, size_t len) +{ + int psize = mem->mem_psize; + void *obj; + + /* align objects to ptr field size */ + + len = align(len, __OBJALIGN); + + if(len > mem->mem_psize) + return (*mem->mem_pfault)(mem, len); + + while(page) + { + if(page->page_used + len <= psize) + { + obj = &page->page[page->page_used]; + page->page_used += len; + return obj; + } + page = page->page_next; + } + + if(mem->mem_pused >= mem->mem_pcount) + return (*mem->mem_pfault)(mem, len); + + ++mem->mem_pused; + page = (MEMPAGE *)malloc(psize + sizeof(MEMPAGE)); + if(!page) + return (*mem->mem_pfault)(mem, len); + + mem->mem_last->page_next = page; + mem->mem_last = page; + page->page_used = len; + page->page_next = NULL; + return page->page; +} + +void *memlreq(MEMPOOL *mem, size_t len) +{ + return memrequest(mem, mem->mem_last, len); +} + +void *memreq(MEMPOOL *mem, size_t len) +{ + return memrequest(mem, memfirst(mem), len); +} + + -- cgit v1.2.3-54-g00ecf