Memory.cpp
Go to the documentation of this file.
00001 // Copyright (c) 2013-2014 by Wayne C. Gramlich.  All rights reserved.
00002 
00003 #include <assert.h>
00004 #include <stdlib.h>
00005 #include "File.hpp"
00006 #include "Memory.hpp"
00007 
00008 #if defined(MEMORY_LEAK_CHECK)
00009     static File Memory__allocate_file = (File)0;
00010     static File Memory__free_file = (File)0;
00011     static Memory Memory__leak = (Memory)0;
00012 #endif // defined(MEMORY_LEAK_CHECK)
00013 
00021 
00022 Memory Memory__allocate(unsigned int bytes, String_Const from) {
00023     Memory memory = (Memory)malloc(bytes);
00024     assert (memory != (Memory)0);
00025     #if defined(MEMORY_LEAK_CHECK)
00026         // Make sure that the logging files are open:
00027         if (Memory__allocate_file == (File)0) {
00028             Memory__allocate_file = File__open("/tmp/memory_allocate.log","w");
00029             Memory__free_file = File__open("/tmp/memory_free.log","w");
00030             assert (Memory__allocate_file != (File)0);
00031             assert (Memory__free_file != (File)0);
00032         }
00033         File__format(Memory__allocate_file, "0x%08x %s\n", memory, from);
00034         File__flush(Memory__allocate_file);
00035 
00036         // Now check for a memory leak match:
00037         if (memory == Memory__leak) {
00038             Memory__leak_found(memory);
00039         }
00040     #endif // defined(MEMORY_LEAK_CHECK)
00041     return memory;
00042 }
00043 
00044 #if defined(MEMORY_LEAK_CHECK)
00045     void Memory__leak_check(Memory memory) {
00046         Memory__leak = memory;
00047     }
00048 
00049     void Memory__leak_found(Memory memory) {
00050         // Plant break point here:
00051         memory = memory;
00052     }
00053 #endif // defined(MEMORY_LEAK_CHECK)
00054 
00059 
00060 void Memory__free(Memory memory) {
00061     #if defined(MEMORY_LEAK_CHECK)
00062         File__format(Memory__free_file, "0x%08x\n", memory);
00063         File__flush(Memory__free_file);
00064     #else
00065         free(memory);
00066     #endif // defined(MEMORY_LEAK_CHECK)
00067 }


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Jun 6 2019 18:08:04