- typedef struct yumei_mem_buf_s yumei_mem_buf_t;
- typedef struct yumei_mem_pool_s yumei_mem_pool_t;
- struct yumei_mem_buf_s
- {
- int size;
- char *pos;
- char *data;
- yumei_mem_pool_t *pool;
- };
- struct yumei_mem_pool_s
- {
- int size;
- char *data;
- char *last;
- yumei_mem_pool_t *next;
- yumei_mem_pool_t *current;
- };
- yumei_mem_pool_t* yumei_mem_pool_create( int block_size, int block_num );
- int yumei_mem_pool_free( yumei_mem_pool_t *pool );
- yumei_mem_buf_t* yumei_mem_malloc( yumei_mem_pool_t *pool, int size );
- int yumei_mem_buf_free( yumei_mem_buf_t *buf );
在每个连接开始的时候,创建连接唯一的内存池,存放IO数据,当要创建新业务时,创建业务内存池,业务处理完毕时释放内存池:
- typedef struct yumei_busi_s yumei_busi_t;
- struct yumei_busi_s
- {
- yumei_mem_pool_t *pool;
- ...
- ...
- }
- #define yumei_BUSI_MEM_BLOCL_SIZE 512
- #define yumei_BUSI_MEM_BLOCK_NUM 32
- yumei_busi_t* yumei_busi_create()
- {
- yumei_busi_t* busi;
- yumei_pool_t* pool;
- yumei_mem_buf_t* buf;
- int size;
- pool = yumei_mem_pool_create( yumei_BUSI_MEM_BLOCL_SIZE, yumei_BUSI_MEM_BLOCK_NUM );
- if( !pool ){
- return 0;
- }
- size = sizeof( yumei_busi_t );
- buf = yumei_mem_buf_malloc( pool, size );
- if( !buf ){
- yumei_mem_pool_free( pool );
- return 0;
- }
- busi = buf->data;
- return busi;
- }
- #define YUMEI_BUSI_ERROR -1
- #define YUMEI_BUSI_OK 0
- int yumei_busi_free( yumei_busi_t* busi )
- {
- if( !busi ){
- return YUMEI_BUSI_ERROR;
- }
- yumei_mem_pool_free( busi->pool );
- return YUMEI_BUSI_OK;
- }
有些时候业务比较简单,一个连接仅对应一个业务或多个业务不是并行执行,这样的情况下,就不再需要业务内存池了,可以直接用连接内存池:
- yumei_busi_t* yumei_busi_create( yumei_conn_t* conn )
- {
- yumei_busi_t* busi;
- yumei_pool_t* pool;
- yumei_mem_buf_t* buf;
- int size;
- pool = conn->pool;
- if( !pool ){
- retur 0;
- }
- size = sizeof( yumei_busi_t );
- buf = yumei_mem_buf_malloc( pool, size );
- if( !buf ){
- yumei_mem_pool_free( pool );
- return 0;
- }
- busi = buf->data;
- return busi;
- }
- #define YUMEI_CONN_ERROR -1
- #define YUMEI_CONN_OK 0
- int yumei_conn_close( yumei_conn_t* conn )
- {
- if( !conn ){
- return YUMEI_CONN_ERROR;
- }
- yumei_mem_pool_free( conn->pool );
- return YUMEI_CONN_OK;
- }
- typedef struct yumei_mem_large_s yumei_mem_large_t;
- struct yumei_mem_large_s
- {
- char *data;
- int size;
- yumei_mem_large_t *next;
- }
- struct yumei_mem_pool_s
- {
- int size;
- char *data;
- char *last;
- yumei_mem_pool_t *next;
- yumei_mem_pool_t *current;
- yumei_mem_large_t *large;
- };
601



被折叠的 条评论
为什么被折叠?



