本文整理汇总了C++中MemPool类的典型用法代码示例。如果您正苦于以下问题:C++ MemPool类的具体用法?C++ MemPool怎么用?C++ MemPool使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MemPool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: while
void Controller::Run()
{
while(true) {
int num = _poller->Wait(-1);
while (num--) {
int listen_fd;
uint64_t u64;
uint32_t events;
_poller->GetEvent(&u64, &events);
if (events != EPOLLIN) {
continue;
}
listen_fd = static_cast<int>(u64);
// TODO 目前暂时只支持TCP
struct sockaddr_in socket_addr;
socklen_t socket_addr_len = sizeof(sockaddr_in);
int client = accept(listen_fd, (struct sockaddr *)&socket_addr, &socket_addr_len);
// TODO 如果是大量短链接的话,比较.......
TaskData * task = static_cast<TaskData*>(gMemPool.Malloc(sizeof(TaskData)));
if (!task) {
LogErr("alloc task data failed, close client connection, client address: %u:%u\n",
socket_addr.sin_addr.s_addr, socket_addr.sin_port);
safe_close(client);
continue;
}
uint32_t io_handler_id;
uint32_t client_id;
SelectIoHandler(client, &io_handler_id, &client_id);
task->cmd = kControlAddClient;
task->data_type = kTcpData;
task->extern_ip = socket_addr.sin_addr.s_addr;
task->extern_port = socket_addr.sin_port;
task->local_port = _listen_sockets[listen_fd].port;
task->fd_id = client_id;
task->headler_id = io_handler_id;
LOGDEBUG("accept client connection: %u:%u", task->extern_ip, task->extern_port);
if (!_task_pool->Put(io_handler_id, static_cast<void*>(task))) {
LogInfo("add task to pool failed");
safe_close(client);
continue;
}
}
}
}
示例2: main
int main()
{
// TestCase Group1:
MemPool objMemPool;
MemPool *pMemPool = &objMemPool;
int32_t nCount = 10;
char *pszArray = NEW_VEC(pMemPool, char, nCount);
int32_t *pnArray = NEW_VEC(pMemPool, int32_t, nCount);
float *pfArray = NEW_VEC(pMemPool, float, nCount);
for (int i = 0; i < nCount; i++) {
pszArray[i] = 'a' + i;
pnArray[i] = i;
pfArray[i] = 1.0 * i;
}
for (int i = 0; i < nCount; i++) {
fprintf(stdout, "pszArray[%d]:\t%c\n", i, pszArray[i]);
fprintf(stdout, "pnArray[%d]:\t%d\n", i, pnArray[i]);
fprintf(stdout, "pfArray[%d]:\t%f\n", i, pfArray[i]);
}
pMemPool->reset();
// TestCase Group2:
MemPool objMemPool2;
MemPool *pMemPool2 = &objMemPool2;
int32_t nCount2 = 10;
MyClass *pMyClass = NEW(pMemPool2, MyClass);
fprintf(stdout, "pMyClass:\t%d\n", pMyClass->getCount());
MyClass *pMyClass2 = NEW(pMemPool2, MyClass)(100);
fprintf(stdout, "pMyClass2:\t%d\n", pMyClass2->getCount());
MyClass *pMyClassArray = NEW_ARRAY(pMemPool2, MyClass, nCount2);
for (int i = 0; i < nCount2; i++) {
fprintf(stdout, "pMyClassArray[%d]:\t%d\n", i, pMyClassArray[i].getCount());
}
pMemPool2->reset();
// TestCase Group3:
MemPool objMemPool3;
MemPool *pMemPool3 = &objMemPool3;
MemMonitor memMonitor(pMemPool3, 1);
pMemPool3->setMonitor(&memMonitor);
memMonitor.enableException();
int32_t nCount3 = 1;
char *pszArray3 = NEW_VEC(pMemPool3, char, nCount3);
if (!pszArray3) {
printf("pszArray3 NEW_VEC Err!\n");
}
nCount3 = 1024;
int32_t *pnArray3 = NEW_VEC(pMemPool3, int32_t, nCount3);
if (!pnArray3) {
printf("pnArray3 NEW_VEC Err!\n");
}
float *pfArray3 = NEW_VEC(pMemPool3, float, nCount3);
if (!pfArray3) {
printf("pfArray3 NEW_VEC Err!\n");
}
for (int i = 0; i < nCount3; i++) {
pszArray3[i] = 'a' + i;
pnArray3[i] = i;
pfArray3[i] = 1.0 * i;
}
for (int i = 0; i < nCount3; i++) {
fprintf(stdout, "pszArray3[%d]:\t%c\n", i, pszArray3[i]);
fprintf(stdout, "pnArray3[%d]:\t%d\n", i, pnArray3[i]);
fprintf(stdout, "pfArray3[%d]:\t%f\n", i, pfArray3[i]);
}
pMemPool3->reset();
// TestCase Group4:
MemPool objMemPool4;
MemPool *pMemPool4 = &objMemPool4;
MemMonitor memMonitor4(pMemPool4, 1);
pMemPool4->setMonitor(&memMonitor4);
memMonitor4.enableException();
int32_t nCountsz4 = 1024;
char *pszArraysz4 = NEW_VEC(pMemPool4, char, nCountsz4);
if (!pszArraysz4) {
printf("pszArraysz4 is NULL\n");
}
MyClass *pMyClass4 = NEW(pMemPool4, MyClass);
if (!pMyClass4) {
printf("pMyClass4 is NULL\n");
}
fprintf(stdout, "pMyClass4:\t%d\n", pMyClass4->getCount());
int32_t nCountsz4_1 = 1024;
char *pszArraysz4_1 = NEW_VEC(pMemPool4, char, nCountsz4_1);
if (!pszArraysz4_1) {
printf("pszArraysz4_1 is NULL\n");
}
MyClass *pMyClass4_1 = NEW(pMemPool4, MyClass)(100);
if (!pMyClass4_1) {
printf("pMyClass4_1 is NULL\n");
}
fprintf(stdout, "pMyClass4_1:\t%d\n", pMyClass4_1->getCount());
int32_t nCountsz4_2 = 1024;
char *pszArraysz4_2 = NEW_VEC(pMemPool4, char, nCountsz4_2);
if (!pszArraysz4_2) {
printf("pszArraysz4_2 is NULL\n");
//.........这里部分代码省略.........
示例3: safe_close
namespace ef {
CMap gConfigureMap;
MemPool gMemPool;
Controller::ListenFdInfo::~ListenFdInfo() {
if (fd >= 0) {
safe_close(fd);
}
}
Controller::Controller()
{
}
Controller::~Controller()
{
// TODO
if (_listen_sockets) {
delete [] _listen_sockets;
_listen_sockets = nullptr;
}
}
bool Controller::Initialize(const char* server_name, const char* configure_file)
{
_server_name = server_name;
_configure_file = configure_file;
// 加载配置
if (!gConfigureMap.Load(configure_file))
{
printf("error: %s\n", gConfigureMap.GetErrMsg().c_str());
return false;
}
gConfigureMap.Print();
// 初始化日志
std::string log_path = JoinPath(gConfigureMap["log"]["log_path"].str(), server_name);
if (!Logger::Initialize(log_path.c_str(),
gConfigureMap["log"]["max_file_size"].as<uint32_t>(DefaultConf::kMaxLogFileSize),
gConfigureMap["log"]["max_file_num"].as<uint32_t>(DefaultConf::kMaxLogFileNum),
gConfigureMap["log"]["log_level"].c_str())) {
printf("logger initialize failed, errmsg: %s\n", Logger::GetErrMsg().c_str());
}
LogInfo("Logger initialize succeed\n");
int poller_max_event = gConfigureMap["poller_max_event"].as<int>(DefaultConf::kPollerMaxEventNum);
assert(poller_max_event > 0);
_poller = new Poller(poller_max_event);
assert(_poller);
LogInfo("Poller initialize succeed\n");
// 初始化监听端口
// TODO different port bind different protocol(unpack function)
// 每个监听端口最好能独立的包完整性检查,每个访问外部server的端口,最好也能绑定独立协议
const Value & val = gConfigureMap["listen"];
_listen_size = val.size() + 128;
_listen_sockets = new ListenFdInfo[_listen_size];
for (uint32_t i=0; i<_listen_size; ++i)
{
unsigned int port = val[i]["port"].as<unsigned>(0);
int fd = ListenTcpV4(port);
if (fd < 0) {
FillErrmsg("listen failed: ", errno);
return false;
}
_listen_sockets[fd].port = port;
_listen_sockets[fd].fd = fd;
if (_poller->Add(fd, (uint64_t)fd, EPOLLIN)) {
FillErrmsg("listen fd add to poller failed: ", errno);
return false;
}
}
_io_handler_num = gConfigureMap["server"]["io_handler_num"].as<uint32_t>(DefaultConf::kIoHandlerNum);
_io_handers = new IoHandler[_io_handler_num];
assert(_io_handers);
for (uint32_t i=0; i<_io_handler_num; ++i) {
if(!_io_handers[i].Initialize()) {
_errmsg = "io handler initialize failed";
return false;
}
}
_worker_num = gConfigureMap["server"]["worker_num"].as<uint32_t>(DefaultConf::kWorkerNum);
_workers = new Worker[_worker_num];
assert(_workers);
for (uint32_t i=0; i<_worker_num; ++i) {
if(!_workers[i].Initialize()) {
_errmsg = "initialize workers failed";
return false;
}
}
//.........这里部分代码省略.........
示例4: test_markSweep
void test_markSweep(void)
{
MemPool* orgMemPool = gMemPool;
void* tmp = HMD_ALLOC(2048);
MemPool::initGMemPool(tmp, 2048);
GC::initialize();
MemPool* pool = gMemPool;
Object* o1 = Object::create(HC_StringBuffer, 20);
MemCell* c2 = pool->alloc(40);
Object* o3 = Object::create(HC_StringBuffer, 60);
MemCell* c4 = pool->alloc(80);
Object* o5 = Object::create(HC_StringBuffer, 100);
Object* o6 = Object::create(HC_StringBuffer, 120);
Object* o7 = Object::create(HC_StringBuffer, 80);
MemCell* c8 = pool->alloc(100);
Object* o9 = Object::create(HC_StringBuffer, 60);
Object* o10 = Object::create(HC_StringBuffer, 40);
// ObjectはGCObjectで、MemCellはGCObjectではない
CPPUNIT_ASSERT(((MemCell*)o1)->isGCObject());
CPPUNIT_ASSERT(((MemCell*)o3)->isGCObject());
CPPUNIT_ASSERT(((MemCell*)o5)->isGCObject());
CPPUNIT_ASSERT(((MemCell*)o6)->isGCObject());
CPPUNIT_ASSERT(((MemCell*)o7)->isGCObject());
CPPUNIT_ASSERT(((MemCell*)o9)->isGCObject());
CPPUNIT_ASSERT(((MemCell*)o10)->isGCObject());
CPPUNIT_ASSERT(! c2->isGCObject());
CPPUNIT_ASSERT(! c4->isGCObject());
CPPUNIT_ASSERT(! c8->isGCObject());
// Object生成当初はマークされている
CPPUNIT_ASSERT(o1->isMarked());
CPPUNIT_ASSERT(o3->isMarked());
CPPUNIT_ASSERT(o5->isMarked());
CPPUNIT_ASSERT(o6->isMarked());
CPPUNIT_ASSERT(o7->isMarked());
CPPUNIT_ASSERT(o9->isMarked());
CPPUNIT_ASSERT(o10->isMarked());
GC::unmark();
CPPUNIT_ASSERT(! o1->isMarked());
CPPUNIT_ASSERT(! o3->isMarked());
CPPUNIT_ASSERT(! o5->isMarked());
CPPUNIT_ASSERT(! o6->isMarked());
CPPUNIT_ASSERT(! o7->isMarked());
CPPUNIT_ASSERT(! o9->isMarked());
CPPUNIT_ASSERT(! o10->isMarked());
o3->m_mark();
o5->m_mark();
o6->m_mark();
o9->m_mark();
CPPUNIT_ASSERT(o3->isMarked());
CPPUNIT_ASSERT(o5->isMarked());
CPPUNIT_ASSERT(o6->isMarked());
CPPUNIT_ASSERT(o9->isMarked());
// 回収
GC::sweep();
// freeじゃないのはMemCellとmarkしたObjectとfinalize用Context
int flag = 0;
for (CellIterator itr = pool->begin(); itr < pool->end(); itr++) {
MemCell* cell = *itr;
if (! cell->m_bFree()) {
if (cell == c2) {
CPPUNIT_ASSERT((flag & 0x01) == 0);
flag |= 0x01;
} else if (cell == c4) {
CPPUNIT_ASSERT((flag & 0x02) == 0);
flag |= 0x02;
} else if (cell == c8) {
CPPUNIT_ASSERT((flag & 0x04) == 0);
flag |= 0x04;
} else if (cell == (MemCell*)o3) {
CPPUNIT_ASSERT((flag & 0x08) == 0);
flag |= 0x08;
} else if (cell == (MemCell*)o5) {
CPPUNIT_ASSERT((flag & 0x10) == 0);
flag |= 0x10;
} else if (cell == (MemCell*)o6) {
CPPUNIT_ASSERT((flag & 0x20) == 0);
flag |= 0x20;
} else if (cell == (MemCell*)o9) {
CPPUNIT_ASSERT((flag & 0x40) == 0);
flag |= 0x40;
}
}
}
CPPUNIT_ASSERT(flag == 0x7f);
GC::finalize();
gMemPool = orgMemPool;
HMD_FREE(tmp);
}
示例5: Init
void Init(MemPool &pool, const Factor *factor) {
lmstate = NULL;
numWords = 1;
lastWords = (const Factor**) pool.Allocate(sizeof(const Factor*));
lastWords[0] = factor;
}
示例6: snprintf
int32_t IndexFieldInc::idx2Txt(idx_dict_t* pdict)
{
if(NULL == pdict) {
return 0;
}
char filename[PATH_MAX];
snprintf(filename, PATH_MAX, "%s/%s.txt", _idxPath, _fieldName);
FILE* fp = fopen(filename, "wb");
if(NULL == fp) {
return -1;
}
unsigned int pos = 0;
MemPool cMemPool;
idict_node_t* pNode = idx_dict_first(pdict, &pos);
DocIdManager* pDoc = DocIdManager::getInstance();
DocIdManager::DeleteMap* del = pDoc->getDeleteMap();
int maxNum = 0, num = 0;
DocListUnit* list = NULL;
while(pNode) {
IndexTerm* pIndexTerm = getTerm(&cMemPool, pNode->sign);
if(NULL == pIndexTerm) {
TERR("get %s %lu error\n", _fieldName, pNode->sign);
fclose(fp);
return -1;
}
const IndexTermInfo* pTermInfo = pIndexTerm->getTermInfo();
if(maxNum < pTermInfo->docNum) {
if(list) delete [] list;
maxNum = pTermInfo->docNum;
list = new DocListUnit[maxNum];
}
uint32_t docId;
if(pTermInfo->maxOccNum > 0) {
num = 0;
while((docId = pIndexTerm->next()) < INVALID_DOCID) {
if(del->isDel(docId)) continue;
int32_t count;
uint8_t* pocc = pIndexTerm->getOcc(count);
for(int32_t i = 0; i < count; i++, num++) {
list[num].doc_id = docId;
list[num].occ = pocc[i];
}
}
} else {
num = 0;
while((docId = pIndexTerm->next()) < INVALID_DOCID) {
if(del->isDel(docId)) continue;
list[num].doc_id = docId;
list[num++].occ = 0;
}
}
if(num > 0) {
fprintf(fp, "term:%lu docNum:%d\n", pNode->sign, num);
if(pTermInfo->maxOccNum > 0) {
for(int32_t i = 0; i < num; i++) {
fprintf(fp, "%lu(%u) ", pDoc->getNid(list[i].doc_id), list[i].occ);
}
} else {
for(int32_t i = 0; i < num; i++) {
fprintf(fp, "%lu ", pDoc->getNid(list[i].doc_id));
}
}
fprintf(fp, "\n");
}
cMemPool.reset();
pNode = idx_dict_next(pdict, &pos);
}
fclose(fp);
if (list) delete [] list;
return 0;
}
示例7: handleTimeout
int32_t SearcherWorker::run() {
const char *pureString = NULL;
uint32_t pureSize = 0;
char *resData = NULL;
uint32_t resSize = 0;
char *resDetailData = NULL;
int32_t resDetailSize = 0;
int32_t ret = 0;
MemPool *memPool = NULL;
int64_t *pNid = NULL;
commdef::ClusterResult *pClusterResult = NULL;
queryparser::QueryRewriterResult *pQRWResult = NULL;
queryparser::QPResult *pQueryResult = NULL;
SearchResult *pSearchResult = NULL;
statistic::StatisticResult *pStatisticResult = NULL;
sort_framework::SortResult *pSortResult = NULL;
ResultSerializer resultSerial;
FRAMEWORK::Context context;
FILE *pOutput = NULL;
//check session status
FRAMEWORK::session_status_t status = _session.getStatus();
if (status == FRAMEWORK::ss_timeout) {
handleTimeout();
return KS_SUCCESS;
}
//get query infomation
FRAMEWORK::Query &query = _session.getQuery();
pureSize = query.getPureQuerySize();
pureString = query.getPureQueryData();
if (!pureString || pureSize == 0) {
_session.setStatus(FRAMEWORK::ss_error);
_session.response();
return KS_EFAILED;
}
//set LogInfo level
_session._logInfo._eRole = FRAMEWORK::sr_simple;
//get MemPool from factory
memPool = _memFactory.make((uint64_t)(getOwner()));
if (memPool == NULL) {
TWARN("Make mem pool failed!");
return KS_EFAILED;
}
//create memory pool monitor
MemMonitor memMonitor(memPool, _memLimit);
memPool->setMonitor(&memMonitor);
memMonitor.enableException();
//initialize context class
context.setMemPool(memPool);
//initialize format processor
_formatProcessor.init(memPool);
//Deal with search proccess
do{
if(_session.isHttp()){
pQRWResult = NEW(memPool, queryparser::QueryRewriterResult)();
if (pQRWResult == NULL) {
TWARN("SEARCHER: new Result no mem");
_session.setStatus(FRAMEWORK::ss_error);
break;
}
}
pQueryResult = NEW(memPool, queryparser::QPResult)(memPool);
pSearchResult = NEW(memPool, SearchResult);
pStatisticResult = NEW(memPool, statistic::StatisticResult);
pSortResult = NEW(memPool, sort_framework::SortResult)(memPool);
if(unlikely(!pQueryResult || !pSearchResult || !pStatisticResult
|| !pSortResult)) {
TWARN("SEARCHER: new Result no mem");
_session.setStatus(FRAMEWORK::ss_error);
break;
}
//add queryrewrite process
if(_session.isHttp()){
ret = _qrewriter.doRewrite(&context, pureString, pureSize, pQRWResult);
if (timeoutCheck && (_timeout > 0) && (_session.getLatencyTime() > _timeout)) {
_session.setStatus(FRAMEWORK::ss_timeout);
TWARN("SEARCHER: qrewriter.doRewrite function over time. query is %s", pureString);
break;
}
if (unlikely(ret != KS_SUCCESS)) {
_session.setStatus(FRAMEWORK::ss_error);
TWARN("qrewriter.doRewrite function error. query is %s", pureString);
break;
}
pureString = pQRWResult->getRewriteQuery();
}
//end add
ret = _qp.doParse(&context, pQueryResult, pureString);
if (timeoutCheck && (_timeout > 0) &&
(_session.getLatencyTime() > _timeout))
{
_session.setStatus(FRAMEWORK::ss_timeout);
TWARN("SEARCHER: qp.doParse function over time. query is %s", pureString);
break;
}
if (unlikely(ret != KS_SUCCESS)){
TWARN("SEARCHER: queryparser doParse function error. query is %s", pureString);
_session.setStatus(FRAMEWORK::ss_error);
break;
}
//.........这里部分代码省略.........
示例8: test1
void test1()
{
MemPool mp;
void* p = mp.alloc(100);
mp.printInfo();
p = mp.alloc(100);
mp.printInfo();
mp.free(p);
mp.printInfo();
void* p1 = mp.alloc(100);
mp.printInfo();
void* p2 = mp.alloc(100);
mp.printInfo();
void* p3 = mp.alloc(100);
mp.printInfo();
void* p4 = mp.alloc(100);
mp.printInfo();
void* p5 = mp.alloc(256);
mp.printInfo();
void* p6 = mp.alloc(257);
mp.printInfo();
mp.free(p1);
mp.printInfo();
mp.free(p2);
mp.printInfo();
mp.free(p3);
mp.printInfo();
mp.free(p4);
mp.printInfo();
}
示例9: linearFree
void linearFree(void* mem)
{
auto node = getNode(mem);
if (!node) return;
// Free the chunk
sLinearPool.Deallocate(node->chunk);
// Free the node
delNode(node);
}
示例10: linearInit
static bool linearInit()
{
auto blk = MemBlock::Create((u8*)__linear_heap, __linear_heap_size);
if (blk)
{
sLinearPool.AddBlock(blk);
rbtree_init(&sAddrMap, addrMapNodeComparator);
return true;
}
return false;
}
示例11: linearMemAlign
void* linearMemAlign(size_t size, size_t alignment)
{
// Enforce minimum alignment
if (alignment < 16)
alignment = 16;
// Convert alignment to shift amount
int shift;
for (shift = 4; shift < 32; shift ++)
{
if ((1U<<shift) == alignment)
break;
}
if (shift == 32) // Invalid alignment
return nullptr;
// Initialize the pool if it is not ready
if (!sLinearPool.Ready() && !linearInit())
return nullptr;
// Allocate the chunk
MemChunk chunk;
if (!sLinearPool.Allocate(chunk, size, shift))
return nullptr;
auto node = newNode(chunk);
if (!node)
{
sLinearPool.Deallocate(chunk);
return nullptr;
}
if (rbtree_insert(&sAddrMap, &node->node));
if (sLinearPool_maxaddr < (u32)sLinearPool.last->base)
sLinearPool_maxaddr = (u32)sLinearPool.last->base;
return chunk.addr;
}
示例12: deleteGesture
void deleteGesture( AbstractGestureEvent *g ){
pool.free( (DragGesture*)g );
}
示例13: clear
void clear() {
states.clear();
pool.clear();
reset();
}
示例14: linearSpaceFree
u32 linearSpaceFree()
{
return sLinearPool.GetFreeSpace();
}
示例15: myNew
PPMonoidElemRawPtr PPMonoidOvImpl::myNew(PPMonoidElemConstRawPtr rawcopypp) const
{
PPMonoidElemRawPtr rawpp(myMemMgr.alloc());
myAssign(rawpp, rawcopypp); // cannot throw
return rawpp;
}