corm_common(3)corm

NAME

corm_common - Corm get, put, del and drop

- Core key/value operations.

SYNOPSIS

Functions

const void * corm_get (uint32_t hd, const void *const key)

Retrieve a value by key.

void corm_get_copy (uint32_t hd, const void *const key, void *dst)

Copy a value into a caller buffer.

int corm_read (uint32_t hd, const void *const key, void *dst, size_t max_sz)

Non-aborting safe record retrieval with guaranteed zero-initialization.

uint32_t corm_put (uint32_t hd, const void *const key, const void *const value)

Insert or update a pair.

void corm_del (uint32_t hd, const void *const key)

Delete an entry by key.

void corm_del_all (uint32_t hd, const void *const key)

Delete all entries with the specified key.

int corm_del_value (uint32_t hd, const void *const key, const void *const value)

Delete the first entry with this key whose value matches.

void corm_drop (uint32_t hd)

Remove all entries from a map.

Detailed Description

Core key/value operations.

Warning

Pointer Ownership and Lifetime: All pointers returned by corm functions (corm_get, corm_next) are owned by the map. They remain valid until:

  • The entry is DELETED (corm_del)
  • The map is CLOSED (corm_close)
  • The entry is REPLACED with a LARGER value (corm_put)
  • The key is CHANGED (corm_put with different key at same hash)

IMPROVED BEHAVIOR: Since v0.6.0, pointers typically remain valid when updating with the same key and a same-or-smaller value. However, for maximum safety, it's still recommended to copy data before modifications.

Do NOT:

SAFE patterns after allocation reuse improvement:

// Updating same key with same-sized value - pointer usually stays valid
uint32_t *counter = corm_get(hd, key);
corm_put(hd, key, &(uint32_t){*counter + 1});
// counter pointer is still valid (same allocation reused)

// For maximum safety, still copy before complex operations:
const uint32_t *old_ptr = corm_get(hd, key);
uint32_t old_val = old_ptr ? *old_ptr : 0;
corm_put(hd, key, &new_val);
use(old_val);  // Safe - using copied value

UNSAFE pattern (pointer invalidated by larger value):

const char *str = corm_get(hd, key);  // str = "short"
corm_put(hd, key, "much longer string");
// str is now INVALID - new allocation was needed

Note

For CM_PTR type values, the returned pointer points to the stored pointer bytes, not the pointer itself.

See also

Corm open, close and save

corm_assoc

Corm iteration

Corm type customization

Function Documentation

void corm_del (uint32_t hd, const void *const key)

Delete an entry by key. For maps with CM_MULTIVALUE flag, this only deletes the FIRST occurrence of the key. To delete all duplicates, call this function multiple times until the key no longer exists.

Parameters

hd Map handle.
key Key to delete.

void corm_del_all (uint32_t hd, const void *const key)

Delete all entries with the specified key. For CM_MULTIVALUE maps, removes all duplicate entries. For regular maps, behaves identically to corm_del().

Parameters

hd Map handle.
key Key of entries to delete.

See also

corm_del

corm_get_multi

int corm_del_value (uint32_t hd, const void *const key, const void *const value)

Delete the first entry with this key whose value matches. For CM_MULTIVALUE maps this removes exactly one duplicate entry: the first one, in insertion order, whose value equals value. Values are compared with the value type's own comparator (see corm_cmp_set), so CM_STR matches by content rather than by pointer identity. This is the per-(key, value) delete that corm_del() โ€” which drops the first entry with this key whatever its value โ€” and corm_del_all() โ€” which drops all of them โ€” cannot express.

For maps without CM_MULTIVALUE there is only ever one value per key and it is not separately addressable, so value is ignored and this behaves exactly like corm_del().

Iteration STOPS at the first match: the entry is unlinked from its duplicate chain and freed before returning, so the cursor is never advanced again.

Parameters

hd Map handle.
key Key to look up.
value Value to match against each duplicate.

Returns

1 if an entry was deleted, 0 if none matched.

See also

corm_del, corm_del_all, corm_get_multi, corm_count

void corm_drop (uint32_t hd)

Remove all entries from a map.

Parameters

hd Map handle.

const void * corm_get (uint32_t hd, const void *const key)

Retrieve a value by key. For maps with CM_MULTIVALUE flag, this returns the FIRST matching value only. To retrieve all values for a key, use corm_get_multi() instead.

Parameters

hd Map handle.
key Key to look up.

Returns

Pointer to value or NULL if not found. For CM_MULTIVALUE maps, returns first match. See corm_common for pointer ownership rules.

void corm_get_copy (uint32_t hd, const void *const key, void * dst)

Copy a value into a caller buffer. Copies corm_len(corm_get_vtype(hd), <value>) bytes out of the map into dst, so it works unchanged for fixed-size registered types (corm_reg, CM_U32, CM_PTR, CM_RECORD) and for the measured types (CM_STR), where corm_len() yields strlen + 1. Note that corm_type_len() is NOT a substitute: it returns 0 for every measured type, so a helper that sizes its copy with it copies nothing.

The destination must be at least that large; the size is not returned and no truncation is performed. A caller that wants truncation must keep using corm_get() with strlcpy().

Note

A miss is a programming error, not a runtime condition: the call CBUG()s. Callers that legitimately probe for presence use corm_get() and test for NULL, or corm_count().

Field access on a record-aware map ("id:field" keys) is rejected with CBUG(): corm_get() resolves those to a pointer inside the stored struct, whose length is the field's and not the map value type's. Use corm_get() for field access.

See also

corm_get, corm_get_vtype, corm_len

uint32_t corm_put (uint32_t hd, const void *const key, const void *const value)

Insert or update a pair. Behavior depends on the CM_MULTIVALUE flag:

Parameters

hd Map handle.
key Key (NULL if CM_AINDEX).
value Value to store.

Returns

Internal index for the entry. With CM_AINDEX, this is the generated key ID.

int corm_read (uint32_t hd, const void *const key, void * dst, size_t max_sz)

Non-aborting safe record retrieval with guaranteed zero-initialization. Copies the stored value into dst up to max_sz bytes without aborting. On miss or error, dst is zero-filled up to max_sz (if max_sz > 0). On hit, any trailing slack between stored length and max_sz is zero-filled. Callers never need to pre-zero buffers with memset before calling.

Parameters

hd Map handle.
key Key to look up.
dst Destination buffer.
max_sz Maximum capacity of dst (0 for full measured length).

Returns

0 on success, -1 on miss or invalid argument.

Author

Generated automatically by Doxygen for corm from the source code.