Mon, 18 Dec 2023 15:13:26 +0100
add cxBufferReset() - resolves #338
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
29 | #include "cx/mempool.h" |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #include "util_allocator.h" |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #include <gtest/gtest.h> |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
33 | TEST(Mempool, Create) { |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
34 | auto pool = cxBasicMempoolCreate(16); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
35 | ASSERT_EQ(pool->auto_destr, nullptr); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | ASSERT_NE(pool->allocator, nullptr); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | ASSERT_NE(pool->allocator->cl, nullptr); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | EXPECT_EQ(pool->allocator->data, pool); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | EXPECT_NE(pool->allocator->cl->malloc, nullptr); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | EXPECT_NE(pool->allocator->cl->calloc, nullptr); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | EXPECT_NE(pool->allocator->cl->realloc, nullptr); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | EXPECT_NE(pool->allocator->cl->free, nullptr); |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
43 | EXPECT_EQ(pool->capacity, 16); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
44 | EXPECT_EQ(pool->size, 0); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
45 | EXPECT_NE(pool->data, nullptr); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
46 | cxMempoolDestroy(pool); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | } |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
49 | TEST(Mempool, malloc) { |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
50 | auto pool = cxBasicMempoolCreate(4); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
53 | EXPECT_EQ(pool->size, 2); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
54 | EXPECT_EQ(pool->capacity, 4); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
55 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
56 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
57 | EXPECT_EQ(pool->size, 4); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
58 | EXPECT_EQ(pool->capacity, 4); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
61 | EXPECT_EQ(pool->size, 6); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
62 | EXPECT_GE(pool->capacity, 6); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
63 | cxMempoolDestroy(pool); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | } |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
66 | TEST(Mempool, calloc) { |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
67 | auto pool = cxBasicMempoolCreate(4); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int)); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | ASSERT_NE(test, nullptr); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | EXPECT_EQ(test[0], 0); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | EXPECT_EQ(test[1], 0); |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
73 | cxMempoolDestroy(pool); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | } |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
76 | static unsigned test_destructor_called; |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | static void test_destructor([[maybe_unused]] void *mem) { |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | test_destructor_called++; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | } |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
82 | TEST(Mempool, realloc) { |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
83 | auto pool = cxMempoolCreate(4, test_destructor); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
84 | ASSERT_EQ(pool->auto_destr, test_destructor); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | auto data = cxMalloc(pool->allocator, sizeof(int)); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | *((int *) data) = 13; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | void *rdata = data; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | unsigned n = 1; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | while (rdata == data) { |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | n <<= 1; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | ASSERT_LT(n, 65536); // eventually the memory should be moved elsewhere |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | } |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | EXPECT_EQ(*((int *) rdata), 13); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | // test if destructor is still intact |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | test_destructor_called = 0; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | cxFree(pool->allocator, rdata); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | EXPECT_EQ(test_destructor_called, 1); |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
101 | cxMempoolDestroy(pool); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | } |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
105 | TEST(Mempool, free) { |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
106 | auto pool = cxBasicMempoolCreate(4); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | void *mem1; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | void *mem2; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | mem1 = cxMalloc(pool->allocator, 16); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | cxFree(pool->allocator, mem1); |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
113 | EXPECT_EQ(pool->size, 0); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | cxMalloc(pool->allocator, 16); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | cxMalloc(pool->allocator, 16); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | mem1 = cxMalloc(pool->allocator, 16); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | cxMalloc(pool->allocator, 16); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | mem2 = cxMalloc(pool->allocator, 16); |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
121 | EXPECT_EQ(pool->size, 5); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | cxFree(pool->allocator, mem1); |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
123 | EXPECT_EQ(pool->size, 4); |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | cxFree(pool->allocator, mem2); |
727
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
125 | EXPECT_EQ(pool->size, 3); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
126 | cxMempoolDestroy(pool); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
127 | } |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
128 | |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
129 | TEST(Mempool, Destroy) { |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
130 | auto pool = cxBasicMempoolCreate(4); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
131 | auto data = cxMalloc(pool->allocator, sizeof(int)); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
132 | *((int *) data) = 13; |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
133 | cxMempoolSetDestructor(data, test_destructor); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
134 | EXPECT_EQ(*((int *) data), 13); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
135 | test_destructor_called = 0; |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
136 | cxFree(pool->allocator, data); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
137 | EXPECT_EQ(test_destructor_called, 1); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
138 | data = cxMalloc(pool->allocator, sizeof(int)); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
139 | cxMempoolSetDestructor(data, test_destructor); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
140 | cxMempoolDestroy(pool); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
141 | EXPECT_EQ(test_destructor_called, 2); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
142 | } |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
143 | |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
144 | TEST(Mempool, Register) { |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
145 | auto pool = cxBasicMempoolCreate(4); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
146 | auto data = cxMalloc(pool->allocator, sizeof(int)); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
147 | test_destructor_called = 0; |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
148 | cxMempoolSetDestructor(data, test_destructor); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
149 | int donotfree = 0; |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
150 | cxMempoolRegister(pool, &donotfree, test_destructor); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
151 | cxMempoolDestroy(pool); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
152 | EXPECT_EQ(test_destructor_called, 2); |
d92a59f5d261
improve mempool implementation
Mike Becker <universe@uap-core.de>
parents:
653
diff
changeset
|
153 | } |