home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_4 / list.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-22  |  749 b   |  33 lines

  1. // -*- C++ -*-
  2. //
  3. // list.cc
  4. // by George Vanecek, 1994
  5. // This List template is used frequently, and consequently, we
  6. // want to improve its performance.  Since memory allocation and
  7. // dealocation is very time consuming, we keep a stack of free ListNodes,
  8. // and reuse them.
  9.  
  10. #include "list.h"
  11.  
  12. ListNode* ListNode::freeList = NULL;
  13.  
  14. // Allocate ListNode first from our free list, then from system heap.
  15. void* ListNode::operator new( size_t s )
  16. {
  17.   if( freeList ) {
  18.     ListNode* const n = freeList;
  19.     freeList = freeList->nxt;
  20.     return n;
  21.   }
  22.   return ::operator new(s);
  23. }
  24.  
  25. // Delete node by placing it on our free list.
  26. void ListNode::operator delete( void* p )
  27. {
  28.   ListNode* const n = (ListNode*)p;
  29.   n->nxt   = freeList;
  30.   freeList = n;
  31. }
  32.  
  33.