mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 14:03:26 +02:00
some refactoring
This commit is contained in:
347
src/framework/util/allocator.cpp
Normal file
347
src/framework/util/allocator.cpp
Normal file
@@ -0,0 +1,347 @@
|
||||
#ifdef _DEBUG_MEMORY
|
||||
|
||||
#include "allocator.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <execinfo.h>
|
||||
|
||||
bool allocatorEnabled = false;
|
||||
Allocator *allocator = NULL;
|
||||
|
||||
void disableAllocator() { allocatorEnabled = false; }
|
||||
void enableAllocator() { allocatorEnabled = true; }
|
||||
bool isAllocatorEnabled() { return allocatorEnabled; }
|
||||
|
||||
class Initializer {
|
||||
public:
|
||||
Initializer() {
|
||||
allocator = new Allocator;
|
||||
enableAllocator();
|
||||
}
|
||||
~Initializer() {
|
||||
disableAllocator();
|
||||
Allocator *tmp = allocator;
|
||||
allocator = NULL;
|
||||
delete tmp;
|
||||
}
|
||||
};
|
||||
Initializer initializer __attribute__((init_priority(101)));
|
||||
|
||||
static void addr2line(void *address, const char* name, bool viewSource = false)
|
||||
{
|
||||
char tmpbuf[1024];
|
||||
char *pos;
|
||||
|
||||
snprintf(tmpbuf, sizeof(tmpbuf), "addr2line --functions --demangle -e %s %p", name, address);
|
||||
|
||||
FILE *output = popen(tmpbuf, "r");
|
||||
if(output) {
|
||||
char filesource[1024];
|
||||
int fileline = 0;
|
||||
filesource[0] = '\0';
|
||||
char *line;
|
||||
|
||||
while(!feof(output)) {
|
||||
line = fgets(tmpbuf, sizeof(tmpbuf) - 1, output);
|
||||
if(!line)
|
||||
break;
|
||||
|
||||
pos = strchr(line, '\n');
|
||||
if(pos > 0)
|
||||
pos[0] = '\0';
|
||||
|
||||
int func = 1;
|
||||
pos = strchr(line, ':');
|
||||
if(pos > 0 && pos[1] != ':')
|
||||
func = 0;
|
||||
|
||||
if(func) {
|
||||
printf("%s ", line);
|
||||
} else if(strncmp(line,"??:0",4) != 0) {
|
||||
if(pos > 0 && fileline == 0) {
|
||||
strncpy(filesource, line, (int)(pos - line));
|
||||
filesource[(int)(pos - line)] = '\0';
|
||||
fileline = atoi(line + (int)(pos - line) + 1);
|
||||
}
|
||||
pos = strrchr(line, '/');
|
||||
if(!pos)
|
||||
pos = line;
|
||||
else
|
||||
pos++;
|
||||
printf("(%s) ", pos);
|
||||
}
|
||||
}
|
||||
pclose(output);
|
||||
|
||||
pos = (char*)strrchr(name, '/');
|
||||
if(!pos)
|
||||
pos = (char*) name;
|
||||
else
|
||||
pos++;
|
||||
printf("(%s)", pos);
|
||||
|
||||
if(viewSource) {
|
||||
if(filesource[0] && fileline > 0) {
|
||||
output = fopen(filesource,"rb");
|
||||
if(output) {
|
||||
int i;
|
||||
for(i=0; i<fileline+1; i++) {
|
||||
line = fgets(tmpbuf, sizeof(tmpbuf) - 1, output);
|
||||
|
||||
if(i >= fileline-1 && strlen(line) > 1) {
|
||||
pos = strchr(line, '\n');
|
||||
if(pos > 0)
|
||||
pos[0] = '\0';
|
||||
printf("\n\t\t%d:%s",i,line);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
pos = (char*)strrchr(name, '/');
|
||||
if(!pos)
|
||||
pos = (char*) name;
|
||||
else
|
||||
pos++;
|
||||
printf("%p (%s)", address, pos);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void printBacktrace(int skips)
|
||||
{
|
||||
void *buffer[128];
|
||||
int size = backtrace(buffer, 128);
|
||||
char **strings = backtrace_symbols(buffer, size);
|
||||
for(int i = 1 + skips; i < size; i++) {
|
||||
if(i == 1 + skips) {
|
||||
printf("\tfrom ");
|
||||
} else {
|
||||
printf("\tat ");
|
||||
}
|
||||
std::string str = strings[i];
|
||||
addr2line(buffer[i], str.substr(0, str.find('(')).c_str());
|
||||
}
|
||||
printf("\n");
|
||||
free(strings);
|
||||
}
|
||||
|
||||
|
||||
Allocator::Allocator()
|
||||
{
|
||||
}
|
||||
|
||||
Allocator::~Allocator()
|
||||
{
|
||||
#ifdef _REENTRANT
|
||||
boost::recursive_mutex::scoped_lock lock(m_allocatorLock);
|
||||
#endif
|
||||
|
||||
dumpLeaks();
|
||||
|
||||
for(AllocationBlocksList::iterator it = m_allocationsBlocks.begin(), end = m_allocationsBlocks.end(); it != end; ++it) {
|
||||
AllocationBlock* block = (*it);
|
||||
free(block->backtraceBuffer);
|
||||
free(block);
|
||||
}
|
||||
m_allocationsBlocks.clear();
|
||||
}
|
||||
|
||||
void Allocator::dumpLeaks()
|
||||
{
|
||||
#ifdef _REENTRANT
|
||||
boost::recursive_mutex::scoped_lock lock(m_allocatorLock);
|
||||
#endif
|
||||
|
||||
bool shouldRenable = false;
|
||||
if(isAllocatorEnabled()) {
|
||||
disableAllocator();
|
||||
shouldRenable = true;
|
||||
}
|
||||
|
||||
unsigned int definitelyLostBytes = 0;
|
||||
unsigned int blockNumber = 1;
|
||||
unsigned int countRecords = 0;
|
||||
unsigned int numberOfLeakedBlocks = 0;
|
||||
unsigned int numberOfBlocks = m_allocationsBlocks.size();
|
||||
|
||||
for(AllocationBlocksList::iterator it = m_allocationsBlocks.begin(), end = m_allocationsBlocks.end(); it != end; ++it) {
|
||||
AllocationBlock* block = (*it);
|
||||
if(block->records != 0) {
|
||||
numberOfLeakedBlocks++;
|
||||
}
|
||||
}
|
||||
|
||||
if(numberOfLeakedBlocks > 0) {
|
||||
printf("== LOST BLOCKS:\n");
|
||||
for(AllocationBlocksList::iterator it = m_allocationsBlocks.begin(), end = m_allocationsBlocks.end(); it != end; ++it) {
|
||||
AllocationBlock* block = (*it);
|
||||
if(block->records != 0) {
|
||||
unsigned int lostBytes = block->bytes * block->records;
|
||||
|
||||
definitelyLostBytes += lostBytes;
|
||||
countRecords += block->records;
|
||||
|
||||
printf("%d bytes in %d block\'s records are definitely lost in loss %d of %d\n",
|
||||
lostBytes, block->records, blockNumber, numberOfLeakedBlocks);
|
||||
|
||||
char **strings = backtrace_symbols(block->backtraceBuffer, block->backtraceSize);
|
||||
for(int i = 0; i < block->backtraceSize; i++) {
|
||||
if(i == 0) {
|
||||
printf("\tat ");
|
||||
} else {
|
||||
printf("\tby ");
|
||||
}
|
||||
std::string str = strings[i];
|
||||
addr2line(block->backtraceBuffer[i], str.substr(0, str.find('(')).c_str());
|
||||
}
|
||||
printf("\n");
|
||||
free(strings);
|
||||
blockNumber++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
printf("== LEAK SUMMARY:\n");
|
||||
printf("definitely lost: %d bytes in %d blocks\n", definitelyLostBytes, numberOfLeakedBlocks);
|
||||
printf("unallocated records: %d records in %d blocks\n", countRecords, numberOfLeakedBlocks);
|
||||
printf("leaked blocks: %d in %d blocks\n", numberOfLeakedBlocks, numberOfBlocks);
|
||||
}
|
||||
|
||||
if(shouldRenable)
|
||||
enableAllocator();
|
||||
}
|
||||
|
||||
AllocationBlock* Allocator::findBlock(void **backtraceBuffer, int backtraceSize, unsigned int bytes)
|
||||
{
|
||||
AllocationBlock blockToFind;
|
||||
blockToFind.backtraceBuffer = backtraceBuffer;
|
||||
blockToFind.backtraceSize = backtraceSize;
|
||||
blockToFind.bytes = bytes;
|
||||
|
||||
auto it = m_allocationsBlocks.find(&blockToFind);
|
||||
if(it != m_allocationsBlocks.end())
|
||||
return (*it);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *Allocator::allocate(size_t bytes)
|
||||
{
|
||||
#ifdef _REENTRANT
|
||||
boost::recursive_mutex::scoped_lock lock(m_allocatorLock);
|
||||
#endif
|
||||
|
||||
if(!isAllocatorEnabled()) {
|
||||
return malloc(bytes);
|
||||
} else {
|
||||
disableAllocator();
|
||||
|
||||
char *allocatedBuffer = (char *)malloc(sizeof(AllocationHead) + bytes);
|
||||
AllocationHead *head = (AllocationHead *)allocatedBuffer;
|
||||
void *p = (void *)(allocatedBuffer + sizeof(AllocationHead));
|
||||
|
||||
static void *buffer[128];
|
||||
int size = backtrace(buffer, 128);
|
||||
AllocationBlock* block = findBlock(&buffer[1], size - 1, bytes);
|
||||
|
||||
if(!block) {
|
||||
block = (AllocationBlock *)malloc(sizeof(AllocationBlock));
|
||||
|
||||
block->backtraceBuffer = (void **)malloc(sizeof(void*) * (size - 1));
|
||||
block->backtraceSize = size - 1;
|
||||
block->bytes = bytes;
|
||||
block->records = 0;
|
||||
|
||||
for(int i=0;i<size-1;i++)
|
||||
block->backtraceBuffer[i] = buffer[i+1];
|
||||
|
||||
m_allocationsBlocks.insert(block);
|
||||
}
|
||||
block->records++;
|
||||
|
||||
head->block = block;
|
||||
head->magicNumber = MAGIC_NUMBER;
|
||||
|
||||
enableAllocator();
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
void Allocator::deallocate(void *p)
|
||||
{
|
||||
#ifdef _REENTRANT
|
||||
boost::recursive_mutex::scoped_lock lock(m_allocatorLock);
|
||||
#endif
|
||||
|
||||
if(!isAllocatorEnabled()) {
|
||||
free(p);
|
||||
} else if(p == NULL) {
|
||||
disableAllocator();
|
||||
|
||||
printf("attempt to delete NULL address\n");
|
||||
printBacktrace(1);
|
||||
|
||||
enableAllocator();
|
||||
} else {
|
||||
disableAllocator();
|
||||
|
||||
p = (void*)((char *)p - sizeof(AllocationHead));
|
||||
AllocationHead *head = (AllocationHead *)p;
|
||||
|
||||
if(head->magicNumber == MAGIC_NUMBER) {
|
||||
head->block->records--;
|
||||
|
||||
memset(p, 0, head->block->bytes + sizeof(AllocationHead));
|
||||
free(p);
|
||||
} else {
|
||||
printf("invalid delete address\n");
|
||||
printBacktrace(1);
|
||||
}
|
||||
|
||||
enableAllocator();
|
||||
}
|
||||
}
|
||||
|
||||
void *operator new(size_t bytes, int dummy)
|
||||
{
|
||||
if(allocator)
|
||||
return allocator->allocate(bytes);
|
||||
return malloc(bytes);
|
||||
}
|
||||
|
||||
void *operator new(size_t bytes)
|
||||
{
|
||||
if(allocator)
|
||||
return allocator->allocate(bytes);
|
||||
return malloc(bytes);
|
||||
}
|
||||
|
||||
void *operator new[](size_t bytes)
|
||||
{
|
||||
if(allocator)
|
||||
return allocator->allocate(bytes);
|
||||
return malloc(bytes);
|
||||
}
|
||||
|
||||
void operator delete(void *p)
|
||||
{
|
||||
if(allocator)
|
||||
allocator->deallocate(p);
|
||||
else
|
||||
free(p);
|
||||
}
|
||||
|
||||
void operator delete[](void *p)
|
||||
{
|
||||
if(allocator)
|
||||
allocator->deallocate(p);
|
||||
else
|
||||
free(p);
|
||||
}
|
||||
|
||||
#endif // _DEBUG_MEMORY
|
88
src/framework/util/allocator.h
Normal file
88
src/framework/util/allocator.h
Normal file
@@ -0,0 +1,88 @@
|
||||
#ifndef __ALLOCATOR_H__
|
||||
#define __ALLOCATOR_H__
|
||||
|
||||
#ifdef _DEBUG_MEMORY
|
||||
|
||||
#include <unordered_set>
|
||||
#include <boost/functional/hash.hpp>
|
||||
|
||||
#ifdef _REENTRANT
|
||||
#include <boost/thread.hpp>
|
||||
#endif
|
||||
|
||||
#define MAGIC_NUMBER 0xA1B2C3D4
|
||||
|
||||
struct AllocationBlock
|
||||
{
|
||||
unsigned int bytes;
|
||||
void** backtraceBuffer;
|
||||
unsigned char backtraceSize;
|
||||
unsigned int records;
|
||||
};
|
||||
|
||||
struct AllocationHead
|
||||
{
|
||||
unsigned int magicNumber;
|
||||
AllocationBlock *block;
|
||||
};
|
||||
|
||||
struct block_hash : std::unary_function<AllocationBlock *, std::size_t> {
|
||||
std::size_t operator()(const AllocationBlock *block) const {
|
||||
std::size_t seed = 0;
|
||||
|
||||
boost::hash_combine(seed, block->bytes);
|
||||
for(int i=0;i<4;++i) {
|
||||
if(i < block->backtraceSize)
|
||||
boost::hash_combine(seed, block->backtraceBuffer[i]);
|
||||
else
|
||||
boost::hash_combine(seed, 0);
|
||||
}
|
||||
boost::hash_combine(seed, block->backtraceSize);
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
struct block_equal_to : std::binary_function<AllocationBlock *, AllocationBlock *, bool> {
|
||||
bool operator()(const AllocationBlock* a, const AllocationBlock *b) const
|
||||
{
|
||||
if(a->bytes != b->bytes || a->backtraceSize != b->backtraceSize)
|
||||
return false;
|
||||
for(int i=0;i<a->backtraceSize;++i)
|
||||
if(a->backtraceBuffer[i] != b->backtraceBuffer[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class Allocator
|
||||
{
|
||||
public:
|
||||
Allocator();
|
||||
~Allocator();
|
||||
|
||||
void *allocate(size_t bytes);
|
||||
void deallocate(void *p);
|
||||
|
||||
void dumpLeaks();
|
||||
|
||||
private:
|
||||
AllocationBlock* findBlock(void** backtraceBuffer, int backtraceSize, unsigned int bytes);
|
||||
|
||||
typedef std::unordered_set<AllocationBlock*, block_hash, block_equal_to> AllocationBlocksList;
|
||||
AllocationBlocksList m_allocationsBlocks;
|
||||
|
||||
#ifdef _REENTRANT
|
||||
boost::recursive_mutex m_allocatorLock;
|
||||
#endif
|
||||
};
|
||||
|
||||
void* operator new(size_t bytes, int dummy);
|
||||
void* operator new(size_t bytes);
|
||||
void* operator new[](size_t bytes);
|
||||
void operator delete(void *p);
|
||||
void operator delete[](void *p);
|
||||
|
||||
#endif // _DEBUG_MEMORY
|
||||
|
||||
#endif
|
||||
|
@@ -3,34 +3,17 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
void log(LogLevel level, const std::string& message, const char *trace)
|
||||
void log(LogLevel level, const std::string& message, std::string prettyFunction)
|
||||
{
|
||||
std::string strace;
|
||||
if(trace) {
|
||||
strace = trace;
|
||||
strace = strace.substr(0, strace.find_first_of('('));
|
||||
if(strace.find_last_of(' ') != std::string::npos)
|
||||
strace = strace.substr(strace.find_last_of(' ') + 1);
|
||||
if(!prettyFunction.empty()) {
|
||||
prettyFunction = prettyFunction.substr(0, prettyFunction.find_first_of('('));
|
||||
if(prettyFunction.find_last_of(' ') != std::string::npos)
|
||||
prettyFunction = prettyFunction.substr(prettyFunction.find_last_of(' ') + 1);
|
||||
if(!prettyFunction.empty())
|
||||
std::cout << "[" << prettyFunction << "] ";
|
||||
}
|
||||
|
||||
#ifdef linux
|
||||
static char const *colors[] = { "\033[01;31m ", "\033[01;31m", "\033[01;33m", "\033[0;32m", "\033[01;34m" };
|
||||
static bool colored = getenv("COLORED_OUTPUT");
|
||||
if(colored)
|
||||
std::cout << colors[level];
|
||||
#endif
|
||||
|
||||
if(!strace.empty())
|
||||
std::cout << "[" << strace << "] ";
|
||||
|
||||
std::cout << message;
|
||||
|
||||
#ifdef linux
|
||||
if(colored)
|
||||
std::cout << "\033[0m";
|
||||
#endif
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << message << std::endl;
|
||||
|
||||
if(level == LogFatal)
|
||||
exit(-1);
|
||||
|
@@ -10,7 +10,7 @@ enum LogLevel {
|
||||
LogError,
|
||||
LogFatal
|
||||
};
|
||||
void log(LogLevel level, const std::string& message, const char *trace = NULL);
|
||||
void log(LogLevel level, const std::string& message, std::string prettyFunction = "");
|
||||
|
||||
// specialized logging
|
||||
template<class... T>
|
||||
@@ -24,7 +24,7 @@ void error(const T&... args) { log(LogError, make_string(args...)); }
|
||||
template<class... T>
|
||||
void fatal(const T&... args) { log(LogFatal, make_string(args...)); }
|
||||
|
||||
#define trace() debug(LogDebug, "", __PRETTY_FUNCTION__)
|
||||
#define trace() log(LogDebug, "", __PRETTY_FUNCTION__)
|
||||
|
||||
// dump utility
|
||||
struct Dump {
|
||||
|
@@ -1,5 +1,4 @@
|
||||
#include "translator.h"
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
|
||||
AlignmentFlag parseAlignment(std::string aligment)
|
||||
|
@@ -2,7 +2,6 @@
|
||||
#define TRANSLATOR_H
|
||||
|
||||
#include <const.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
AlignmentFlag parseAlignment(std::string aligment);
|
||||
|
@@ -1,52 +0,0 @@
|
||||
/* The MIT License
|
||||
*
|
||||
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#include <util/logger.h>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <constants.h>
|
||||
|
||||
// Easy/fast writing formater
|
||||
#define fmt(a, b) (boost::format(a) % b).str()
|
||||
|
||||
/// Convert any data type through boost::lexical_cast
|
||||
template<class R, class T>
|
||||
R convert_cast(T t)
|
||||
{
|
||||
R ret = R();
|
||||
try {
|
||||
ret = boost::lexical_cast<R>(t);
|
||||
} catch(boost::bad_lexical_cast bad) {
|
||||
flogError("Error converting value: %s", bad.what());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
AlignmentFlag parseAlignment(std::string aligment);
|
||||
|
||||
|
||||
#endif // UTIL_H
|
Reference in New Issue
Block a user