.TH "corm_common" 3 "corm" \" -*- nroff -*- .ad l .nh .SH NAME corm_common \- Corm get, put, del and drop .PP \- Core key/value operations\&. .SH SYNOPSIS .br .PP .SS "Functions" .in +1c .ti -1c .RI "const void * \fBcorm_get\fP (uint32_t hd, const void *const key)" .br .RI "Retrieve a value by key\&. " .ti -1c .RI "void \fBcorm_get_copy\fP (uint32_t hd, const void *const key, void *dst)" .br .RI "Copy a value into a caller buffer\&. " .ti -1c .RI "int \fBcorm_read\fP (uint32_t hd, const void *const key, void *dst, size_t max_sz)" .br .RI "Non-aborting safe record retrieval with guaranteed zero-initialization\&. " .ti -1c .RI "uint32_t \fBcorm_put\fP (uint32_t hd, const void *const key, const void *const value)" .br .RI "Insert or update a pair\&. " .ti -1c .RI "void \fBcorm_del\fP (uint32_t hd, const void *const key)" .br .RI "Delete an entry by key\&. " .ti -1c .RI "void \fBcorm_del_all\fP (uint32_t hd, const void *const key)" .br .RI "Delete all entries with the specified key\&. " .ti -1c .RI "int \fBcorm_del_value\fP (uint32_t hd, const void *const key, const void *const value)" .br .RI "Delete the first entry with this key whose value matches\&. " .ti -1c .RI "void \fBcorm_drop\fP (uint32_t hd)" .br .RI "Remove all entries from a map\&. " .in -1c .SH "Detailed Description" .PP Core key/value operations\&. .PP \fBWarning\fP .RS 4 Pointer Ownership and Lifetime: All pointers returned by corm functions (corm_get, corm_next) are owned by the map\&. They remain valid until: .IP "\(bu" 2 The entry is DELETED (corm_del) .IP "\(bu" 2 The map is CLOSED (corm_close) .IP "\(bu" 2 The entry is REPLACED with a LARGER value (corm_put) .IP "\(bu" 2 The key is CHANGED (corm_put with different key at same hash) .PP .RE .PP 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\&. .PP Do NOT: .IP "\(bu" 2 Free returned pointers .IP "\(bu" 2 Use pointers after the entry is deleted .IP "\(bu" 2 Store pointers long-term without copying the data .PP .PP SAFE patterns after allocation reuse improvement: .PP .nf // 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 .fi .PP .PP UNSAFE pattern (pointer invalidated by larger value): .PP .nf const char *str = corm_get(hd, key); // str = "short" corm_put(hd, key, "much longer string"); // str is now INVALID \- new allocation was needed .fi .PP .PP \fBNote\fP .RS 4 For CM_PTR type values, the returned pointer points to the stored pointer bytes, not the pointer itself\&. .RE .PP \fBSee also\fP .RS 4 \fBCorm open, close and save\fP .PP \fBcorm_assoc\fP .PP \fBCorm iteration\fP .PP \fBCorm type customization\fP .RE .PP .SH "Function Documentation" .PP .SS "void corm_del (uint32_t hd, const void *const key)" .PP 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\&. .PP \fBParameters\fP .RS 4 \fIhd\fP Map handle\&. .br \fIkey\fP Key to delete\&. .RE .PP .SS "void corm_del_all (uint32_t hd, const void *const key)" .PP Delete all entries with the specified key\&. For CM_MULTIVALUE maps, removes all duplicate entries\&. For regular maps, behaves identically to \fBcorm_del()\fP\&. .PP \fBParameters\fP .RS 4 \fIhd\fP Map handle\&. .br \fIkey\fP Key of entries to delete\&. .RE .PP \fBSee also\fP .RS 4 \fBcorm_del\fP .PP \fBcorm_get_multi\fP .RE .PP .SS "int corm_del_value (uint32_t hd, const void *const key, const void *const value)" .PP 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 \fRvalue\fP\&. 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 \fBcorm_del()\fP — which drops the first entry with this key whatever its value — and \fBcorm_del_all()\fP — which drops all of them — cannot express\&. .PP For maps without CM_MULTIVALUE there is only ever one value per key and it is not separately addressable, so \fRvalue\fP is ignored and this behaves exactly like \fBcorm_del()\fP\&. .PP 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\&. .PP \fBParameters\fP .RS 4 \fIhd\fP Map handle\&. .br \fIkey\fP Key to look up\&. .br \fIvalue\fP Value to match against each duplicate\&. .RE .PP \fBReturns\fP .RS 4 1 if an entry was deleted, 0 if none matched\&. .RE .PP \fBSee also\fP .RS 4 \fBcorm_del\fP, \fBcorm_del_all\fP, \fBcorm_get_multi\fP, \fBcorm_count\fP .RE .PP .SS "void corm_drop (uint32_t hd)" .PP Remove all entries from a map\&. .PP \fBParameters\fP .RS 4 \fIhd\fP Map handle\&. .RE .PP .SS "const void * corm_get (uint32_t hd, const void *const key)" .PP 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 \fBcorm_get_multi()\fP instead\&. .PP \fBParameters\fP .RS 4 \fIhd\fP Map handle\&. .br \fIkey\fP Key to look up\&. .RE .PP \fBReturns\fP .RS 4 Pointer to value or NULL if not found\&. For CM_MULTIVALUE maps, returns first match\&. See corm_common for pointer ownership rules\&. .RE .PP .SS "void corm_get_copy (uint32_t hd, const void *const key, void * dst)" .PP Copy a value into a caller buffer\&. Copies corm_len(corm_get_vtype(hd), ) bytes out of the map into \fRdst\fP, 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 \fBcorm_len()\fP yields strlen + 1\&. Note that \fBcorm_type_len()\fP is NOT a substitute: it returns 0 for every measured type, so a helper that sizes its copy with it copies nothing\&. .PP 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 \fBcorm_get()\fP with strlcpy()\&. .PP \fBNote\fP .RS 4 A miss is a programming error, not a runtime condition: the call CBUG()s\&. Callers that legitimately probe for presence use \fBcorm_get()\fP and test for NULL, or \fBcorm_count()\fP\&. .PP Field access on a record-aware map ("id:field" keys) is rejected with CBUG(): \fBcorm_get()\fP resolves those to a pointer inside the stored struct, whose length is the field's and not the map value type's\&. Use \fBcorm_get()\fP for field access\&. .RE .PP \fBSee also\fP .RS 4 \fBcorm_get\fP, \fBcorm_get_vtype\fP, \fBcorm_len\fP .RE .PP .SS "uint32_t corm_put (uint32_t hd, const void *const key, const void *const value)" .PP Insert or update a pair\&. Behavior depends on the CM_MULTIVALUE flag: .IP "\(bu" 2 Without CM_MULTIVALUE: Replaces existing value if key exists .IP "\(bu" 2 With CM_MULTIVALUE: Always adds a new entry (duplicates allowed) .PP .PP \fBParameters\fP .RS 4 \fIhd\fP Map handle\&. .br \fIkey\fP Key (NULL if CM_AINDEX)\&. .br \fIvalue\fP Value to store\&. .RE .PP \fBReturns\fP .RS 4 Internal index for the entry\&. With CM_AINDEX, this is the generated key ID\&. .RE .PP .SS "int corm_read (uint32_t hd, const void *const key, void * dst, size_t max_sz)" .PP 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\&. .PP \fBParameters\fP .RS 4 \fIhd\fP Map handle\&. .br \fIkey\fP Key to look up\&. .br \fIdst\fP Destination buffer\&. .br \fImax_sz\fP Maximum capacity of dst (0 for full measured length)\&. .RE .PP \fBReturns\fP .RS 4 0 on success, -1 on miss or invalid argument\&. .RE .PP .SH "Author" .PP Generated automatically by Doxygen for corm from the source code\&.