int pa_copy_protocol_format(struct protocol_analyzer_s *pa) { int ret = 0; if(pa->pf_list_head_p == NULL){ dbg_str(PA_ERROR,"pa_copy_protocol_format,pf_list_head_p == NULL"); return -1; } proto_head_list_t *head_list,*head_list_copy; proto_info_list_t *info_list,*info_list_copy; struct list_head *hl_head = pa->pf_list_head_p; struct list_head *pos,*n; if(hl_head == NULL){ dbg_str(PA_ERROR,"a_copy_protocol_format err,hl_head is NULL"); return -1; } head_list = container_of(hl_head,proto_head_list_t,list_head); head_list_copy = (proto_head_list_t *)allocator_mem_alloc(pa->allocator,sizeof(proto_head_list_t)); memcpy(head_list_copy,head_list,sizeof(proto_head_list_t)); head_list_copy->allocator = pa->allocator; head_list_copy->list_count = 0; INIT_LIST_HEAD(&head_list_copy->list_head); list_for_each_safe(pos, n, hl_head) { info_list = container_of(pos,proto_info_list_t,list_head); info_list_copy = (proto_info_list_t *)allocator_mem_alloc(pa->allocator,sizeof(proto_info_list_t)); memcpy(info_list_copy,info_list,sizeof(proto_info_list_t)); pfs_add_list(&info_list_copy->list_head,&head_list_copy->list_head); }
int vector_push_back(vector_t *vector,void *data) { uint32_t data_size = vector->data_size; void *vector_head = vector->vector_head; uint32_t step = vector->step; uint32_t capacity = vector->capacity; uint32_t push_pos = vector->end.vector_pos; sync_lock(&vector->vector_lock,NULL); if(push_pos < capacity){ memcpy(vector_head + (push_pos++)*step,data,data_size); vector_pos_init(&vector->end,push_pos,vector); }else{ dbg_str(DBG_CONTAINER_WARNNING,"realloc mem"); vector->vector_head = allocator_mem_alloc( vector->allocator, 2*capacity * (vector->step)); if(vector->vector_head == NULL){ dbg_str(DBG_CONTAINER_ERROR,"vector_push_back,realloc mem"); } vector->capacity = 2*capacity; memcpy(vector->vector_head,vector_head,capacity*step); memcpy(vector->vector_head + (push_pos++)*step,data,data_size); vector_pos_init(&vector->end,push_pos,vector); allocator_mem_free(vector->allocator,vector_head); } sync_unlock(&vector->vector_lock); dbg_str(DBG_CONTAINER_DETAIL,"vector_push_back,push_pos=%d,capacity=%d",push_pos,vector->capacity); return 0; }
void test_cds_alloc() { allocator_t *allocator; void *p ,*p2,*p3; uint32_t size = 8; /* *alloc_p->slab_max_num = SLAB_ARRAY_MAX_NUM; *alloc_p->data_min_size = 8; *alloc_p->mempool_capacity = MEM_POOL_MAX_SIZE; */ allocator = allocator_creator(ALLOCATOR_TYPE_CDS_MALLOC); allocator_cds_init(allocator, 0, 0, 1024); /* *allocator_cds_init(allocator,0,0,0); */ p = allocator_mem_alloc(allocator,7); dbg_str(DBG_CONTAINER_DETAIL,"alloc addr:%p",p); allocator_mem_free(allocator,p); p2 = allocator_mem_alloc(allocator,8); dbg_str(DBG_CONTAINER_DETAIL,"alloc addr:%p",p2); p3 = allocator_mem_alloc(allocator,200); dbg_str(DBG_CONTAINER_DETAIL,"alloc addr:%p",p3); dbg_str(DBG_CONTAINER_DETAIL,"inquire alloc info"); allocator_mem_info(allocator); allocator_mem_free(allocator,p); allocator_mem_free(allocator,p2); allocator_mem_free(allocator,p3); dbg_str(DBG_CONTAINER_DETAIL,"batch alloc"); int i; for(size = 8,i = 0; i< 20; i++,size += 8){ p = allocator_mem_alloc(allocator,size); } dbg_str(DBG_CONTAINER_DETAIL,"inquire alloc info"); allocator_mem_info(allocator); allocator_destroy(allocator); dbg_str(DBG_CONTAINER_DETAIL,"test cds alloc end"); }
vector_t *vector_create(allocator_t *allocator,uint8_t lock_type) { vector_t *ret = NULL; ret = (vector_t *)allocator_mem_alloc(allocator,sizeof(vector_t)); if(ret == NULL){ dbg_str(DBG_ERROR,"allock err"); } ret->allocator = allocator; ret->lock_type = lock_type; return ret; }
/* *typedef struct shm_queue_s{ * uint32_t fifo_fd; * share_mem_t *shm; *}shm_queue_t; */ shm_queue_t *shm_queue_create(allocator_t *allocator) { shm_queue_t *que; que = (shm_queue_t *)allocator_mem_alloc(allocator,sizeof(shm_queue_t)); if(que == NULL){ dbg_str(DBG_DETAIL,"allocator_mem_alloc"); return que; } return que; }
struct list_head *pfs_create_head_list(allocator_t *allocator) { proto_head_list_t *head_list; head_list = (proto_head_list_t *)allocator_mem_alloc(allocator,sizeof(proto_head_list_t)); if(head_list == NULL) { dbg_str(DBG_ERROR,"create head_list err"); return NULL; } /* *pthread_rwlock_init(&head_list->head_lock,NULL); */ head_list->allocator = allocator; dbg_str(DBG_DETAIL,"allocator=%p",allocator); return &head_list->list_head; }
int vector_init(vector_t *vector,uint32_t data_size,uint32_t capacity) { dbg_str(DBG_CONTAINER_DETAIL,"vector init"); vector->step = data_size; vector->data_size = data_size; vector->capacity = capacity; vector->vector_head = allocator_mem_alloc( vector->allocator, capacity * (vector->step)); if(vector->vector_head == NULL){ dbg_str(DBG_CONTAINER_ERROR,"vector_init"); } dbg_str(DBG_CONTAINER_DETAIL,"vector_head:%p,size=%d", vector->vector_head, capacity * (vector->step)); vector_pos_init(&vector->begin,0,vector); vector_pos_init(&vector->end,0,vector); sync_lock_init(&vector->vector_lock,vector->lock_type); return 0; }
container_t *container_creator(uint32_t container_type,allocator_t *allocator) { container_t *p; p = (container_t *)allocator_mem_alloc(allocator,sizeof(container_t)); if(p == NULL){ goto err; } dbg_str(DBG_CONTAINER_DETAIL,"container_t addr:%p,sizeof_container_struct:%d",p,sizeof(container_t)); p->container_type = container_type; p->allocator = allocator; p->c_ops_p = &container_modules[container_type].c_ops; p->it_ops_p = &container_modules[container_type].it_ops; pthread_rwlock_init(&p->head_lock,NULL); if(p->c_ops_p == NULL || p->it_ops_p == NULL){ allocator_mem_free(p->allocator,p); p = NULL; } err: return p; }