home *** CD-ROM | disk | FTP | other *** search
- /*
- * Example 9: Demonstrates virtual heaps, using a disk-based heap.
- *
- * $Header: E:/vcs/toolexam/example9.cpv 1.0 01 Apr 1992 16:52:16 keffer $
- *
- ****************************************************************************
- *
- * Rogue Wave Software, Inc.
- * P.O. Box 2328
- * Corvallis, OR 97339
- *
- * Copyright (C) 1992.
- * This software is subject to copyright protection under the laws of
- * the United States and other countries.
- *
- ***************************************************************************
- *
- * $Log: E:/vcs/toolexam/example9.cpv $
- *
- * Rev 1.0 01 Apr 1992 16:52:16 keffer
- * Initial revision.
- */
-
- #include <rw/diskpage.h>
- #include <rw/rstream.h>
-
- struct Node {
- int key;
- RWHandle next;
- };
-
- RWHandle head = 0;
-
- const int N = 100; // Exercise 100 Nodes
-
- main() {
-
- // Construct a disk-based page heap with page size equal
- // to the size of Node and with 10 buffers:
- RWDiskPageHeap heap(0, 10, sizeof(Node));
-
- // Build the linked list:
- for (int i=0; i<N; i++){
- RWHandle h = heap.allocate();
- Node* newNode = (Node*)heap.lock(h);
- newNode->key = i;
- newNode->next = head;
- head = h;
- heap.dirty(h);
- heap.unlock(h);
- }
-
- // Now walk the list:
- unsigned count = 0;
- RWHandle nodeHandle = head;
- while(nodeHandle){
- Node* node = (Node*)heap.lock(nodeHandle);
- RWHandle nextHandle = node->next;
- heap.unlock(nodeHandle);
- heap.deallocate(nodeHandle);
- nodeHandle = nextHandle;
- count++;
- }
-
- cout << "List with " << count << " nodes walked.\n";
- return 0;
- }
-
-