1 /*
2 * Copyright (c) 2021 Calvin Rose
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22 
23 #ifndef JANET_GC_H
24 #define JANET_GC_H
25 
26 #ifndef JANET_AMALG
27 #include "features.h"
28 #include <janet.h>
29 #endif
30 
31 /* The metadata header associated with an allocated block of memory */
32 #define janet_gc_header(mem) ((JanetGCObject *)(mem))
33 
34 #define JANET_MEM_TYPEBITS 0xFF
35 #define JANET_MEM_REACHABLE 0x100
36 #define JANET_MEM_DISABLED 0x200
37 
38 #define janet_gc_settype(m, t) ((janet_gc_header(m)->flags |= (0xFF & (t))))
39 #define janet_gc_type(m) (janet_gc_header(m)->flags & 0xFF)
40 
41 #define janet_gc_mark(m) (janet_gc_header(m)->flags |= JANET_MEM_REACHABLE)
42 #define janet_gc_reachable(m) (janet_gc_header(m)->flags & JANET_MEM_REACHABLE)
43 
44 /* Memory types for the GC. Different from JanetType to include funcenv and funcdef. */
45 enum JanetMemoryType {
46     JANET_MEMORY_NONE,
47     JANET_MEMORY_STRING,
48     JANET_MEMORY_SYMBOL,
49     JANET_MEMORY_ARRAY,
50     JANET_MEMORY_TUPLE,
51     JANET_MEMORY_TABLE,
52     JANET_MEMORY_STRUCT,
53     JANET_MEMORY_FIBER,
54     JANET_MEMORY_BUFFER,
55     JANET_MEMORY_FUNCTION,
56     JANET_MEMORY_ABSTRACT,
57     JANET_MEMORY_FUNCENV,
58     JANET_MEMORY_FUNCDEF,
59     JANET_MEMORY_THREADED_ABSTRACT,
60 };
61 
62 /* To allocate collectable memory, one must call janet_alloc, initialize the memory,
63  * and then call when janet_enablegc when it is initailize and reachable by the gc (on the JANET stack) */
64 void *janet_gcalloc(enum JanetMemoryType type, size_t size);
65 
66 #endif
67