示例#1
0
文件: hash.c 项目: dekai-wang/hashmap
void hash_set(Hash *tab, const char *key, int value)
{
    int h = _hash_func(key);    
    HashNode *node = _hash_get(tab, h, key);
    if (value == HASH_NULL)
    {
        if (node)
        {
            _hash_del(tab, h, node);
            _hash_list_del(tab, node);
            free(node);
            node = NULL;
        }        
    }
    else
    {
        if (node)
        {
            node->value = value;
        }
        else
        {
            node = HASH_CREATE(HashNode); 
            node->key = key;
            node->value = value;
            _hash_add(tab, h, node);
            _hash_list_add(tab, node);
        }
    }
}
示例#2
0
文件: hash.c 项目: dekai-wang/hashmap
Hash * hash_create()
{
    Hash *tab = HASH_CREATE(Hash);
    tab->data = HASH_CREATE_N(HashNode*, HASH_MAXN);
    tab->head = NULL;
    tab->tail = NULL;
    tab->last = NULL;
}
/** 
 *  Class Type create API.
 *
 *  Creates a new class type with the given information like class id,
 *  number of attributes, max and minimum instances for the class.
 *  NOTE: What happens in case of different versions?? need to figure
 *  out.
 *
 *  @param id            class id
 *  @param nAttrs        Number of attributes in class.
 *
 *  @returns 
 *    CORClass_h   (non-null) valid class handle
 *      null(0)    on failure.
 * 
 */
ClRcT
dmClassCreate(ClCorClassTypeT id,
              ClCorClassTypeT inhId)
{
    CORClass_h tmp = 0;
    CORClass_h tmp1 = 0;
    ClRcT ret = CL_OK;

    CL_FUNC_ENTER();

    if(!dmGlobal)
    {
        CL_FUNC_EXIT();  
        return(CL_COR_SET_RC(CL_COR_ERR_NULL_PTR));
    }
    
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ( "ClassCreate (Class:%04x, Inh:%04x)", id, inhId));
    
    /* check if id & inhId is already present 
     */
    HASH_GET(dmGlobal->classTable, id, tmp);
    if(inhId > 0) 
    {
        HASH_GET(dmGlobal->classTable, inhId, tmp1);
    }
    
    if(tmp) 
    {
        CL_DEBUG_PRINT(CL_DEBUG_TRACE, ( "ClassCreate (Class:%04x, Inh:%04x) [Class present]", id, inhId));
        ret = CL_COR_SET_RC(CL_COR_ERR_CLASS_PRESENT);
    } 
    else if(tmp1 == 0 && inhId > 0) 
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "ClassCreate (Class:%04x, Inh:%04x) [Superclass unknown]", id, inhId));
        ret = CL_COR_SET_RC(CL_COR_ERR_CLASS_NOT_PRESENT);
    } 
    else 
    {
        /* Create the new class
         */
        tmp = (CORClass_h) clHeapAllocate(sizeof(CORClass_t));
        if(tmp != 0) 
        {
            /* init stuff here */
            tmp->classId = id;
            tmp->superClassId = inhId; 
            tmp->size = -1;
            tmp->version.releaseCode = CL_RELEASE_VERSION;
            tmp->version.majorVersion = CL_MAJOR_VERSION;
            tmp->version.minorVersion = CL_MINOR_VERSION;
            tmp->flags = 0;
            tmp->moClassInstances = 0;

            if (CL_OK != (ret = HASH_CREATE(&tmp->attrList)))
            { 
                  clHeapFree(tmp);
                  CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "Failed to create hash table for Attributes [rc 0x%x]", ret));
                  CL_FUNC_EXIT();  
                  return (ret);
            }
        
            if (CL_OK != (ret = COR_LLIST_CREATE(&tmp->objFreeList)))
            {
                clHeapFree(tmp);
                CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "Failed to create hash table for free objects [rc 0x%x]", ret));
                CL_FUNC_EXIT();
                return (ret);
            }

            tmp->nAttrs = 0;
            tmp->noOfChildren = 0;
            tmp->recordId = 1;
            tmp->objCount = 0;
            tmp->objBlockSz = COR_OBJ_DEF_BLOCK_SIZE;

            /* init the vector, check the return value
             * and remove class/hashtable
             */
            if (CL_OK != (ret = corVectorInit(&tmp->attrs, 
                          sizeof(CORAttr_t),
                          COR_CLASS_DEF_BLOCK_SIZE)))
            {
                HASH_FREE(tmp->attrList);
                COR_LLIST_FREE(tmp->objFreeList);
                clHeapFree(tmp);
    	        CL_FUNC_EXIT();  
     	        return (ret);
	        }

            /* add the newly created class to the class table */
            if (CL_OK != (ret = HASH_PUT(dmGlobal->classTable, id, tmp)))
	        {
              corVectorRemoveAll(&tmp->attrs);
              HASH_FREE(tmp->attrList);
              COR_LLIST_FREE(tmp->objFreeList);
              clHeapFree(tmp);
       	      CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "Failed to add class in Data Manager [rc 0x%x]", ret));
    	      CL_FUNC_EXIT();  
     	      return (ret);
	        }
            /* declare the inh class as base class */
            if(inhId > 0) 
            {
                tmp1->noOfChildren++;
                COR_CLASS_SETAS_BASE(*tmp1);
            }
        } 
        else 
        {
	        clLogWrite(CL_LOG_HANDLE_APP, CL_LOG_DEBUG, NULL,
				CL_LOG_MESSAGE_0_MEMORY_ALLOCATION_FAILED);
                CL_DEBUG_PRINT(CL_DEBUG_CRITICAL, (CL_COR_ERR_STR_MEM_ALLOC_FAIL)); 
            ret = CL_COR_SET_RC(CL_COR_ERR_NO_MEM);
        }
    }

    CL_FUNC_EXIT();  
    return (ret);
}