home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX09101.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  4.2 KB  |  154 lines

  1. // \EXAMPLES\EX09101.CPP
  2.  
  3. // Demonstration of overloaded new and delete operators.
  4.  
  5. // Files used in this example:
  6. //------------------------------------------------------------
  7. // %F,15,EX09101.H%EX09101.H
  8. // EX09101.CPP      this file
  9. // %F,15,EX0910.CPP%EX0910.CPP
  10. //------------------------------------------------------------
  11.  
  12. #include <iostream.h>
  13. #include "EX09101.H"
  14.  
  15.  
  16.  
  17. long        CachedNode::cacheSize = 3;
  18. long        CachedNode::numUsed = 0;
  19. CachedNode* CachedNode::nextFree = (CachedNode *)NULL;
  20. CachedNode* CachedNode::pCache = CachedNode::IniCache();
  21.  
  22.  
  23.  
  24. //---------------------------------------------------------
  25. // FUNCTION: operator new
  26. //           allocates nodes from the cache until the cache is
  27. //           full, then allocates using ::new.
  28. //---------------------------------------------------------
  29. void*
  30. CachedNode::operator new( size_t bytesReq)
  31. {
  32.    CachedNode* tempNode = (CachedNode *)NULL;
  33.  
  34.    //------------------------------------------------------
  35.    //  Allocate from the cache if space is available, oterwise
  36.    //  allocate from free store using ::new
  37.    //------------------------------------------------------
  38.    if (numUsed < cacheSize)
  39.    {
  40.       numUsed++;
  41.       tempNode = nextFree;
  42.       nextFree = nextFree->next;
  43.       cout << "Allocated using CachedNode::new." << endl;
  44.    }
  45.    else
  46.    {
  47.       tempNode = (CachedNode *)::new char[sizeof( CachedNode)];
  48.       cout << "Allocating using ::new." << endl;
  49.    }
  50.  
  51.    // If node was succesfully allocated assign null to next
  52.    if ( tempNode != (CachedNode *)NULL)
  53.       tempNode->next = (CachedNode *)NULL;
  54.  
  55.    // return pointer to the new node
  56.    return tempNode;
  57. }
  58.  
  59.  
  60. //---------------------------------------------------------
  61. // FUNCTION: operator delete()
  62. //           Nodes allocated from free store are deallocated,
  63. //           nodes allocated from the cache are returned to the
  64. //           cache.
  65. //---------------------------------------------------------
  66. void
  67. CachedNode::operator delete( void* p, size_t bytesUsed)
  68. {
  69.  
  70.    //------------------------------------------------------
  71.    //  If the node was allocated from the cache insert the
  72.    //  node into the cache, otherwise deallocate the node from
  73.    //  free store, using ::delete
  74.    //------------------------------------------------------
  75.    if ( (p >= pCache) &&
  76.        (p <= (pCache + cacheSize )))
  77.    {
  78.       ((CachedNode *)p)->next = nextFree;
  79.       nextFree = (CachedNode *)p;
  80.       cout << "Deallocating using CachedNode::delete." << endl;
  81.    }
  82.    else
  83.    {
  84.       ::delete[] p;
  85.       cout << "Deallocating using ::delete." << endl;
  86.    } /* endif */
  87. }
  88.  
  89.  
  90. //---------------------------------------------------------
  91. // FUNCTION: IniCache()
  92. //           Initializes the array used as the cache.
  93. //---------------------------------------------------------
  94. CachedNode*
  95. CachedNode::IniCache()
  96. {
  97.    CachedNode* pTemp =
  98.       (CachedNode *)::new char[cacheSize * sizeof(CachedNode)];
  99.  
  100.    // initialize the cache
  101.    for (int i = 0; i < cacheSize; i++)
  102.    {
  103.       pTemp[i].next = &pTemp[i + 1];
  104.    } /* endfor */
  105.    pTemp[cacheSize].next = (CachedNode *)NULL;
  106.  
  107.    // nextFree points the the first element
  108.    nextFree = pTemp;
  109.  
  110.    // return a pointer to the cache
  111.    return pTemp;
  112. }
  113.  
  114. //---------------------------------------------------------
  115. // FUNCTION: Next()
  116. //           Returns the next node
  117. //---------------------------------------------------------
  118. CachedNode*
  119. CachedNode::Next()
  120. {
  121.    return next;
  122. }
  123.  
  124. //---------------------------------------------------------
  125. // FUNCTION: Next()
  126. //           assigns p to the next pointer
  127. //---------------------------------------------------------
  128. void
  129. CachedNode::Next( CachedNode* p)
  130. {
  131.    next = p;
  132. }
  133.  
  134. //---------------------------------------------------------
  135. // FUNCTION: Data()
  136. //           Returns the nodes data
  137. //---------------------------------------------------------
  138. long
  139. CachedNode::Data()
  140. {
  141.    return data;
  142. }
  143.  
  144. //---------------------------------------------------------
  145. // FUNCTION: Data()
  146. //           assigns datum to data
  147. //---------------------------------------------------------
  148. void
  149. CachedNode::Data(long datum)
  150. {
  151.    data = datum;
  152. }
  153.  
  154.