1 // ANNA - Anna is Not 'N' Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // https://bitbucket.org/testillano/anna
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Google Inc. nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
40 #include <anna/core/core.hpp>
41 #include <anna/timex/MicroMeter.hpp>
45 static const int N = 10000;
47 void print_const (const Recycler<int>& numbers)
52 for (Recycler <int>::const_iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++) {
53 c = Recycler <int>::data (ii);
54 msg = anna::functions::asString ("Number 3 (p=0x%x): %d", c, *c);
55 Logger::debug (msg, ANNA_FILE_LOCATION);
59 void debug (const char* str, Recycler<int>::iterator ii)
62 int* p = Recycler<int>::data (ii);
66 msg += functions::asHexString (anna_ptrnumber_cast (p));
67 msg += functions::asText ("): ", *p);
68 Logger::debug (msg, ANNA_FILE_LOCATION);
71 void run (const bool randomAccess, const int freeCounter)
72 throw (RuntimeException)
74 Recycler <int> numbers (randomAccess);
80 for (i = 0; i < N; i ++) {
81 p = numbers.create ();
86 for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++)
87 debug ("Number 1", ii);
89 // Calcule cuantos objetos tiene que liberar
92 for (int nn = 0; nn < freeCounter; nn ++) {
93 number = rand () % (N * 2);
95 for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++) {
96 p = Recycler <int>::data (ii);
99 debug ("Release | ", ii);
106 for (i = 0; i < N; i ++) {
107 p = numbers.create ();
110 for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++)
111 debug ("Number 2", ii);
114 for (i = 0; i < N; i ++) {
115 p = numbers.create ();
116 *p = (10 + i) * 2000;
119 print_const (numbers);
122 void recycling (const bool randomAccess, const int freeCounter)
123 throw (RuntimeException)
125 Recycler <int> numbers (randomAccess);
133 for (i = 0; i < N; i ++) {
134 p = pp [i] = numbers.create ();
138 // How many objects must be released?
141 for (int nn = 0; nn < freeCounter; nn ++) {
142 number = rand () % N;
148 void reuse (const int freeCounter)
149 throw (RuntimeException)
158 for (i = 0; i < N; i ++) {
159 p = pp [i] = new int;
163 // How many objects must be released?
166 for (int nn = 0; nn < freeCounter; nn ++) {
167 number = rand () % N;
173 int main (int argc, const char** argv)
175 timex::MicroMeter meter;
178 Logger::setLevel (Logger::Debug);
179 Logger::initialize ("recycler", new TraceWriter ("file.trace", 2048000));
181 const int freeCounter = rand () % N;
183 Logger::debug (functions::asText ("Nodes to delete: ", freeCounter), ANNA_FILE_LOCATION);
185 cout << "Nodes to delete: " << freeCounter << endl << endl;
187 run (false, freeCounter);
188 cout << "run (false, freeCounter): " << meter.getDelay () << " us" << endl;
190 meter.setControlPoint ();
191 run (true, freeCounter);
192 cout << "run (true, freeCounter): " << meter.getDelay () << " us" << endl;
194 meter.setControlPoint ();
195 recycling (false, freeCounter);
196 cout << "recycling(false): " << meter.getDelay () << " us" << endl;
198 meter.setControlPoint ();
199 recycling (true, freeCounter);
200 cout << "recycling(true): " << meter.getDelay () << " us" << endl;
202 meter.setControlPoint ();
204 cout << "reuse(): " << meter.getDelay () << " us" << endl;
206 catch (Exception& ex) {
207 cout << ex.asString () << endl;
210 Recycler <string> rstr;
211 meter.setControlPoint ();