C 工具庫3 固定大小的 obj pool

2022-02-24 05:05:07 字數 4230 閱讀 1937

對大小固定的物件提供的物件池工具,當對記憶體分配的請求大小不固定時,使用其它的通用記憶體池.

fix_obj_pool.h

#ifndef _fix_obj_pool_h

#define _fix_obj_pool_h

struct fix_obj_pool;

/** obj_size:物件大小

* default_size:預設物件池大小

* align4:返回的物件位址是否需要對齊到4位元組

*/extern

struct fix_obj_pool *create_pool(unsigned int obj_size,int default_size,int align4);

extern

void destroy_pool(struct fix_obj_pool **pool);

/** 分配和**

*/extern

void *pool_alloc(struct fix_obj_pool*);

extern

void pool_dealloc(struct fix_obj_pool*,void*);

extern unsigned int alignsize(unsigned int obj_size);

extern unsigned int get_free_size(struct fix_obj_pool *pool);

#endif

fix_obj_pool.c

#include "

fix_obj_pool.h

"#include

#include

#include

static

const

char invalid_index = -1;

static

const unsigned int block_obj_size = 128;

struct block;};

inline void *block_get(struct block *_block,unsigned int index)

inline int block_check(struct block *_block,void *obj)

void block_destroy(struct block **_block)

void block_init(struct block *_block,unsigned int obj_size)

char *link = (char*)block_get(_block,block_obj_size-1);

*link = invalid_index;

_block->m_next_block = 0;

}struct block *block_create(unsigned int obj_size)

void* block_alloc(struct block *_block)

inline unsigned int block_get_obj_index(struct block *_block,void* obj)

void block_dealloc(struct block *_block,void* obj)

else

_block->m_tail = index;

++_block->m_freesize;

}inline unsigned char block_getfreesize(struct block *_block)

inline void block_setnextblock(struct block *_block,struct block *next)

inline struct block *block_getnext(struct block *_block)

inline char *block_getlastaddr(struct block *_block)

struct chunk

;inline struct block *chunk_get(struct chunk *_chunk,unsigned int index)

void chunk_init(struct chunk *_chunk,unsigned int block_count,unsigned int block_size,unsigned int obj_size)

for(i = 0; i < block_count-1; ++i)

block_setnextblock(chunk_get(_chunk,block_count-1),0);

_chunk->m_head = chunk_get(_chunk,0);

_chunk->m_tail = chunk_get(_chunk,block_count-1);

_chunk->m_next = 0;

}struct chunk *chunk_create(unsigned int block_count,unsigned int obj_size)

void chunk_destroy(struct chunk **_chunk)

void* chunk_alloc(struct chunk *_chunk)

--_chunk->m_freesize;

return ret;

}inline int chunk_check(struct chunk *_chunk,void *obj)

void chunk_dealloc(struct chunk *_chunk,void* obj)

else

_chunk->m_tail = chunk_get(_chunk,index);

}++_chunk->m_freesize;

}inline unsigned int chunk_getfreesize(struct chunk *_chunk)

inline char *chunk_getblocksaddr(struct chunk *_chunk)

inline char *chunk_getlastaddr(struct chunk *_chunk)

inline void chunk_setnext(struct chunk *_chunk, struct chunk * next)

inline struct chunk *chunk_getnext(struct chunk *_chunk)

#define default_chunk_count 128 //

當chunk數量大於此值時將採用動態分配

struct fix_obj_pool

;unsigned int alignsize(unsigned int obj_size)

struct fix_obj_pool *create_pool(unsigned int obj_size,int default_size,int align4)

struct fix_obj_pool *pool = malloc(sizeof(*pool));

if(pool)

return pool;

}void destroy_pool(struct fix_obj_pool **pool)

void* pool_alloc(struct fix_obj_pool *pool)

//對所有的chunk按其位址順序排序

unsigned int size = pool->chunk_count;

int i = size-2;

for( ; i >= 0; --i)

else}}

}return ret;

}struct chunk *binsearch(struct fix_obj_pool *pool,void *obj)

if(chunk_check(pool->ptr_chunk[beg],obj) == 1)

return pool->ptr_chunk[beg];

return0;}

void pool_dealloc(struct fix_obj_pool *pool,void* obj)

else

pool->m_head = pool->m_tail = _chunk;

}pool->m_lastdealloc = _chunk;}}

unsigned int get_free_size(struct fix_obj_pool *pool)

return totalsize;

}

Unity3D中C 呼叫iOS的靜態庫 a

c 呼叫其他模組的介面都是通過dllimport的方式來實現的。首先匯入名字空間 using system.runtime.interopservices 宣告需要使用的c介面 dllimport internal dllimport internal private static extern v...

Unity3D中C 呼叫iOS的靜態庫 a

c 端的處理 c 呼叫其他模組的介面都是通過dllimport的方式來實現的。首先匯入名字空間 using system.runtime.interopservices 宣告需要使用的c介面 dllimport internal private static extern void setversi...

資料結構3 C 標準模板庫STL中的寶貝

目錄 資料結構3 c 標準庫 1.stack 2.queue 3.vector 4.list 總結 在很多程式語言中,演算法與資料結構的相關類和函式都與輸入輸出,字串等一同加入了庫,我們可以直接從庫中呼叫。但是在使用之前,我們不但要了解庫的特徵和複雜度,還要將其有效運用至更高的演算法與資料結構之中。...