bd7c46b697f02900505b4a3827af99c56ecb00ae
[muddle-interpreter.git] / src / alloc.h
1 /*
2 Copyright (C) 2017-2018 Keziah Wesley
3
4 You can redistribute and/or modify this file under the terms of the
5 GNU Affero General Public License as published by the Free Software
6 Foundation, either version 3 of the License, or (at your option) any
7 later version.
8
9 This file is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Affero General Public License for more details.
13
14 You should have received a copy of the GNU Affero General Public
15 License along with this file. If not, see
16 <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef ALLOC_H
20 #define ALLOC_H
21
22 #include <assert.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25
26 typedef uint32_t pool_ptr;
27 typedef int32_t heap_ptr;
28
29 typedef union pool_object pool_object;
30 typedef union object object;
31 typedef union uv_val uv_val;
32
33 pool_object *POOL_OBJECT (pool_ptr p);
34 object *HEAP_OBJECT (heap_ptr p);
35 uv_val *UV_VAL (heap_ptr p);
36
37 pool_ptr pool_alloc (uint32_t len);
38 heap_ptr heap_alloc (uint32_t len);
39 inline static heap_ptr
40 heap_alloc_uv (uint32_t len)
41 {
42   // divide by 2 (rounding up), then add one for dope
43   return heap_alloc (((len + 1) >> 1) + 1);
44 }
45
46 // given a headerless array of objects of known size,
47 // copy it backwards into newly-allocated pool space
48 pool_ptr pool_copy_array_rev (const pool_object * objs, uint32_t len);
49 heap_ptr heap_copy_array_rev (const object * objs, uint32_t len);
50
51 #endif // ALLOC_H