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
28 #include "st-system.h"
30 #define PAGE_SIZE (st_system_pagesize ())
33 round_pagesize (st_uint size
)
35 return ((size
+ PAGE_SIZE
- 1) / PAGE_SIZE
) * PAGE_SIZE
;
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
48 st_assert (reserved_size
> 0);
49 size
= round_pagesize (reserved_size
);
51 result
= st_system_reserve_memory (NULL
, size
);
55 heap
= st_new0 (st_heap
);
58 heap
->end
= result
+ size
;
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.
74 st_assert (grow_size
> 0);
75 size
= round_pagesize (grow_size
);
77 if ((heap
->p
+ size
) >= heap
->end
)
80 result
= st_system_commit_memory (heap
->p
, size
);
90 st_heap_shrink (st_heap
*heap
, st_uint shrink_size
)
92 /* Shrinks the heap by the specified amount (in bytes).
97 st_assert (shrink_size
> 0);
98 size
= round_pagesize (shrink_size
);
100 if ((heap
->p
- size
) < heap
->start
)
103 result
= st_system_decommit_memory (heap
->p
- size
, size
);
113 st_heap_destroy (st_heap
*heap
)
115 st_system_release_memory (heap
->start
, heap
->end
- heap
->start
);