d32b0dde51f5809361fd1a4f0c6f390a2c2f56fd
[anna.git] / example / core / recycler / main.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // http://redmine.teslayout.com/projects/anna-suite
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     *  Neither the name of the copyright holder 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <iostream>
38 #include <functional>
39
40 #include <anna/core/core.hpp>
41 #include <anna/timex/MicroMeter.hpp>
42
43 using namespace std;
44
45 static const int N = 10000;
46
47 void print_const (const Recycler<int>& numbers)
48 {
49    string msg;
50
51    const int* c;
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);
56    }
57 }
58
59 void debug (const char* str, Recycler<int>::iterator ii)
60    throw ()
61 {
62    int* p = Recycler<int>::data (ii);
63
64    string msg (str);
65    msg += " (p=";
66    msg += functions::asHexString (anna_ptrnumber_cast (p));
67    msg += functions::asText ("): ", *p);
68    Logger::debug (msg, ANNA_FILE_LOCATION);
69 }
70
71 void run (const bool randomAccess, const int freeCounter) 
72    throw (RuntimeException)
73 {
74    Recycler <int> numbers (randomAccess);
75    int* p;
76    int i;
77    string msg;
78
79    // Crear N elementos
80    for (i = 0; i < N; i ++) {
81       p = numbers.create ();
82       *p = i;
83    }
84
85    // Los recorre
86    for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++) 
87       debug ("Number 1", ii);
88
89    // Calcule cuantos objetos tiene que liberar
90    int number;
91    
92    for (int nn = 0; nn < freeCounter; nn ++) {
93       number = rand () % (N * 2);
94
95       for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++) {
96          p = Recycler <int>::data (ii);
97
98          if (*p == number) {
99             debug ("Release | ", ii);
100             numbers.release (p);
101             break;
102          }
103       }
104    }
105       
106    for (i = 0; i < N; i ++) {
107       p = numbers.create ();
108       *p = (1 + i) * 1000;
109    }
110    for (Recycler <int>::iterator ii = numbers.begin (), maxii = numbers.end (); ii != maxii; ii ++) 
111       debug ("Number 2", ii);
112
113    numbers.clear ();
114    for (i = 0; i < N; i ++) {
115       p = numbers.create ();
116       *p = (10 + i) * 2000;
117    }
118
119    print_const (numbers);
120 }
121
122 void recycling (const bool randomAccess, const int freeCounter)
123    throw (RuntimeException)
124 {
125    Recycler <int> numbers (randomAccess);
126    int** pp;
127    int* p;
128    int i;
129  
130    pp = new int* [N];
131
132    // Creating N items 
133    for (i = 0; i < N; i ++) {
134       p = pp [i] = numbers.create ();
135       *p = i;
136    }
137
138    // How many objects must be released? 
139    int number;
140
141    for (int nn = 0; nn < freeCounter; nn ++) {
142       number = rand () % N;
143       p = pp [number];
144       numbers.release (p);
145    }
146 }
147
148 void reuse (const int freeCounter)
149    throw (RuntimeException)
150 {
151    int** pp;
152    int* p;
153    int i;
154  
155    pp = new int* [N];
156
157    // Creating N items 
158    for (i = 0; i < N; i ++) {
159       p = pp [i] = new int;
160       *p = i;
161    }
162
163    // How many objects must be released? 
164    int number;
165
166    for (int nn = 0; nn < freeCounter; nn ++) {
167       number = rand () % N;
168       p = pp [number];
169       delete p;
170       pp [number] = NULL;
171    }
172 }
173 int main (int argc, const char** argv)
174 {
175    timex::MicroMeter meter;
176
177    try {
178       Logger::setLevel (Logger::Debug); 
179       Logger::initialize ("recycler", new TraceWriter ("file.trace", 2048000));
180  
181       const int freeCounter = rand () % N;
182    
183       Logger::debug (functions::asText ("Nodes to delete: ", freeCounter), ANNA_FILE_LOCATION);
184
185       cout << "Nodes to delete: " << freeCounter << endl << endl;
186
187       run (false, freeCounter);
188       cout << "run (false, freeCounter): " << meter.getDelay () << " us" << endl;
189
190       meter.setControlPoint ();
191       run (true, freeCounter);
192       cout <<  "run (true, freeCounter): " << meter.getDelay () << " us" << endl;
193
194       meter.setControlPoint ();
195       recycling (false, freeCounter);
196       cout << "recycling(false): " << meter.getDelay () << " us" << endl;
197
198       meter.setControlPoint ();
199       recycling (true, freeCounter);
200       cout <<  "recycling(true): " << meter.getDelay () << " us" << endl;
201
202       meter.setControlPoint ();
203       reuse (freeCounter);
204       cout <<  "reuse(): " << meter.getDelay () << " us" << endl;
205    }
206    catch (Exception& ex) {
207       cout << ex.asString () << endl;
208    }
209
210    Recycler <string> rstr;
211    meter.setControlPoint ();
212    
213    
214    return 0;
215 }
216