Memory.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013-2014 by Wayne C. Gramlich. All rights reserved.
2 
3 #include <assert.h>
4 #include <stdlib.h>
5 #include "File.hpp"
6 #include "Memory.hpp"
7 
8 #if defined(MEMORY_LEAK_CHECK)
9  static File Memory__allocate_file = (File)0;
10  static File Memory__free_file = (File)0;
11  static Memory Memory__leak = (Memory)0;
12 #endif // defined(MEMORY_LEAK_CHECK)
13 
21 
22 Memory Memory__allocate(unsigned int bytes, String_Const from) {
23  Memory memory = (Memory)malloc(bytes);
24  assert (memory != (Memory)0);
25  #if defined(MEMORY_LEAK_CHECK)
26  // Make sure that the logging files are open:
27  if (Memory__allocate_file == (File)0) {
28  Memory__allocate_file = File__open("/tmp/memory_allocate.log","w");
29  Memory__free_file = File__open("/tmp/memory_free.log","w");
30  assert (Memory__allocate_file != (File)0);
31  assert (Memory__free_file != (File)0);
32  }
33  File__format(Memory__allocate_file, "0x%08x %s\n", memory, from);
34  File__flush(Memory__allocate_file);
35 
36  // Now check for a memory leak match:
37  if (memory == Memory__leak) {
38  Memory__leak_found(memory);
39  }
40  #endif // defined(MEMORY_LEAK_CHECK)
41  return memory;
42 }
43 
44 #if defined(MEMORY_LEAK_CHECK)
45  void Memory__leak_check(Memory memory) {
46  Memory__leak = memory;
47  }
48 
49  void Memory__leak_found(Memory memory) {
50  // Plant break point here:
51  memory = memory;
52  }
53 #endif // defined(MEMORY_LEAK_CHECK)
54 
59 
60 void Memory__free(Memory memory) {
61  #if defined(MEMORY_LEAK_CHECK)
62  File__format(Memory__free_file, "0x%08x\n", memory);
63  File__flush(Memory__free_file);
64  #else
65  free(memory);
66  #endif // defined(MEMORY_LEAK_CHECK)
67 }
Memory Memory__allocate(unsigned int bytes, String_Const from)
Allocates bytes of memory and returns a pointer to it.
Definition: Memory.cpp:22
FILE * File
FILE is a file I/O object.
Definition: File.hpp:12
void File__flush(File file)
Flushes file content out of internal buffers.
Definition: File.cpp:166
void File__format(File file, String_Const format,...)
will write format out to file with all patterns that start with "%" replaced by formatted versions of...
Definition: File.cpp:107
void Memory__free(Memory memory)
Releases the storage associated with memory.
Definition: Memory.cpp:60
void * Memory
Memory is a pointer to memory.
Definition: Memory.hpp:27
File File__open(String_Const file_name, String_Const flags)
will open file_name using flags to specify read/write options.
Definition: File.cpp:243
const char * String_Const
Definition: String.hpp:9


fiducial_lib
Author(s): Wayne Gramlich
autogenerated on Thu Dec 28 2017 04:06:53