]>
Commit | Line | Data |
---|---|---|
0545caaa | 1 | /* |
bf95c10a | 2 | * Copyright (C) 1996-2022 The Squid Software Foundation and contributors |
0545caaa AJ |
3 | * |
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. | |
7 | */ | |
8cfaefed HN |
8 | |
9 | /* | |
2be7332c | 10 | * AUTHOR: Alex Rousskov, Andres Kroonmaa, Robert Collins, Henrik Nordstrom |
8cfaefed HN |
11 | */ |
12 | ||
f7f3304a | 13 | #include "squid.h" |
ed6e9fb9 | 14 | #include "mem/PoolMalloc.h" |
a7508376 | 15 | #include "mem/Stats.h" |
8cfaefed | 16 | |
074d6a40 AJ |
17 | #include <cassert> |
18 | #include <cstring> | |
8cfaefed | 19 | |
8cfaefed HN |
20 | extern time_t squid_curtime; |
21 | ||
22 | void * | |
23 | MemPoolMalloc::allocate() | |
24 | { | |
aee3523a | 25 | void *obj = nullptr; |
3aa53107 FC |
26 | if (!freelist.empty()) { |
27 | obj = freelist.top(); | |
28 | freelist.pop(); | |
29 | } | |
2be7332c | 30 | if (obj) { |
9663db1c | 31 | --meter.idle; |
742a021b | 32 | ++saved_calls; |
2be7332c | 33 | } else { |
3b08e399 AJ |
34 | if (doZero) |
35 | obj = xcalloc(1, obj_size); | |
36 | else | |
37 | obj = xmalloc(obj_size); | |
9663db1c | 38 | ++meter.alloc; |
2be7332c | 39 | } |
9663db1c | 40 | ++meter.inuse; |
2be7332c | 41 | return obj; |
8cfaefed HN |
42 | } |
43 | ||
44 | void | |
2be7332c | 45 | MemPoolMalloc::deallocate(void *obj, bool aggressive) |
8cfaefed | 46 | { |
9663db1c | 47 | --meter.inuse; |
2be7332c | 48 | if (aggressive) { |
3b32112a | 49 | xfree(obj); |
9663db1c | 50 | --meter.alloc; |
2be7332c | 51 | } else { |
3b08e399 | 52 | if (doZero) |
3b32112a | 53 | memset(obj, 0, obj_size); |
9663db1c | 54 | ++meter.idle; |
3aa53107 | 55 | freelist.push(obj); |
2be7332c | 56 | } |
8cfaefed HN |
57 | } |
58 | ||
59 | /* TODO extract common logic to MemAllocate */ | |
a7508376 AJ |
60 | size_t |
61 | MemPoolMalloc::getStats(Mem::PoolStats &stats) | |
8cfaefed | 62 | { |
a7508376 AJ |
63 | stats.pool = this; |
64 | stats.label = objectType(); | |
65 | stats.meter = &meter; | |
66 | stats.obj_size = obj_size; | |
67 | stats.chunk_capacity = 0; | |
68 | ||
69 | stats.chunks_alloc += 0; | |
70 | stats.chunks_inuse += 0; | |
71 | stats.chunks_partial += 0; | |
72 | stats.chunks_free += 0; | |
73 | ||
74 | stats.items_alloc += meter.alloc.currentLevel(); | |
75 | stats.items_inuse += meter.inuse.currentLevel(); | |
76 | stats.items_idle += meter.idle.currentLevel(); | |
77 | ||
78 | stats.overhead += sizeof(MemPoolMalloc) + strlen(objectType()) + 1; | |
8cfaefed | 79 | |
9663db1c | 80 | return meter.inuse.currentLevel(); |
8cfaefed HN |
81 | } |
82 | ||
83 | int | |
84 | MemPoolMalloc::getInUseCount() | |
85 | { | |
9663db1c | 86 | return meter.inuse.currentLevel(); |
8cfaefed HN |
87 | } |
88 | ||
89 | MemPoolMalloc::MemPoolMalloc(char const *aLabel, size_t aSize) : MemImplementingAllocator(aLabel, aSize) | |
90 | { | |
91 | } | |
92 | ||
2be7332c HN |
93 | MemPoolMalloc::~MemPoolMalloc() |
94 | { | |
9663db1c | 95 | assert(meter.inuse.currentLevel() == 0); |
2be7332c HN |
96 | clean(0); |
97 | } | |
98 | ||
8cfaefed HN |
99 | bool |
100 | MemPoolMalloc::idleTrigger(int shift) const | |
101 | { | |
4c9eadc2 | 102 | return freelist.size() >> (shift ? 8 : 0); |
8cfaefed HN |
103 | } |
104 | ||
105 | void | |
ced8def3 | 106 | MemPoolMalloc::clean(time_t) |
8cfaefed | 107 | { |
3aa53107 FC |
108 | while (!freelist.empty()) { |
109 | void *obj = freelist.top(); | |
110 | freelist.pop(); | |
9663db1c AJ |
111 | --meter.idle; |
112 | --meter.alloc; | |
3b32112a | 113 | xfree(obj); |
2be7332c | 114 | } |
8cfaefed HN |
115 | } |
116 |