LinuxDev 板


LINE

这几天对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/libapr-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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP