2003-10-03 03:34:14 +02:00
|
|
|
#ifndef JRD_BLOCK_CACHE_H
|
|
|
|
#define JRD_BLOCK_CACHE_H
|
2001-12-24 03:51:06 +01:00
|
|
|
|
2003-01-16 18:47:10 +01:00
|
|
|
#include "../common/classes/alloc.h"
|
2001-12-24 03:51:06 +01:00
|
|
|
#include "../jrd/smp_impl.h"
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class BlockCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlockCache(MemoryPool& p) : pool(p), head(0) {}
|
|
|
|
~BlockCache();
|
|
|
|
|
|
|
|
T* newBlock();
|
|
|
|
void returnBlock(T*) ;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Node
|
|
|
|
{
|
|
|
|
Node* next;
|
|
|
|
};
|
|
|
|
MemoryPool& pool;
|
|
|
|
Node* head;
|
|
|
|
V4Mutex lock;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2003-02-25 01:05:06 +01:00
|
|
|
inline T* BlockCache<T>::newBlock()
|
2001-12-24 03:51:06 +01:00
|
|
|
{
|
2005-04-02 05:10:33 +02:00
|
|
|
lock.acquire();
|
2001-12-24 03:51:06 +01:00
|
|
|
if (head)
|
|
|
|
{
|
|
|
|
T* result = reinterpret_cast<T*>(head);
|
|
|
|
head = head->next;
|
|
|
|
lock.release();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
lock.release();
|
2002-09-25 19:12:16 +02:00
|
|
|
return FB_NEW(pool) T;
|
2001-12-24 03:51:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2003-02-25 01:05:06 +01:00
|
|
|
inline void BlockCache<T>::returnBlock(T* back)
|
2001-12-24 03:51:06 +01:00
|
|
|
{
|
|
|
|
Node* returned = reinterpret_cast<Node*>(back);
|
2005-04-02 05:10:33 +02:00
|
|
|
lock.acquire();
|
2001-12-24 03:51:06 +01:00
|
|
|
returned->next = head;
|
|
|
|
head = returned;
|
|
|
|
lock.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
BlockCache<T>::~BlockCache()
|
|
|
|
{
|
|
|
|
// Notice there is no destructor? This is because all our memory
|
|
|
|
// is allocated from a pool. When the pool gets freed so will our
|
|
|
|
// storage, simple as that. No need to waste extra processor time
|
|
|
|
// freeing memory just to have it freed again!
|
|
|
|
/* Node *next;
|
2004-02-02 12:02:12 +01:00
|
|
|
while (head)
|
2001-12-24 03:51:06 +01:00
|
|
|
{
|
|
|
|
next = head->next;
|
|
|
|
delete ((T*)head);
|
|
|
|
head = next;
|
|
|
|
} */
|
|
|
|
}
|
|
|
|
|
2003-10-03 03:34:14 +02:00
|
|
|
#endif // JRD_BLOCK_CACHE_H
|
|
|
|
|