int main()
{
	insert_node_at_end(7);
    insert_node_at_start(5);
    insert_node_at_start(6);
    insert_node_at_end(4);
    insert_node_at_nth_position(10,5);
    print();
    delete_node_at_start();
    print();
    delete_node_at_end();
    print();
    delete_node_at_nth_poistion(1);
    print();
    insert_node_at_end(10);
    insert_node_at_start(5);
    print();
    reverse_linkedlist();
    print();
	return 0;
}
int main()
{
	insert_node_at_start(4);
	insert_node_at_start(3);
	insert_node_at_end(5);
	insert_node_at_nth_position(6,3);
	print();
	print_reverse();
	delete_node_from_start();
	print();
	delete_node_from_end();
	print();
	delete_node_from_nth_position(3);
	print();
	return 0;


}
Exemplo n.º 3
0
/*****************************************************************************
 * function:

 *         crypto_free_to_slab(void *ptr)
 *
 * @param[in] ptr, pointer to the memory to be freed
 *
 * @description
 *      free a slot of memory back to its slab
 *
 *****************************************************************************/
static void crypto_free_to_slab(void *ptr)
{
    qae_slab_pools_local *tls_ptr =
                    (qae_slab_pools_local *)pthread_getspecific(qae_key);

    qae_slot *slt = (void *)((unsigned char *)ptr - sizeof(qae_slot));
    if (!slt) {
        MEM_ERROR("Error freeing memory - unknown address\n");
        return;
    }

    qae_slab *slb = slt->slab;
    int i = slt->pool_index;

    if (slt->sig != SIG_ALLOC) {
        MEM_ERROR("%s error trying to free slot that hasn't been alloc'd %p\n",
              __func__, slt);
        return;
    }

    free(slt->file);
    slt->sig = SIG_FREE;
    slt->file = NULL;
    slt->line = 0;

    /* insert the slot into the slab */
    slt->next = slb->next_slot;
    slb->next_slot = slt;
    /* decrease the reference count */
    slb->used_slots--;
    /* if the used_slots is 0, this slab is empty, it should be
     * processed properly */
    if(slb->used_slots == 0) {
        /* remove this slab from the slab list */
        switch(slb->list_index) {
            case IN_AVAILABLE_LIST:
                remove_node_from_list(&tls_ptr->available_slab_list[i],slb);
                break;
            case IN_FULL_LIST:
                remove_node_from_list(&tls_ptr->full_slab_list,slb);
                break;
            default:
                break;
        }
        /* free slab or assign it to the head of the empty slab list */
        if(tls_ptr->empty_slab_list[i].slot_size >= MAX_EMPTY_SLAB) {
            crypto_free_slab(slb,(void *)tls_ptr);
            slb = NULL;
        } else {
            insert_node_at_head(&tls_ptr->empty_slab_list[i],slb);
            slb->list_index = IN_EMPTY_LIST;
        }
    } else {
    /* if current slab is in full slab list,
     *  remove it from the full_slab_list list and then
     *  append it at the end of the available list */
        switch(slb->list_index) {
            case IN_FULL_LIST:
                remove_node_from_list(&tls_ptr->full_slab_list,slb);
                insert_node_at_end(&tls_ptr->available_slab_list[i],slb);
                slt->slab->list_index = IN_AVAILABLE_LIST;
                break;
            default:
                break;
        }
    }

}
Exemplo n.º 4
0
/*****************************************************************************
 * function:
 *         crypto_alloc_from_slab(int size, const char *file, int line)
 *
 * @param[in] size, the size of the memory block required
 * @param[in] file, the C source filename of the call site
 * @param[in] line, the line number withing the C source file of the call site
 *
 * @description
 *      allocate a slot of memory from some slab
 *      retval pointer to the allocated block
 *
 *****************************************************************************/
static void *crypto_alloc_from_slab(int size, const char *file, int line)
{
    qae_slab *slb = NULL;
    qae_slot *slt;
    int slot_size;
    void *result = NULL;
    int i;
    qae_slab_pools_local *tls_ptr;

    tls_ptr = (qae_slab_pools_local *)pthread_getspecific(qae_key);

    if(tls_ptr == NULL) {
        crypto_init();
        tls_ptr = (qae_slab_pools_local *)pthread_getspecific(qae_key);
    }

    size += sizeof(qae_slot);
    size += QAE_BYTE_ALIGNMENT;

    slot_size = SLOT_DEFAULT_INIT;

    for (i = 0; i < sizeof(slot_sizes_available) / sizeof(int); i++) {
        if (size < slot_sizes_available[i]) {
            slot_size = slot_sizes_available[i];
            break;
        }
    }

    if (SLOT_DEFAULT_INIT == slot_size) {
        if (size <= MAX_ALLOC) {
            slot_size = MAX_ALLOC;
        } else {
            MEM_ERROR("%s Allocation of %d bytes is too big\n", __func__, size);
            goto exit;
        }
    }

    if(tls_ptr->available_slab_list[i].slot_size > 0) {
        slt = tls_ptr->available_slab_list[i].next->next_slot;
    } else {
        /* no free slots need to allocate new slab */
        slb = crypto_get_empty_slab(slot_size, i, (void *)tls_ptr);
        if (NULL == slb) {
            MEM_ERROR("%s error, create_slab failed - memory allocation error\n",
                  __func__);
            goto exit;
        }
        /* allocate a new slab, add it into the available slab list */
        slt = slb->next_slot;
        slb->list_index = IN_AVAILABLE_LIST;
        insert_node_at_head(&tls_ptr->available_slab_list[i],slb);
    }

    slb = slt->slab;

    if (slt->sig != SIG_FREE) {
        MEM_ERROR("%s error alloc slot that isn't free %p\n", __func__, slt);
        exit(1);
    }

    slt->sig = SIG_ALLOC;
    slt->file = strdup(file);
    slt->line = line;

    /* increase the reference counter */
    slb->used_slots++;
    /* get the available slot from the head of available slab list */
    slb->next_slot = slt->next;
    slt->next = NULL;
    /* if current slab has no slot available, remove the slab from
     * available slab list and add it to the full slab list */
    if(slb->used_slots >= slb->total_slots) {
        remove_node_from_list(&tls_ptr->available_slab_list[i],slb);
        insert_node_at_end(&tls_ptr->full_slab_list,slb);
        slb->list_index = IN_FULL_LIST;
    }

    result = (void *)((unsigned char *)slt + sizeof(qae_slot));

exit:
    return result;
}