Github user jpeach commented on a diff in the pull request: https://github.com/apache/trafficserver/pull/653#discussion_r64970668 --- Diff: iocore/hostdb/P_RefCountCache.h --- @@ -0,0 +1,572 @@ +#ifndef _P_RefCountCache_h_ +#define _P_RefCountCache_h_ + +#include "I_EventSystem.h" + +#include <ts/Map.h> +#include <ts/List.h> + +#include <cstdint> +// TODO: no vector? +#include <vector> +#include <unistd.h> +#include <string.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <cmath> +#include <fstream> + +#include "ts/I_Version.h" + +#define REFCOUNT_CACHE_EVENT_SYNC REFCOUNT_CACHE_EVENT_EVENTS_START + +#define REFCOUNTCACHE_MAGIC_NUMBER 0x0BAD2D9 +#define REFCOUNTCACHE_MAJOR_VERSION 1 +#define REFCOUNTCACHE_MINOR_VERSION 0 + +// TODO: elsewhere? +#define PtrMutex Ptr<ProxyMutex> + +// In the cache we want to recount our items +// This class will handle memory allocations (on the freelist) and +// take care of implementing RefCountObj-- so that the items we want in +// the cache don't have to inherit from it (which makes serialization/deserialization much easier) +class RefCountCacheItemBase : public RefCountObj +{ +public: + RefCountCacheItemBase(uint64_t key = 0, unsigned int size = 0, char *iobuf = NULL, int iobuffer_index = 0) + { + this->key = key; + this->size = size; + this->iobuf = iobuf; + this->iobuffer_index = iobuffer_index; + } + void + free() + { + if (this->size > 0) { + // free the block + ioBufAllocator[iobuffer_index].free_void((void *)(iobuf)); + } + } + uint64_t key; // Key of this item + unsigned int size; // how much space does this guy get + int64_t iobuffer_index; + char *iobuf; +}; + +// Template to the particular class, so we can caste the item ptr correctly +template <class C> class RefCountCacheItem : public RefCountCacheItemBase +{ +public: + using RefCountCacheItemBase::RefCountCacheItemBase; + C *item(); +}; + +template <class C> +C * +RefCountCacheItem<C>::item() +{ + return (C *)this->iobuf; +} + +// Layer of inderection for the hashmap-- since it needs lots of things inside of it +class RefCountCacheHashingValue +{ +public: + Ptr<RefCountCacheItemBase> item; + LINK(RefCountCacheHashingValue, item_link); + RefCountCacheHashingValue(Ptr<RefCountCacheItemBase> i) : item(i){}; +}; + +// TODO: template? +struct RefCountCacheHashing { + typedef uint64_t ID; + typedef uint64_t const Key; + typedef RefCountCacheHashingValue Value; + typedef DList(RefCountCacheHashingValue, item_link) ListHead; + + static ID + hash(Key key) + { + return key; + } + static Key + key(Value const *value) + { + return value->item->key; + } + static bool + equal(Key lhs, Key rhs) + { + return lhs == rhs; + } +}; + +// The RefCountCachePartition is simply a map of key -> Ptr<RefCountCacheItem<YourClass>> +// TODO: add metrics +// TODO: store partition number for debugging +template <class C> class RefCountCachePartition +{ +public: + RefCountCachePartition(int maxSize, int maxItems); + RefCountCacheItem<C> *alloc(uint64_t key, int size = 0); + Ptr<RefCountCacheItem<C>> get(uint64_t key); + void del(uint64_t key); + void clear(); + + int numItems(); --- End diff -- The number of items you have is ``size``, the number of items you have current storage for is ``capacity``.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---