size_t ht_strdup(void *base, const char *s) { size_t va_s = lmc_valloc(base, strlen(s) + 1); if (!va_s) { return 0; } memcpy(base + va_s, s, strlen(s) + 1); return va_s; }
va_ht_hash_t ht_hash_create(void *base, lmc_error_t *e) { va_ht_hash_t va_ht = lmc_valloc(base, sizeof(ht_hash_t)); if (!va_ht) { lmc_handle_error_with_err_string("ht_hash_create", "memory pool full", e); return 0; } memset(base + va_ht, 0, sizeof(ht_hash_t)); return va_ht; }
size_t lmc_test_valloc_fail(const char *file, int line, const char *function, void *base, size_t s) { if (!lmc_showed_status) { lmc_showed_status = 1; printf("lmc_test_valloc enabled\n"); } int r = rand(); if ((r & 255) == 0) { printf("[%s:%d %s] valloc fail: %zd\n", file, line, function, s); return 0; } return lmc_valloc(base, s); }
int ht_set(void *base, va_ht_hash_t va_ht, const char *key, const char *value, lmc_error_t *e) { ht_hash_t *ht = base + va_ht; ht_hash_entry_t *hr = ht_lookup(base, va_ht, key); unsigned v; if (hr->va_key == 0) { va_ht_hash_entry_t va = lmc_valloc(base, sizeof(ht_hash_entry_t)); hr = va ? base + va : 0; if (hr == NULL || (hr->va_key = ht_strdup(base, key)) == 0) { lmc_handle_error_with_err_string("ht_set", "memory pool full", e); return 0; } v = ht_hash_key(key); hr->va_next = ht->va_buckets[v]; ht->va_buckets[v] = va; } else { lmc_free(base, hr->va_value); } if ((hr->va_value = ht_strdup(base, value)) == 0) { lmc_handle_error_with_err_string("ht_set", "memory pool full", e); return 0; } return 1; }