2019-07-27 22:44:32 +08:00
|
|
|
|
#include "stdafx.h"
|
2016-04-30 11:49:41 +08:00
|
|
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
2015-11-12 22:24:05 +08:00
|
|
|
|
#include <sys/time.h>
|
2016-04-30 11:49:41 +08:00
|
|
|
|
#endif
|
2015-11-12 22:24:05 +08:00
|
|
|
|
|
2015-11-14 22:15:22 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* dbuf_obj 子类,在 dbuf_pool 上动态分配,由 dbuf_guard 统一进行管理
|
2015-11-14 22:15:22 +08:00
|
|
|
|
*/
|
2015-11-12 22:24:05 +08:00
|
|
|
|
class myobj : public acl::dbuf_obj
|
|
|
|
|
{
|
|
|
|
|
public:
|
2015-11-16 20:00:44 +08:00
|
|
|
|
myobj(acl::dbuf_guard* guard = NULL) : dbuf_obj(guard)
|
2015-11-12 22:24:05 +08:00
|
|
|
|
{
|
|
|
|
|
ptr_ = strdup("hello");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void run()
|
|
|
|
|
{
|
2015-11-14 22:15:22 +08:00
|
|
|
|
printf("----> run->hello world <-----\r\n");
|
2015-11-12 22:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
char* ptr_;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 将析构声明为私人,以强制要求该对象被动态分配,该析构函数将由
|
|
|
|
|
// dbuf_guard 统一调用,以释放本类对象中产生的动态内存(ptr_)
|
2015-11-12 22:24:05 +08:00
|
|
|
|
~myobj()
|
|
|
|
|
{
|
|
|
|
|
free(ptr_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-14 22:15:22 +08:00
|
|
|
|
static void test_dbuf(acl::dbuf_guard& dbuf)
|
2015-11-12 22:24:05 +08:00
|
|
|
|
{
|
2015-11-14 22:15:22 +08:00
|
|
|
|
for (int i = 0; i < 102400; i++)
|
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 动态分配内存
|
2015-11-14 22:15:22 +08:00
|
|
|
|
char* ptr = (char*) dbuf.dbuf_alloc(10);
|
|
|
|
|
(void) ptr;
|
|
|
|
|
}
|
2015-11-12 22:24:05 +08:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 102400; i++)
|
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 动态分配字符串
|
2015-11-14 22:15:22 +08:00
|
|
|
|
char* str = dbuf.dbuf_strdup("hello world");
|
|
|
|
|
if (i < 5)
|
|
|
|
|
printf(">>str->%s\r\n", str);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 动态分配内存
|
2015-11-14 22:15:22 +08:00
|
|
|
|
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(2048);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(1024);
|
|
|
|
|
(void) dbuf.dbuf_alloc(10240);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10000; i++)
|
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 动态分配 dbuf_obj 子类对象,并通过将 dbuf_guard 对象传入
|
|
|
|
|
// dbuf_obj 的构造函数,从而将之由 dbuf_guard 统一管理,
|
2015-11-14 22:15:22 +08:00
|
|
|
|
|
2015-11-16 20:00:44 +08:00
|
|
|
|
myobj* obj = dbuf.create<myobj>(&dbuf);
|
2015-11-14 22:15:22 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 验证 dbuf_obj 对象在 dbuf_guard 中的在在一致性
|
2015-11-14 22:15:22 +08:00
|
|
|
|
assert(obj == dbuf[obj->pos()]);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 调用 dbuf_obj 子类对象 myobj 的函数 run
|
2015-11-14 22:15:22 +08:00
|
|
|
|
if (i < 10)
|
|
|
|
|
obj->run();
|
2015-11-12 22:24:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-14 22:15:22 +08:00
|
|
|
|
for (int i = 0; i < 10000; i++)
|
|
|
|
|
{
|
2015-11-16 20:00:44 +08:00
|
|
|
|
myobj* obj = dbuf.create<myobj>();
|
2015-11-14 22:15:22 +08:00
|
|
|
|
|
2015-11-16 20:00:44 +08:00
|
|
|
|
assert(dbuf[obj->pos()] == obj);
|
2015-11-14 22:15:22 +08:00
|
|
|
|
|
|
|
|
|
if (i < 10)
|
|
|
|
|
obj->run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10000; i++)
|
|
|
|
|
{
|
2015-11-16 20:00:44 +08:00
|
|
|
|
myobj* obj = dbuf.create<myobj>(&dbuf);
|
2015-11-14 22:15:22 +08:00
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 虽然多次将 dbuf_obj 对象置入 dbuf_guard 中,因为 dbuf_obj
|
|
|
|
|
// 内部的引用计数,所以可以防止被重复添加
|
2015-11-14 22:15:22 +08:00
|
|
|
|
(void) dbuf.push_back(obj);
|
|
|
|
|
(void) dbuf.push_back(obj);
|
|
|
|
|
(void) dbuf.push_back(obj);
|
|
|
|
|
|
|
|
|
|
assert(obj == dbuf[obj->pos()]);
|
|
|
|
|
|
|
|
|
|
if (i < 10)
|
|
|
|
|
obj->run();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void wait_pause()
|
|
|
|
|
{
|
|
|
|
|
printf("Enter any key to continue ...");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
getchar();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test1()
|
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// dbuf_gaurd 对象创建在栈上,函数返回前该对象自动销毁
|
2015-11-14 22:15:22 +08:00
|
|
|
|
acl::dbuf_guard dbuf;
|
|
|
|
|
|
|
|
|
|
test_dbuf(dbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test2()
|
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 动态创建 dbuf_guard 对象,需要手动销毁该对象
|
2015-11-14 22:15:22 +08:00
|
|
|
|
acl::dbuf_guard* dbuf = new acl::dbuf_guard;
|
|
|
|
|
|
|
|
|
|
test_dbuf(*dbuf);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 手工销毁该对象
|
2015-11-14 22:15:22 +08:00
|
|
|
|
delete dbuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test3()
|
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 将内存池对象 dbuf_pool 做为 dbuf_guard 构造函数参数传入,当
|
|
|
|
|
// dbuf_guard 对象销毁时,dbuf_pool 对象一同被销毁
|
2015-11-14 22:15:22 +08:00
|
|
|
|
acl::dbuf_guard dbuf(new acl::dbuf_pool);
|
|
|
|
|
|
|
|
|
|
test_dbuf(dbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test4()
|
|
|
|
|
{
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 动态创建 dbuf_guard 对象,同时指定内存池中内存块的分配倍数为 10,
|
|
|
|
|
// 即指定内部每个内存块大小为 4096 * 10 = 40 KB,同时
|
|
|
|
|
// 指定内部动态数组的初始容量大小
|
2015-11-14 22:15:22 +08:00
|
|
|
|
acl::dbuf_guard dbuf(10, 100);
|
|
|
|
|
|
|
|
|
|
test_dbuf(dbuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test5()
|
|
|
|
|
{
|
|
|
|
|
acl::dbuf_pool* dp = new acl::dbuf_pool;
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 在内存池对象上动态创建 dbuf_guard 对象,这样可以将内存分配的次数
|
|
|
|
|
// 进一步减少一次
|
2015-11-14 22:15:22 +08:00
|
|
|
|
acl::dbuf_guard* dbuf = new (dp->dbuf_alloc(sizeof(acl::dbuf_guard)))
|
|
|
|
|
acl::dbuf_guard(dp);
|
|
|
|
|
|
|
|
|
|
test_dbuf(*dbuf);
|
|
|
|
|
|
2019-07-27 22:44:32 +08:00
|
|
|
|
// 因为 dbuf_gaurd 对象也是在 dbuf_pool 内存池对象上动态创建的,所以
|
|
|
|
|
// 只能通过直接调用 dbuf_guard 的析构函数来释放所有的内存对象;
|
|
|
|
|
// 既不能直接 dbuf_pool->desotry(),也不能直接 delete dbuf_guard 来
|
|
|
|
|
// 销毁 dbuf_guard 对象
|
2015-11-14 22:15:22 +08:00
|
|
|
|
dbuf->~dbuf_guard();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 20:00:44 +08:00
|
|
|
|
class myobj2 : public acl::dbuf_obj
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
myobj2() {}
|
|
|
|
|
|
|
|
|
|
void run()
|
|
|
|
|
{
|
|
|
|
|
printf("hello world\r\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
~myobj2() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class myobj3 : public acl::dbuf_obj
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
myobj3(int i) : i_(i) {}
|
|
|
|
|
|
|
|
|
|
void run()
|
|
|
|
|
{
|
|
|
|
|
printf("hello world: %d\r\n", i_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
~myobj3() {}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int i_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class myobj_dummy // : public acl::dbuf_obj
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
myobj_dummy() {}
|
|
|
|
|
|
|
|
|
|
void run()
|
|
|
|
|
{
|
|
|
|
|
printf("can't be compiled\r\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
~myobj_dummy() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void test6()
|
|
|
|
|
{
|
|
|
|
|
acl::dbuf_guard dbuf;
|
|
|
|
|
|
|
|
|
|
myobj* o = dbuf.create<myobj>();
|
|
|
|
|
o->run();
|
|
|
|
|
|
|
|
|
|
myobj* o1 = dbuf.create<myobj>(&dbuf);
|
|
|
|
|
o1->run();
|
|
|
|
|
|
|
|
|
|
myobj2* o2 = dbuf.create<myobj2>();
|
|
|
|
|
o2->run();
|
|
|
|
|
|
2015-11-20 23:29:10 +08:00
|
|
|
|
myobj3* o3 = dbuf.create<myobj3>(10);
|
|
|
|
|
o3->run();
|
|
|
|
|
|
2015-11-16 20:00:44 +08:00
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
|
{
|
2015-11-20 23:29:10 +08:00
|
|
|
|
myobj3* o4 = dbuf.create<myobj3>(i);
|
|
|
|
|
o4->run();
|
2015-11-16 20:00:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// below codes can't be compiled, because myobj_dummy isn't
|
|
|
|
|
// acl::dbuf_obj's subclass
|
|
|
|
|
// myobj_dummy* dummy = dbuf.create<myobj_dummy>();
|
|
|
|
|
// dummy->run();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-14 22:15:22 +08:00
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
|
|
|
|
test1();
|
|
|
|
|
wait_pause();
|
|
|
|
|
|
|
|
|
|
test2();
|
|
|
|
|
wait_pause();
|
|
|
|
|
|
|
|
|
|
test3();
|
|
|
|
|
wait_pause();
|
|
|
|
|
|
|
|
|
|
test4();
|
|
|
|
|
wait_pause();
|
|
|
|
|
|
|
|
|
|
test5();
|
2015-11-16 20:00:44 +08:00
|
|
|
|
wait_pause();
|
|
|
|
|
|
|
|
|
|
test6();
|
2015-11-12 22:24:05 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|