Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
Scratch

Detailed Description

Data Structures

struct  reduct_scratch_t
 Scratch buffer structure. More...
 
struct  reduct_scratch_local_t
 Per-thread scratch-related state structure. More...
 

Macros

#define REDUCT_SCRATCH_INITIAL   128
 Initial scratch buffer size.
 
#define REDUCT_SCRATCH_MAX   16
 The maximum number of scratch buffers.
 
#define REDUCT_SCRATCH_GET(_reduct, _name, _type, _length)
 Allocate a scratch buffer.
 
#define REDUCT_SCRATCH_GROW(_reduct, _name, _type, _length)
 Grow an allocated scratch buffer, the current buffer must be the last one allocated.
 
#define REDUCT_SCRATCH_PUT(_reduct, _name)
 Free a scratch buffer, the current buffer must be the last one allocated.
 

Functions

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.
 

Macro Definition Documentation

◆ REDUCT_SCRATCH_INITIAL

#define REDUCT_SCRATCH_INITIAL   128

Initial scratch buffer size.

Definition at line 20 of file scratch.h.

◆ REDUCT_SCRATCH_MAX

#define REDUCT_SCRATCH_MAX   16

The maximum number of scratch buffers.

Definition at line 21 of file scratch.h.

◆ REDUCT_SCRATCH_GET

#define REDUCT_SCRATCH_GET (   _reduct,
  _name,
  _type,
  _length 
)
Value:
_type* _name = NULL; \
do \
{ \
size_t _needed = (_length) * sizeof(_type); \
if ((_reduct)->scratch.size >= REDUCT_SCRATCH_MAX) \
{ \
REDUCT_ERROR_INTERNAL(_reduct, "scratch buffer overflow"); \
} \
reduct_scratch_t* _s = &(_reduct)->scratch.buffers[(_reduct)->scratch.size++]; \
_s->buffer = malloc(_needed); \
if (_s->buffer == NULL) \
{ \
REDUCT_ERROR_INTERNAL(_reduct, "out of memory"); \
} \
_s->length = _needed; \
_name = (_type*)_s->buffer; \
} while (0)
#define REDUCT_SCRATCH_MAX
The maximum number of scratch buffers.
Definition scratch.h:21
Scratch buffer structure.
Definition scratch.h:28
size_t length
Definition scratch.h:30
char * buffer
Definition scratch.h:29

Allocate a scratch buffer.

Parameters
_reductPointer to the Reduct structure.
_nameThe name of the buffer pointer.
_typeThe type of the elements.
_lengthThe number of elements of _type to reserve memory for.

Definition at line 65 of file scratch.h.

◆ REDUCT_SCRATCH_GROW

#define REDUCT_SCRATCH_GROW (   _reduct,
  _name,
  _type,
  _length 
)
Value:
do \
{ \
size_t _needed = (_length) * sizeof(_type); \
reduct_scratch_t* _s = &(_reduct)->scratch.buffers[(_reduct)->scratch.size - 1]; \
_s->buffer = realloc(_s->buffer, _needed); \
if (_s->buffer == NULL) \
{ \
REDUCT_ERROR_INTERNAL(_reduct, "out of memory"); \
} \
_s->length = _needed; \
_name = (_type*)_s->buffer; \
} while (0)

Grow an allocated scratch buffer, the current buffer must be the last one allocated.

Parameters
_reductPointer to the Reduct structure.
_nameThe name of the buffer pointer.
_typeThe type of the elements.
_lengthThe number of elements of _type to reserve memory for.

Definition at line 92 of file scratch.h.

◆ REDUCT_SCRATCH_PUT

#define REDUCT_SCRATCH_PUT (   _reduct,
  _name 
)
Value:
do \
{ \
assert((_reduct)->scratch.size > 0); \
reduct_scratch_t* _s = &(_reduct)->scratch.buffers[--(_reduct)->scratch.size]; \
free(_s->buffer); \
_s->buffer = NULL; \
_s->length = 0; \
_name = NULL; \
} while (0)

Free a scratch buffer, the current buffer must be the last one allocated.

Parameters
_reductPointer to the Reduct structure.
_nameThe name of the buffer pointer.

Definition at line 112 of file scratch.h.

Function Documentation

◆ reduct_scratch_local_init()

REDUCT_API void reduct_scratch_local_init ( reduct_scratch_local_t local)

Initialize a local scratch state.

Parameters
localPointer to the local scratch state to initialize.

◆ reduct_scratch_local_deinit()

REDUCT_API void reduct_scratch_local_deinit ( reduct_scratch_local_t local)

Deinitialize a local scratch state.

Parameters
localPointer to the local scratch state to deinitialize.