我的github:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。
这些代码大部分以Linux为目标但部分代码是纯C++的,可以在任何平台上使用。
相关专题:共享内存、数据结构与算法_初级代码游戏的博客-CSDN博客
相关文章:19、基于共享内存的内存池_共享内存 对象池-CSDN博客
基础操作:github源码指引:共享内存、数据结构与算法-CSDN博客
基础:github源码指引:源码结构、编译、运行_github编译-CSDN博客
目录
一、运行示例
在shmfc目录下运行run.sh,显示命令表:
----------------------------------------
命令表:(q=exit)
0 管理
1 test_BinaryPool
3 test_CMultiProcessServer
4 test_CMultiProcessServer_view
5 test_CMultiProcessServer_speed
6 test_CMultiProcessServer_direct
7 test_RebuildSet
12 UDP测试客户端
13 UDP测试服务端
14 CStressTesting
15 CTestCSimpleMultiProcess_mutex atomic
16 CTest_hash
17 T_SHM_HASH
88 test_CMyRWMutex
89 test_T_SHM_SET_GROUP
90 test_shm_IActiveObject
91 test_ShmMultiMap
99 test_ParseFromXml
........................................
----------------------------------------
选择1执行基于共享内存的内存池的测试代码:
[08-26 08:21:09][应用][信息][shmBinaryPool.h : 174(Show)][ 0.14]
ShmPool 开始报告T_ARRAY......
pHead=0x7f2cd2dfd000 pData=0x7f2cd2e08448 isPrivate=0 shmid=327681 sizeof(T)=1
GUID = 8AA20E2E-4891-4df1-A87E-E5A23CB8D076
sizeof short= 2 int= 4 long= 8 byteorder= 1
0 = 46152 1 = 43400 2 = 1 3 = 0 4 = 998 5 = 999
6 = 1000
name=ShmPool part=0 capacity=104,size=0(0.00%) bytes=46256
地址映射表: GET_PP_SET=(nil)
分块数 1
0 : shm_id=327681 handle[0,104) 容量=104 连接信息: shm_id=327681 addr=0x7f2cd2e08448
--------------------------------------------
总分配 0,总释放 0,最大入口点数 1024,已用 0
空闲链表数 100
0 : 0B 0 个,h=-1
开始报告共享内存池 ......
起始位置 0 总大小 0
下一个位置 0 剩余字节数 0
[08-26 08:21:09][应用][信息][shmfc_t.cpp : 344(test_BinaryPool)][ 0.14]
[08-26 08:21:09][应用][信息][shmBinaryPool.h : 302(_Allocate)][ 0.14]由于池为空,先申请8字节填充HANDLE为0的位置
[08-26 08:21:09][应用][信息][shmBinaryPool.h : 321(_Allocate)][ 0.14]n=18 freelist=1 newsize=24
[08-26 08:21:09][应用][信息][shmBinaryPool.h : 357(_Allocate)][ 0.14]Allocate 18(24) 8
[08-26 08:21:09][应用][信息][shmfc_t.cpp : 350(test_BinaryPool)][ 0.14]
[08-26 08:21:09][应用][信息][shmfc_t.cpp : 352(test_BinaryPool)][ 0.14]
[08-26 08:21:09][应用][信息][shmBinaryPool.h : 312(_Allocate)][ 0.14]池size 32
[08-26 08:21:09][应用][信息][shmBinaryPool.h : 321(_Allocate)][ 0.14]n=18 freelist=1 newsize=24
[08-26 08:21:09][应用][信息][shmBinaryPool.h : 357(_Allocate)][ 0.14]Allocate 18(24) 32
[08-26 08:21:09][应用][信息][shmfc_t.cpp : 358(test_BinaryPool)][ 0.14]
[08-26 08:21:09][应用][信息][shmfc_t.cpp : 360(test_BinaryPool)][ 0.14]
[08-26 08:21:09][应用][信息][shmBinaryPool.h : 376(_Deallocate)][ 0.14]Deallocate n=18 freelist=1 newsize=24 h=8
[08-26 08:21:09][应用][信息][shmfc_t.cpp : 366(test_BinaryPool)][ 0.14]
共享内存显示 enter=继续显示 num=起始位置 max=设置每次显示的行数 b=break(q=exit ):
输入回车继续显示内容:
[08-26 08:22:01][应用][信息] 用户输入的是:
[08-26 08:22:01][应用][信息][shmBinaryPool.h : 174(Show)][ 0.14]
ShmPool 开始报告T_ARRAY......
pHead=0x7f2cd2dfd000 pData=0x7f2cd2e08448 isPrivate=0 shmid=327681 sizeof(T)=1
GUID = 8AA20E2E-4891-4df1-A87E-E5A23CB8D076
sizeof short= 2 int= 4 long= 8 byteorder= 1
0 = 46152 1 = 43400 2 = 1 3 = 0 4 = 998 5 = 999
6 = 1000
name=ShmPool part=0 capacity=104,size=56(53.85%) bytes=46256
地址映射表: GET_PP_SET=(nil)
分块数 1
0 : shm_id=327681 handle[0,104) 容量=104 连接信息: shm_id=327681 addr=0x7f2cd2e08448
--------------------------------------------
总分配 36,总释放 18,最大入口点数 1024,已用 0
空闲链表数 100
0 : 0B 0 个,h=-1
1 : 24B 1 个,h=8
开始报告共享内存池 ......
起始位置 0 总大小 56
0 : 4E 78 N 55 85 U 4C 76 L 4C 76 L 4E 78 N 55 85 U 4C 76 L 4C 76 L | NULLNULL | 4C4C554E4C4C554E 5497863039888414030
8 : FF -1 FF -1 FF -1 FF -1 FF -1 FF -1 FF -1 FF -1 | ........ | FFFFFFFFFFFFFFFF -1
16 : E3 -29 E3 -29 E3 -29 E3 -29 E3 -29 E3 -29 E3 -29 E3 -29 | ▒▒▒▒▒▒▒▒ | E3E3E3E3E3E3E3E3 -2025524839466146845
24 : E3 -29 E3 -29 E3 -29 E3 -29 E3 -29 E3 -29 E3 -29 E3 -29 | ▒▒▒▒▒▒▒▒ | E3E3E3E3E3E3E3E3 -2025524839466146845
32 : 0A 10 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 000000000000000A 10
40 : 64 100 d 65 101 e 66 102 f 00 0 00 0 00 0 00 0 00 0 | def..... | 0000000000666564 6710628
48 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
下一个位置 56 剩余字节数 0
共享内存显示 enter=继续显示 num=起始位置 max=设置每次显示的行数 b=break(q=exit ):
剩余字节数0表示没有更多数据了,按b返回上一级(这里是退出显示继续执行测试):
下一个位置 512 剩余字节数 1054848
共享内存显示 enter=继续显示 num=起始位置 max=设置每次显示的行数 b=break(q=exit ):
[08-26 08:23:25][应用][信息] 用户输入的是:
[08-26 08:23:25][应用][信息][shmBinaryPool.h : 175(Show)][ 0.27]
起始位置 512 总大小 1055360
512 : 69 105 i 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | i....... | 0000000000000069 105
520 : 6A 106 j 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | j....... | 000000000000006A 106
528 : 6B 107 k 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | k....... | 000000000000006B 107
536 : 45 69 E 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | E....... | 0000000000000045 69
544 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
552 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
560 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
568 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
576 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
584 : 28 40 ( 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | (....... | 0000000000000028 40
592 : 01 1 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000001 1
600 : 10 16 41 65 A E1 -31 D2 -46 2C 44 , 7F 127 00 0 00 0 | .A▒▒,... | 00007F2CD2E14110 139830493266192
608 : 40 64 @ 50 80 P E1 -31 D2 -46 2C 44 , 7F 127 00 0 00 0 | @P▒▒,... | 00007F2CD2E15040 139830493270080
616 : E0 -32 40 64 @ E1 -31 D2 -46 2C 44 , 7F 127 00 0 00 0 | .@▒▒,... | 00007F2CD2E140E0 139830493266144
624 : 01 1 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000001 1
632 : 28 40 ( 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | (....... | 0000000000000028 40
640 : 01 1 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000001 1
648 : B0 -80 40 64 @ E1 -31 D2 -46 2C 44 , 7F 127 00 0 00 0 | .@▒▒,... | 00007F2CD2E140B0 139830493266096
656 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
664 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
672 : 02 2 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000002 2
680 : 28 40 ( 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | (....... | 0000000000000028 40
688 : 01 1 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000001 1
696 : 38 56 8 30 48 0 E1 -31 D2 -46 2C 44 , 7F 127 00 0 00 0 | 80▒▒,... | 00007F2CD2E13038 139830493261880
704 : B0 -80 40 64 @ E1 -31 D2 -46 2C 44 , 7F 127 00 0 00 0 | .@▒▒,... | 00007F2CD2E140B0 139830493266096
712 : 70 112 p 41 65 A E1 -31 D2 -46 2C 44 , 7F 127 00 0 00 0 | pA▒▒,... | 00007F2CD2E14170 139830493266288
720 : 03 3 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000003 3
728 : 28 40 ( 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | (....... | 0000000000000028 40
736 : 01 1 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000001 1
744 : 70 112 p 41 65 A E1 -31 D2 -46 2C 44 , 7F 127 00 0 00 0 | pA▒▒,... | 00007F2CD2E14170 139830493266288
752 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
760 : 00 0 00 0 00 0 00 0 00 0 00 0 00 0 00 0 | ........ | 0000000000000000 0
下一个位置 768 剩余字节数 1054592
共享内存显示 enter=继续显示 num=起始位置 max=设置每次显示的行数 b=break(q=exit ):
b
[08-26 08:23:29][应用][信息] 用户输入的是:b
[08-26 08:23:29][应用][信息][shmfc_t.cpp : 945(main)][ 0.27]命令 1 返回 0
看到“命令 1 返回 0”表示测试程序已经执行完毕。
具体信息要结合代码解读。
二、代码解读
入口点是这里:
测试函数:
int test_BinaryPool(int argc, char** argv)
{
g_isShmPoolTrance = true;
g_isMakeShmPoolData = true;
string str;
long i;
ShmPool& shmpool = *ShmPool::getInstPtr();
ShmPool::HANDLE h, h2;
thelog << "=====================================" << endi;
if (shmpool.IsConnected())
{
if (shmpool.Detach())thelog << "成功断开" << endi;
else thelog << "断开失败" << ende;
}
if (shmpool.DestoryShm())thelog << "成功删除" << endi;
else thelog << "删除失败" << ende;
if (shmpool.Attach(false))
{
thelog << "连接成功,显示" << endi;
shmpool.view();
}
else
{
thelog << "连接失败,重新创建" << endi;
shmpool.DestoryShm();
shmpool.CreateShm();
if (!shmpool.Attach(false))return __LINE__;
thelog << "创建完成,显示" << endi;
shmpool.Report();
thelog << endi;
if (!shmpool.Allocate(10, h))
{
thelog << "Allocate error" << ende;
return __LINE__;
}
thelog << endi;
strcpy(&*h, "abc");
thelog << endi;
if (!shmpool.Allocate(10, h2))
{
thelog << "Allocate error" << ende;
return __LINE__;
}
thelog << endi;
strcpy(&*h2, "def");
thelog << endi;
if (!shmpool.Deallocate(h))
{
thelog << "Deallocate error" << ende;
return __LINE__;
}
thelog << endi;
shmpool.view();
thelog << "-------------------------------" << endi;
if (!shmpool.Allocate(sizeof(shm_vector<long >), h))
{
thelog << "申请数组头失败" << ende;
return __LINE__;
}
new(&*h) shm_vector<long >;
shmpool.AddEntry("v", h);
shmpool.Report();
thelog << "-------------------------------" << endi;
if (!shmpool.Allocate(sizeof(shm_set<long >), h))
{
thelog << "申请set头失败" << ende;
return __LINE__;
}
new(&*h) shm_set<long >;
shmpool.AddEntry("s", h);
thelog << "-------------------------------" << endi;
if (!shmpool.Allocate(sizeof(shm_map<long, long >), h))
{
thelog << "申请map头失败" << ende;
return __LINE__;
}
new(&*h) shm_map<long, long >;
shmpool.AddEntry("m", h);
thelog << "-------------------------------" << endi;
}
h = shmpool.GetEntry("v");
shm_vector<long >& v = *(shm_vector<long >*) & *h;
shm_vector<long >::const_iterator it_v;
h = shmpool.GetEntry("s");
shm_set<long >& s = *(shm_set<long >*) & *h;
shm_set<long >::const_iterator it_s;
h = shmpool.GetEntry("m");
shm_map<long, long >& m = *(shm_map<long, long >*) & *h;
shm_map<long, long >::const_iterator it_m;
thelog << "-------------------------------" << endi;
shmpool.Report();
thelog << "-------------------------------" << endi;
//v.clear();
thelog << "-------------------------------" << endi;
thelog << v.capacity() << endi;
thelog << "-------------------------------" << endi;
thelog << "v.size=" << v.size() << endi;
thelog << "-------------------------------" << endi;
v.reserve(5);
thelog << "-------------------------------" << endi;
shmpool.Report();
thelog << "v.capacity=" << v.capacity() << endi;
thelog << "-------------------------------" << endi;
thelog << "v.size=" << v.size() << endi;
thelog << "-------------------------------" << endi;
v.push_back('a');
thelog << "-------------------------------" << endi;
v.push_back('a');
thelog << "-------------------------------" << endi;
v.push_back(0x0123456789abcdef);
thelog << "-------------------------------" << endi;
thelog << v[0] << endi;
thelog << "-------------------------------" << endi;
thelog << v[1] << endi;
thelog << "-------------------------------" << endi;
thelog << v[2] << endi;
thelog << "-------------------------------" << endi;
thelog << v.capacity() << endi;
thelog << "-------------------------------" << endi;
thelog << "v.size=" << v.size() << endi;
shmpool.Report();
thelog << "-------------------------------" << endi;
v.push_back('a');
v.push_back('b');
v.push_back('c');
v.push_back('d');
v.push_back('e');
v.push_back('f');
v.push_back('g');
v.push_back('h');
v.push_back('i');
v.push_back('j');
v.push_back('k');
v.push_back('E');
thelog << "-------------------------------" << endi;
thelog << v.capacity() << endi;
thelog << "-------------------------------" << endi;
thelog << "v.size=" << v.size() << endi;
thelog << "-------------------------------" << endi;
shmpool.Report();
for (it_v = v.begin(); it_v != v.end(); ++it_v)
{
thelog << *it_v << endi;
}
thelog << "-------------------------------" << endi;
shmpool.Report();
thelog << "-------------------------------" << endi;
for (i = 0; i < 100; ++i)s.insert(i);
for (i = -1; i < 101; ++i)
{
it_s = s.find(i);
if (s.end() != it_s)thelog << "找到 " << *it_s << endi;
else thelog << "没找到 " << i << endi;
}
thelog << "s.size()=" << s.size() << endi;
for (it_s = s.begin(); it_s != s.end(); ++it_s)
{
thelog << *it_s << endi;
}
shmpool.Report();
thelog << "-------------------------------" << endi;
m[0] = 100;
m[1] = 101;
thelog << "m.size()=" << m.size() << endi;
for (it_m = m.begin(); it_m != m.end(); ++it_m)
{
thelog << (*it_m).first << " - " << (*it_m).second << endi;
}
shmpool.Report();
if (false)
{
//这一段在ubuntu下异常,当然shm_string放在私有内存可能也是问题(或许之前在centos下是没问题的)
BINARYPOOL_TRANCE = true;
thelog << "================================" << endi;
shm_string bs;
thelog << "================================" << endi;
shm_string bs2;
thelog << "================================" << endi;
bs.reserve(51);
thelog << "================================" << endi;
bs = 'c';
thelog << "================================" << endi;
bs = "basic";
bs2 = "basic_string";
if (bs < bs2)thelog << "" << endi;
if (bs.find(""))thelog << "" << endi;
if (bs.find_first_of(""))thelog << "" << endi;
thelog << bs.capacity() << endi;
thelog << bs.size() << endi;
thelog << "=====================================" << endi;
}
i = 3;
shmpool.Allocate(i, h);
shmpool.Deallocate(h, i);
i = 1024 * 1024;
shmpool.Allocate(i, h);
shmpool.Deallocate(h, i);
shmpool.Allocate(i, h);
shmpool.Deallocate(h, i);
i = 32;
shmpool.Allocate(i, h);
i = 32;
shmpool.Allocate(i, h);
shmpool.Allocate(i, h2);
shmpool.Deallocate(h, i);
shmpool.Deallocate(h2, i);
shmpool.view();
return 0;
}
具体实现代码则在shmBinaryPool.h。
我用这个确实实现了把一些复杂数据结构放在共享内存的目标,但是考虑到兼容性和共享内存的利用率,并没有实际应用。相比复杂的业务和维护,这点工作量其实并不算什么。
(这里是文档结束)