]> git.ipfire.org Git - thirdparty/squid.git/blob - src/mem/PoolChunked.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / mem / PoolChunked.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
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 */
8
9 #ifndef _MEM_POOL_CHUNKED_H_
10 #define _MEM_POOL_CHUNKED_H_
11
12 #include "mem/Pool.h"
13
14 #define MEM_CHUNK_SIZE 4 * 4096 /* 16KB ... 4 * VM_PAGE_SZ */
15 #define MEM_CHUNK_MAX_SIZE 256 * 1024 /* 2MB */
16
17 class MemChunk;
18
19 /// \ingroup MemPoolsAPI
20 class MemPoolChunked : public MemImplementingAllocator
21 {
22 public:
23 friend class MemChunk;
24 MemPoolChunked(const char *label, size_t obj_size);
25 ~MemPoolChunked();
26 void convertFreeCacheToChunkFreeCache();
27 virtual void clean(time_t maxage);
28
29 /**
30 \param stats Object to be filled with statistical data about pool.
31 \retval Number of objects in use, ie. allocated.
32 */
33 virtual int getStats(MemPoolStats * stats, int accumulate);
34
35 void createChunk();
36 void *get();
37 void push(void *obj);
38 virtual int getInUseCount();
39 protected:
40 virtual void *allocate();
41 virtual void deallocate(void *, bool aggressive);
42 public:
43 /**
44 * Allows you tune chunk size of pooling. Objects are allocated in chunks
45 * instead of individually. This conserves memory, reduces fragmentation.
46 * Because of that memory can be freed also only in chunks. Therefore
47 * there is tradeoff between memory conservation due to chunking and free
48 * memory fragmentation.
49 *
50 \note As a general guideline, increase chunk size only for pools that keep
51 * very many items for relatively long time.
52 */
53 virtual void setChunkSize(size_t chunksize);
54
55 virtual bool idleTrigger(int shift) const;
56
57 size_t chunk_size;
58 int chunk_capacity;
59 int chunkCount;
60 void *freeCache;
61 MemChunk *nextFreeChunk;
62 MemChunk *Chunks;
63 Splay<MemChunk *> allChunks;
64 };
65
66 /// \ingroup MemPoolsAPI
67 class MemChunk
68 {
69 public:
70 MemChunk(MemPoolChunked *pool);
71 ~MemChunk();
72 void *freeList;
73 void *objCache;
74 int inuse_count;
75 MemChunk *nextFreeChunk;
76 MemChunk *next;
77 time_t lastref;
78 MemPoolChunked *pool;
79 };
80
81 #endif /* _MEM_POOL_CHUNKED_H_ */
82