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; }
void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size) { size_t len = st_mult(count, size); void *r = mem_pool_alloc(mem_pool, len); memset(r, 0, len); return r; }
reshandle_t rs_texture_queueload(const char* tex_filepath, uint first_mipidx, int srgb, reshandle_t override_hdl) { /* if we have an override handle, check in existing queue and see if it's already exists */ if (override_hdl != INVALID_HANDLE && rs_loadqueue_search(override_hdl) != NULL) return override_hdl; reshandle_t hdl = rs_add_resource(tex_filepath, g_rs.blank_tex, override_hdl, rs_texture_unload); if (hdl == INVALID_HANDLE) return hdl; struct rs_load_data* ldata = (struct rs_load_data*)mem_pool_alloc(&g_rs.load_data_pool); ASSERT(ldata); memset(ldata, 0x00, sizeof(struct rs_load_data)); str_safecpy(ldata->filepath, sizeof(ldata->filepath), tex_filepath); ldata->hdl = hdl; ldata->type = RS_RESOURCE_TEXTURE; ldata->params.tex.first_mipidx = first_mipidx; ldata->params.tex.srgb = srgb; ldata->reload = (override_hdl != INVALID_HANDLE); /* push to load list */ list_addlast(&g_rs.load_list, &ldata->lnode, ldata); return hdl; }
/** * \brief Allocate a buffer structure * \return A new buffer structure, or NULL if the buffer pool is * exhausted. */ struct buffer *buffer_alloc(void) { struct buffer *buf; buf = mem_pool_alloc(&buffer_pool); buf->dma_desc = NULL; return buf; }
int heap_init (int size, struct heap_h *head, int fd) { head->size = ++size; head->cnt = 0; if ((head->p = fd?mem_pool_alloc (sizeof (struct heap_t *) * size, fd): calloc (size, sizeof (struct heap_t *))) == NULL) return -1; return 0; }
static const char *inet62str(const uint8_t *addr, size_t len, struct mem_pool *pool) { sanity(len == 16, "Inet6 address of size %zu\n", len); struct in6_addr addr_str; sanity(sizeof addr_str == 16, "Wrong size of struct in6_addr (%zu)\n", sizeof addr_str); memcpy(&addr_str.s6_addr, addr, len); const char *result = inet_ntop(AF_INET6, &addr_str, mem_pool_alloc(pool, INET6_ADDRSTRLEN), INET6_ADDRSTRLEN); // The mem_pool_hex is misuse of the memory pool, but it happens only when we are about to crash the plugin sanity(result, "Couldn't convert address %s to string\n", mem_pool_hex(pool, addr, len)); return result; }
void *mem_pool_calloc(mem_pool_t *mem_pool, int size) { void *addr = NULL; addr = mem_pool_alloc(mem_pool, size); if(NULL == addr){ return NULL; } bzero(addr, size); return addr; }
void hash_conflictset (char *key, void *content_cap, unsigned int cnt, struct http_request_t *t, int fd) { struct http_attrihash *p = t->list[cnt]; p = p->next; while (p != NULL) p = p->next; p = (struct http_attrihash *)mem_pool_alloc (sizeof (struct http_attrihash), fd); p->content = content_cap; p->attri = key; }
const char *str_pool_add(str_pool_t *self, const char *text) { /* - - - - - - - - - - - - - - - - - - - * * Make a word aligned & zero padded to * multiples of word version of the * string. This way we can use faster * version of the hash function. * - - - - - - - - - - - - - - - - - - - */ size_t len = strlen(text); size_t size = (len + 3) & ~3; void *temp = strncpy(alloca(size), text, size); unsigned hash = jhash2(temp, size>>2, 0); /* - - - - - - - - - - - - - - - - - - - * * locate/create a pooled string matching * the parameter * - - - - - - - - - - - - - - - - - - - */ unsigned h = hash & (self->hmax-1); pooled_str_t *p = self->htab[h]; for( ;; p = p->next ) { if( p == 0 ) { /* - - - - - - - - - - - - - - - - - - - * * no match -> create a new entry * - - - - - - - - - - - - - - - - - - - */ p = mem_pool_alloc(&self->pool, sizeof *p + len); p->refs = 0; p->next = self->htab[h]; p->hash = hash; memcpy(p->text, text, len+1); self->htab[h] = p; break; } if( p->hash == hash && !strcmp(text, p->text) ) { /* - - - - - - - - - - - - - - - - - - - * * found match, use it * - - - - - - - - - - - - - - - - - - - */ break; } } p->refs += 1; return p->text; }
xml_raw_item_t* xml_raw_item_create(mem_pool_t* pool,int type, const char* data, size_t size){ xml_raw_item_t* item; if((type==item_type_str && data==0) || !pool){ return 0; } item = (xml_raw_item_t*)mem_pool_alloc(pool,sizeof(xml_raw_item_t)); if(!item){ return 0; } item->type = type; item->next = 0; item->size = size; if(type == item_type_str){ // size can be zero, so item->data = NULL item->data = mem_pool_alloc(pool,size); if(item->data){ memcpy(item->data,data,size); } } return item; }
/* * Allocate an fsentry structure on the heap. */ static struct fsentry *fsentry_alloc(struct fscache *cache, struct fsentry *list, const char *name, size_t len) { /* overallocate fsentry and copy the name to the end */ struct fsentry *fse = mem_pool_alloc(cache->mem_pool, sizeof(struct fsentry) + len + 1); char *nm = ((char*) fse) + sizeof(struct fsentry); memcpy(nm, name, len); nm[len] = 0; /* init the rest of the structure */ fsentry_init(fse, list, nm, len); fse->next = NULL; fse->refcnt = 1; return fse; }
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; }
struct http_request_t *http_parser (char *h, int fd) { char *toklist, *capture, *content_cap, *compat; int hash_listcnt; struct http_request_t *t = (struct http_request_t *)mem_pool_alloc (sizeof (struct http_request_t), fd); size_t size = strlen (h); char parse[size]; /*Protect original data*/ strncpy (parse, h, size); /*Test the method string*/ t->method = strtok_r (h, " ", &toklist); if (method_test (t.method) < 0) return NULL; /*Get url*/ t->url = strtok_r (NULL, " ", &toklist); if (toklist == NULL) return NULL; /*Get http version*/ t->protocal = strtok_r (NULL, "\r\n", &toklist); if (protocal_test (t.protocal) < 0) return NULL; /*Parse the rest string*/ while (*(toklist+1) != '\n') { capture = strtok_r (NULL, ": ", &toklist); content_cap = strtok_r (NULL, "\n", &toklist); /*Clear invaild chars*/ while (*content_cap == ' ') content_cap++; lida_breakline (content_cap); hash_set (capture, (void *)content_cap, t, fd, lida_hashkey); if (!*(toklist+1)) return NULL; } toklist+=2; /*Here is the request body*/ t->offset = strlen (toklist); return (t); }
static int add_task_to_thread(thread_t *t, task_func_t func, void *task_param) { task_t *task = (task_t *)mem_pool_alloc(t->mem_pool); if (!task) return 1; task->func = func; task->param.real_time_param = task_param; task->param.thread_local_data = t->thread_local_data; RING_ELEM_INIT(task, link); pthread_mutex_lock(&t->task_list_lock); if (RING_EMPTY(&t->tasks, task, link)) pthread_cond_signal(&t->task_list_aval_cond); RING_INSERT_TAIL(&t->tasks, task, task, link); pthread_mutex_unlock(&t->task_list_lock); return 0; }
uint8_t queue_init(queue_t *queue, MemPool *mem_pool) { queue_node_t *head_node = NULL; head_node = (queue_node_t *)mem_pool_alloc(mem_pool, queue_node_size #if MEM_POOL_DEBUG , __FUNCTION__ #endif ); if(!head_node) return QUEUE_ERR; head_node->next = head_node->prev = NULL; queue->length = 0; queue->head = queue->tail = head_node; splock_init(&queue->head_lock); splock_init(&queue->tail_lock); return QUEUE_OK; }
xml_doc_t* xml_doc_create(){ xml_doc_t* doc; mem_pool_t* pool; pool = mem_pool_create(); if(!pool){ return 0; } doc = (xml_doc_t*)mem_pool_alloc(pool,sizeof(xml_doc_t)); if(!doc){ mem_pool_destroy(pool); return 0; } doc->pool = pool; doc->root = 0; doc->_raw_item_head = xml_raw_item_create(doc->pool,item_type_none,0,0); doc->_raw_item_cur = doc->_raw_item_head; return doc; }
queue_t *create_queue(mem_pool_t *pool, size_t size) { size_t tot_size; char *base; queue_t *q; tot_size = sizeof(queue_t) + sizeof(void*) * size; base = (char*)mem_pool_alloc(pool, tot_size); if (base == NULL) { return NULL; } q = (queue_t*)base; base += sizeof(queue_t); q->entry = (void**)base; q->pool = pool; q->size = size; q->head = 0; q->tail = 0; return q; }
reshandle_t rs_animreel_queueload(const char* reel_filepath, reshandle_t override_hdl) { if (override_hdl != INVALID_HANDLE && rs_loadqueue_search(override_hdl) != NULL) return override_hdl; reshandle_t hdl = rs_add_resource(reel_filepath, NULL, override_hdl, rs_animreel_unload); if (hdl == INVALID_HANDLE) return hdl; struct rs_load_data* ldata = (struct rs_load_data*)mem_pool_alloc(&g_rs.load_data_pool); ASSERT(ldata); memset(ldata, 0x00, sizeof(struct rs_load_data)); str_safecpy(ldata->filepath, sizeof(ldata->filepath), reel_filepath); ldata->hdl = hdl; ldata->type = RS_RESOURCE_ANIMREEL; ldata->reload = (override_hdl != INVALID_HANDLE); /* push to load list */ list_addlast(&g_rs.load_list, &ldata->lnode, ldata); return hdl; }
reshandle_t rs_phxprefab_queueload(const char* phx_filepath, reshandle_t override_hdl) { /* if we have an override handle, check in existing queue and see if it's already exists */ if (override_hdl != INVALID_HANDLE && rs_loadqueue_search(override_hdl) != NULL) return override_hdl; reshandle_t hdl = rs_add_resource(phx_filepath, NULL, override_hdl, rs_phxprefab_unload); if (hdl == INVALID_HANDLE) return hdl; struct rs_load_data* ldata = (struct rs_load_data*)mem_pool_alloc(&g_rs.load_data_pool); ASSERT(ldata); memset(ldata, 0x00, sizeof(struct rs_load_data)); str_safecpy(ldata->filepath, sizeof(ldata->filepath), phx_filepath); ldata->hdl = hdl; ldata->type = RS_RESOURCE_PHXPREFAB; ldata->reload = (override_hdl != INVALID_HANDLE); /* push to load list */ list_addlast(&g_rs.load_list, &ldata->lnode, ldata); return hdl; }
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; }