Exemple #1
0
void asterisk_acc_unpack_socket(char *buff, perm_node_t **key_list, asterisk_acc_table_t *asterisk_acc_table, opool_t *opool) {
    int i;
    json_object *new_obj, *jvalue;
    opool_item_t *item, *item2;
    perm_node_t *key_element, *value_element, *data_list;

    perm_node_t *temp, *entry;
    perm_node_t *temp2, *entry2;
    perm_node_t *data_list2;

    char id[10], pwd[10];

	new_obj = json_tokener_parse(buff);
	//printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

    for(i = 0; i < json_object_array_length(new_obj); i++)
	{ 
        item = opool_get(opool);
        EXIT_IF_TRUE(item == NULL, "Cannot get from object pool\n");
        key_element = (perm_node_t *)item->data;

        item2 = opool_get(opool);
        EXIT_IF_TRUE(item == NULL, "Cannot get from object pool\n");
        value_element = (perm_node_t *)item2->data;

        json_object *obj = json_object_array_get_idx(new_obj, i);

        json_object_object_get_ex(obj, "account", &jvalue);
        strcpy(id, json_object_get_string(jvalue));

        json_object_object_get_ex(obj, "password", &jvalue);
        strcpy(pwd, json_object_get_string(jvalue));

        ansi_copy_str(key_element->value, id);
        ansi_copy_str(value_element->value, pwd);

        data_list = (perm_node_t *)ht_get_item(asterisk_acc_table, (void *)id);
        if (data_list == NULL) {
            DL_APPEND(*key_list, key_element);
            DL_APPEND(data_list, value_element);
            ht_add_item(asterisk_acc_table, id, data_list);
        }
        else {
            DL_APPEND(data_list, value_element);
        }
    }

}
chunk_t *ch_init(off_t index, off_t len, chpool_t *chpool)
{
    if(!chpool)
    {
        log_write(ERROR, "ch_init(index=%d, len=%d): invaid input",
                  index, len);
        return NULL;
    }
    log_write(DEBUG, "ch_init(index=%d, len=%d): started", index, len);

    chunk_t *new_chunk;
    int error = chp_get_free_chptr(chpool, &new_chunk);
    if(error)
    {
        log_write(ERROR, "ch_init(index=%d, len=%d: can't get free chunk, error=%d",
                  index, len, error);
        return NULL;
    }

    new_chunk->data = mmap(NULL, get_chunk_size(len), chpool->prot,
                           MAP_SHARED, chpool->fd, get_chunk_size(index));
    if(new_chunk->data == MAP_FAILED)
    {
        log_write(ERROR, "ch_init(index=%d, len=%d): mmap failed, errno=%d",
                  index, len, errno);
        printf("errno=%d\n", errno);
        return NULL;
    }

    new_chunk->rc = 1;
    new_chunk->len = len;
    new_chunk->index = index;
    new_chunk->chpool = chpool;

    error = ht_add_item(chpool->ht, (hkey_t)index, (hvalue_t)new_chunk);
    if(error)
    {
        log_write(ERROR, "ch_init(index=%d, len=%d): can't add chunk to hash table, error=%d",
                  index, len, error);
        return NULL;
    }

    log_write(DEBUG, "ch_init(index=%d, len=%d): finished", index, len);

    return new_chunk;
}
chunk_t* chunk_init(off_t index, off_t len, chunk_pool_t* cpool){
    if(!cpool) {
        //log_write(ERROR, "chunk_init: invalid args (cpool));
        return NULL;
    }
    
    chunk_t* chunk;
    if(cpool->free_chunks->size == 0) {
        if(cpool->zero_chunks->size == 0) {
            cpool->loafs = (chunk_t**)realloc(cpool->loafs, (cpool->loafs_count+1)*sizeof(chunk_t*));
            if(!cpool->loafs) {
                //log_write(ERROR, "chunk_init: cannot reallocate memory);
                return NULL;
            }
            
            cpool->loafs[cpool->loafs_count] = (chunk_t*)calloc(DEFAULT_ARRAY_SIZE, sizeof(chunk_t));
            if(!cpool->loafs[cpool->loafs_count]) {
                //log_write(ERROR, "chunk_init: cannot allocate memory);
                return NULL;
            }
            cpool->loafs_count += 1;
            
            int i = 0;
            for(i; i < DEFAULT_ARRAY_SIZE; i++) {
                list_add_last(cpool->free_chunks, (value_t*)&(cpool->loafs[cpool->loafs_count - 1])[i]);
            }
            
            chunk = cpool->free_chunks->head->value;
            list_delete_first(cpool->free_chunks);
        } else {
            chunk = cpool->zero_chunks->head->value;
            if(munmap(chunk->data, chunk->len) == -1) {
                //log_write(ERROR, "chunk_init: cannot munmap memory);
                return errno;
            }
            list_delete_first(cpool->zero_chunks);
        }
    } else {
        chunk = cpool->free_chunks->head->value;
        list_delete_first(cpool->free_chunks);
    }
    
    if(!chunk) {
        //log_write(ERROR, "chunk_init: cannot allocate memory);
        return NULL;
    }

    int size = cpool->pg_size;
    
    chunk->data = mmap(NULL, size*len, cpool->protection, MAP_SHARED,
                       cpool->fd, size*index);
    
    if(chunk->data == MAP_FAILED) {
        return NULL;
    }
    
    chunk->index = index;
    chunk->len = len;
    chunk->ref_counter = 1;
    chunk->cpool = cpool;
    
    int error = ht_add_item(cpool->hash, chunk->index, chunk);
    if(error) {
        //log_write(ERROR, "chunk_init: cannot add chunk to hash table);
        return error;
    }
    
    //log_write(DEBUG, "chunk_init: end of work);
    return chunk;
}