Skip to content

Commit

Permalink
Merge pull request #1375 from basvodde/master
Browse files Browse the repository at this point in the history
StringCacheAllocator
  • Loading branch information
basvodde committed May 6, 2020
2 parents b8b53db + 494a1d6 commit d289eaf
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 125 deletions.
22 changes: 17 additions & 5 deletions include/CppUTest/SimpleString.h
Expand Up @@ -102,14 +102,26 @@ class SimpleString
static char ToLower(char ch);
static int MemCmp(const void* s1, const void *s2, size_t n);
static char* allocStringBuffer(size_t size, const char* file, size_t line);
static void deallocStringBuffer(char* str, const char* file, size_t line);
static void deallocStringBuffer(char* str, size_t size, const char* file, size_t line);
private:

const char* getBuffer() const;

void deallocateInternalBuffer();
void setInternalBufferAsEmptyString();
void setInternalBufferToNewBuffer(size_t size);
void setInternalBufferTo(char* buffer, size_t size);
void copyBufferToNewInternalBuffer(const char* otherBuffer);
void copyBufferToNewInternalBuffer(const char* otherBuffer, size_t size);
void copyBufferToNewInternalBuffer(const SimpleString& otherBuffer);

char *buffer_;
size_t bufferSize_;

static TestMemoryAllocator* stringAllocator_;

char* getEmptyString() const;
static char* copyToNewBuffer(const char* bufferToCopy, size_t bufferSize=0);
static char* copyToNewBuffer(const char* bufferToCopy, size_t bufferSize);
static bool isDigit(char ch);
static bool isSpace(char ch);
static bool isUpper(char ch);
Expand Down Expand Up @@ -196,12 +208,12 @@ class SimpleStringInternalCache
SimpleStringInternalCacheNode* createInternalCacheNodes();
void destroyInternalCacheNode(SimpleStringInternalCacheNode * node);
SimpleStringMemoryBlock* createSimpleStringMemoryBlock(size_t sizeOfString, SimpleStringMemoryBlock* next);
void destroySimpleStringMemoryBlock(SimpleStringMemoryBlock * block);
void destroySimpleStringMemoryBlockList(SimpleStringMemoryBlock * block);
void destroySimpleStringMemoryBlock(SimpleStringMemoryBlock * block, size_t size);
void destroySimpleStringMemoryBlockList(SimpleStringMemoryBlock * block, size_t size);

SimpleStringMemoryBlock* reserveCachedBlockFrom(SimpleStringInternalCacheNode* node);
void releaseCachedBlockFrom(char* memory, SimpleStringInternalCacheNode* node);
void releaseNonCachedMemory(char* memory);
void releaseNonCachedMemory(char* memory, size_t size);

SimpleStringMemoryBlock* allocateNewCacheBlockFrom(SimpleStringInternalCacheNode* node);
SimpleStringMemoryBlock* addToSimpleStringMemoryBlockList(SimpleStringMemoryBlock* newBlock, SimpleStringMemoryBlock* previousHead);
Expand Down
29 changes: 25 additions & 4 deletions include/CppUTest/TestMemoryAllocator.h
Expand Up @@ -67,7 +67,7 @@ class TestMemoryAllocator
bool hasBeenDestroyed();

virtual char* alloc_memory(size_t size, const char* file, size_t line);
virtual void free_memory(char* memory, const char* file, size_t line);
virtual void free_memory(char* memory, size_t size, const char* file, size_t line);

virtual const char* name() const;
virtual const char* alloc_name() const;
Expand Down Expand Up @@ -96,7 +96,7 @@ class MemoryLeakAllocator : public TestMemoryAllocator
virtual ~MemoryLeakAllocator() _destructor_override;

virtual char* alloc_memory(size_t size, const char* file, size_t line) _override;
virtual void free_memory(char* memory, const char* file, size_t line) _override;
virtual void free_memory(char* memory, size_t size, const char* file, size_t line) _override;

virtual const char* name() const _override;
virtual const char* alloc_name() const _override;
Expand Down Expand Up @@ -127,7 +127,7 @@ class NullUnknownAllocator: public TestMemoryAllocator
virtual ~NullUnknownAllocator() _destructor_override;

virtual char* alloc_memory(size_t size, const char* file, size_t line) _override;
virtual void free_memory(char* memory, const char* file, size_t line) _override;
virtual void free_memory(char* memory, size_t size, const char* file, size_t line) _override;

static TestMemoryAllocator* defaultAllocator();
};
Expand Down Expand Up @@ -209,7 +209,7 @@ class AccountingTestMemoryAllocator : public TestMemoryAllocator
virtual ~AccountingTestMemoryAllocator() _destructor_override;

virtual char* alloc_memory(size_t size, const char* file, size_t line) _override;
virtual void free_memory(char* memory, const char* file, size_t line) _override;
virtual void free_memory(char* memory, size_t size, const char* file, size_t line) _override;

virtual TestMemoryAllocator* actualAllocator() _override;
TestMemoryAllocator* originalAllocator();
Expand Down Expand Up @@ -256,5 +256,26 @@ class GlobalMemoryAccountant
AccountingTestMemoryAllocator* newArrayAllocator_;
};

class SimpleStringInternalCache;

class SimpleStringCacheAllocator : public TestMemoryAllocator
{
public:
SimpleStringCacheAllocator(SimpleStringInternalCache& cache, TestMemoryAllocator* previousAllocator);
virtual ~SimpleStringCacheAllocator() _destructor_override;

virtual char* alloc_memory(size_t size, const char* file, size_t line) _override;
virtual void free_memory(char* memory, size_t size, const char* file, size_t line) _override;

virtual const char* name() const _override;
virtual const char* alloc_name() const _override;
virtual const char* free_name() const _override;

virtual TestMemoryAllocator* actualAllocator() _override;
private:
SimpleStringInternalCache& cache_;
TestMemoryAllocator* originalAllocator_;
};

#endif

2 changes: 2 additions & 0 deletions include/CppUTest/TestTestingFixture.h
Expand Up @@ -69,6 +69,8 @@ class TestTestingFixture
static void lineExecutedAfterCheck();

private:
void clearExecFunction();

static bool lineOfCodeExecutedAfterCheck;

TestRegistry* registry_;
Expand Down
2 changes: 1 addition & 1 deletion include/CppUTestExt/MemoryReportAllocator.h
Expand Up @@ -49,7 +49,7 @@ class MemoryReportAllocator : public TestMemoryAllocator
virtual TestMemoryAllocator* getRealAllocator();

virtual char* alloc_memory(size_t size, const char* file, size_t line) _override;
virtual void free_memory(char* memory, const char* file, size_t line) _override;
virtual void free_memory(char* memory, size_t size, const char* file, size_t line) _override;

virtual const char* name() const _override;
virtual const char* alloc_name() const _override;
Expand Down
2 changes: 1 addition & 1 deletion src/CppUTest/MemoryLeakDetector.cpp
Expand Up @@ -682,7 +682,7 @@ void MemoryLeakDetector::deallocMemory(TestMemoryAllocator* allocator, void* mem
#endif
if (!allocator->hasBeenDestroyed()) {
checkForCorruption(node, file, line, allocator, allocatNodesSeperately);
allocator->free_memory((char*) memory, file, line);
allocator->free_memory((char*) memory, node->size_, file, line);
}
}

Expand Down

0 comments on commit d289eaf

Please sign in to comment.