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"
31 #define PAGE_SIZE (st_system_pagesize ())
34 round_pagesize (st_uint size
)
36 return ((size
+ PAGE_SIZE
- 1) / PAGE_SIZE
) * PAGE_SIZE
;
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
49 st_assert (reserved_size
> 0);
50 size
= round_pagesize (reserved_size
);
52 result
= st_system_reserve_memory (NULL
, size
);
56 heap
= st_new0 (st_heap
);
59 heap
->end
= result
+ size
;
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.
75 st_assert (grow_size
> 0);
76 size
= round_pagesize (grow_size
);
78 if ((heap
->p
+ size
) >= heap
->end
)
81 result
= st_system_commit_memory (heap
->p
, size
);
91 st_heap_shrink (st_heap
*heap
, st_uint shrink_size
)
93 /* Shrinks the heap by the specified amount (in bytes).
98 st_assert (shrink_size
> 0);
99 size
= round_pagesize (shrink_size
);
101 if ((heap
->p
- size
) < heap
->start
)
104 result
= st_system_decommit_memory (heap
->p
- size
, size
);
114 st_heap_destroy (st_heap
*heap
)
116 st_system_release_memory (heap
->start
, heap
->end
- heap
->start
);