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 
25 /// @cond QA
26 
27 #include <blackboard/internal/memory_manager.h>
28 #include <blackboard/exceptions.h>
29 #include <blackboard/bbconfig.h>
30 
31 #include <core/exceptions/system.h>
32 
33 #include <signal.h>
34 #include <cstdlib>
35 #include <cstdio>
36 
37 #include <iostream>
38 #include <vector>
39 
40 using namespace std;
41 using namespace fawkes;
42 
43 
44 bool quit = false;
45 
46 void
47 signal_handler(int signum)
48 {
49  quit = true;
50 }
51 
52 
53 #define NUM_CHUNKS 5
54 #define BLACKBOARD_MEMORY_SIZE 2 * 1024 * 1024
55 
56 int
57 main(int argc, char **argv)
58 {
59 
60  signal(SIGINT, signal_handler);
61 
62  // BlackBoardMemoryManager *mm = new BlackBoardMemoryManager( BLACKBOARD_MEMORY_SIZE,
63  // BLACKBOARD_VERSION,
64  // "FawkesBBMemMgrQA" /* token */ );
65  BlackBoardMemoryManager *mm = new BlackBoardMemoryManager(BLACKBOARD_MEMORY_SIZE);
66 
67  void *m[NUM_CHUNKS];
68 
69  cout << "Running basic tests" << endl;
70  cout << "=========================================================================" << endl;
71 
73 
74  unsigned int free_before = mm->max_free_size();
75 
76  for (unsigned int i = 0; i < NUM_CHUNKS; ++i) {
77  cout << "Allocating m[" << i << "] with " << (i+1) * 1000 << " bytes.." << flush;
78  m[i] = mm->alloc( (i+1) * 1000 );
79  cout << "done" << endl;
80  }
81 
82  if ( mm->max_allocated_size() != (NUM_CHUNKS * 1000) ) {
83  cout << "Largest chunk is not " << NUM_CHUNKS * 1000 << " bytes, error, aborting" << endl;
84  delete mm;
85  exit(1);
86  }
87 
88  cout << "Free chunks:" << endl;
90  cout << "Allocated chunks:" << endl;
93 
94  for (unsigned int i = 0; i < NUM_CHUNKS; ++i) {
95  cout << "Freeing m[" << i << "].." << flush;
96  mm->free( m[i] );
97  cout << "done" << endl;
98  }
99 
100  if ( mm->max_allocated_size() != 0 ) {
101  cout << "Largest chunk is not 0 bytes, error, aborting" << endl;
102  delete mm;
103  exit(2);
104  }
105 
106  if ( mm->max_free_size() != free_before ) {
107  cout << "Max free size after tests differe from before test, error, aborting" << endl;
108  delete mm;
109  exit(3);
110  }
111 
112  cout << "Free chunks:" << endl;
114  cout << "Allocated chunks:" << endl;
117 
118  cout << "Basic tests finished" << endl;
119  cout << "=========================================================================" << endl;
120 
121  cout << endl << "Running gremlin tests, press Ctrl-C to stop" << endl;
122  cout << "=========================================================================" << endl;
123 
124  std::vector< void * > ptrs;
125  ptrs.clear();
126 
127  unsigned int modcount = 0;
128  while ( ! quit ) {
129  if (rand() < RAND_MAX / 2) {
130  cout << "a" << flush;
131  // alloc
132  unsigned int s = (rand() % BLACKBOARD_MEMORY_SIZE) / 1000;
133  if ( s < 20 ) {
134  // min 20 bytes
135  s = 20;
136  }
137  void *m;
138  try {
139  m = mm->alloc(s);
140  ptrs.push_back( m );
141  } catch ( OutOfMemoryException &e ) {
142  cout << "Memory Manager ran out of memory, tried to allocate "
143  << s << " bytes, detailed info:" << endl;
144  cout << "Free chunks:" << endl;
146  cout << "Allocated chunks:" << endl;
149  }
150  } else {
151  cout << "f" << flush;
152  // free
153  if ( ptrs.size() > 0 ) {
154  // there is something to delete
155  unsigned int erase = rand() % ptrs.size();
156  try {
157  mm->free( ptrs[erase] );
158  ptrs.erase( ptrs.begin() + erase );
160  cout << "Ouch, tried to free invalid pointer" << endl;
161  cout << "Allocated chunks:" << endl;
163  printf("Pointer tried to free: 0x%lx\n", (long unsigned int)ptrs[erase]);
164  }
165  }
166  }
167 
168  try {
169  mm->check();
170  } catch ( BBInconsistentMemoryException &e ) {
171  cout << "Inconsistent memory found, printing exception trace" << endl;
172  e.print_trace();
173  cout << "Free chunks:" << endl;
175  cout << "Allocated chunks:" << endl;
178  quit = true;
179  }
180 
181  if ( modcount % 10 == 0 ) {
182  cout << endl;
184  if ( mm->overhang_size() > 0 ) {
185  cout << "Overhang detected, allocated chunks:" << endl;
187  }
188  // sleep(10);
189  }
190  ++modcount;
191  usleep(0);
192  }
193 
194  delete mm;
195 }
196 
197 
198 /// @endcond
void print_performance_info() const
Prints out performance info.
Thrown when BlackBoard memory has been corupted This exception is thrown by the memory manager if the...
Definition: exceptions.h:47
void print_allocated_chunks_info() const
Print out info about allocated chunks.
Fawkes library namespace.
BlackBoard memory manager.
STL namespace.
void * alloc(unsigned int num_bytes)
Allocate memory.
A NULL pointer was supplied where not allowed.
Definition: exceptions.h:35
void print_free_chunks_info() const
Print out info about free chunks.
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
void check()
Check memory consistency.
unsigned int max_allocated_size() const
Get maximum alloced memory size.
void free(void *chunk_ptr)
Free a memory chunk.
unsigned int max_free_size() const
Get maximum allocatable memory size.
System ran out of memory and desired operation could not be fulfilled.
Definition: system.h:32
unsigned int overhang_size() const
Get number of overhanging bytes.