Exemplo n.º 1
0
static void
dwarf_tdestroy_inner(struct ts_entry*p,
    void (*free_node)(void *nodep),
    int depth)
{
    if(p->llink) {
        dwarf_tdestroy_inner(p->llink,free_node,depth+1);
        p->llink = 0;
    }
    if(p->rlink) {
        dwarf_tdestroy_inner(p->rlink,free_node,depth+1);
        p->rlink = 0;
    }
    free_node((void *)p->keyptr);
    free(p);
}
Exemplo n.º 2
0
static void
dwarf_tdestroy_inner(struct ts_entry*p,
    void (*free_node)(void *nodep),
    int depth)
{
    if(p->llink) {
        dwarf_tdestroy_inner(p->llink,free_node,depth+1);
        p->llink = 0;
    }
    if(p->rlink) {
        dwarf_tdestroy_inner(p->rlink,free_node,depth+1);
        p->rlink = 0;
    }
    /* Discards const.  Required by the interface definition. */
    free_node((void *)p->keyptr);
    free(p);
}
Exemplo n.º 3
0
/*  Walk the tree, freeing all space in the tree
    and calling the user's callback function on each node.

    It is up to the caller to zero out anything pointing to
    head (ie, that has the value rootp holds) after this
    returns.
*/
void
dwarf_tdestroy(void *rootp, void (*free_node)(void *nodep))
{
    struct hs_base *head = (struct hs_base *)rootp;
    struct ts_entry *root = 0;
    if(!head) {
        return;
    }
    root = head->hashtab_;
    dwarf_tdestroy_inner(head,free_node,0);
    free(root);
    free(head);
}
Exemplo n.º 4
0
/*  Walk the tree, freeing all space in the tree
    and calling the user's callback function on each node.

    It is up to the caller to zero out anything pointing to
    head (ie, that has the value rootp holds) after this
    returns.
*/
void
dwarf_tdestroy(void *rootp, void (*free_node)(void *nodep))
{
    struct ts_entry *head = (struct ts_entry *)rootp;
    struct ts_entry *root = 0;
    if(!head) {
        return;
    }
    root = head->rlink;
    if(root) {
        dwarf_tdestroy_inner(root,free_node,0);
    }
    free(head);
}
Exemplo n.º 5
0
static void
resize_table(struct hs_base *head,
             int (*compar)(const void *, const void *))
{
    struct hs_base newhead;
    unsigned new_entry_index = 0;
    unsigned long prime_to_use = 0;

    /* Copy the values we have. */
    newhead = *head;
    /* But drop the hashtab_ from new. calloc below. */
    newhead.hashtab_ = 0;
    newhead.record_count_ = 0;
    new_entry_index = head->tablesize_entry_index_ +1;
    prime_to_use = primes[new_entry_index];
    if(prime_to_use == 0) {
        /*  Oops, too large. Leave table size as is, though
            it will get slow as it overfills. */
        return;
    }
    newhead.tablesize_ = prime_to_use;
    newhead.allowed_fill_ = calculate_allowed_fill(allowed_fill_percent,
                            prime_to_use);
    if( newhead.allowed_fill_< (newhead.tablesize_/2)) {
        /* Oops. We are in trouble.  */
        return;
    }
    newhead.tablesize_entry_index_ = new_entry_index;
    newhead.hashtab_ = calloc(sizeof(struct ts_entry),newhead.tablesize_);
    if(!newhead.hashtab_) {
        /*  Oops, too large. Leave table size as is, though
            things will get slow as it overfills. */
        free(newhead.hashtab_);
        return;
    }
    {
        /*  Insert all the records from the old table into
            the new table. */
        int fillnewfail = 0;
        unsigned long ix = 0;
        unsigned long tsize = head->tablesize_;
        struct ts_entry *p = &head->hashtab_[0];
        for(  ; ix < tsize; ix++,p++) {
            int inserted = 0;
            struct ts_entry*n = 0;
            if(fillnewfail) {
                break;
            }
            if(p->keyptr) {
                tsearch_inner(p->keyptr,
                              &newhead,compar,
                              want_insert,
                              &inserted,
                              0);
                if(!inserted) {
                    fillnewfail = 1;
                    break;
                }
            }
            for(n = p->next; n ; n = n->next) {
                inserted = 0;
                tsearch_inner(n->keyptr,
                              &newhead,compar,
                              want_insert,
                              &inserted,
                              0);
                if(!inserted) {
                    fillnewfail = 1;
                    break;
                }
            }
        }
        if(fillnewfail) {
            free(newhead.hashtab_);
            return;
        }
    }
    /* Now get rid of the chain entries of the old table. */
    dwarf_tdestroy_inner(head,0,0);
    /* Now get rid of the old table itself. */
    free(head->hashtab_);
    head->hashtab_ = 0;
    *head = newhead;
    return;
}