Update LMDB blittable safety checks (#32)
Co-authored-by: Zachary Levy <zachary@sunforge.is> Reviewed-on: #32
This commit was merged in pull request #32.
This commit is contained in:
Vendored
+71
-34
@@ -169,58 +169,86 @@ import "core:fmt"
|
||||
import "core:reflect"
|
||||
import "core:sys/posix"
|
||||
|
||||
import b "../../basic"
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
// ----- Added Odin Helpers ------------------------
|
||||
// ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// Wrap a blittable value's bytes as an LMDB Val.
|
||||
// Wrap a POD value's bytes as an LMDB Val.
|
||||
// T must be a contiguous type with no indirection (no pointers, slices, strings, maps, etc.).
|
||||
blittable_val :: #force_inline proc(val_ptr: ^$T) -> Val {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"blitval: type '%v' contains indirection and cannot be stored directly in LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
pod_val :: #force_inline proc(val_ptr: ^$T) -> Val {
|
||||
when ODIN_DEBUG {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"pod_val: type '%v' contains indirection and cannot be stored directly in LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
}
|
||||
return Val{size_of(T), val_ptr}
|
||||
}
|
||||
|
||||
// Reads a blittable T out of the LMDB memory map by copying it into caller
|
||||
// Reads a POD T out of the LMDB memory map by copying it into caller
|
||||
// storage. The returned T has no lifetime tie to the transaction.
|
||||
blittable_copy :: #force_inline proc(val: Val, $T: typeid) -> T {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"blitval_copy: type '%v' contains indirection and cannot be read directly from LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
pod_copy :: #force_inline proc(val: Val, $T: typeid) -> T {
|
||||
when ODIN_DEBUG {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"pod_copy: type '%v' contains indirection and cannot be read directly from LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
}
|
||||
when b.ODIN_BOUNDS_CHECK {
|
||||
fmt.assertf(
|
||||
val.size == size_of(T),
|
||||
"size_of(%v) (%v) != val.size (%v)",
|
||||
typeid_of(T),
|
||||
size_of(T),
|
||||
val.size,
|
||||
)
|
||||
}
|
||||
return (cast(^T)val.data)^
|
||||
}
|
||||
|
||||
// Zero-copy pointer view into the LMDB memory map as a ^T.
|
||||
// Useful for large blittable types where you want to read individual fields
|
||||
// Useful for large POD types where you want to read individual fields
|
||||
// without copying the entire value (e.g. ptr.timestamp, ptr.flags).
|
||||
// MUST NOT be written through — writes either segfault (default env mode)
|
||||
// or silently corrupt the database (ENV_WRITEMAP).
|
||||
// MUST NOT be retained past txn_commit, txn_abort, or any subsequent write
|
||||
// operation on the same env — the pointer is invalidated.
|
||||
blittable_view :: #force_inline proc(val: Val, $T: typeid) -> ^T {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"blitval_view: type '%v' contains indirection and cannot be viewed directly from LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
pod_view :: #force_inline proc(val: Val, $T: typeid) -> ^T {
|
||||
when ODIN_DEBUG {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"pod_view: type '%v' contains indirection and cannot be viewed directly from LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
}
|
||||
when b.ODIN_BOUNDS_CHECK {
|
||||
fmt.assertf(
|
||||
val.size == size_of(T),
|
||||
"size_of(%v) (%v) != val.size (%v)",
|
||||
typeid_of(T),
|
||||
size_of(T),
|
||||
val.size,
|
||||
)
|
||||
}
|
||||
return cast(^T)val.data
|
||||
}
|
||||
|
||||
// Wrap a slice of blittable elements as an LMDB Val for use with put/get.
|
||||
// Wrap a slice of POD elements as an LMDB Val for use with put/get.
|
||||
// T must be a contiguous type with no indirection.
|
||||
// The caller's slice must remain valid (not freed, not resized) for the
|
||||
// duration of the put call that consumes this Val.
|
||||
slice_val :: #force_inline proc(s: []$T) -> Val {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"slice_val: element type '%v' contains indirection and cannot be stored directly in LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
pod_slice_val :: #force_inline proc(s: []$T) -> Val {
|
||||
when ODIN_DEBUG {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"pod_slice_val: element type '%v' contains indirection and cannot be stored directly in LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
}
|
||||
return Val{uint(len(s) * size_of(T)), raw_data(s)}
|
||||
}
|
||||
|
||||
@@ -231,12 +259,21 @@ slice_val :: #force_inline proc(s: []$T) -> Val {
|
||||
// MUST be copied (e.g. slice.clone) if it needs to outlive the current
|
||||
// transaction; the view is invalidated by txn_commit, txn_abort, or any
|
||||
// subsequent write operation on the same env.
|
||||
slice_view :: #force_inline proc(val: Val, $T: typeid) -> []T {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"slice_view: element type '%v' contains indirection and cannot be read directly from LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
pod_slice_view :: #force_inline proc(val: Val, $T: typeid) -> []T {
|
||||
when ODIN_DEBUG {
|
||||
fmt.assertf(
|
||||
reflect.has_no_indirections(type_info_of(T)),
|
||||
"pod_slice_view: element type '%v' contains indirection and cannot be read directly from LMDB",
|
||||
typeid_of(T),
|
||||
)
|
||||
fmt.assertf(
|
||||
val.size % size_of(T) == 0,
|
||||
"pod_slice_view: val.size (%v) is not a multiple of size_of(%v) (%v)",
|
||||
val.size,
|
||||
typeid_of(T),
|
||||
size_of(T),
|
||||
)
|
||||
}
|
||||
return (cast([^]T)val.data)[:val.size / size_of(T)]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user