apr-pool 内存池测试用例

来源:互联网 发布:php网站破解工具 编辑:程序博客网 时间:2024/05/29 04:07
#include "stdafx.h"#include <new>#include "apr_pools.h"#pragma comment(lib,"libapr.lib")int _tmain(int argc, _TCHAR* argv[]){apr_pool_t *root;apr_pool_initialize();//初始化全局分配子(allocator),并为它设置mutext,以用于多线程环境,初始化全局池,指定全局分配子的owner是全局池apr_pool_create(&root,NULL);//创建根池(默认父池是全局池),根池生命期为进程生存期。分配子默认为全局分配子{apr_pool_t *child;apr_pool_create(&child,root);//创建子池,指定父池为root。分配子默认为父池分配子void *pBuff=apr_palloc(child,sizeof(int));//从子池分配内存int *pInt=new (pBuff)  int(5);//随便举例下基于已分配内存后,面向对象构造函数的调用。printf("pInt=%d\n",*pInt);{typedef struct StudentInfo{char szName[20];bool nSex;};apr_pool_t *grandson;apr_pool_create(&grandson,root);void *pBuff2=apr_palloc(grandson,sizeof(StudentInfo));StudentInfo *pSI=new (pBuff2)  StudentInfo();strcpy(pSI->szName,"zhangsan");pSI->nSex = 1;printf("Name=%s,sex=%d\n",pSI->szName,pSI->nSex);   apr_pool_destroy(grandson);}apr_pool_destroy(child);//释放子池,将内存归还给分配子}apr_pool_destroy(root);//释放父池,apr_pool_terminate();//释放全局池,释放全局allocator,将内存归还给系统return 0;}


0 0