1
0
Fork 0

fix define name

This commit is contained in:
jsteemann 2018-11-10 23:17:23 +01:00
parent 62645c1370
commit 02cf9013e3
5 changed files with 66 additions and 55 deletions

View File

@ -3802,13 +3802,8 @@ AstNode* Ast::createNode(AstNodeType type) {
auto node = new AstNode(type); auto node = new AstNode(type);
try { // register the node so it gets freed automatically later
// register the node so it gets freed automatically later _query->addNode(node);
_query->addNode(node);
} catch (...) {
delete node;
throw;
}
return node; return node;
} }

View File

@ -37,7 +37,9 @@ static char const* EmptyString = "";
QueryResources::QueryResources(ResourceMonitor* resourceMonitor) QueryResources::QueryResources(ResourceMonitor* resourceMonitor)
: _resourceMonitor(resourceMonitor), : _resourceMonitor(resourceMonitor),
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
_stringsLength(0), _stringsLength(0),
#endif
_shortStringStorage(_resourceMonitor, 1024) {} _shortStringStorage(_resourceMonitor, 1024) {}
QueryResources::~QueryResources() { QueryResources::~QueryResources() {
@ -51,7 +53,7 @@ QueryResources::~QueryResources() {
delete it; delete it;
} }
#ifdef ARANGODB_USE_MAINTAINER_MODE #ifdef ARANGODB_ENABLE_MAINTAINER_MODE
// we are in the destructor here already. decreasing the memory usage counters will only // we are in the destructor here already. decreasing the memory usage counters will only
// provide a benefit (in terms of assertions) if we are in maintainer mode, so we can // provide a benefit (in terms of assertions) if we are in maintainer mode, so we can
// save all these operations in non-maintainer mode // save all these operations in non-maintainer mode
@ -60,14 +62,13 @@ QueryResources::~QueryResources() {
#endif #endif
} }
void QueryResources::steal() {
// we are not responsible for freeing any data, so we delete our inventory
_strings.clear();
_nodes.clear();
}
/// @brief add a node to the list of nodes /// @brief add a node to the list of nodes
void QueryResources::addNode(AstNode* node) { void QueryResources::addNode(AstNode* node) {
auto guard = scopeGuard([node] () {
// in case something goes wrong, we must free the node we got to prevent memleaks
delete node;
});
size_t capacity; size_t capacity;
if (_nodes.empty()) { if (_nodes.empty()) {
@ -99,6 +100,9 @@ void QueryResources::addNode(AstNode* node) {
// will not fail // will not fail
_nodes.emplace_back(node); _nodes.emplace_back(node);
// safely took over the ownership for the node, cancel the deletion now
guard.cancel();
} }
/// @brief register a string /// @brief register a string
@ -143,46 +147,55 @@ char* QueryResources::registerEscapedString(char const* p, size_t length,
return registerLongString(copy, outLength); return registerLongString(copy, outLength);
} }
/// @brief registers a long string and takes over the ownership for it
char* QueryResources::registerLongString(char* copy, size_t length) { char* QueryResources::registerLongString(char* copy, size_t length) {
if (copy == nullptr) { if (copy == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY); THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
} }
try { auto guard = scopeGuard([copy] () {
size_t capacity; // in case something goes wrong, we must free the string we got to prevent memleaks
if (_strings.empty()) {
// reserve some initial space for string storage
capacity = 8;
} else {
capacity = _strings.size() + 1;
if (capacity > _strings.capacity()) {
capacity *= 2;
}
}
TRI_ASSERT(capacity > _strings.size());
// reserve space
if (capacity > _strings.capacity()) {
_resourceMonitor->increaseMemoryUsage(((capacity - _strings.size()) * sizeof(char*)) + length);
try {
_strings.reserve(capacity);
} catch (...) {
// revert change in memory increase
_resourceMonitor->decreaseMemoryUsage(((capacity - _strings.size()) * sizeof(char*)) + length);
throw;
}
}
// will not fail
_strings.emplace_back(copy);
_stringsLength += length;
return copy;
} catch (...) {
// prevent memleak
TRI_FreeString(copy); TRI_FreeString(copy);
throw; });
size_t capacity;
if (_strings.empty()) {
// reserve some initial space for string storage
capacity = 8;
} else {
capacity = _strings.size() + 1;
if (capacity > _strings.capacity()) {
capacity *= 2;
}
} }
TRI_ASSERT(capacity > _strings.size());
// reserve space
if (capacity > _strings.capacity()) {
// not enough capacity...
_resourceMonitor->increaseMemoryUsage(((capacity - _strings.size()) * sizeof(char*)) + length);
try {
_strings.reserve(capacity);
} catch (...) {
// revert change in memory increase
_resourceMonitor->decreaseMemoryUsage(((capacity - _strings.size()) * sizeof(char*)) + length);
throw;
}
} else {
// got enough capacity for the new string
_resourceMonitor->increaseMemoryUsage(length);
}
// will not fail
_strings.emplace_back(copy);
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
_stringsLength += length;
#endif
// safely took over the ownership fo the string, cancel the deletion now
guard.cancel();
return copy;
} }

View File

@ -42,8 +42,6 @@ class QueryResources {
explicit QueryResources(ResourceMonitor*); explicit QueryResources(ResourceMonitor*);
~QueryResources(); ~QueryResources();
void steal();
/// @brief add a node to the list of nodes /// @brief add a node to the list of nodes
void addNode(AstNode*); void addNode(AstNode*);
@ -62,6 +60,7 @@ class QueryResources {
char* registerEscapedString(char const* p, size_t length, size_t& outLength); char* registerEscapedString(char const* p, size_t length, size_t& outLength);
private: private:
/// @brief registers a long string and takes over the ownership for it
char* registerLongString(char* copy, size_t length); char* registerLongString(char* copy, size_t length);
private: private:
@ -74,7 +73,9 @@ class QueryResources {
std::vector<char*> _strings; std::vector<char*> _strings;
/// @brief cumulated length of strings in _strings /// @brief cumulated length of strings in _strings
#ifdef ARANGODB_ENABLE_MAINTAINER_MODE
size_t _stringsLength; size_t _stringsLength;
#endif
/// @brief short string storage. uses less memory allocations for short /// @brief short string storage. uses less memory allocations for short
/// strings /// strings

View File

@ -30,7 +30,10 @@ using namespace arangodb::aql;
/// @brief create a short string storage instance /// @brief create a short string storage instance
ShortStringStorage::ShortStringStorage(ResourceMonitor* resourceMonitor, size_t blockSize) ShortStringStorage::ShortStringStorage(ResourceMonitor* resourceMonitor, size_t blockSize)
: _resourceMonitor(resourceMonitor), _blocks(), _blockSize(blockSize), _current(nullptr), _end(nullptr) { : _resourceMonitor(resourceMonitor),
_blockSize(blockSize),
_current(nullptr),
_end(nullptr) {
TRI_ASSERT(blockSize >= 64); TRI_ASSERT(blockSize >= 64);
} }

View File

@ -61,7 +61,7 @@ class FixedSizeAllocator {
if (this != &other) { if (this != &other) {
TRI_ASSERT(_itemSize == other._itemSize); TRI_ASSERT(_itemSize == other._itemSize);
delete [] _alloc; delete[] _alloc;
_nrAlloc = other._nrAlloc; _nrAlloc = other._nrAlloc;
_nrUsed = other._nrUsed; _nrUsed = other._nrUsed;
_alloc = other._alloc; _alloc = other._alloc;
@ -155,8 +155,7 @@ class FixedSizeAllocator {
void allocateBlock() { void allocateBlock() {
size_t const size = 128 << (std::min)(size_t(8), _blocks.size()); size_t const size = 128 << (std::min)(size_t(8), _blocks.size());
auto block = std::make_unique<MemoryBlock>(_itemSize, size); auto block = std::make_unique<MemoryBlock>(_itemSize, size);
_blocks.emplace_back(block.get()); _blocks.emplace_back(std::move(block));
block.release();
} }
std::vector<std::unique_ptr<MemoryBlock>> _blocks; std::vector<std::unique_ptr<MemoryBlock>> _blocks;