Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
scratch.h
Go to the documentation of this file.
1#ifndef REDUCT_SCRATCH_H
2#define REDUCT_SCRATCH_H 1
3
4#include <reduct/atom.h>
5#include <reduct/defs.h>
6#include <reduct/error.h>
7#include <reduct/item.h>
8#include <reduct/list.h>
9#include <reduct/native.h>
10#include <reduct/schema.h>
11
12/**
13 * @file scratch.h
14 * @brief Scratch buffer allocation
15 * @defgroup Scratch
16 *
17 * @{
18 */
19
20#define REDUCT_SCRATCH_INITIAL 128 ///< Initial scratch buffer size.
21#define REDUCT_SCRATCH_MAX 16 ///< The maximum number of scratch buffers.
22
23/**
24 * @brief Scratch buffer structure.
25 * @struct reduct_scratch_t
26 */
27typedef struct reduct_scratch
28{
29 char* buffer;
30 size_t length;
32
33/**
34 * @brief Per-thread scratch-related state structure.
35 * @struct reduct_scratch_local_t
36 */
37typedef struct reduct_scratch_state
38{
39 size_t size;
42
43/**
44 * @brief Initialize a local scratch state.
45 *
46 * @param local Pointer to the local scratch state to initialize.
47 */
49
50/**
51 * @brief Deinitialize a local scratch state.
52 *
53 * @param local Pointer to the local scratch state to deinitialize.
54 */
56
57/**
58 * @brief Allocate a scratch buffer.
59 *
60 * @param _reduct Pointer to the Reduct structure.
61 * @param _name The name of the buffer pointer.
62 * @param _type The type of the elements.
63 * @param _length The number of elements of `_type` to reserve memory for.
64 */
65#define REDUCT_SCRATCH_GET(_reduct, _name, _type, _length) \
66 _type* _name = NULL; \
67 do \
68 { \
69 size_t _needed = (_length) * sizeof(_type); \
70 if ((_reduct)->scratch.size >= REDUCT_SCRATCH_MAX) \
71 { \
72 REDUCT_ERROR_INTERNAL(_reduct, "scratch buffer overflow"); \
73 } \
74 reduct_scratch_t* _s = &(_reduct)->scratch.buffers[(_reduct)->scratch.size++]; \
75 _s->buffer = malloc(_needed); \
76 if (_s->buffer == NULL) \
77 { \
78 REDUCT_ERROR_INTERNAL(_reduct, "out of memory"); \
79 } \
80 _s->length = _needed; \
81 _name = (_type*)_s->buffer; \
82 } while (0)
83
84/**
85 * @brief Grow an allocated scratch buffer, the current buffer must be the last one allocated.
86 *
87 * @param _reduct Pointer to the Reduct structure.
88 * @param _name The name of the buffer pointer.
89 * @param _type The type of the elements.
90 * @param _length The number of elements of `_type` to reserve memory for.
91 */
92#define REDUCT_SCRATCH_GROW(_reduct, _name, _type, _length) \
93 do \
94 { \
95 size_t _needed = (_length) * sizeof(_type); \
96 reduct_scratch_t* _s = &(_reduct)->scratch.buffers[(_reduct)->scratch.size - 1]; \
97 _s->buffer = realloc(_s->buffer, _needed); \
98 if (_s->buffer == NULL) \
99 { \
100 REDUCT_ERROR_INTERNAL(_reduct, "out of memory"); \
101 } \
102 _s->length = _needed; \
103 _name = (_type*)_s->buffer; \
104 } while (0)
105
106/**
107 * @brief Free a scratch buffer, the current buffer must be the last one allocated.
108 *
109 * @param _reduct Pointer to the Reduct structure.
110 * @param _name The name of the buffer pointer.
111 */
112#define REDUCT_SCRATCH_PUT(_reduct, _name) \
113 do \
114 { \
115 assert((_reduct)->scratch.size > 0); \
116 reduct_scratch_t* _s = &(_reduct)->scratch.buffers[--(_reduct)->scratch.size]; \
117 free(_s->buffer); \
118 _s->buffer = NULL; \
119 _s->length = 0; \
120 _name = NULL; \
121 } while (0)
122
123/** @} */
124
125#endif
Atom representation and operations.
#define REDUCT_API
Definition defs.h:24
Error handling and reporting.
REDUCT_API void reduct_scratch_local_init(reduct_scratch_local_t *local)
Initialize a local scratch state.
REDUCT_API void reduct_scratch_local_deinit(reduct_scratch_local_t *local)
Deinitialize a local scratch state.
#define REDUCT_SCRATCH_MAX
The maximum number of scratch buffers.
Definition core.h:100
Item management.
List management.
Native function and intrinsic registration.
Schema transformation.
Per-thread scratch-related state structure.
Definition scratch.h:38
Scratch buffer structure.
Definition scratch.h:28
size_t length
Definition scratch.h:30
char * buffer
Definition scratch.h:29