]> git.ipfire.org Git - thirdparty/squid.git/blame - src/MemStore.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / MemStore.h
CommitLineData
bbc27441 1/*
4ac4a490 2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
bbc27441
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 */
8
9487bae9
AR
9#ifndef SQUID_MEMSTORE_H
10#define SQUID_MEMSTORE_H
11
68353d5a 12#include "ipc/mem/Page.h"
06684a9b 13#include "ipc/mem/PageStack.h"
68353d5a 14#include "ipc/StoreMap.h"
9487bae9 15#include "Store.h"
2745fea5 16#include "store/Controlled.h"
68353d5a
DK
17
18// StoreEntry restoration info not already stored by Ipc::StoreMap
1860fbac 19struct MemStoreMapExtraItem {
6d68a230 20 Ipc::Mem::PageId page; ///< shared memory page with entry slice content
68353d5a 21};
1860fbac
AR
22typedef Ipc::StoreMapItems<MemStoreMapExtraItem> MemStoreMapExtras;
23typedef Ipc::StoreMap MemStoreMap;
9487bae9 24
abf396ec
AR
25class ShmWriter;
26
9487bae9
AR
27/// Stores HTTP entities in RAM. Current implementation uses shared memory.
28/// Unlike a disk store (SwapDir), operations are synchronous (and fast).
2745fea5 29class MemStore: public Store::Controlled, public Ipc::StoreMapCleaner
9199139f 30{
9487bae9
AR
31public:
32 MemStore();
33 virtual ~MemStore();
34
96a7de88
DK
35 /// whether e should be kept in local RAM for possible future caching
36 bool keepInLocalMemory(const StoreEntry &e) const;
37
4475555f
AR
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
ce49546e 44 /// called when the entry is about to forget its association with mem cache
29c56e41 45 void disconnect(StoreEntry &e);
ce49546e 46
2745fea5
AR
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;
abf396ec 60 virtual void updateHeaders(StoreEntry *e) override;
2745fea5
AR
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;
1a210de4 66 virtual bool smpAware() const override { return true; }
9487bae9 67
68353d5a 68 static int64_t EntryLimit();
a4555399 69
9487bae9 70protected:
abf396ec
AR
71 friend ShmWriter;
72
97754f5a 73 bool shouldCache(StoreEntry &e) const;
4475555f 74 bool startCaching(StoreEntry &e);
9487bae9 75
4475555f 76 void copyToShm(StoreEntry &e);
abf396ec 77 void copyToShmSlice(StoreEntry &e, Ipc::StoreMapAnchor &anchor, Ipc::StoreMap::Slice &slice);
06684a9b 78 bool copyFromShm(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor);
ce49546e
AR
79 bool copyFromShmSlice(StoreEntry &e, const StoreIOBuffer &buf, bool eof);
80
abf396ec
AR
81 void updateHeadersOrThrow(Ipc::StoreMapUpdate &update);
82
ce49546e
AR
83 void anchorEntry(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor);
84 bool updateCollapsedWith(StoreEntry &collapsed, const sfileno index, const Ipc::StoreMapAnchor &anchor);
06684a9b 85
abf396ec
AR
86 Ipc::Mem::PageId pageForSlice(Ipc::StoreMapSliceId sliceId);
87 Ipc::StoreMap::Slice &nextAppendableSlice(const sfileno entryIndex, sfileno &sliceOffset);
06684a9b 88 sfileno reserveSapForWriting(Ipc::Mem::PageId &page);
9487bae9 89
7f6748c8 90 // Ipc::StoreMapCleaner API
a9b0467a 91 virtual void noteFreeMapSlice(const Ipc::StoreMapSliceId sliceId) override;
7f6748c8 92
9487bae9 93private:
06684a9b
AR
94 // TODO: move freeSlots into map
95 Ipc::Mem::Pointer<Ipc::Mem::PageStack> freeSlots; ///< unused map slot IDs
9487bae9 96 MemStoreMap *map; ///< index of mem-cached entries
06684a9b 97
1860fbac
AR
98 typedef MemStoreMapExtras Extras;
99 Ipc::Mem::Pointer<Extras> extras; ///< IDs of pages with slice data
100
06684a9b
AR
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
9d4e9cfb
AR
105 class SlotAndPage
106 {
06684a9b
AR
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
9487bae9
AR
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 */
f53969cc 123