Skip to content

Commit

Permalink
Merge 879e277 into 5174a45
Browse files Browse the repository at this point in the history
  • Loading branch information
basvodde committed May 1, 2020
2 parents 5174a45 + 879e277 commit 0814878
Show file tree
Hide file tree
Showing 14 changed files with 824 additions and 46 deletions.
106 changes: 104 additions & 2 deletions include/CppUTest/TestMemoryAllocator.h
Expand Up @@ -46,6 +46,19 @@ extern TestMemoryAllocator* getCurrentMallocAllocator();
extern void setCurrentMallocAllocatorToDefault();
extern TestMemoryAllocator* defaultMallocAllocator();

class GlobalMemoryAllocatorStash
{
public:
GlobalMemoryAllocatorStash();
void save();
void restore();

private:
TestMemoryAllocator* originalMallocAllocator;
TestMemoryAllocator* originalNewAllocator;
TestMemoryAllocator* originalNewArrayAllocator;
};

class TestMemoryAllocator
{
public:
Expand All @@ -65,6 +78,8 @@ class TestMemoryAllocator
virtual char* allocMemoryLeakNode(size_t size);
virtual void freeMemoryLeakNode(char* memory);

virtual TestMemoryAllocator* actualAllocator();

protected:

const char* name_;
Expand All @@ -79,6 +94,7 @@ class CrashOnAllocationAllocator : public TestMemoryAllocator
unsigned allocationToCrashOn_;
public:
CrashOnAllocationAllocator();
virtual ~CrashOnAllocationAllocator() _destructor_override;

virtual void setNumberToCrashOn(unsigned allocationToCrashOn);

Expand All @@ -90,6 +106,8 @@ class NullUnknownAllocator: public TestMemoryAllocator
{
public:
NullUnknownAllocator();
virtual ~NullUnknownAllocator() _destructor_override;

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

Expand All @@ -102,9 +120,10 @@ class FailableMemoryAllocator: public TestMemoryAllocator
{
public:
FailableMemoryAllocator(const char* name_str = "failable alloc", const char* alloc_name_str = "alloc", const char* free_name_str = "free");
virtual ~FailableMemoryAllocator() _destructor_override;

virtual char* alloc_memory(size_t size, const char* file, int line);
virtual char* allocMemoryLeakNode(size_t size);
virtual char* alloc_memory(size_t size, const char* file, int line) _override;
virtual char* allocMemoryLeakNode(size_t size) _override;

virtual void failAllocNumber(int number);
virtual void failNthAllocAt(int allocationNumber, const char* file, int line);
Expand All @@ -118,5 +137,88 @@ class FailableMemoryAllocator: public TestMemoryAllocator
int currentAllocNumber_;
};

struct MemoryAccountantAllocationNode;

class MemoryAccountant
{
public:
MemoryAccountant();

void clear();

void alloc(size_t size);
void dealloc(size_t size);

size_t totalAllocationsOfSize(size_t size) const;
size_t totalDeallocationsOfSize(size_t size) const;
size_t maximumAllocationAtATimeOfSize(size_t size) const;

size_t totalAllocations() const;
size_t totalDeallocations() const;

SimpleString report() const;

void setAllocator(TestMemoryAllocator* allocator);
private:
MemoryAccountantAllocationNode* findOrCreateNodeOfSize(size_t size);
MemoryAccountantAllocationNode* findNodeOfSize(size_t size) const;

MemoryAccountantAllocationNode* createNewAccountantAllocationNode(size_t size, MemoryAccountantAllocationNode* next);
void destroyAccountantAllocationNode(MemoryAccountantAllocationNode* node);

MemoryAccountantAllocationNode* head_;
TestMemoryAllocator* allocator_;
};

struct AccountingTestMemoryAllocatorMemoryNode;

class AccountingTestMemoryAllocator : public TestMemoryAllocator
{
public:
AccountingTestMemoryAllocator(MemoryAccountant& accountant, TestMemoryAllocator* originalAllocator);
virtual ~AccountingTestMemoryAllocator() _destructor_override;

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

virtual TestMemoryAllocator* actualAllocator() _override;
TestMemoryAllocator* originalAllocator();
private:

void addMemoryToMemoryTrackingToKeepTrackOfSize(char* memory, size_t size);
size_t removeMemoryFromTrackingAndReturnAllocatedSize(char* memory);

size_t removeNextNodeAndReturnSize(AccountingTestMemoryAllocatorMemoryNode* node);
size_t removeHeadAndReturnSize();

MemoryAccountant& accountant_;
TestMemoryAllocator* originalAllocator_;
AccountingTestMemoryAllocatorMemoryNode* head_;
};

class GlobalMemoryAccountant
{
public:
GlobalMemoryAccountant();
~GlobalMemoryAccountant();

void start();
void stop();
SimpleString report();

TestMemoryAllocator* getMallocAllocator();
TestMemoryAllocator* getNewAllocator();
TestMemoryAllocator* getNewArrayAllocator();

private:

void restoreMemoryAllocators();

MemoryAccountant accountant_;
AccountingTestMemoryAllocator* mallocAllocator_;
AccountingTestMemoryAllocator* newAllocator_;
AccountingTestMemoryAllocator* newArrayAllocator_;
};

#endif

2 changes: 1 addition & 1 deletion include/CppUTest/Utest.h
Expand Up @@ -201,7 +201,7 @@ class ExecFunction
ExecFunction();
virtual ~ExecFunction();

virtual void exec();
virtual void exec()=0;
};

class ExecFunctionWithoutParameters : public ExecFunction
Expand Down
5 changes: 4 additions & 1 deletion scripts/travis_ci_build.sh
Expand Up @@ -115,12 +115,15 @@ if [ "x$BUILD" = "xdocker_dos" ]; then
fi

if [ "x$BUILD" = "xmake_dos" ]; then
git clone https://github.com/cpputest/watcom-compiler.git watcom
if [ ! -d watcom ]; then
git clone https://github.com/cpputest/watcom-compiler.git watcom
fi
export PATH=$PATH:$PWD/watcom/binl
export WATCOM=$PWD/watcom
export CC=wcl
export CXX=wcl
$CC --version
make -f $CPPUTEST_HOME/platforms/Dos/Makefile clean
make -f $CPPUTEST_HOME/platforms/Dos/Makefile
$CPPUTEST_HOME/platforms/Dos/alltests.sh
fi
Expand Down
6 changes: 3 additions & 3 deletions src/CppUTest/MemoryLeakDetector.cpp
Expand Up @@ -611,10 +611,10 @@ bool MemoryLeakDetector::matchingAllocation(TestMemoryAllocator *alloc_allocator

void MemoryLeakDetector::checkForCorruption(MemoryLeakDetectorNode* node, const char* file, int line, TestMemoryAllocator* allocator, bool allocateNodesSeperately)
{
if (!matchingAllocation(node->allocator_, allocator))
outputBuffer_.reportAllocationDeallocationMismatchFailure(node, file, line, allocator, reporter_);
if (!matchingAllocation(node->allocator_->actualAllocator(), allocator->actualAllocator()))
outputBuffer_.reportAllocationDeallocationMismatchFailure(node, file, line, allocator->actualAllocator(), reporter_);
else if (!validMemoryCorruptionInformation(node->memory_ + node->size_))
outputBuffer_.reportMemoryCorruptionFailure(node, file, line, allocator, reporter_);
outputBuffer_.reportMemoryCorruptionFailure(node, file, line, allocator->actualAllocator(), reporter_);
else if (allocateNodesSeperately)
allocator->freeMemoryLeakNode((char*) node);
}
Expand Down
7 changes: 6 additions & 1 deletion src/CppUTest/TestHarness_c.cpp
Expand Up @@ -133,15 +133,20 @@ int cpputest_malloc_get_count()
return malloc_count;
}

static TestMemoryAllocator* originalAllocator = NULLPTR;

void cpputest_malloc_set_out_of_memory()
{
if (originalAllocator == NULLPTR)
originalAllocator = getCurrentMallocAllocator();
setCurrentMallocAllocator(NullUnknownAllocator::defaultAllocator());
}

void cpputest_malloc_set_not_out_of_memory()
{
malloc_out_of_memory_counter = NO_COUNTDOWN;
setCurrentMallocAllocatorToDefault();
setCurrentMallocAllocator(originalAllocator);
originalAllocator = NULLPTR;
}

void cpputest_malloc_set_out_of_memory_countdown(int count)
Expand Down

0 comments on commit 0814878

Please sign in to comment.