]> git.ipfire.org Git - thirdparty/squid.git/blob - src/MemStore.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / MemStore.h
1 /*
2 * Copyright (C) 1996-2017 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 SQUID_MEMSTORE_H
10 #define SQUID_MEMSTORE_H
11
12 #include "ipc/mem/Page.h"
13 #include "ipc/mem/PageStack.h"
14 #include "ipc/StoreMap.h"
15 #include "Store.h"
16 #include "store/Controlled.h"
17
18 // StoreEntry restoration info not already stored by Ipc::StoreMap
19 struct MemStoreMapExtraItem {
20 Ipc::Mem::PageId page; ///< shared memory page with entry slice content
21 };
22 typedef Ipc::StoreMapItems<MemStoreMapExtraItem> MemStoreMapExtras;
23 typedef Ipc::StoreMap MemStoreMap;
24
25 class ShmWriter;
26
27 /// Stores HTTP entities in RAM. Current implementation uses shared memory.
28 /// Unlike a disk store (SwapDir), operations are synchronous (and fast).
29 class MemStore: public Store::Controlled, public Ipc::StoreMapCleaner
30 {
31 public:
32 MemStore();
33 virtual ~MemStore();
34
35 /// whether e should be kept in local RAM for possible future caching
36 bool keepInLocalMemory(const StoreEntry &e) const;
37
38 /// copy non-shared entry data of the being-cached entry to our cache
39 void write(StoreEntry &e);
40
41 /// all data has been received; there will be no more write() calls
42 void completeWriting(StoreEntry &e);
43
44 /// called when the entry is about to forget its association with mem cache
45 void disconnect(StoreEntry &e);
46
47 /* Storage API */
48 virtual void create() override {}
49 virtual void init() override;
50 virtual StoreEntry *get(const cache_key *) override;
51 virtual uint64_t maxSize() const override;
52 virtual uint64_t minSize() const override;
53 virtual uint64_t currentSize() const override;
54 virtual uint64_t currentCount() const override;
55 virtual int64_t maxObjectSize() const override;
56 virtual void getStats(StoreInfoStats &stats) const override;
57 virtual void stat(StoreEntry &e) const override;
58 virtual void reference(StoreEntry &e) override;
59 virtual bool dereference(StoreEntry &e) override;
60 virtual void updateHeaders(StoreEntry *e) override;
61 virtual void maintain() override;
62 virtual bool anchorCollapsed(StoreEntry &e, bool &inSync) override;
63 virtual bool updateCollapsed(StoreEntry &e) override;
64 virtual void markForUnlink(StoreEntry &) override;
65 virtual void unlink(StoreEntry &e) override;
66 virtual bool smpAware() const override { return true; }
67
68 static int64_t EntryLimit();
69
70 protected:
71 friend ShmWriter;
72
73 bool shouldCache(StoreEntry &e) const;
74 bool startCaching(StoreEntry &e);
75
76 void copyToShm(StoreEntry &e);
77 void copyToShmSlice(StoreEntry &e, Ipc::StoreMapAnchor &anchor, Ipc::StoreMap::Slice &slice);
78 bool copyFromShm(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor);
79 bool copyFromShmSlice(StoreEntry &e, const StoreIOBuffer &buf, bool eof);
80
81 void updateHeadersOrThrow(Ipc::StoreMapUpdate &update);
82
83 void anchorEntry(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor);
84 bool updateCollapsedWith(StoreEntry &collapsed, const sfileno index, const Ipc::StoreMapAnchor &anchor);
85
86 Ipc::Mem::PageId pageForSlice(Ipc::StoreMapSliceId sliceId);
87 Ipc::StoreMap::Slice &nextAppendableSlice(const sfileno entryIndex, sfileno &sliceOffset);
88 sfileno reserveSapForWriting(Ipc::Mem::PageId &page);
89
90 // Ipc::StoreMapCleaner API
91 virtual void noteFreeMapSlice(const Ipc::StoreMapSliceId sliceId) override;
92
93 private:
94 // TODO: move freeSlots into map
95 Ipc::Mem::Pointer<Ipc::Mem::PageStack> freeSlots; ///< unused map slot IDs
96 MemStoreMap *map; ///< index of mem-cached entries
97
98 typedef MemStoreMapExtras Extras;
99 Ipc::Mem::Pointer<Extras> extras; ///< IDs of pages with slice data
100
101 /// the last allocate slice for writing a store entry (during copyToShm)
102 sfileno lastWritingSlice;
103
104 /// temporary storage for slot and page ID pointers; for the waiting cache
105 class SlotAndPage
106 {
107 public:
108 SlotAndPage(): slot(NULL), page(NULL) {}
109 bool operator !() const { return !slot && !page; }
110 Ipc::Mem::PageId *slot; ///< local slot variable, waiting to be filled
111 Ipc::Mem::PageId *page; ///< local page variable, waiting to be filled
112 };
113 SlotAndPage waitingFor; ///< a cache for a single "hot" free slot and page
114 };
115
116 // Why use Store as a base? MemStore and SwapDir are both "caches".
117
118 // Why not just use a SwapDir API? That would not help much because Store has
119 // to check/update memory cache separately from the disk cache. And same API
120 // would hurt because we can support synchronous get/put, unlike the disks.
121
122 #endif /* SQUID_MEMSTORE_H */
123