flcontainers.h
Go to the documentation of this file.
/*
flext - C++ layer for Max and Pure Data externals
Copyright (c) 2001-2015 Thomas Grill (gr@grrrr.org)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.
*/
#ifndef __FLCONTAINERS_H
#define __FLCONTAINERS_H
#include "flprefix.h"
#include "lockfree/stack.hpp"
#include "lockfree/fifo.hpp"
#include "flpushns.h"
class LifoCell: public lockfree::stack_node {};
class Lifo
: public lockfree::intrusive_stack<LifoCell>
{
public:
inline void Push(LifoCell *cell) { this->push(cell); }
inline LifoCell *Pop() { return this->pop(); }
inline bool Avail() const { return !this->empty(); }
};
template <typename T>
class TypedLifo
: public Lifo
{
public:
inline void Push(T *c) { Lifo::Push(static_cast<T *>(c)); }
inline T *Pop() { return static_cast<T *>(Lifo::Pop()); }
};
template <typename T>
class ValueLifoCell
: public LifoCell
{
public:
ValueLifoCell(T v): value(v) {}
T value;
};
template <typename T>
class ValueLifo
: public TypedLifo<ValueLifoCell<T> >
{
public:
inline void Push(T v)
{
TypedLifo<ValueLifoCell<T> >::Push(new ValueLifoCell<T>(v));
}
inline T Pop()
{
ValueLifoCell<T> *p = TypedLifo<ValueLifoCell<T> >::Pop();
T v = p->value;
delete p;
return v;
}
};
template <typename T,int M = 2,int O = 1>
class PooledLifo
: public TypedLifo<T>
{
public:
PooledLifo(): sz(0),resz(0) {}
void Push(T *c) { TypedLifo<T>::Push(c); ++sz; }
T *Pop() { T *r = TypedLifo<T>::Pop(); if(r) --sz; return r; }
T *New()
{
T *n = reuse.Pop();
if(n) {
--resz;
return n;
}
else
return new T;
}
inline void Free(T *p)
{
if(resz < sz*M+O) { reuse.Push(p); ++resz; }
else delete p;
}
private:
TypedLifo<T> reuse;
size_t sz,resz;
};
class FifoCell: public lockfree::fifo_node {};
class Fifo
: public lockfree::intrusive_fifo<FifoCell>
{
public:
inline void Put(FifoCell *cl) { this->enqueue(cl); }
inline FifoCell *Get() { return this->dequeue(); }
inline bool Avail() const { return !this->empty(); }
};
template <typename T>
class TypedFifo
: public Fifo
{
public:
inline void Put(T *c) { Fifo::Put(static_cast<T *>(c)); }
inline T *Get() { return static_cast<T *>(Fifo::Get()); }
};
template <typename T>
class ValueFifoCell
: public FifoCell
{
public:
ValueFifoCell(T v): value(v) {}
T value;
};
template <typename T>
class ValueFifo
: public TypedFifo<ValueFifoCell<T> >
{
public:
inline void Put(T v)
{
TypedFifo<ValueFifoCell<T> >::Put(new ValueFifoCell<T>(v));
}
inline T Get()
{
ValueFifoCell<T> *p = TypedFifo<ValueFifoCell<T> >::Get();
T v = p->value;
delete p;
return v;
}
};
template <typename T,int M = 2,int O = 1>
class PooledFifo
: public TypedFifo<T>
{
public:
~PooledFifo() { T *n; while((n = reuse.Get()) != NULL) delete n; }
inline T *New() { T *n = reuse.Get(); return n?n:new T; }
inline void Free(T *p) { if(resz < sz*M+O) reuse.Put(p); else delete p; }
private:
TypedFifo<T> reuse;
size_t sz,resz;
};
#include "flpopns.h"
#endif
TypedFifo::Put
void Put(T *c)
Definition: flcontainers.h:120
ValueLifo::Pop
T Pop()
Definition: flcontainers.h:62
PooledLifo::sz
size_t sz
Definition: flcontainers.h:99
TypedLifo::Pop
T * Pop()
Definition: flcontainers.h:40
PooledFifo::New
T * New()
Definition: flcontainers.h:161
PooledLifo::reuse
TypedLifo< T > reuse
Definition: flcontainers.h:98
PooledLifo::Pop
T * Pop()
Definition: flcontainers.h:79
ValueLifoCell::value
T value
Definition: flcontainers.h:49
PooledLifo::Free
void Free(T *p)
Definition: flcontainers.h:92
ValueFifoCell::ValueFifoCell
ValueFifoCell(T v)
Definition: flcontainers.h:130
PooledFifo::~PooledFifo
~PooledFifo()
Definition: flcontainers.h:159
Fifo::Get
FifoCell * Get()
Definition: flcontainers.h:110
TypedFifo::Get
T * Get()
Definition: flcontainers.h:121
Fifo
Definition: flcontainers.h:105
ValueLifoCell::ValueLifoCell
ValueLifoCell(T v)
Definition: flcontainers.h:48
PooledFifo::reuse
TypedFifo< T > reuse
Definition: flcontainers.h:164
PooledFifo::Free
void Free(T *p)
Definition: flcontainers.h:162
flprefix.h
Try to find out the platform.
Lifo::Push
void Push(LifoCell *cell)
Definition: flcontainers.h:29
TypedLifo
Definition: flcontainers.h:35
Fifo::Avail
bool Avail() const
Definition: flcontainers.h:111
ValueLifo::Push
void Push(T v)
Definition: flcontainers.h:57
ValueFifoCell::value
T value
Definition: flcontainers.h:131
ValueFifo::Put
void Put(T v)
Definition: flcontainers.h:139
flpushns.h
ValueLifoCell
Definition: flcontainers.h:44
FifoCell
Definition: flcontainers.h:103
flpopns.h
Lifo
Definition: flcontainers.h:25
PooledFifo
Definition: flcontainers.h:155
ValueLifo
Definition: flcontainers.h:53
PooledLifo::Push
void Push(T *c)
Definition: flcontainers.h:78
ValueFifo
Definition: flcontainers.h:135
PooledLifo::PooledLifo
PooledLifo()
Definition: flcontainers.h:76
TypedFifo
Definition: flcontainers.h:116
ValueFifo::Get
T Get()
Definition: flcontainers.h:144
PooledLifo::New
T * New()
Definition: flcontainers.h:81
Lifo::Avail
bool Avail() const
Definition: flcontainers.h:31
LifoCell
Definition: flcontainers.h:23
Fifo::Put
void Put(FifoCell *cl)
Definition: flcontainers.h:109
Lifo::Pop
LifoCell * Pop()
Definition: flcontainers.h:30
TypedLifo::Push
void Push(T *c)
Definition: flcontainers.h:39
PooledLifo
Definition: flcontainers.h:72
PooledFifo::sz
size_t sz
Definition: flcontainers.h:165
ValueFifoCell
Definition: flcontainers.h:126