fork

Fork of cpputest by Rohit Grover

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TestMemoryAllocator.h Source File

TestMemoryAllocator.h

00001 /*
00002  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *     * Redistributions of source code must retain the above copyright
00008  *       notice, this list of conditions and the following disclaimer.
00009  *     * Redistributions in binary form must reproduce the above copyright
00010  *       notice, this list of conditions and the following disclaimer in the
00011  *       documentation and/or other materials provided with the distribution.
00012  *     * Neither the name of the <organization> nor the
00013  *       names of its contributors may be used to endorse or promote products
00014  *       derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
00017  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 #ifndef D_TestMemoryAllocator_h
00029 #define D_TestMemoryAllocator_h
00030 
00031 struct MemoryLeakNode;
00032 class TestMemoryAllocator;
00033 
00034 extern void setCurrentNewAllocator(TestMemoryAllocator* allocator);
00035 extern TestMemoryAllocator* getCurrentNewAllocator();
00036 extern void setCurrentNewAllocatorToDefault();
00037 extern TestMemoryAllocator* defaultNewAllocator();
00038 
00039 extern void setCurrentNewArrayAllocator(TestMemoryAllocator* allocator);
00040 extern TestMemoryAllocator* getCurrentNewArrayAllocator();
00041 extern void setCurrentNewArrayAllocatorToDefault();
00042 extern TestMemoryAllocator* defaultNewArrayAllocator();
00043 
00044 extern void setCurrentMallocAllocator(TestMemoryAllocator* allocator);
00045 extern TestMemoryAllocator* getCurrentMallocAllocator();
00046 extern void setCurrentMallocAllocatorToDefault();
00047 extern TestMemoryAllocator* defaultMallocAllocator();
00048 
00049 class TestMemoryAllocator
00050 {
00051 public:
00052     TestMemoryAllocator(const char* name_str = "generic", const char* alloc_name_str = "alloc", const char* free_name_str = "free");
00053     virtual ~TestMemoryAllocator();
00054     bool hasBeenDestroyed();
00055 
00056     virtual char* alloc_memory(size_t size, const char* file, int line);
00057     virtual void free_memory(char* memory, const char* file, int line);
00058 
00059     virtual const char* name();
00060     virtual const char* alloc_name();
00061     virtual const char* free_name();
00062 
00063     virtual bool isOfEqualType(TestMemoryAllocator* allocator);
00064 
00065     virtual char* allocMemoryLeakNode(size_t size);
00066     virtual void freeMemoryLeakNode(char* memory);
00067 
00068 protected:
00069 
00070     const char* name_;
00071     const char* alloc_name_;
00072     const char* free_name_;
00073 
00074     bool hasBeenDestroyed_;
00075 };
00076 
00077 class CrashOnAllocationAllocator : public TestMemoryAllocator
00078 {
00079     unsigned allocationToCrashOn_;
00080 public:
00081     CrashOnAllocationAllocator();
00082 
00083     virtual void setNumberToCrashOn(unsigned allocationToCrashOn);
00084 
00085     virtual char* alloc_memory(size_t size, const char* file, int line) _override;
00086 };
00087 
00088 
00089 class NullUnknownAllocator: public TestMemoryAllocator
00090 {
00091 public:
00092     NullUnknownAllocator();
00093     virtual char* alloc_memory(size_t size, const char* file, int line) _override;
00094     virtual void free_memory(char* memory, const char* file, int line) _override;
00095 
00096     static TestMemoryAllocator* defaultAllocator();
00097 };
00098 
00099 #endif
00100