summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/kernel/mem-test.cpp
blob: 2072da9f689216c636b714201fdd06543149a437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

/**
 * Test memory allocation functions.
 */

#include <assert.h>
#include "stream.hpp"
#include "string.hpp"
#include "gc.hpp"
#include "function.hpp"
#include "perf.hpp"

namespace tuscany {

int countElements = 0;
int maxElements = 0;

class Element {
public:
    Element() : i(0) {
        countElements++;
        if (countElements > maxElements)
            maxElements = countElements;
    }

    Element(const int i) : i(i) {
        countElements++;
        if (countElements > maxElements)
            maxElements = countElements;
    }

    Element(const Element& o) : i(o.i) {
        countElements++;
        if (countElements > maxElements)
            maxElements = countElements;
    }

    ~Element() {
        countElements--;
    }

    const bool operator==(const Element& o) const {
        return o.i == i;
    }

private:
    friend ostream& operator<<(ostream& out, const Element& v);

    const int i;
    char c[20];
};

ostream& operator<<(ostream& out, const Element& v) {
    out << v.i ;
    return out;
}

const bool poolAlloc(Element** const p, const int count) {
    if (count == 0)
        return true;
    p[count - 1] = new (gc_new<Element>()) Element();
    return poolAlloc(p, count - 1);
};

bool poolFree(Element** const p, const int count) {
    if (count == 0)
        return true;
    // Do nothing to free the element, but cycle through them just
    // to get a fair comparison with the other memory alloc tests
    return poolFree(p, count - 1);
};

const bool testPoolAllocPerf() {
    const int count = 10000;
    Element** const elements = new Element*[count];
    const blambda pl = [elements, count]() -> const bool {
        const gc_scoped_pool gc;
        poolAlloc(elements, count);
        return true;
    };
    maxElements = 0;
    cout << "Memory pool alloc test " << (time(pl, 1, 1) / count) << " ms" << endl;
    assert(countElements == 0);
    assert(maxElements == count);
    return true;
}

const bool stdAlloc(Element** const p, const int count) {
    if (count == 0)
        return true;
    p[count - 1] = new Element();
    return stdAlloc(p, count - 1);
};

const bool stdFree(Element** const p, const int count) {
    if (count == 0)
        return true;
    delete p[count -1];
    return stdFree(p, count - 1);
};

const bool testStdAllocPerf() {
    const int count = 10000;
    Element** const elements = new Element*[count];
    const blambda sl = [elements, count]() -> const bool {
        stdAlloc(elements, count);
        stdFree(elements, count);
        return true;
    };
    maxElements = 0;
    cout << "Memory standard alloc test " << (time(sl, 1, 1) / count) << " ms" << endl;
    assert(countElements == 0);
    assert(maxElements == count);
    return true;
}

}

int main() {
    const tuscany::gc_scoped_pool p;
    tuscany::cout << "Testing..." << tuscany::endl;

    tuscany::testPoolAllocPerf();
    tuscany::testStdAllocPerf();

    tuscany::cout << "OK" << tuscany::endl;

    return 0;
}