Exemplo n.º 1
0
static int
insertbyvalue(void **tree, VALTYPE addr,int ct)
{
    void *retval = 0;
    /*  tsearch adds an entry if its not present already. */
    /*  Since in this test we do not malloc anything there is no free needed either.
        Instead we just let tsearch store the value in the pointer in the tree.   */
    VALTYPE  newval = addr;
    retval = dwarf_tsearch((void *)newval,tree, value_compare_func  );
    if(retval == 0) {
        printf("FAIL ENOMEM in search  on item %d, value %lu, "
            "error in insertbyvalue\n",
            ct, (unsigned long)newval);
        exit(1);
    } else {
        /*  Since we insert a value there is no  possible distinction
            to be made between newly-inserted and found-in-tree. */
#ifndef FULL_SPEED_RUN
        {
            /* For debugging. */
            VALTYPE mt2 = addr;
            retval = dwarf_tfind((void *)mt2,tree,value_compare_func);
            if(!retval) {
                printf("insertone record %d value 0x%lu failed to add"
                    " as desired, error\n",
                    ct,(unsigned long)mt2);
                return 1;
            }
        }
#endif /*FULL_SPEED_RUN */
    }
    return 0;

}
Exemplo n.º 2
0
static struct Macrocheck_Map_Entry_s *
macrocheck_map_insert(Dwarf_Unsigned offset,
    unsigned add_prim,unsigned add_sec,void **tree1)
{
    void *retval = 0;
    struct Macrocheck_Map_Entry_s *re = 0;
    struct Macrocheck_Map_Entry_s *e;
    e  = macrocheck_map_create_entry(offset,add_prim,add_sec);
    /*  tsearch records e's contents unless e
        is already present . We must not free it till
        destroy time if it got added to tree1.  */
    retval = dwarf_tsearch(e,tree1, macrocheck_map_compare_func);
    if (retval) {
        re = *(struct Macrocheck_Map_Entry_s **)retval;
        if (re != e) {
            /*  We returned an existing record, e not needed.
                Increment refcounts. */
            re->mp_refcount_primary += add_prim;
            re->mp_refcount_secondary += add_sec;
            macrocheck_map_free_func(e);
        } else {
            /* Record e got added to tree1, do not free record e. */
        }
    }
    return NULL;
}
Exemplo n.º 3
0
struct Helpertree_Map_Entry_s *
helpertree_add_entry(Dwarf_Unsigned offset,
    int val,struct Helpertree_Base_s *base)
{
    void *retval = 0;
    struct  Helpertree_Map_Entry_s *re = 0;
    struct  Helpertree_Map_Entry_s *e;
    void **tree1 = 0;

    tree1 = &base->hb_base;
    e  = helpertree_map_create_entry(offset,val);
    /*  tsearch records e's contents unless e
        is already present . We must not free it till
        destroy time if it got added to tree1.  */
    retval = dwarf_tsearch(e,tree1, helpertree_map_compare_func);
    if (retval) {
        re = *(struct Helpertree_Map_Entry_s **)retval;
        if (re != e) {
            /*  We returned an existing record, e not needed.
                Set val. */
            re->hm_val = val;
            helpertree_map_free_func(e);
        } else {
            /* Record e got added to tree1, do not free record e. */
        }
        return retval;
    }
    return NULL;
}
Exemplo n.º 4
0
static int
insertrecsbypointer(int max, void **tree, const enum insertorder order)
{
    int indx = 0;
    for(indx = 0 ; indx < max ; ++indx) {
        int i = 0;
        int k = 0;
        char kbuf[40];
        char dbuf[60];
        struct example_tentry *mt = 0;
        struct example_tentry *retval = 0;

        i = get_record_id(order,indx);
        snprintf(kbuf,sizeof(kbuf),"%u",i);
        strcpy(dbuf," data for ");
        strcat(dbuf,kbuf);
        printf("insertrec %d\n",i);
        /*  Do it twice so we have test the case where
            tsearch adds and one
            where it finds an existing record. */

        for (k = 0; k < 2 ;++k) {
            mt = make_example_tentry(i,dbuf);
            errno = 0;
            /* tsearch adds an entry if its not present already. */
            retval = dwarf_tsearch(mt,tree, mt_compare_func  );
            if(retval == 0) {
                printf("FAIL ENOMEM in search on  %d, give up insertrecsbypointer\n",i);
                exit(1);
            } else {
                struct example_tentry *re = 0;
                re = *(struct example_tentry **)retval;
                if(re != mt) {
                    if(!k) {
                        printf("FAIL found existing an error %u\n",i);
                        mt_free_func(mt);
                        return 1;
                    } else {
                        printf("found existing ok  %u\n",i);
                    }
                    /*  Prevents data leak: mt was
                        already present. */
                    mt_free_func(mt);
                } else {
                    if(!k) {
                        printf("insert new ok %u\n",i);
                    } else {
                        printf("FAIL new found but expected existing %u\n",i);
                    }
                    /* New entry mt was added. */
                }
            }
        }
    }
    return 0;
}
Exemplo n.º 5
0
/*  mt must point to data in static storage for this to make any sense.
    Malloc()ed data or unique static data for this instance mt points at.
    For example, if there was an immobile array and make_example_tentry() somehow
    selected a unique entry.
*/
static int
insertonebypointer(void **tree, unsigned long addr,int ct)
{
    struct example_tentry *mt = 0;
    void *retval = 0;
    mt = make_example_tentry(addr,0);
    /* tsearch adds an entry if its not present already. */
    retval = dwarf_tsearch(mt,tree, mt_compare_func  );
    if(retval == 0) {
        printf("FAIL ENOMEM in search on rec %d adr  0x%lu,"
            " error in insertonebypointer\n",
            ct,(unsigned long)addr);
        exit(1);
    } else {
        struct example_tentry *re = 0;
        re = *(struct example_tentry **)retval;
        if(re != mt) {
            /* Found existing, error. */
            printf("insertonebypointer rec %d addr %lu 0x%lx found record"
                " preexisting, error\n",
                ct,
                (unsigned long)addr,
                (unsigned long)addr);
            mt_free_func(mt);
            return 1;
        } else {
            /* inserted new entry, make sure present. */
#ifndef FULL_SPEED_RUN
            struct example_tentry *mt2 = make_example_tentry(addr,0);
            retval = dwarf_tfind(mt2,tree,mt_compare_func);
            mt_free_func(mt2);
            if(!retval) {
                printf("insertonebypointer record %d addr 0x%lu "
                    "failed to add as desired,"
                    " error\n",
                    ct,(unsigned long)addr);
                return 1;
            }
#endif /* FULL_SPEED_RUN */
        }
    }
    return 0;
}
Exemplo n.º 6
0
char *
_dwarf_get_alloc(Dwarf_Debug dbg,
    Dwarf_Small alloc_type, Dwarf_Unsigned count)
{
    char * alloc_mem = 0;
    Dwarf_Signed basesize = 0;
    Dwarf_Signed size = 0;
    unsigned int type = alloc_type;
    short action = 0;

    if (dbg == NULL) {
        return (NULL);
    }
    if (type >= ALLOC_AREA_INDEX_TABLE_MAX) {
        /* internal error */
        return NULL;
    }
    basesize = alloc_instance_basics[alloc_type].ia_struct_size;
    action = alloc_instance_basics[alloc_type].ia_multiply_count;
    if(action == MULTIPLY_NO) {
        /* Usually count is 1, but do not assume it. */
        size = basesize;
    } else if (action == MULTIPLY_CT) {
        size = basesize * count;
    }  else {
        /* MULTIPLY_SP */
        /* DW_DLA_ADDR.. count * largest size */
        size = count *
            (sizeof(Dwarf_Addr) > sizeof(Dwarf_Off) ?
            sizeof(Dwarf_Addr) : sizeof(Dwarf_Off));
    }
    size += DW_RESERVE;
    alloc_mem = malloc(size);
    if (!alloc_mem) {
        return NULL;
    }
    {
        char * ret_mem = alloc_mem + DW_RESERVE;
        void *key = ret_mem;
        struct reserve_data_s *r = (struct reserve_data_s*)alloc_mem;
        void *result = 0;

        memset(alloc_mem, 0, size);
        /* We are not actually using rd_dbg, we are using rd_type. */
        r->rd_dbg = dbg;
        r->rd_type = alloc_type;
        if (alloc_instance_basics[type].specialconstructor) {
            int res =
                alloc_instance_basics[type].specialconstructor(dbg, ret_mem);
            if (res != DW_DLV_OK) {
                /*  We leak what we allocated in _dwarf_find_memory when
                    constructor fails. */
                return NULL;
            }
        }
        result = dwarf_tsearch((void *)key,
            &dbg->de_alloc_tree,simple_compare_function);
        if(!result) {
            /*  Something badly wrong. Out of memory.
                pretend all is well. */
        }
        return (ret_mem);
    }
}