Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
item.h
Go to the documentation of this file.
1#ifndef REDUCT_ITEM_H
2#define REDUCT_ITEM_H 1
3
4#include <reduct/arena.h>
5#include <reduct/atom.h>
6#include <reduct/closure.h>
7#include <reduct/defs.h>
8#include <reduct/function.h>
9#include <reduct/future.h>
10#include <reduct/list.h>
11#include <reduct/rvsdg.h>
12#include <reduct/task.h>
13
14/**
15 * @file item.h
16 * @brief Item management.
17 * @defgroup item Item
18 *
19 * An item is a generic container for all data types and heap allocated structures within Reduct.
20 *
21 * To optimize memory cacheing and reduce fragmentation, all items are 64 bytes and aligned to cache lines.
22 *
23 * @{
24 */
25
26/**
27 * @brief Item type enumeration.
28 */
29typedef uint8_t reduct_item_type_t;
30#define REDUCT_ITEM_TYPE_NONE 0 ///< No type.
31#define REDUCT_ITEM_TYPE_ATOM 1 ///< An atom.
32#define REDUCT_ITEM_TYPE_ARENA 2 ///< An arena.
33#define REDUCT_ITEM_TYPE_LIST 3 ///< A list.
34#define REDUCT_ITEM_TYPE_FUNCTION 4 ///< A function.
35#define REDUCT_ITEM_TYPE_CLOSURE 5 ///< A closure.
36#define REDUCT_ITEM_TYPE_RVSDG_NODE 6 ///< An IR node.
37#define REDUCT_ITEM_TYPE_RVSDG_EDGE 7 ///< An IR edge.
38#define REDUCT_ITEM_TYPE_RVSDG_REGION 8 ///< An IR region.
39#define REDUCT_ITEM_TYPE_RVSDG_USER 9 ///< An IR user (input/result).
40#define REDUCT_ITEM_TYPE_RVSDG_ORIGIN 10 ///< An IR origin (output/argument).
41#define REDUCT_ITEM_TYPE_FUTURE 11 ///< A future.
42
43typedef uint8_t reduct_item_flags_t; ///< Item flags enumeration.
44#define REDUCT_ITEM_FLAG_NONE 0 ///< No flags.
45#define REDUCT_ITEM_FLAG_MARKED (1 << 0) ///< Item is marked by the GC.
46#define REDUCT_ITEM_FLAG_RETAINED (1 << 1) ///< Item is retained and should be considered a root by the GC.
47
48#define REDUCT_ITEM_PAYLOAD_MAX 56 ///< The maximum size of the item payload.
49
50/**
51 * @brief Item structure.
52 * @struct reduct_item_t
53 *
54 * Should be exactly 64 bytes for caching.
55 *
56 * @see handle
57 */
58typedef struct reduct_item
59{
60 uint32_t position; ///< The position in the input buffer where the item was parsed.
61 _Atomic(reduct_item_flags_t) flags; ///< Flags for the item.
62 reduct_item_type_t type; ///< The type of the item.
63 reduct_input_id_t inputId; ///< The input ID of the item.
64 union {
65 uint32_t length; ///< Common length for the item. (Stored in the union due to padding rules.)
66 reduct_atom_t atom; ///< An atom.
67 reduct_arena_t arena; ///< An arena.
68 reduct_list_t list; ///< A list.
69 reduct_function_t function; ///< A function.
70 reduct_closure_t closure; ///< A closure.
76 reduct_future_t future; ///< A future.
77 struct reduct_item* free; ///< The next free item in the free list.
79 };
81
82#ifdef _Static_assert
83_Static_assert(sizeof(reduct_item_t) == REDUCT_ALIGNMENT, "reduct_item_t must be aligned");
84#endif
85
86#define REDUCT_ITEM_BLOCK_MAX 127 ///< The maximum number of items in a block.
87
88/**
89 * @brief Item block structure.
90 * @struct reduct_item_block_t
91 *
92 * Should be a power of two size as that should help most memory allocators.
93 */
94typedef struct reduct_item_block
95{
96 void* allocated; ///< The actual pointer returned by the memory allocation.
97 struct reduct_item_block* next;
98 uint8_t _padding[REDUCT_ALIGNMENT - sizeof(void*) - sizeof(struct reduct_item_block*)];
101
102#ifdef _Static_assert
103_Static_assert((sizeof(reduct_item_block_t) & (sizeof(reduct_item_block_t) - 1)) == 0,
104 "reduct_item_block_t must be a power of two");
105#endif
106
107/**
108 * @brief Global item-related state structure.
109 * @struct reduct_item_global_t
110 */
111typedef struct
112{
116 mtx_t mutex;
117 struct reduct_item* globalFreeList;
120
121/**
122 * @brief Per-thread item-related state structure.
123 * @struct reduct_item_local_t
124 */
130
131/**
132 * @brief Initialize a global item state.
133 *
134 * @param global Pointer to the global item state to initialize.
135 */
137
138/**
139 * @brief Deinitialize a global item state.
140 *
141 * @param reduct Pointer to the Reduct structure.
142 * @param global Pointer to the global item state to deinitialize.
143 */
144REDUCT_API void reduct_item_global_deinit(struct reduct* reduct, reduct_item_global_t* global);
145
146/**
147 * @brief Initialize a local item state.
148 *
149 * @param local Pointer to the local item state to initialize.
150 */
152
153/**
154 * @brief Deinitialize a local item state.
155 *
156 * @param local Pointer to the local item state to deinitialize.
157 */
159
160/**
161 * @brief Allocate a new item.
162 *
163 * @param reduct Pointer to the Reduct structure.
164 * @return A pointer to the newly created item.
165 */
167
168/**
169 * @brief Deinitialize a item without adding it to the freelist.
170 *
171 * @param reduct Pointer to the Reduct structure.
172 * @param item Pointer to the item to deinitialize.
173 */
174REDUCT_API void reduct_item_deinit(struct reduct* reduct, reduct_item_t* item);
175
176/**
177 * @brief Free an item.
178 *
179 * Intended to be used by the GC or when it is known that an item has no more users and was allocated by the current
180 * thread.
181 *
182 * @param reduct Pointer to the Reduct structure.
183 * @param item Pointer to the item to free.
184 */
185REDUCT_API void reduct_item_free(struct reduct* reduct, reduct_item_t* item);
186
187/**
188 * @brief Get the string representation of the type of an item.
189 *
190 * @param item Pointer to the item.
191 * @return The string representation of the item type.
192 */
194
195/**
196 * @brief Mark an item as reachable.
197 *
198 * Used by the garbage collector.
199 *
200 * @param item Pointer to the item to mark.
201 */
203
204/**
205 * @brief Retain an item, preventing it from being collected by the garbage collector.
206 *
207 * @param item Pointer to the item to retain.
208 */
209static inline REDUCT_ALWAYS_INLINE void reduct_item_retain(struct reduct_item* item)
210{
211 assert(item != NULL);
212 atomic_fetch_or(&item->flags, REDUCT_ITEM_FLAG_RETAINED);
213}
214
215/**
216 * @brief Release an item, potentially allowing the garbage collector to collect it.
217 *
218 * @param item Pointer to the item to release.
219 */
220static inline REDUCT_ALWAYS_INLINE void reduct_item_release(struct reduct_item* item)
221{
222 assert(item != NULL);
223 atomic_fetch_and(&item->flags, ~REDUCT_ITEM_FLAG_RETAINED);
224}
225
226/** @} */
227
228#endif
Arena allocation.
Atom representation and operations.
Closure management.
#define REDUCT_ALIGNMENT
The memory alignment for items.
Definition defs.h:154
uint16_t reduct_input_id_t
Identifies a reduct_input_t within a Reduct structure.
Definition defs.h:147
#define REDUCT_ALWAYS_INLINE
Definition defs.h:61
#define REDUCT_API
Definition defs.h:24
Compiled function.
Future primitives.
static REDUCT_ALWAYS_INLINE void reduct_item_release(struct reduct_item *item)
Release an item, potentially allowing the garbage collector to collect it.
Definition item.h:220
REDUCT_API void reduct_item_local_deinit(reduct_item_local_t *local)
Deinitialize a local item state.
REDUCT_API void reduct_item_global_init(reduct_item_global_t *global)
Initialize a global item state.
REDUCT_API void reduct_item_mark(reduct_item_t *item)
Mark an item as reachable.
REDUCT_API void reduct_item_free(struct reduct *reduct, reduct_item_t *item)
Free an item.
#define REDUCT_ITEM_FLAG_RETAINED
Item is retained and should be considered a root by the GC.
Definition item.h:46
REDUCT_API reduct_item_t * reduct_item_new(struct reduct *reduct)
Allocate a new item.
REDUCT_API const char * reduct_item_type_str(reduct_item_t *item)
Get the string representation of the type of an item.
#define REDUCT_ITEM_PAYLOAD_MAX
The maximum size of the item payload.
Definition item.h:48
uint8_t reduct_item_type_t
Item type enumeration.
Definition item.h:29
REDUCT_API void reduct_item_global_deinit(struct reduct *reduct, reduct_item_global_t *global)
Deinitialize a global item state.
#define REDUCT_ITEM_BLOCK_MAX
The maximum number of items in a block.
Definition item.h:86
static REDUCT_ALWAYS_INLINE void reduct_item_retain(struct reduct_item *item)
Retain an item, preventing it from being collected by the garbage collector.
Definition item.h:209
uint8_t reduct_item_flags_t
Item flags enumeration.
Definition item.h:43
REDUCT_API void reduct_item_deinit(struct reduct *reduct, reduct_item_t *item)
Deinitialize a item without adding it to the freelist.
REDUCT_API void reduct_item_local_init(reduct_item_local_t *local)
Initialize a local item state.
List management.
Intermediate Representation.
Arena structure.
Definition arena.h:26
Atom structure.
Definition atom.h:88
Closure structure.
Definition closure.h:26
Compiled function structure.
Definition function.h:68
Future structure.
Definition future.h:28
Item block structure.
Definition item.h:95
struct reduct_item_block * next
Definition item.h:97
void * allocated
The actual pointer returned by the memory allocation.
Definition item.h:96
Global item-related state structure.
Definition item.h:112
struct reduct_item * globalFreeList
Definition item.h:117
reduct_item_block_t * block
Definition item.h:115
size_t prevBlockCount
Definition item.h:113
size_t globalFreeCount
Definition item.h:118
size_t blockCount
Definition item.h:114
Per-thread item-related state structure.
Definition item.h:126
reduct_item_t * freeList
Definition item.h:128
size_t freeCount
Definition item.h:127
Item structure.
Definition item.h:59
reduct_list_t list
A list.
Definition item.h:68
reduct_rvsdg_node_t rvsdgNode
An ir node.
Definition item.h:71
reduct_closure_t closure
A closure.
Definition item.h:70
reduct_rvsdg_origin_t rvsdgOrigin
An ir origin.
Definition item.h:75
reduct_rvsdg_user_t rvsdgUser
An ir user.
Definition item.h:74
reduct_input_id_t inputId
The input ID of the item.
Definition item.h:63
uint32_t position
The position in the input buffer where the item was parsed.
Definition item.h:60
reduct_arena_t arena
An arena.
Definition item.h:67
reduct_function_t function
A function.
Definition item.h:69
_Atomic(reduct_item_flags_t) flags
Flags for the item.
struct reduct_item * free
The next free item in the free list.
Definition item.h:77
reduct_rvsdg_region_t rvsdgRegion
An ir region.
Definition item.h:73
reduct_atom_t atom
An atom.
Definition item.h:66
reduct_future_t future
A future.
Definition item.h:76
reduct_rvsdg_edge_t rvsdgEdge
An ir edge.
Definition item.h:72
reduct_item_type_t type
The type of the item.
Definition item.h:62
uint32_t length
Common length for the item. (Stored in the union due to padding rules.)
Definition item.h:65
List structure.
Definition list.h:52
Edge structure representing a data dependency.
Definition rvsdg.h:139
Origin of a data dependency edge.
Definition rvsdg.h:106
User of a data dependency edge.
Definition rvsdg.h:123
Task primitives.