home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / hl10osrc.lzh / Lib / LList.cc < prev    next >
Text File  |  1994-04-23  |  3KB  |  92 lines

  1. /* -*- Mode: C -*- */
  2. /* LList.cc - LineList implementation code
  3.  * Created by Robert Heller on Tue Mar 24 19:25:29 1992
  4.  *
  5.  * ------------------------------------------------------------------
  6.  * Home Libarian by Deepwoods Software
  7.  * Common Class library implementation code
  8.  * ------------------------------------------------------------------
  9.  * Modification History:
  10.  * ------------------------------------------------------------------
  11.  * Contents:
  12.  * ------------------------------------------------------------------
  13.  * 
  14.  * 
  15.  * Copyright (c) 1991,1992 by Robert heller (D/B/A Deepwoods Software)
  16.  *        All Rights Reserved
  17.  * 
  18.  */
  19.  
  20. #include <LList.h>
  21.  
  22. int LineList::numblocks;    // current number of allocated blocks
  23. LineList* LineList::blocks[maxblocks]; // allocated LineLists
  24. LineList* LineList::freelist;    // linked list of available LineLists
  25.  
  26. static LineList dummy;        // dummy LineList
  27.  
  28. // Helper function to allocate an additional block of 100 LineLists
  29. void LineList::moreblocks ()
  30. {
  31.     // Have we run out of blocks?
  32.     if (numblocks < maxblocks) {
  33.         // if not...  bump block counter
  34.         int iblock = numblocks++;
  35.         // snarf some memory
  36.         LineList* p = blocks[iblock] = (LineList*) new char[sizeof(LineList)*blocksize];
  37.         // build linked list.  nextline field doubles as next pointer.
  38.         for (int ic = 1; ic < blocksize; ic++) {
  39.             p->nextline = (p+1);
  40.             p++;
  41.         }
  42.         // point last tail at current freelist
  43.         p->nextline = freelist;
  44.         // and set freelist head to first LineList in fresh block
  45.         freelist = blocks[iblock];
  46.     }
  47. }
  48.             
  49. // overloaded new operator for LineLists
  50. void* LineList::operator new (long bytes)
  51. {
  52.     // if freelist is empty, get more memory
  53.     if (dummy.freelist == 0) dummy.moreblocks();
  54.     // if freelist is still empty, return null
  55.     if (dummy.freelist == 0) return(0);
  56.     // get pointer to head of list
  57.     LineList* newLineList = dummy.freelist;
  58.     // set freelist to next LineList in the list
  59.     dummy.freelist = (LineList*) newLineList->nextline;
  60.     // unlink new LineList from the list
  61.     newLineList->nextline = 0;
  62.     // retun new LineList
  63.     return(newLineList);
  64. }
  65.  
  66. // overloaded delete operator for LineLists
  67. void LineList::operator delete(void* vptr)
  68. {
  69.     // convert pointer to a LineList*
  70.     LineList* ptr = (LineList*) vptr;
  71.     // if a null pointer, just return
  72.     if (ptr == 0) return;
  73.     // has this LineList already be freed?
  74.     for (LineList* p = dummy.freelist; p != 0; p = (LineList*) p->nextline) {
  75.         if (p == ptr) return;    // if so, don't free it again
  76.     }
  77.     // link onto head of free list
  78.     ptr->nextline = dummy.freelist;
  79.     dummy.freelist = ptr;
  80. }
  81.  
  82. void FreeLineList(LineList* l)
  83. {
  84.     if (l == (LineList*) 0) return;
  85.     for (LineList* n = l->nextline; l != (LineList*) 0;) {
  86.         delete l;
  87.         l = n;
  88.         if (l != (LineList*) 0) n = l->nextline;
  89.     }
  90. }
  91.  
  92.