MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / shard_insert

Function shard_insert

src/foundation/mem_events.c:579–615  ·  view source on GitHub ↗

Caller holds the shard lock. */

Source from the content-addressed store, hash-verified

577
578/* Caller holds the shard lock. */
579static ptr_entry_t *shard_insert(shard_t *sh, uintptr_t block) {
580 if (!sh->entries && !shard_resize(sh, MEMEV_SHARD_INIT_BITS)) {
581 return NULL;
582 }
583 uint32_t cap = 1U << sh->bits;
584 if ((uint64_t)(sh->used + 1) * MEMEV_LOAD_DEN > (uint64_t)cap * MEMEV_LOAD_NUM) {
585 /* Mostly tombstones: rehash in place at the same size; otherwise double. */
586 uint32_t want = ((uint64_t)sh->live * 2 * MEMEV_LOAD_DEN > (uint64_t)cap * MEMEV_LOAD_NUM)
587 ? sh->bits + 1
588 : sh->bits;
589 if (!shard_resize(sh, want)) {
590 return NULL;
591 }
592 cap = 1U << sh->bits;
593 }
594 uint32_t s = slot_of(block, sh->bits);
595 ptr_entry_t *grave = NULL;
596 for (uint32_t probe = 0; probe < cap; probe++) {
597 ptr_entry_t *e = &sh->entries[s];
598 if (e->block == block) {
599 return e; /* address reuse without a free we saw: overwrite */
600 }
601 if (e->block == MEMEV_TOMBSTONE && !grave) {
602 grave = e;
603 } else if (e->block == 0) {
604 ptr_entry_t *dst = grave ? grave : e;
605 if (!grave) {
606 sh->used++;
607 }
608 sh->live++;
609 dst->block = block;
610 return dst;
611 }
612 s = (s + 1) & (cap - 1);
613 }
614 return NULL;
615}
616
617/* Caller holds the shard lock. */
618static ptr_entry_t *shard_find(shard_t *sh, uintptr_t block) {

Callers 2

record_allocFunction · 0.85
cbm_memev_reallocFunction · 0.85

Calls 2

shard_resizeFunction · 0.85
slot_ofFunction · 0.70

Tested by

no test coverage detected