aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/other/memreq.c
blob: 16f8433f417776fa2fc9d516a6ef20c44088e51b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 <other/memory.h>
#include <std/math.h>

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);
}