2009-09-26 23:00:55 +02:00
|
|
|
/*
|
|
|
|
|
TYPED-COUNTER.hpp - maintain a set of type based contexts
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2009-09-27 02:46:26 +02:00
|
|
|
/** @file typed-counter.hpp
|
|
|
|
|
** Creating sets of type-based contexts.
|
|
|
|
|
** The idea is to get a "slot" for any given type, so we can build
|
|
|
|
|
** tables or families of implementations based on these types. Currently,
|
|
|
|
|
** the slot allocation is based on static variables and thus is global.
|
|
|
|
|
** This leads to a waste of slots, as the various clients of this service
|
|
|
|
|
** typically will utilise different sets of types.
|
|
|
|
|
** @todo WIP WIP... this is the first, preliminary version of a facility,
|
|
|
|
|
** which is expected to get quite important for custom allocation management.
|
2009-09-27 17:29:05 +02:00
|
|
|
**
|
2009-09-27 02:46:26 +02:00
|
|
|
** @see typed-counter-test.cpp
|
|
|
|
|
** @see TypedAllocationManager
|
|
|
|
|
** @see AllocationCluster (custom allocation scheme using a similar idea inline)
|
2009-09-26 23:00:55 +02:00
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LIB_TYPED_COUNTER_H
|
|
|
|
|
#define LIB_TYPED_COUNTER_H
|
|
|
|
|
|
|
|
|
|
//#include "pre.hpp"
|
|
|
|
|
#include "lib/error.hpp"
|
2009-09-27 17:29:05 +02:00
|
|
|
#include "lib/sync-classlock.hpp"
|
2009-09-26 23:00:55 +02:00
|
|
|
|
2009-09-27 17:29:05 +02:00
|
|
|
#include <vector>
|
2009-09-26 23:00:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
2009-09-27 17:29:05 +02:00
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Providing type-IDs for a specific context.
|
|
|
|
|
* This facility allows to access a numeric ID for each
|
|
|
|
|
* provided distinct type. Type-IDs may be used e.g. for
|
|
|
|
|
* dispatcher tables or for custom allocators.
|
|
|
|
|
* The type-IDs generated here are not completely global though.
|
|
|
|
|
* Rather, they are tied to a specific type context, e.g. a class
|
|
|
|
|
* implementing a custom allocator.
|
|
|
|
|
*/
|
|
|
|
|
template<class CX>
|
|
|
|
|
class TypedContext
|
|
|
|
|
{
|
|
|
|
|
static size_t lastGeneratedTypeID;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static size_t
|
|
|
|
|
newTypeID (size_t& typeID)
|
|
|
|
|
{
|
|
|
|
|
ClassLock<TypedContext> synchronised();
|
|
|
|
|
if (!typeID)
|
|
|
|
|
typeID = ++lastGeneratedTypeID;
|
|
|
|
|
return typeID;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** type-ID */
|
|
|
|
|
template<typename TY>
|
|
|
|
|
class ID
|
|
|
|
|
{
|
|
|
|
|
static size_t typeID;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static size_t
|
|
|
|
|
get()
|
|
|
|
|
{
|
|
|
|
|
if (typeID)
|
|
|
|
|
return typeID;
|
|
|
|
|
else
|
|
|
|
|
return newTypeID(typeID);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** storage for the type-ID generation mechanism */
|
|
|
|
|
template<class CX>
|
|
|
|
|
size_t TypedContext<CX>::lastGeneratedTypeID (0);
|
|
|
|
|
|
|
|
|
|
/** table holding all the generated type-IDs */
|
|
|
|
|
template<class CX>
|
|
|
|
|
template<typename TY>
|
|
|
|
|
size_t TypedContext<CX>::ID<TY>::typeID (0);
|
2009-09-26 23:00:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-27 02:46:26 +02:00
|
|
|
/**
|
2009-09-27 17:29:05 +02:00
|
|
|
* Helper providing a set of counters, each tied to a specific type.
|
2009-09-27 02:46:26 +02:00
|
|
|
*/
|
2009-09-26 23:00:55 +02:00
|
|
|
class TypedCounter
|
2009-09-27 17:29:05 +02:00
|
|
|
: public Sync<>
|
2009-09-26 23:00:55 +02:00
|
|
|
{
|
2009-09-27 17:29:05 +02:00
|
|
|
mutable vector<long> counters_;
|
|
|
|
|
|
|
|
|
|
template<typename TY>
|
|
|
|
|
size_t
|
|
|
|
|
slot() const
|
|
|
|
|
{
|
|
|
|
|
size_t typeID = TypedContext<TypedCounter>::ID<TY>::get();
|
|
|
|
|
if (size() < typeID)
|
|
|
|
|
counters_.resize (typeID);
|
|
|
|
|
|
|
|
|
|
ENSURE (counters_.capacity() >= typeID);
|
|
|
|
|
return (typeID - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-09-27 02:46:26 +02:00
|
|
|
public:
|
2009-09-27 17:29:05 +02:00
|
|
|
TypedCounter()
|
|
|
|
|
{
|
|
|
|
|
counters_.reserve(5); // pre-allocated 5 slots
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-09-27 02:46:26 +02:00
|
|
|
template<class X>
|
2009-09-27 17:29:05 +02:00
|
|
|
long
|
2009-09-27 02:46:26 +02:00
|
|
|
get() const
|
|
|
|
|
{
|
2009-09-27 17:29:05 +02:00
|
|
|
Lock sync(this);
|
|
|
|
|
return counters_[slot<X>()];
|
2009-09-27 02:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class X>
|
2009-09-27 17:29:05 +02:00
|
|
|
long
|
2009-09-27 02:46:26 +02:00
|
|
|
inc()
|
|
|
|
|
{
|
2009-09-27 17:29:05 +02:00
|
|
|
Lock sync(this);
|
|
|
|
|
return ++counters_[slot<X>()];
|
2009-09-27 02:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class X>
|
2009-09-27 17:29:05 +02:00
|
|
|
long
|
2009-09-27 02:46:26 +02:00
|
|
|
dec()
|
|
|
|
|
{
|
2009-09-27 17:29:05 +02:00
|
|
|
Lock sync(this);
|
|
|
|
|
return --counters_[slot<X>()];
|
2009-09-27 02:46:26 +02:00
|
|
|
}
|
2009-09-27 17:29:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* == diagnostics == */
|
|
|
|
|
|
|
|
|
|
size_t size() const { return counters_.size(); }
|
|
|
|
|
bool empty() const { return counters_.empty();}
|
2009-09-26 23:00:55 +02:00
|
|
|
};
|
|
|
|
|
|
2009-09-27 02:46:26 +02:00
|
|
|
|
|
|
|
|
|
2009-09-26 23:00:55 +02:00
|
|
|
} // namespace lib
|
|
|
|
|
#endif
|