blob: dddc26199cf2f1c0eed0d4e857f5c45efdb45dd7 [file] [log] [blame]
Alexandre Lisionddd731e2014-01-31 11:50:08 -05001// Copyright (C) 2010 David Sugar, Tycho Softworks.
2//
3// This file is part of GNU uCommon C++.
4//
5// GNU uCommon C++ is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published
7// by the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// GNU uCommon C++ is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17
18#ifndef DEBUG
19#define DEBUG
20#endif
21
22#include <ucommon/ucommon.h>
23
24#include <stdio.h>
25
26using namespace UCOMMON_NAMESPACE;
27
28static unsigned reused = 0;
29
30class myobject : public ReusableObject
31{
32public:
33 unsigned count;
34
35 myobject()
36 {count = ++reused;};
37};
38
39static mempager pool;
40static paged_reuse<myobject> myobjects(&pool, 100);
41static queueof<myobject> mycache(&pool, 10);
42
43extern "C" int main()
44{
45 unsigned i;
46
47 myobject *x;
48 for(i = 0; i < 10; ++i) {
49 x = myobjects.create();
50 mycache.post(x);
51 }
52 assert(x->count == 10);
53
54 for(i = 0; i < 3; ++i) {
55 x = mycache.lifo();
56 assert(x != NULL);
57 }
58 assert(x->count == 8);
59
60 init<myobject>(x);
61 assert(x->count == 11);
62
63 for(i = 0; i < 3; ++i) {
64 x = mycache.lifo();
65 assert(x != NULL);
66 myobjects.release(x);
67 }
68
69 x = init<myobject>(NULL);
70 assert(x == NULL);
71 assert(reused == 11);
72 return 0;
73}
74