Previous topicNext topic
Help > XEngine_Core > XEngine_ManagePool > MemoryPool >
ManagePool_Memory_Create

函数

ManagePool_Memory_Create

功能

创建一个内存池

语法

XMPOOL ManagePool_Memory_Create(size_t nSize = 4096, size_t nEstimateSize = 102400, size_t nMaxSize = 256)

参数

 参数.一:nSize
  In/Out:In
  类型:无符号整数型
  可空:N
  意思:要初始化内存池的大小
 参数.二:nEstimateSize
  In/Out:In
  类型:无符号整数型
  可空:Y
  意思:预测单个内存最大大小,为0不启用内存预测
 参数.三:nMaxSize
  In/Out:In
  类型:无符号整数型
  可空:Y
  意思:最大允许内存池大小,超过将无法允许申请内存,为0 不启用,单位MB

返回值

类型:内存池句柄
意思:成功返回句柄,失败返回NULL

备注

内存池非线程安全

示例

int MemoryPool_Test()
{
    XMPOOL xmPool;
    MANAGEPOOL_MEMORY_CLEANUP st_Cleanup;

    char* p1, * p2, * p3, * p4;

    xmPool = ManagePool_Memory_Create();
    p1 = (char*)ManagePool_Memory_Alloc(xmPool, 416); /*分配内存,在可分配内*/
    memcpy(p1, "p1", 2);
    printf("%s\n", p1);

    p2 = (char*)ManagePool_Memory_Alloc(xmPool, 200);/*分配内存,在可分配内,但pool中没有足够的内存空间*/
    memcpy(p2, "p2", 2);
    printf("%s\n", p2);

    ManagePool_Memory_Free(xmPool, p1);
    ManagePool_Memory_Free(xmPool, p2);

    p3 = (char*)ManagePool_Memory_Alloc(xmPool, 624);//分配大内存
    memcpy(p3, "p3", 2);
    printf("%s\n", p3);

    p4 = (char*)ManagePool_Memory_Alloc(xmPool, 100);//分配内存
    memcpy(p4, "p4", 2);
    printf("%s\n", p4);

    char* p5 = (char*)ManagePool_Memory_Alloc(xmPool, 4000);//分配内存
    memcpy(p5, "p5", 2);
    printf("%s,%s\n", p5, p4);

    char* p6 = (char*)ManagePool_Memory_Alloc(xmPool, 5000);//分配大内存
    memcpy(p6, "p6", 2);
    printf("%s,%s\n", p6, p4);

    ManagePool_Memory_Free(xmPool, p6);
    char* p7 = (char*)ManagePool_Memory_Alloc(xmPool, 5000);//分配大内存
    memcpy(p7, "p7", 2);
    printf("%s,%s,%s\n", p7, p4, p5);

    st_Cleanup.fpCall_Cleanup = &fun;
    ManagePool_Memory_CleanupAdd(xmPool, &st_Cleanup);/*注册销毁回调函数*/

    ManagePool_Memory_Destory(xmPool);/*pool池注销*/
    printf("\n");
    return 0;
}