Remove expensive assertion checks in st-array.h
[panda.git] / src / st-heap.c
blobcf3071be5084b317ce3bbf3ef611f40eb85d218a
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"
31 #define PAGE_SIZE (st_system_pagesize ())
33 static inline st_uint
34 round_pagesize (st_uint size)
36 return ((size + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE;
39 st_heap *
40 st_heap_new (st_uint reserved_size)
42 /* Create a new heap with a reserved address space.
43 * Returns NULL if address space could not be reserved
45 st_pointer result;
46 st_heap *heap;
47 st_uint size;
49 st_assert (reserved_size > 0);
50 size = round_pagesize (reserved_size);
52 result = st_system_reserve_memory (NULL, size);
53 if (result == NULL)
54 return NULL;
56 heap = st_new0 (st_heap);
58 heap->start = result;
59 heap->end = result + size;
60 heap->p = result;
62 return heap;
65 bool
66 st_heap_grow (st_heap *heap, st_uint grow_size)
68 /* Grows the heap by the specified amount (in bytes).
69 * The primitive will not succeed if the heap runs out
70 * of reserved address space.
72 st_pointer result;
73 st_uint size;
75 st_assert (grow_size > 0);
76 size = round_pagesize (grow_size);
78 if ((heap->p + size) >= heap->end)
79 return false;
81 result = st_system_commit_memory (heap->p, size);
82 if (result == NULL)
83 return false;
85 heap->p += size;
87 return true;
90 bool
91 st_heap_shrink (st_heap *heap, st_uint shrink_size)
93 /* Shrinks the heap by the specified amount (in bytes).
95 st_pointer result;
96 st_uint size;
98 st_assert (shrink_size > 0);
99 size = round_pagesize (shrink_size);
101 if ((heap->p - size) < heap->start)
102 return false;
104 result = st_system_decommit_memory (heap->p - size, size);
105 if (result == NULL)
106 return false;
108 heap->p -= size;
110 return true;
113 void
114 st_heap_destroy (st_heap *heap)
116 st_system_release_memory (heap->start, heap->end - heap->start);
117 st_free (heap);