#include <stdio.h>
#include <pjlib.h>
#include <pj/types.h> // for pj_init.
#define THIS_FILE "main.c"
int main(int argc, char** argv)
{
pj_status_t status;
pj_caching_pool ch_pool;
// pjlib库初始化
status = pj_init();
if (status != PJ_SUCCESS)
{
char errmsg[PJ_ERR_MSG_SIZE];
pj_strerror(status, errmsg, sizeof(errmsg));
PJ_LOG(1,(THIS_FILE, "%s: %s [status=%d]", pj_init, errmsg, status));
return -1;
}
// pjlib创建缓冲池工厂
pj_caching_pool_init(&ch_pool, NULL, 1024*1024);
/* 分配内存示例 */
{
pj_pool_t *pool;
pj_pool_factory *factory =&ch_pool.factory;
// 在分配内存前必须创建内存池
pool = pj_pool_create(factory, "pjlib_demo", 4*1024, 4*1024, NULL);
if (pool == NULL)
{
PJ_LOG(1,(THIS_FILE, "Error:pj_pool_create is NULL."));
return PJ_ENOMEM;
}
// 循环申请内存
{
int i;
for (i=0; i<1000; i++)
{
void* p;
p = pj_pool_alloc(pool, (pj_rand()+1) % 512);
}
// 没有释放内存的函数!!!
}
// 释放内存池
pj_pool_release(pool);
}
// pjlib销毁缓冲池工厂
pj_caching_pool_destroy(&ch_pool);
// pjlib库关闭
pj_shutdown();
return 0;
}
PJLIB库基础框架-初始化与缓冲池和内存池的使用
最新推荐文章于 2023-04-12 10:22:52 发布