All pastes #2130974 Raw Edit

Miscellany

public text v1 · immutable
#2130974 ·published 2012-03-21 20:09 UTC
rendered paste body
struct bu_hash_entry *
bu_hash_add_entry(struct bu_hash_tbl *hsh_tbl, unsigned char *key, int key_len, int *new_entry)
{
    struct bu_hash_entry *hsh_entry, *prev;
    unsigned long idx;

    BU_CK_HASH_TBL(hsh_tbl);

    /* do a find for three reasons.
     * does this key already exist in the table?
     * get the hash bin index for this key.
     * find the previous entry to link the new one to.
     */
    hsh_entry = bu_find_hash_entry(hsh_tbl, key, key_len, &prev, &idx);

    if (hsh_entry) {
	/* this key is already in the table, return the entry, with
	 * flag set to 0
	 */
	*new_entry = 0;
	return hsh_entry;
    }

    if (prev) {
	/* already have an entry in this bin, just link to it */
	prev->next = (struct bu_hash_entry *)calloc(1, sizeof(struct bu_hash_entry));
	hsh_entry = prev->next;
    } else {
	/* first entry in this bin */
	hsh_entry = (struct bu_hash_entry *)calloc(1, sizeof(struct bu_hash_entry));
	hsh_tbl->lists[idx] = hsh_entry;
    }

    /* fill in the structure */
    hsh_entry->next = NULL;
    hsh_entry->value = NULL;
    hsh_entry->key_len = key_len;
    hsh_entry->magic = BU_HASH_ENTRY_MAGIC;

    /* make a copy of the key */
    hsh_entry->key = (unsigned char *)malloc((size_t)key_len);
    memcpy(hsh_entry->key, key, (size_t)key_len);

    /* set "new" flag, increment count of entries, and return new
     * entry.
     */
    *new_entry = 1;
    hsh_tbl->num_entries++;
    return hsh_entry;
}