summaryrefslogtreecommitdiff
path: root/zen/string_base.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:20:29 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:20:29 +0200
commitb8f13e45be884dc12884ebe8f3dcd9eecb23a106 (patch)
tree22a6d8b96815d626061ff3e2d432c13078fca5c4 /zen/string_base.h
parent5.4 (diff)
downloadFreeFileSync-b8f13e45be884dc12884ebe8f3dcd9eecb23a106.tar.gz
FreeFileSync-b8f13e45be884dc12884ebe8f3dcd9eecb23a106.tar.bz2
FreeFileSync-b8f13e45be884dc12884ebe8f3dcd9eecb23a106.zip
5.5
Diffstat (limited to 'zen/string_base.h')
-rw-r--r--zen/string_base.h8
1 files changed, 5 insertions, 3 deletions
diff --git a/zen/string_base.h b/zen/string_base.h
index 16731089..4c339d56 100644
--- a/zen/string_base.h
+++ b/zen/string_base.h
@@ -31,7 +31,8 @@ public:
//::operator new/ ::operator delete show same performance characterisics like malloc()/free()!
static void* allocate(size_t size) { return ::operator new(size); } //throw std::bad_alloc
static void deallocate(void* ptr) { ::operator delete(ptr); }
- static size_t calcCapacity(size_t length) { return std::max<size_t>(16, length + length / 2); } //any growth rate should not exceed golden ratio: 1.618033989
+ static size_t calcCapacity(size_t length) { return std::max<size_t>(std::max<size_t>(16, length), length + length / 2); } //size_t might overflow!
+ //any growth rate should not exceed golden ratio: 1.618033989
};
@@ -68,9 +69,9 @@ protected:
static Char* create(size_t size) { return create(size, size); }
static Char* create(size_t size, size_t minCapacity)
{
+ assert(size <= minCapacity);
const size_t newCapacity = AP::calcCapacity(minCapacity);
assert(newCapacity >= minCapacity);
- assert(minCapacity >= size);
Descriptor* const newDescr = static_cast<Descriptor*>(AP::allocate(sizeof(Descriptor) + (newCapacity + 1) * sizeof(Char)));
@@ -121,9 +122,10 @@ protected:
static Char* create(size_t size) { return create(size, size); }
static Char* create(size_t size, size_t minCapacity)
{
+ assert(size <= minCapacity);
+
const size_t newCapacity = AP::calcCapacity(minCapacity);
assert(newCapacity >= minCapacity);
- assert(minCapacity >= size);
Descriptor* const newDescr = static_cast<Descriptor*>(AP::allocate(sizeof(Descriptor) + (newCapacity + 1) * sizeof(Char)));
new (newDescr) Descriptor(1, size, newCapacity);
bgstack15