2002-08-27 00:14:54 +04:00
|
|
|
#include "pageManager.h"
|
|
|
|
|
2002-09-05 07:26:02 +04:00
|
|
|
#include "vmHeaderBlock.h"
|
|
|
|
|
|
|
|
extern vmHeaderBlock *vmBlock;
|
2002-08-27 00:14:54 +04:00
|
|
|
/* This is the template
|
|
|
|
* replace TYPE with the type you need a pool for
|
|
|
|
*
|
|
|
|
class poolTYPE
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
list unused;
|
|
|
|
sem_id inUse;
|
|
|
|
public:
|
2002-11-19 07:05:16 +03:00
|
|
|
poolTYPE(void) { inUse = create_sem(1,"TYPEpool");
|
2002-08-27 00:14:54 +04:00
|
|
|
}
|
2002-11-19 07:05:16 +03:00
|
|
|
TYPE *get(void) {
|
2002-08-27 00:14:54 +04:00
|
|
|
TYPE *ret=NULL;
|
2002-11-19 07:05:16 +03:00
|
|
|
if (unused.count()) {
|
2002-09-16 07:14:44 +04:00
|
|
|
error ("poolTYPE::get: Getting an unused one!\n");
|
2002-08-27 00:14:54 +04:00
|
|
|
acquire_sem(inUse);
|
|
|
|
ret=(TYPE *)unused.next();
|
|
|
|
release_sem(inUse);
|
|
|
|
}
|
2002-11-19 07:05:16 +03:00
|
|
|
if (ret) {
|
2002-09-16 07:14:44 +04:00
|
|
|
error ("poolTYPE::get: Returning address:%x \n",ret);
|
2002-08-27 00:14:54 +04:00
|
|
|
return ret;
|
|
|
|
}
|
2002-11-19 07:05:16 +03:00
|
|
|
else {
|
2002-09-16 07:14:44 +04:00
|
|
|
error ("poolTYPE::get: Getting a new page!\n");
|
2002-09-05 07:26:02 +04:00
|
|
|
page *newPage=vmBlock->pageMan->getPage();
|
2002-08-27 00:14:54 +04:00
|
|
|
if (!newPage)
|
|
|
|
throw ("Out of pages to allocate a pool!");
|
|
|
|
int newCount=PAGE_SIZE/sizeof(TYPE);
|
|
|
|
acquire_sem(inUse);
|
2002-09-16 07:14:44 +04:00
|
|
|
error ("poolTYPE::get: Adding %d new elements to the pool!\n",newCount);
|
2002-08-27 00:14:54 +04:00
|
|
|
for (int i=0;i<newCount;i++)
|
|
|
|
unused.add(((void *)(newPage->getAddress()+(i*sizeof(TYPE)))));
|
|
|
|
release_sem(inUse);
|
|
|
|
return (get()); // A little cheat - call self again to get the first one from stack...
|
|
|
|
}
|
|
|
|
}
|
2002-11-19 07:05:16 +03:00
|
|
|
|
|
|
|
void put(TYPE *in) {
|
2002-08-27 00:14:54 +04:00
|
|
|
acquire_sem(inUse);
|
|
|
|
unused.add(in);
|
|
|
|
release_sem(inUse);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|