acl/lib_acl_cpp/samples/dbuf/dbuf2/main.cpp

263 lines
4.7 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include <sys/time.h>
/**
* dbuf_obj <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool <EFBFBD>϶<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_guard ͳһ<EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>
*/
class myobj : public acl::dbuf_obj
{
public:
myobj(acl::dbuf_guard* guard = NULL) : dbuf_obj(guard)
{
ptr_ = strdup("hello");
}
void run()
{
printf("----> run->hello world <-----\r\n");
}
private:
char* ptr_;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ˽<CEAA>ˣ<EFBFBD><CBA3><EFBFBD>ǿ<EFBFBD><C7BF>Ҫ<EFBFBD><D2AA><EFBFBD>ö<EFBFBD><C3B6>󱻶<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// dbuf_guard ͳһ<CDB3><D2BB><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD>ͷű<CDB7><C5B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD>Ķ<EFBFBD>̬<EFBFBD>ڴ<EFBFBD>(ptr_)
~myobj()
{
free(ptr_);
}
};
static void test_dbuf(acl::dbuf_guard& dbuf)
{
for (int i = 0; i < 102400; i++)
{
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
char* ptr = (char*) dbuf.dbuf_alloc(10);
(void) ptr;
}
for (int i = 0; i < 102400; i++)
{
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
char* str = dbuf.dbuf_strdup("hello world");
if (i < 5)
printf(">>str->%s\r\n", str);
}
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
(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++)
{
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD> dbuf_obj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣬲<EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD> dbuf_guard <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// dbuf_obj <20>Ĺ<EFBFBD><C4B9><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD>֮<EFBFBD><D6AE> dbuf_guard ͳһ<CDB3><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
myobj* obj = dbuf.create<myobj>(&dbuf);
// <20><>֤ dbuf_obj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_guard <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
assert(obj == dbuf[obj->pos()]);
// <20><><EFBFBD><EFBFBD> dbuf_obj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> myobj <20>ĺ<EFBFBD><C4BA><EFBFBD> run
if (i < 10)
obj->run();
}
for (int i = 0; i < 10000; i++)
{
myobj* obj = dbuf.create<myobj>();
assert(dbuf[obj->pos()] == obj);
if (i < 10)
obj->run();
}
for (int i = 0; i < 10000; i++)
{
myobj* obj = dbuf.create<myobj>(&dbuf);
// <20><>Ȼ<EFBFBD><C8BB><EFBFBD>ν<EFBFBD> dbuf_obj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_guard <20>У<EFBFBD><D0A3><EFBFBD>Ϊ dbuf_obj
// <20>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF>Է<EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>
(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()
{
// dbuf_gaurd <20><><EFBFBD>󴴽<EFBFBD><F3B4B4BD><EFBFBD>ջ<EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ö<EFBFBD><C3B6><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
acl::dbuf_guard dbuf;
test_dbuf(dbuf);
}
static void test2()
{
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD> dbuf_guard <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD>ٸö<D9B8><C3B6><EFBFBD>
acl::dbuf_guard* dbuf = new acl::dbuf_guard;
test_dbuf(*dbuf);
// <20>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>ٸö<D9B8><C3B6><EFBFBD>
delete dbuf;
}
static void test3()
{
// <20><><EFBFBD>ڴ<EFBFBD><DAB4>ض<EFBFBD><D8B6><EFBFBD> dbuf_pool <20><>Ϊ dbuf_guard <20><><EFBFBD><EFBFBD><ECBAAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EBA3AC>
// dbuf_guard <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>dbuf_pool <20><><EFBFBD><EFBFBD>һͬ<D2BB><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
acl::dbuf_guard dbuf(new acl::dbuf_pool);
test_dbuf(dbuf);
}
static void test4()
{
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD> dbuf_guard <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬʱָ<CAB1><D6B8><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><E4B1B6>Ϊ 10<31><30>
// <20><>ָ<EFBFBD><D6B8><EFBFBD>ڲ<EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>СΪ 4096 * 10 = 40 KB<4B><42>ͬʱ
// ָ<><D6B8><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
acl::dbuf_guard dbuf(10, 100);
test_dbuf(dbuf);
}
static void test5()
{
acl::dbuf_pool* dp = new acl::dbuf_pool;
// <20><><EFBFBD>ڴ<EFBFBD><DAB4>ض<EFBFBD><D8B6><EFBFBD><EFBFBD>϶<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD> dbuf_guard <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
acl::dbuf_guard* dbuf = new (dp->dbuf_alloc(sizeof(acl::dbuf_guard)))
acl::dbuf_guard(dp);
test_dbuf(*dbuf);
// <20><>Ϊ dbuf_gaurd <20><><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD> dbuf_pool <20>ڴ<EFBFBD><DAB4>ض<EFBFBD><D8B6><EFBFBD><EFBFBD>϶<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD>
// ֻ<><D6BB>ͨ<EFBFBD><CDA8>ֱ<EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD> dbuf_guard <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20>Ȳ<EFBFBD><C8B2><EFBFBD>ֱ<EFBFBD><D6B1> dbuf_pool->desotry()<29><>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1> delete dbuf_guard <20><>
// <20><><EFBFBD><EFBFBD> dbuf_guard <20><><EFBFBD><EFBFBD>
dbuf->~dbuf_guard();
}
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();
myobj3* o3 = dbuf.create<myobj3>(10);
o3->run();
for (int i = 0; i < 10; i++)
{
myobj3* o4 = dbuf.create<myobj3>(i);
o4->run();
}
// 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();
}
int main(void)
{
acl::log::stdout_open(true);
test1();
wait_pause();
test2();
wait_pause();
test3();
wait_pause();
test4();
wait_pause();
test5();
wait_pause();
test6();
return 0;
}