Example #1
0
void destroy_queue(queue_t *q) {
	mem_pool_t *pool;
	if (q) {
		pool = q->pool;
		mem_pool_free(pool, q);
	}
}
Example #2
0
void *process_message_loop(ifreechat_t *ifc) {
	udp_socket_t *usock;
	mem_pool_t *pool;
	packet_t *pkt;
	protocol_t *proto;
	msg_t *msg;

	usock = (udp_socket_t*)ifc->usock;
	proto = (protocol_t*)ifc->proto;

	for(;;) {
		udp_recv(usock, (void**)&pkt);
		if (ifc->shutdown == 1)
			break;
		msg = (msg_t*)mem_pool_alloc(ifc->pool, sizeof(msg_t));
		if (protocol_parse_packet(proto, pkt, msg) < 0) {
			fprintf(stderr, "parse protocol error...\n");
		} else {
			msg->user_data = (void*)proto;
			if (protocol_handle_msg(proto, msg, (void*)ifc) < 0) {
				fprintf(stderr, "handle_message() occurs errors...\n");
			}
		}
		mem_pool_free(pool, pkt);
	}
	pthread_exit(0);
	return 0;
}
Example #3
0
static void * 
thread_func(void *param)
{
	thread_t *t = (thread_t *)param;
	while (1)
	{
		if (t->state == DONE)
			return NULL;

		task_t *task = NULL;
		pthread_mutex_lock(&t->task_list_lock);
		while (RING_EMPTY(&t->tasks, task, link))
		{
			if (t->state == DONE)
				return NULL;
			pthread_cond_wait(&t->task_list_aval_cond, &t->task_list_lock);
		}

		task = RING_FIRST(&t->tasks);
		RING_REMOVE(task, link);
		pthread_mutex_unlock(&t->task_list_lock);
		if (task)
		{
			(*task->func)(&(task->param));
			mem_pool_free(t->mem_pool, (void *)task);
		}
	}

	return NULL;

}
int main(void)
{
	pid_t pid;
	int i;
	int n = 2;
	char buf[BUFSIZ];
	char *b;
	int size;
	char *p;

#if DEBUG_MEM_POOL == 2
	while (1) {
		b = fgets(buf, BUFSIZ, stdin);
		buf[strlen(b) - 1] = '\0';
		if (strcmp(b, "q") == 0)
			break;
		size = atoi(buf);
		
		p = mem_pool(size);
	}
	mem_pool_free();
#endif


#if DEBUG_MEM_POOL == 1								/* 匿名 内存 映射区, 用于有 父子,兄弟 关系的进程*/
	int *p, *q;

	p = (int *) memory_pool(4084);
	*p = 100;

	for (i = 0; i < n; i++)
		if ((pid = fork()) == 0)
			break;

	if (i == n) { /* parent */
		sleep(1);
		printf("*p = %d\n", *p);
	} else { /* child */
		*p = 200;
	}
	mem_pool_free();
#endif

	return 0;
}
Example #5
0
/******************************************************************//**
Closes the memory system. */
UNIV_INTERN
void
mem_close(void)
/*===========*/
{
	mem_pool_free(mem_comm_pool);
	mem_comm_pool = NULL;
#ifdef UNIV_MEM_DEBUG
	mutex_free(&mem_hash_mutex);
	mem_hash_initialized = FALSE;
#endif /* UNIV_MEM_DEBUG */
}
Example #6
0
int main(int argc, char * argv[]) {
    struct mem_pool * pool = mem_pool_new(4);

    int r1,r2,r3,r4;

    r1 = mem_pool_alloc(pool, 16);
    printb(pool, r1);

    r2 = mem_pool_alloc(pool, 32);
    printb(pool, r2);

    r3 = mem_pool_alloc(pool, 16);
    printb(pool, r3);

    mem_pool_free(pool, r1);
    mem_pool_free(pool, r3);

    r4 = mem_pool_alloc(pool, 32);
    printb(pool, r4);

    mem_pool_destroy(pool);
    return 0;
}
Example #7
0
int
name_tree_delete(name_tree_t *tree,
                    const char *name, del_node del_func)
{
    if (!name || (strlen(name) >= MAX_NAME_LEN))
        return -1;

    node_value_t nv;
    strcpy(nv.fqdn, name);
    if (!name_end_with_dot(name))
        strcat(nv.fqdn, ".");

    rbnode_t *del_node = rbtree_delete(tree->_tree, (void *)&nv);
    if (!del_node)
        return -1;

	if (del_func)
		del_func( tree->lru, ((node_value_t *)del_node->value)->ptr);
    mem_pool_free(tree->name_pool, del_node->value);
    mem_pool_free(tree->node_pool, (void *)del_node);

    tree->count--;
    return 0;
}
Example #8
0
void
handle_dns_query(link_queue_t *queue, void *arg)
{
    query_session_t *session = NULL;
    while((session = dqueue_dequeue(queue)) != NULL)
    {
        uint8_t *header = (uint8_t *)session->raw_data;
        uint16_t flag = *((uint16_t *)header + 1);
        *((uint16_t *)header + 1) = flag | 128;

        int len = socket_write_to(session->client_socket,
                                  (uint8_t *)session->raw_data,
                                  session->data_len,
                                  &session->client_addr);
        mem_pool_free(session->mp, session);
    }
}
Example #9
0
void rs_unload(reshandle_t hdl)
{
    ASSERT(hdl != INVALID_HANDLE);

    if (!g_rs.init)
        return;

    /* make sure handle is binded properly and is not invalid */
    uptr_t idx = GET_INDEX(hdl);
    ASSERT(idx < g_rs.ress.item_cnt);
    struct rs_resource* rs = &((struct rs_resource*)g_rs.ress.buffer)[idx];
    ASSERT(rs->hdl != INVALID_HANDLE);

    /* decr reference count, and see if have to release it */
    rs->ref_cnt --;
    if (rs->ref_cnt == 0)   {
        /* check in pending threaded loads
         * If exists, it means that there is a loading job queued inside thread that
         * needs to be unloaded after it's done (see rs_update) */
        for (uint i = 0, cnt = g_rs.job_params.cnt; i < cnt; i++) {
            if (g_rs.job_params.load_items[i]->hdl == hdl)  {
                struct rs_unload_item* uitem = (struct rs_unload_item*)ALLOC(
                    sizeof(struct rs_unload_item), MID_RES);
                ASSERT(uitem);
                uitem->hdl = hdl;
                list_add(&g_rs.unload_items, &uitem->lnode, uitem);
                return;
            }
        }

        /* also search in queued loads and remove from list immediately */
        struct linked_list* lnode = rs_loadqueue_search(hdl);
        if (lnode != NULL)  {
            list_remove(&g_rs.load_list, lnode);
            mem_pool_free(&g_rs.load_data_pool, lnode->data);
        }

        /* remove from hot-loading list */
        if (BIT_CHECK(g_rs.flags, RS_FLAG_HOTLOADING))
            fio_mon_unreg(rs->filepath);

        rs_remove_fromdb(hdl);
    }
}
Example #10
0
/* Runs in main thread, syncs resources */
void rs_update()
{
    uint job_id = g_rs.load_jobid;
    if (job_id != 0)    {
        if (!tsk_check_finished(job_id))    {
            gfx_delayed_createobjects();
            return;
        }   else    {
            gfx_delayed_finalizeobjects();
        }
    }

    struct rs_load_job_params* params = &g_rs.job_params;
    struct rs_load_job_result* result = &g_rs.job_result;

    /* if we already have a job_id, it means that the job is finished
     * update the database and destroy the task */
    if (job_id != 0)    {
        struct rs_load_job_params* params = (struct rs_load_job_params*)tsk_get_params(job_id);
        struct rs_load_job_result* result = (struct rs_load_job_result*)tsk_get_result(job_id);

        for (uint i = 0; i < params->cnt; i++)    {
            struct rs_load_data* ldata = params->load_items[i];
            reshandle_t hdl = ldata->hdl;

            /* check the handle in unload list and unload immediately */
            int must_unload = rs_search_in_unloads(hdl);

            struct rs_resource* r = rs_resource_get(hdl);
            if (result->ptrs[i] != NULL)    {
                r->ptr = result->ptrs[i];

                if (!must_unload)    {
                    /* register hot-loading */
                    if (BIT_CHECK(g_rs.flags, RS_FLAG_HOTLOADING) && !ldata->reload)
                        rs_register_hotload(ldata);

                    /* apply reload funcs */
                    rs_resource_manualreload(ldata);
                }   else    {
                    rs_remove_fromdb(hdl);
                }
            }   else if (must_unload)   {
                rs_remove_fromdb(hdl);
            }

            mem_pool_free(&g_rs.load_data_pool, params->load_items[i]);
        }

        /* cleanup */
        tsk_destroy(job_id);
    }

    /* fill new params and dispatch the job */
    struct linked_list* lnode = g_rs.load_list;
    uint qcnt = 0;
    while (qcnt < g_rs.load_threads_max && lnode != NULL)    {
        params->load_items[qcnt++] = (struct rs_load_data*)lnode->data;
        list_remove(&g_rs.load_list, lnode);
        lnode = g_rs.load_list;
    }
    params->cnt = qcnt;

    /* dispatch to first thread only (exclusive mode) */
    if (qcnt > 0)   {
        int thread_cnt = (int)g_rs.load_threads_max;
        struct allocator* tmp_alloc = tsk_get_tmpalloc(0);
        int* thread_idxs = (int*)A_ALLOC(tmp_alloc, sizeof(uint)*thread_cnt, MID_RES);
        ASSERT(thread_idxs);
        for (int i = 0; i < thread_cnt; i++)
            thread_idxs[i] = i;
        g_rs.load_jobid = tsk_dispatch_exclusive(rs_threaded_load_fn, thread_idxs, thread_cnt,
            params, result);
        A_FREE(tmp_alloc, thread_idxs);
    }   else    {
        g_rs.load_jobid = 0;
    }
}
Example #11
0
int
name_tree_insert(name_tree_t *tree, const char *name)
{
    if (!name || (strlen(name) >= MAX_NAME_LEN))
        return -1;

    node_value_t *nv = name_tree_exactly_find(tree, name);
    if (nv == NULL)
    {
       nv = (node_value_t *)mem_pool_alloc(tree->name_pool);
       if (!nv)
       {
           return -1;
       }

       rbnode_t *node = (rbnode_t *)mem_pool_alloc(tree->node_pool);
       if (!node) {
           mem_pool_free(tree->name_pool, (void *)nv);
           return -1;
       }

       strcpy(nv->fqdn, name);
       if (!name_end_with_dot(name))
           strcat(nv->fqdn, ".");
       nv->count = 0;
       nv->heap_index = -1;
       node->value = (void *)nv;

       rbnode_t *insert_node = rbtree_insert(tree->_tree, node);
       if (!insert_node) {
           mem_pool_free(tree->node_pool, (void *)node);
           mem_pool_free(tree->name_pool, (void *)nv);
           return -1;
       }
	   lru_node_t *lru_node = lru_list_insert(tree->lru, nv);
	   nv->ptr = lru_node;
       tree->count++;
    }else {
		lru_list_move_to_first(tree->lru, nv->ptr);
	}
    nv->count++;
    if (nv->heap_index != -1)
    {
        heap_adjust_siftdown(tree->heap, nv->heap_index, elem_insert);
    }
    else
    {
        void *new_elem =(void *)nv;
        if (heap_reach_roof(tree->heap, 1000))
        {
            if (heap_min_less_than(tree->heap, new_elem))
            {
               heap_replace_least(tree->heap, new_elem, elem_insert);
            }
        }
        else
        {
            int index = heap_insert(tree->heap, new_elem, elem_insert);
        }
    }
    return 0;
}
/**
 * \brief Free the buffer structure \a buf
 */
void buffer_free(struct buffer *buf)
{
	mem_pool_free(&buffer_pool, buf);
}