home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / C < prev    next >
Encoding:
Text File  |  1992-04-01  |  1.6 KB  |  69 lines

  1. /*
  2.  * Example 9: Demonstrates virtual heaps, using a disk-based heap.
  3.  *
  4.  * $Header:   E:/vcs/toolexam/example9.cpv   1.0   01 Apr 1992 16:52:16   keffer  $
  5.  *
  6.  ****************************************************************************
  7.  *
  8.  * Rogue Wave Software, Inc.
  9.  * P.O. Box 2328
  10.  * Corvallis, OR 97339
  11.  *
  12.  * Copyright (C) 1992.
  13.  * This software is subject to copyright protection under the laws of
  14.  * the United States and other countries.
  15.  *
  16.  ***************************************************************************
  17.  *
  18.  * $Log:   E:/vcs/toolexam/example9.cpv  $
  19.  * 
  20.  *    Rev 1.0   01 Apr 1992 16:52:16   keffer
  21.  * Initial revision.
  22.  */
  23.  
  24. #include <rw/diskpage.h>
  25. #include <rw/rstream.h>
  26.  
  27. struct Node {
  28.   int    key;
  29.   RWHandle    next;
  30. };
  31.  
  32. RWHandle head = 0;
  33.  
  34. const int N = 100;    // Exercise 100 Nodes
  35.  
  36. main() {
  37.  
  38.   // Construct a disk-based page heap with page size equal
  39.   // to the size of Node and with 10 buffers:
  40.   RWDiskPageHeap heap(0, 10, sizeof(Node));
  41.  
  42.   // Build the linked list:
  43.   for (int i=0; i<N; i++){
  44.     RWHandle h = heap.allocate();
  45.     Node* newNode = (Node*)heap.lock(h);
  46.     newNode->key  = i;
  47.     newNode->next = head;
  48.     head = h;
  49.     heap.dirty(h);
  50.     heap.unlock(h);
  51.   }
  52.  
  53.   // Now walk the list:
  54.   unsigned count = 0;
  55.   RWHandle nodeHandle = head;
  56.   while(nodeHandle){
  57.     Node* node = (Node*)heap.lock(nodeHandle);
  58.     RWHandle nextHandle = node->next;
  59.     heap.unlock(nodeHandle);
  60.     heap.deallocate(nodeHandle);
  61.     nodeHandle = nextHandle;
  62.     count++;
  63.   }
  64.  
  65.   cout << "List with " << count << " nodes walked.\n";
  66.   return 0;
  67. }
  68.  
  69.