// // Basic Summary: pool memory allocation // This is a pool of elements of the same size. // We preallocate POOL_NUMBER_ELEMENTS at one time. // They are then chained on the available list. We // allocate pools as we need them. Once a pool chunk is // allocated, it never goes away until the entire pool // is deleted. // // Implementation file for pool definition // // Copyright: // // Copyright 1993 Ron Burback // Copyright 1993 Objects Plus // All rights reserved // // Description: // // Include files: // #include #include "pool.h" // // define the version of this structure // char * pool::version = "Objects Plus Pool Version 1.0" ; // // function: pool // parameters: the size of each element in the pool // goal: basic constructor // returns: new empty pool // pool::pool(unsigned int size) : ElementSize ( size < sizeof(chunk) ? sizeof(chunk) : size ){ TheHead = nil ; ThePools = nil ; }; // // function: ~pool // parameters: none // goal: basic destructor // returns: element to the heap // pool::~pool(){ pools * p ; pools * q ; p=ThePools; while (p != nil) { delete p->storage ; q = p->next ; delete p ; p = q ; } TheHead = nil ; ThePools = nil ; }; // // function: alloc // parameters: none // goal: get a new element from the pool // returns: the new element // void * pool::alloc(){ if (TheHead==0) grow() ; chunk * p = TheHead ; TheHead = p->next ; return p ; }; // // function: free // parameters: pointer to an allocated piece of memory // goal: return the memory to the pool // returns: none // void pool::free(void * element){ chunk * p = (chunk *)element ; p->next = TheHead; TheHead = p ; }; // // function: grow // parameters: none // goal: expand the pool // returns: none // void pool::grow(){ const int chunk_size = POOL_NUMBER_ELEMENTS * ElementSize ; const int number_elements = POOL_NUMBER_ELEMENTS ; // allocate space char * start = new char[chunk_size] ; char * last = start + (number_elements-1)*ElementSize; // keep track of this pool pools * q = new pools() ; q->next = ThePools ; q->storage = start ; ThePools = q ; // chain all elements on the available list for (char * p = start ; p < last ; p += ElementSize ) ((chunk *)p)->next = (chunk *)(p+ElementSize) ; // fix up the last element ((chunk *)last)->next = 0 ; // fix up tha available list TheHead = (chunk *) start ; }; // // function: test // parameters: none // goal: do a self check of the class // returns: true/false // note: this algorith is fail soft with no memory leaks // Boolean pool::test(){ const unsigned int LOOP_COUNT = 100 ; Boolean results ; struct things { int elements [10] ; } ; things * Athing [LOOP_COUNT] ; unsigned int i ; results = true ; // create a pool // allocate lots of things from the pool // place a unique element at each location, and verify // and return them pool * ThePool = new pool (sizeof(things)) ; for (i = 0; i < LOOP_COUNT ; i++ ) { Athing[i] = (things *)ThePool->alloc() ; Athing[i]->elements[0] = i ; } for ( i = 0; i < LOOP_COUNT ; i++ ) { results &= Athing[i]->elements[0] == i ; } for (i = 0; i < LOOP_COUNT ; i++ ) { ThePool->free(Athing[i]); } delete ThePool; return results ; };