Fawkes API  Fawkes Development Version
qa_bb_memmgr.cpp
1 
2 /***************************************************************************
3  * memory_manager.h - BlackBoard memory manager QA
4  *
5  * Generated: Thu Oct 05 16:09:25 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 /// @cond QA
25 
26 #include <blackboard/bbconfig.h>
27 #include <blackboard/exceptions.h>
28 #include <blackboard/internal/memory_manager.h>
29 #include <core/exceptions/system.h>
30 
31 #include <cstdio>
32 #include <cstdlib>
33 #include <iostream>
34 #include <signal.h>
35 #include <vector>
36 
37 using namespace std;
38 using namespace fawkes;
39 
40 bool quit = false;
41 
42 void
43 signal_handler(int signum)
44 {
45  quit = true;
46 }
47 
48 #define NUM_CHUNKS 5
49 #define BLACKBOARD_MEMORY_SIZE 2 * 1024 * 1024
50 
51 int
52 main(int argc, char **argv)
53 {
54  signal(SIGINT, signal_handler);
55 
56  // BlackBoardMemoryManager *mm = new BlackBoardMemoryManager( BLACKBOARD_MEMORY_SIZE,
57  // BLACKBOARD_VERSION,
58  // "FawkesBBMemMgrQA" /* token */ );
59  BlackBoardMemoryManager *mm = new BlackBoardMemoryManager(BLACKBOARD_MEMORY_SIZE);
60 
61  void *m[NUM_CHUNKS];
62 
63  cout << "Running basic tests" << endl;
64  cout << "=========================================================================" << endl;
65 
67 
68  unsigned int free_before = mm->max_free_size();
69 
70  for (unsigned int i = 0; i < NUM_CHUNKS; ++i) {
71  cout << "Allocating m[" << i << "] with " << (i + 1) * 1000 << " bytes.." << flush;
72  m[i] = mm->alloc((i + 1) * 1000);
73  cout << "done" << endl;
74  }
75 
76  if (mm->max_allocated_size() != (NUM_CHUNKS * 1000)) {
77  cout << "Largest chunk is not " << NUM_CHUNKS * 1000 << " bytes, error, aborting" << endl;
78  delete mm;
79  exit(1);
80  }
81 
82  cout << "Free chunks:" << endl;
84  cout << "Allocated chunks:" << endl;
87 
88  for (unsigned int i = 0; i < NUM_CHUNKS; ++i) {
89  cout << "Freeing m[" << i << "].." << flush;
90  mm->free(m[i]);
91  cout << "done" << endl;
92  }
93 
94  if (mm->max_allocated_size() != 0) {
95  cout << "Largest chunk is not 0 bytes, error, aborting" << endl;
96  delete mm;
97  exit(2);
98  }
99 
100  if (mm->max_free_size() != free_before) {
101  cout << "Max free size after tests differe from before test, error, aborting" << endl;
102  delete mm;
103  exit(3);
104  }
105 
106  cout << "Free chunks:" << endl;
108  cout << "Allocated chunks:" << endl;
111 
112  cout << "Basic tests finished" << endl;
113  cout << "=========================================================================" << endl;
114 
115  cout << endl << "Running gremlin tests, press Ctrl-C to stop" << endl;
116  cout << "=========================================================================" << endl;
117 
118  std::vector<void *> ptrs;
119  ptrs.clear();
120 
121  unsigned int modcount = 0;
122  while (!quit) {
123  if (rand() < RAND_MAX / 2) {
124  cout << "a" << flush;
125  // alloc
126  unsigned int s = (rand() % BLACKBOARD_MEMORY_SIZE) / 1000;
127  if (s < 20) {
128  // min 20 bytes
129  s = 20;
130  }
131  void *m;
132  try {
133  m = mm->alloc(s);
134  ptrs.push_back(m);
135  } catch (OutOfMemoryException &e) {
136  cout << "Memory Manager ran out of memory, tried to allocate " << s
137  << " bytes, detailed info:" << endl;
138  cout << "Free chunks:" << endl;
140  cout << "Allocated chunks:" << endl;
143  }
144  } else {
145  cout << "f" << flush;
146  // free
147  if (ptrs.size() > 0) {
148  // there is something to delete
149  unsigned int erase = rand() % ptrs.size();
150  try {
151  mm->free(ptrs[erase]);
152  ptrs.erase(ptrs.begin() + erase);
154  cout << "Ouch, tried to free invalid pointer" << endl;
155  cout << "Allocated chunks:" << endl;
157  printf("Pointer tried to free: 0x%lx\n", (long unsigned int)ptrs[erase]);
158  }
159  }
160  }
161 
162  try {
163  mm->check();
164  } catch (BBInconsistentMemoryException &e) {
165  cout << "Inconsistent memory found, printing exception trace" << endl;
166  e.print_trace();
167  cout << "Free chunks:" << endl;
169  cout << "Allocated chunks:" << endl;
172  quit = true;
173  }
174 
175  if (modcount % 10 == 0) {
176  cout << endl;
178  if (mm->overhang_size() > 0) {
179  cout << "Overhang detected, allocated chunks:" << endl;
181  }
182  // sleep(10);
183  }
184  ++modcount;
185  usleep(0);
186  }
187 
188  delete mm;
189 }
190 
191 /// @endcond
Thrown when BlackBoard memory has been corupted This exception is thrown by the memory manager if the...
Definition: exceptions.h:50
A NULL pointer was supplied where not allowed.
Definition: exceptions.h:36
BlackBoard memory manager.
unsigned int overhang_size() const
Get number of overhanging bytes.
void * alloc(unsigned int num_bytes)
Allocate memory.
void print_free_chunks_info() const
Print out info about free chunks.
void print_allocated_chunks_info() const
Print out info about allocated chunks.
void free(void *chunk_ptr)
Free a memory chunk.
unsigned int max_free_size() const
Get maximum allocatable memory size.
void check()
Check memory consistency.
void print_performance_info() const
Prints out performance info.
unsigned int max_allocated_size() const
Get maximum alloced memory size.
void print_trace() noexcept
Prints trace to stderr.
Definition: exception.cpp:601
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32
Fawkes library namespace.