作者qrtt1 (愚人)
看板LinuxDev
标题[LIB ] memory pooling (libapr)
时间Wed Nov 15 21:17:51 2006
这几天对libapr稍为好奇了一下:)
大致上试用了一下memory pooling的功能。
libapr - apache portable runtime
是一整组c/cpp用的library
<%
The mission of the Apache Portable Runtime (APR) project is to create and
maintain software libraries that provide a predictable and consistent
interface to underlying platform-specific implementations. The primary goal
is to provide an API to which software developers may code and be assured of
predictable if not identical behaviour regardless of the platform on which
their software is built, relieving them of the need to code special-case
conditions to work around or take advantage of platform-specific deficiencies
or features.
%>
简单的来讲,就是提供一套library让c/cpp在做port到各平台时
所产生的疼痛达到最小的目的
============================================================ 谜样的分隔线 =
而什麽是memory pool
http://en.wikipedia.org/wiki/Memory_pool
简单说,我们虽然可以在需要memory时用malloc/new等方式即时取得。但是在配置记忆
体的代价(程式与系统要资源的沟通时间与转手的时间)仍是不容小觑,所以较有效率的
方法是事件配置一大块memory,在需要的时候直接由这些区块中取用。在没有超过预设
配置大小的前提下,与系统打交道的时间应该只剩预配与交还的时间:)
============================================================ 谜样的分隔线 =
请先准备材料:
a your favorite editor (or ide)
c/cpp compiler
install libapr
============================================================ 谜样的分隔线 =
一点使用前先讲的小[常]识,在用libapr时,不管你要做什麽之前一定要先呼叫:
rect = apr_initialize ();
他有一个传回值,type是apr_status_t
可以用下列判断成功与否
if (rect != APR_SUCCESS)
{
assert (0);
return -1;
}
用完了之後要叫一下:
apr_terminate ();
============================================================ 谜样的分隔线 =
为了使我们compile顺利,请先查一下你的header与lib放在那叫什麽名字
依俺的为例
header file在/usr/include/apr-0目录下
lib在/usr/lib/lib
apr-0.a (依国际惯例-l要填浅蓝的部分lib(.*).a)
为了避免出现极为可怕的#include
所以俺决定偷懒一下,写了一个简单的makefile
qrtt1@ubuntu:~/test/apr$ cat makefile
CFLAG = -I /usr/include/apr-0
LIB = -lapr-0
CC = g++
test:
$(CC) $(CFLAG) $(LIB) test.c
============================================================ 谜样的分隔线 =
#include <apr.h>
#include <apr_general.h>
#include <stdio.h>
#include <assert.h>
#include <apr_pools.h>
apr_status_t rect;
int
main ()
{
rect = apr_initialize ();
if (rect != APR_SUCCESS)
{
assert (0);
return -1;
}
/* create memory pool. */
// 宣告一下pool用的type
// apr的type都是以_t为suffix
// 开头是apr_这是传统的c name space惯例
apr_pool_t *mem;
// 配置记忆体"池", 第二个参数是parent :)
// 因为apr的设计, pool是有阶层性的,所以只要祖先有人被回起来了
// 下面的小人、小小人都会被收起来,这里填NULL是因为没有parent
rect = apr_pool_create (&mem, NULL);
if (rect != APR_SUCCESS)
{
assert (0);
return -1;
}
char *buf;
// 跟pool要记忆体
buf = (char *) apr_palloc (mem, 1024);
// 试看看会不会segment fault
for (int i = 0; i < 1024; i++)
{
buf[i] = i + 3;
printf ("%d ", (int) buf[i]);
}
// 用完了,丢弃他、催毁他 :)
apr_pool_destroy (mem);
// 结束时要跟apr讲一声 拜
apr_terminate ();
return 0;
}
============================================================ 谜样的分隔线 =
btw. 看教学文件讲,pool预设大小是8K,并设pool的设计不是用来大量的存取的
所以您如果有"一秒钟几十万上下"的大事业可能还是用mmap比较好呦
REFERENCE
1. APR project
http://apr.apache.org/
2. APR api doc
:: Memory Pool Functions
http://apr.apache.org/docs/apr/group__apr__pools.html
:: MMAP (Memory Map) Routines
http://apr.apache.org/docs/apr/group__apr__mmap.html
3. tutorials
:: Programming with Memory Pools (for svn hacker)
http://svnbook.red-bean.com/en/1.1/ch08s05.html
:: libapr tutorial - memory mapping
http://dev.ariel-networks.com/apr/apr-tutorial/html/apr-tutorial-11.html
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 210.59.94.3
1F:推 dozer:什麽样的应用会需要这种东西呀 11/16 11:52
2F:推 andytzeng:个人认为,需要频繁的 memory dynamic 动作时非常有用 12/27 02:04