MDBX Containers
Loading...
Searching...
No Matches
mdbxc::SerializeScratch Class Reference

Per-call scratch buffer to produce MDBX_val without using thread_local. More...

#include <utils.hpp>

Public Member Functions

MDBXC_NODISCARD MDBX_val view_copy (const void *p, size_t n)
 Copy n bytes from p into bytes and return a view.
 
MDBXC_NODISCARD MDBX_val view_bytes () const noexcept
 Return a view over current bytes (no copy).
 
MDBXC_NODISCARD MDBX_val view_small_copy (const void *p, size_t n) noexcept
 Copy n bytes into the small inline buffer and return a view.
 
void assign_bytes (const void *p, size_t n)
 Replace bytes content with a copy of p..p+n .
 
void clear () noexcept
 Optionally clear and release capacity.
 

Static Public Member Functions

static MDBXC_NODISCARD MDBX_val view (const void *p, size_t n) noexcept
 Zero-copy view over external memory (no ownership).
 

Public Attributes

unsigned char small [16]
 Small inline buffer (16 bytes) aligned to 8 — good for 4/8-byte keys.
 
std::vector< uint8_t > bytes
 Owned dynamic buffer for cases when data must be copied.
 

Detailed Description

Per-call scratch buffer to produce MDBX_val without using thread_local.

Why not thread_local? On Windows/MinGW, destructors of thread_local STL containers (e.g. std::vector) may run at thread teardown when parts of CRT/heap are already being finalized or when different heap arenas are involved. This can lead to heap corruption or std::terminate() without an exception.

To avoid those issues, we keep a small stack-like inline buffer and a per-call dynamic buffer owned by the caller object (not thread_local). The lifetime of returned MDBX_val is guaranteed only until the next call that mutates this scratch, or until it goes out of scope.

Invariants & usage:

  • view(...) does not copy; caller must ensure (ptr,len) lives through the MDBX call.
  • view_small_copy(...) copies up to 16 bytes into an aligned inline buffer. It is intended for small keys (e.g., 4/8-byte INTEGERKEY).
  • view_copy(...) and assign_bytes(...) own copied data in bytes.
  • view_bytes() exposes the current contents of bytes without copying.
  • Do not store the returned MDBX_val beyond the scope of immediate MDBX API call.

Definition at line 168 of file utils.hpp.

Member Function Documentation

◆ assign_bytes()

void mdbxc::SerializeScratch::assign_bytes ( const void * p,
size_t n )
inline

Replace bytes content with a copy of p..p+n .

Definition at line 211 of file utils.hpp.

◆ clear()

void mdbxc::SerializeScratch::clear ( )
inlinenoexcept

Optionally clear and release capacity.

Definition at line 218 of file utils.hpp.

◆ view()

static MDBXC_NODISCARD MDBX_val mdbxc::SerializeScratch::view ( const void * p,
size_t n )
inlinestaticnoexcept

Zero-copy view over external memory (no ownership).

Warning
Caller must guarantee lifetime of p,n until MDBX finishes with it.

Definition at line 174 of file utils.hpp.

◆ view_bytes()

MDBXC_NODISCARD MDBX_val mdbxc::SerializeScratch::view_bytes ( ) const
inlinenoexcept

Return a view over current bytes (no copy).

Definition at line 193 of file utils.hpp.

◆ view_copy()

MDBXC_NODISCARD MDBX_val mdbxc::SerializeScratch::view_copy ( const void * p,
size_t n )
inline

Copy n bytes from p into bytes and return a view.

Definition at line 182 of file utils.hpp.

◆ view_small_copy()

MDBXC_NODISCARD MDBX_val mdbxc::SerializeScratch::view_small_copy ( const void * p,
size_t n )
inlinenoexcept

Copy n bytes into the small inline buffer and return a view.

Note
n must be <= sizeof(small).

Definition at line 202 of file utils.hpp.

Member Data Documentation

◆ bytes

std::vector<uint8_t> mdbxc::SerializeScratch::bytes

Owned dynamic buffer for cases when data must be copied.

Definition at line 170 of file utils.hpp.

◆ small

unsigned char mdbxc::SerializeScratch::small[16]

Small inline buffer (16 bytes) aligned to 8 — good for 4/8-byte keys.

Definition at line 169 of file utils.hpp.


The documentation for this class was generated from the following file: