]>
Commit | Line | Data |
---|---|---|
697079bb | 1 | /* |
4ac4a490 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
697079bb | 3 | * |
4e0938ef AJ |
4 | * Squid software is distributed under GPLv2+ license and includes |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
697079bb | 7 | */ |
8 | ||
582c2af2 | 9 | #include "squid.h" |
ed07aba1 | 10 | |
fbc04c0a | 11 | #if USE_MEMPOOLS |
ed07aba1 | 12 | |
697079bb | 13 | #include "MemPool.h" |
27e059d4 | 14 | |
697079bb | 15 | #include <iostream> |
16 | ||
17 | /* TODO: put this in a libTest */ | |
18 | void | |
19 | xassert(const char *msg, const char *file, int line) | |
20 | { | |
21 | std::cout << "Assertion failed: (" << msg << ") at " << file << ":" << line << std::endl; | |
22 | exit (1); | |
23 | } | |
697079bb | 24 | |
26ac0430 AJ |
25 | class MemPoolTest |
26 | { | |
697079bb | 27 | public: |
26ac0430 | 28 | void run(); |
697079bb | 29 | private: |
26ac0430 AJ |
30 | class SomethingToAlloc |
31 | { | |
32 | public: | |
33 | int aValue; | |
34 | }; | |
35 | static MemAllocator *Pool; | |
697079bb | 36 | }; |
04eb0689 | 37 | MemAllocator *MemPoolTest::Pool = NULL; |
697079bb | 38 | |
39 | void | |
40 | MemPoolTest::run() | |
41 | { | |
42 | assert (Pool == NULL); | |
04eb0689 | 43 | Pool = memPoolCreate("Test Pool", sizeof(SomethingToAlloc)); |
697079bb | 44 | assert (Pool); |
b001e822 | 45 | SomethingToAlloc *something = static_cast<SomethingToAlloc *>(Pool->alloc()); |
697079bb | 46 | assert (something); |
47 | assert (something->aValue == 0); | |
48 | something->aValue = 5; | |
dc47f531 | 49 | Pool->freeOne(something); |
b001e822 | 50 | SomethingToAlloc *otherthing = static_cast<SomethingToAlloc *>(Pool->alloc()); |
697079bb | 51 | assert (otherthing == something); |
52 | assert (otherthing->aValue == 0); | |
dc47f531 | 53 | Pool->freeOne(otherthing); |
b001e822 | 54 | delete Pool; |
697079bb | 55 | } |
56 | ||
fbc04c0a | 57 | #endif /* USE_MEMPOOLS */ |
ed07aba1 | 58 | |
697079bb | 59 | int |
b610642c | 60 | main (int argc, char **argv) |
697079bb | 61 | { |
fbc04c0a | 62 | #if USE_MEMPOOLS |
697079bb | 63 | MemPoolTest aTest; |
64 | aTest.run(); | |
ed07aba1 | 65 | #endif |
697079bb | 66 | return 0; |
67 | } | |
68 |