Speed up method lookup. Replaced tagging/detagging macros with
[panda.git] / src / st-heap.c
blob22cbeec29f46635bd9edf4a3b4f80abad26d0b6f
1 /*
2 * st-heap.c
4 * Copyright (C) 2008 Vincent Geddes
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
26 #include "st-heap.h"
27 #include "st-utils.h"
28 #include "st-system.h"
30 #define PAGE_SIZE (st_system_pagesize ())
32 static inline st_uint
33 round_pagesize (st_uint size)
35 return ((size + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE;
38 st_heap *
39 st_heap_new (st_uint reserved_size)
41 /* Create a new heap with a reserved address space.
42 * Returns NULL if address space could not be reserved
44 st_pointer result;
45 st_heap *heap;
46 st_uint size;
48 st_assert (reserved_size > 0);
49 size = round_pagesize (reserved_size);
51 result = st_system_reserve_memory (NULL, size);
52 if (result == NULL)
53 return NULL;
55 heap = st_new0 (st_heap);
57 heap->start = result;
58 heap->end = result + size;
59 heap->p = result;
61 return heap;
64 bool
65 st_heap_grow (st_heap *heap, st_uint grow_size)
67 /* Grows the heap by the specified amount (in bytes).
68 * The primitive will not succeed if the heap runs out
69 * of reserved address space.
71 st_pointer result;
72 st_uint size;
74 st_assert (grow_size > 0);
75 size = round_pagesize (grow_size);
77 if ((heap->p + size) >= heap->end)
78 return false;
80 result = st_system_commit_memory (heap->p, size);
81 if (result == NULL)
82 return false;
84 heap->p += size;
86 return true;
89 bool
90 st_heap_shrink (st_heap *heap, st_uint shrink_size)
92 /* Shrinks the heap by the specified amount (in bytes).
94 st_pointer result;
95 st_uint size;
97 st_assert (shrink_size > 0);
98 size = round_pagesize (shrink_size);
100 if ((heap->p - size) < heap->start)
101 return false;
103 result = st_system_decommit_memory (heap->p - size, size);
104 if (result == NULL)
105 return false;
107 heap->p -= size;
109 return true;
112 void
113 st_heap_destroy (st_heap *heap)
115 st_system_release_memory (heap->start, heap->end - heap->start);
116 st_free (heap);