Reduct  v1.0.4-3-gdaf0d70
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 "atom.h"
5#include "closure.h"
6#include "defs.h"
7#include "function.h"
8#include "list.h"
9
10/**
11 * @file item.h
12 * @brief Item management.
13 * @defgroup item Item
14 *
15 * An item is a generic container for all data types and heap alloacted structures within Reduct.
16 *
17 * To optimize memory cacheing and reduce fragmentation, all items are 64 bytes and aligned to cache lines.
18 *
19 * @{
20 */
21
22/**
23 * @brief Item type enumeration.
24 */
26#define REDUCT_ITEM_TYPE_NONE 0 ///< No type.
27#define REDUCT_ITEM_TYPE_ATOM 1 ///< An atom.
28#define REDUCT_ITEM_TYPE_LIST 2 ///< A list.
29#define REDUCT_ITEM_TYPE_FUNCTION 3 ///< A function.
30#define REDUCT_ITEM_TYPE_CLOSURE 4 ///< A closure.
31#define REDUCT_ITEM_TYPE_LIST_NODE 5 ///< A list node.
32
33/**
34 * @brief Item flags enumeration.
35 */
37#define REDUCT_ITEM_FLAG_NONE 0 ///< No flags.
38#define REDUCT_ITEM_FLAG_FALSY (1 << 0) ///< Item is falsy.
39#define REDUCT_ITEM_FLAG_INT_SHAPED (1 << 1) ///< Item is an integer shaped atom.
40#define REDUCT_ITEM_FLAG_FLOAT_SHAPED (1 << 2) ///< Item is a float shaped atom.
41#define REDUCT_ITEM_FLAG_INTRINSIC (1 << 3) ///< Item is an atom and a intrinsic.
42#define REDUCT_ITEM_FLAG_NATIVE (1 << 4) ///< Item is an atom and a native function.
43#define REDUCT_ITEM_FLAG_QUOTED (1 << 5) ///< Item is a quoted atom.
44#define REDUCT_ITEM_FLAG_GC_MARK (1 << 6) ///< Item is marked by GC.
45
46#define REDUCT_ITEM_PAYLOAD_MAX 48 ///< The maximum size of the item payload.
47
48/**
49 * @brief Item structure.
50 * @struct reduct_item_t
51 *
52 * Should be exactly 64 bytes for caching.
53 *
54 * @see handle
55 */
56typedef struct reduct_item
57{
58 struct reduct_input* input; ///< The parsed input that created this item.
59 reduct_uint32_t position; ///< The position in the input buffer where the item was parsed.
60 reduct_item_flags_t flags; ///< Flags for the item.
61 reduct_item_type_t type; ///< The type of the item.
62 reduct_uint16_t retainCount; ///< The reference count for GC retention.
63 union {
65 length; ///< Common length for the item. (Stored in the union to save space due to padding rules.)
66 reduct_atom_t atom; ///< An atom.
67 reduct_list_t list; ///< A list.
68 reduct_list_node_t node; ///< A list node.
69 reduct_function_t function; ///< A function.
70 reduct_closure_t closure; ///< A closure.
71 struct reduct_item* free; ///< The next free item in the free list.
73 };
75
76#ifdef _Static_assert
77_Static_assert(sizeof(reduct_item_t) == 64, "reduct_item_t must be 64 bytes");
78#endif
79
80#define REDUCT_ITEM_BLOCK_MAX 255 ///< The maximum number of items in a block.
81
82/**
83 * @brief Item block structure.
84 * @struct reduct_item_block_t
85 *
86 * Should be a power of two size as that should help most memory allocators.
87 */
88typedef struct reduct_item_block
89{
90 struct reduct_item_block* next;
91 reduct_uint8_t _padding[sizeof(reduct_item_t) - sizeof(struct reduct_item_block*)];
94
95#ifdef _Static_assert
96_Static_assert((sizeof(reduct_item_block_t) & (sizeof(reduct_item_block_t) - 1)) == 0,
97 "reduct_item_block_t must be a power of two");
98#endif
99
100/**
101 * @brief Allocate a new Reduct item.
102 *
103 * @param reduct Pointer to the Reduct structure.
104 * @return A pointer to the newly created item.
105 */
107
108/**
109 * @brief Free an Reduct item.
110 *
111 * @param reduct Pointer to the Reduct structure.
112 * @param item Pointer to the item to free.
113 */
114REDUCT_API void reduct_item_free(struct reduct* reduct, reduct_item_t* item);
115
116/**
117 * @brief Get the integer value of an item if it is number shaped.
118 *
119 * @param item Pointer to the item.
120 * @return The integer value, or 0 if not number shaped.
121 */
123
124/**
125 * @brief Get the float value of an item if it is number shaped.
126 *
127 * @param item Pointer to the item.
128 * @return The float value, or 0.0 if not number shaped.
129 */
131
132/**
133 * @brief Get the string representation of an Reduct item type.
134 *
135 * @param type The item type.
136 * @return The string representation of the item type.
137 */
139
140/** @} */
141
142#endif
Atom representation and operations.
Closure management.
int64_t reduct_int64_t
Definition defs.h:92
uint32_t reduct_uint32_t
Definition defs.h:95
double reduct_float_t
Definition defs.h:102
uint8_t reduct_uint8_t
Definition defs.h:99
#define REDUCT_API
Definition defs.h:7
uint16_t reduct_uint16_t
Definition defs.h:97
Compiled function.
REDUCT_API const char * reduct_item_type_str(reduct_item_type_t type)
Get the string representation of an Reduct item type.
Definition item_impl.h:139
REDUCT_API reduct_float_t reduct_item_get_float(reduct_item_t *item)
Get the float value of an item if it is number shaped.
Definition item_impl.h:126
REDUCT_API reduct_int64_t reduct_item_get_int(reduct_item_t *item)
Get the integer value of an item if it is number shaped.
Definition item_impl.h:113
reduct_uint8_t reduct_item_flags_t
Item flags enumeration.
Definition item.h:36
reduct_uint8_t reduct_item_type_t
Item type enumeration.
Definition item.h:25
REDUCT_API void reduct_item_free(struct reduct *reduct, reduct_item_t *item)
Free an Reduct item.
REDUCT_API reduct_item_t * reduct_item_new(struct reduct *reduct)
Allocate a new Reduct item.
#define REDUCT_ITEM_PAYLOAD_MAX
The maximum size of the item payload.
Definition item.h:46
#define REDUCT_ITEM_BLOCK_MAX
The maximum number of items in a block.
Definition item.h:80
List management.
Atom structure.
Definition atom.h:48
Closure structure.
Definition closure.h:26
Compiled function structure.
Definition function.h:78
Item block structure.
Definition item.h:89
struct reduct_item_block * next
Definition item.h:90
Item structure.
Definition item.h:57
reduct_uint32_t position
The position in the input buffer where the item was parsed.
Definition item.h:59
reduct_list_t list
A list.
Definition item.h:67
reduct_closure_t closure
A closure.
Definition item.h:70
reduct_uint32_t length
Common length for the item. (Stored in the union to save space due to padding rules....
Definition item.h:65
struct reduct_input * input
The parsed input that created this item.
Definition item.h:58
reduct_function_t function
A function.
Definition item.h:69
reduct_item_flags_t flags
Flags for the item.
Definition item.h:60
struct reduct_item * free
The next free item in the free list.
Definition item.h:71
reduct_atom_t atom
An atom.
Definition item.h:66
reduct_list_node_t node
A list node.
Definition item.h:68
reduct_uint16_t retainCount
The reference count for GC retention.
Definition item.h:62
reduct_item_type_t type
The type of the item.
Definition item.h:61
List node structure.
Definition list.h:36
List structure.
Definition list.h:48