home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX09101.CPP
-
- // Demonstration of overloaded new and delete operators.
-
- // Files used in this example:
- //------------------------------------------------------------
- // %F,15,EX09101.H%EX09101.H
- // EX09101.CPP this file
- // %F,15,EX0910.CPP%EX0910.CPP
- //------------------------------------------------------------
-
- #include <iostream.h>
- #include "EX09101.H"
-
-
-
- long CachedNode::cacheSize = 3;
- long CachedNode::numUsed = 0;
- CachedNode* CachedNode::nextFree = (CachedNode *)NULL;
- CachedNode* CachedNode::pCache = CachedNode::IniCache();
-
-
-
- //---------------------------------------------------------
- // FUNCTION: operator new
- // allocates nodes from the cache until the cache is
- // full, then allocates using ::new.
- //---------------------------------------------------------
- void*
- CachedNode::operator new( size_t bytesReq)
- {
- CachedNode* tempNode = (CachedNode *)NULL;
-
- //------------------------------------------------------
- // Allocate from the cache if space is available, oterwise
- // allocate from free store using ::new
- //------------------------------------------------------
- if (numUsed < cacheSize)
- {
- numUsed++;
- tempNode = nextFree;
- nextFree = nextFree->next;
- cout << "Allocated using CachedNode::new." << endl;
- }
- else
- {
- tempNode = (CachedNode *)::new char[sizeof( CachedNode)];
- cout << "Allocating using ::new." << endl;
- }
-
- // If node was succesfully allocated assign null to next
- if ( tempNode != (CachedNode *)NULL)
- tempNode->next = (CachedNode *)NULL;
-
- // return pointer to the new node
- return tempNode;
- }
-
-
- //---------------------------------------------------------
- // FUNCTION: operator delete()
- // Nodes allocated from free store are deallocated,
- // nodes allocated from the cache are returned to the
- // cache.
- //---------------------------------------------------------
- void
- CachedNode::operator delete( void* p, size_t bytesUsed)
- {
-
- //------------------------------------------------------
- // If the node was allocated from the cache insert the
- // node into the cache, otherwise deallocate the node from
- // free store, using ::delete
- //------------------------------------------------------
- if ( (p >= pCache) &&
- (p <= (pCache + cacheSize )))
- {
- ((CachedNode *)p)->next = nextFree;
- nextFree = (CachedNode *)p;
- cout << "Deallocating using CachedNode::delete." << endl;
- }
- else
- {
- ::delete[] p;
- cout << "Deallocating using ::delete." << endl;
- } /* endif */
- }
-
-
- //---------------------------------------------------------
- // FUNCTION: IniCache()
- // Initializes the array used as the cache.
- //---------------------------------------------------------
- CachedNode*
- CachedNode::IniCache()
- {
- CachedNode* pTemp =
- (CachedNode *)::new char[cacheSize * sizeof(CachedNode)];
-
- // initialize the cache
- for (int i = 0; i < cacheSize; i++)
- {
- pTemp[i].next = &pTemp[i + 1];
- } /* endfor */
- pTemp[cacheSize].next = (CachedNode *)NULL;
-
- // nextFree points the the first element
- nextFree = pTemp;
-
- // return a pointer to the cache
- return pTemp;
- }
-
- //---------------------------------------------------------
- // FUNCTION: Next()
- // Returns the next node
- //---------------------------------------------------------
- CachedNode*
- CachedNode::Next()
- {
- return next;
- }
-
- //---------------------------------------------------------
- // FUNCTION: Next()
- // assigns p to the next pointer
- //---------------------------------------------------------
- void
- CachedNode::Next( CachedNode* p)
- {
- next = p;
- }
-
- //---------------------------------------------------------
- // FUNCTION: Data()
- // Returns the nodes data
- //---------------------------------------------------------
- long
- CachedNode::Data()
- {
- return data;
- }
-
- //---------------------------------------------------------
- // FUNCTION: Data()
- // assigns datum to data
- //---------------------------------------------------------
- void
- CachedNode::Data(long datum)
- {
- data = datum;
- }
-
-