413490c660b27ea43a06d32bc6dd919e261328f0
[muddle-interpreter.git] / src / alloc.c
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 #include "alloc.h"
20 #include "object.h"
21
22 extern pool_object *pool;
23 extern pool_ptr ptop;
24
25 pool_ptr
26 pool_alloc (uint32_t len)
27 {
28   pool_ptr p = ptop;
29   ptop += len;
30   return p;
31 }
32
33 pool_object *
34 POOL_OBJECT (pool_ptr p)
35 {
36   if (!p)
37     return (pool_object *) 0;
38   return &pool[p];
39 }
40
41 pool_ptr
42 pool_copy_array_rev (const pool_object * objs, uint32_t len)
43 {
44   if (!len)
45     return 0;
46   pool_ptr p = pool_alloc (len);
47   for (int i = 0; i < len; i++)
48     {
49       pool[p + i] = (pool_object)
50       {
51       .type = objs[len - i - 1].type,.rest = p + i + 1,.val =
52           objs[len - i - 1].val};
53     }
54   pool[p + len - 1].rest = 0;
55   return p;
56 }
57
58 heap_ptr
59 heap_copy_array_rev (const object * objs, uint32_t len)
60 {
61   object *xs = heap_alloc (len);
62   for (int i = 0; i < (int) len; i++)
63     {
64       xs[i] = objs[len - 1 - (unsigned) i];
65     }
66   return HEAP_PTR_OF_OBJECT (xs);
67 }