home *** CD-ROM | disk | FTP | other *** search
/ Programmer's ROM - The Computer Language Library / programmersrom.iso / ada / misc / garbage.src < prev    next >
Encoding:
Text File  |  1988-05-03  |  6.4 KB  |  209 lines

  1. ::::::::::
  2. garb_coll_v.ada
  3. ::::::::::
  4.  
  5. -------- SIMTEL20 Ada Software Repository Prologue ------------
  6. --                                                           -*
  7. -- Unit name    : generic package Garbage_Collection
  8. -- Version      : 1.0
  9. -- Author       : Doug Bryan
  10. --              : Computer Systems Lab
  11. --              : Stanford University
  12. --              : Stanford, CA 94305
  13. -- DDN Address  : bryan@su-sierra
  14. -- Copyright    : (c) -none-
  15. -- Date created : 10 Aug 1985
  16. -- Release date : 16 Aug 1985
  17. -- Machine/System Compiled/Run on :
  18. --   Data General MV/10000 running the Ada Development Environment 2.2
  19. ---------------------------------------------------------------
  20. --                                                           -*
  21. -- Keywords     : MEMORY, GARBAGE, COLLECTION
  22. ----------------:
  23. --
  24. -- Abstract     : This is a generic garbage collector.  It simply
  25. ----------------: maintains an internal linked list of items which
  26. --              : have been freed then reuses these items when more
  27. --              : are needed.
  28. --                                                           -*
  29. ------------------ Distribution and Copyright -----------------
  30. --                                                           -*
  31. -- This prologue must be included in all copies of this software.
  32. --
  33. -- This software is copyright by the author.
  34. --
  35. -- This software is released to the Ada community.
  36. -- This software is released to the Public Domain (note:
  37. --   software released to the Public Domain is not subject
  38. --   to copyright protection).
  39. -- Restrictions on use or distribution:  NONE
  40. --                                                           -*
  41. ------------------ Disclaimer ---------------------------------
  42. --                                                           -*
  43. -- This software and its documentation are provided "AS IS" and
  44. -- without any expressed or implied warranties whatsoever.
  45. -- No warranties as to performance, merchantability, or fitness
  46. -- for a particular purpose exist.
  47. --
  48. -- Because of the diversity of conditions and hardware under
  49. -- which this software may be used, no warranty of fitness for
  50. -- a particular purpose is offered.  The user is advised to
  51. -- test the software thoroughly before relying on it.  The user
  52. -- must assume the entire risk and liability of using this
  53. -- software.
  54. --
  55. -- In no event shall any person or organization of people be
  56. -- held responsible for any direct, indirect, consequential
  57. -- or inconsequential damages or lost profits.
  58. --                                                           -*
  59. -------------------END-PROLOGUE--------------------------------
  60. generic
  61.     type Item is limited private;
  62.     type Link is access Item;
  63. package Garbage_Collection is
  64.  
  65.     procedure Free (Item : in out Link);
  66.     --| out (item = null);
  67.     -- if item = null, then do nothing.
  68.     -- may raise storage_error;
  69.  
  70.     procedure Get (New_Item : in out Link);
  71.     --| out (new_item /= null);
  72.     -- if new_item /= null then do nothing.
  73.     -- may raise storage_error;
  74.  
  75. end Garbage_Collection;
  76.  
  77. ::::::::::
  78. garb_coll_b.ada
  79. ::::::::::
  80. package body Garbage_Collection is
  81.  
  82. -- type item is limited private;
  83. -- type link is access item;
  84.  
  85.     type Node_Type;
  86.     type List_Type is access Node_Type;
  87.     type Node_Type is
  88.     record
  89.         L    : Link;
  90.         Next : List_Type;
  91.     end record;
  92.  
  93.     Free_Nodes_With_No_Items,
  94.     Free_Nodes_With_Free_Items : List_Type;
  95.     Free_Items : List_Type renames Free_Nodes_With_Free_Items;
  96.     Free_Nodes : List_Type renames Free_Nodes_With_No_Items;
  97.  
  98. ----------------------------------------------------------
  99. -- A logical improvement on this package would be to have the
  100. -- procedures call Unchecked_Deallocation if Storage_Error is
  101. -- ever raised.  They could deallocate all the items and nodes
  102. -- in the local list.  The reasons this is simply not done instead
  103. -- of maintaining the list are:
  104. --   1- an implementation need not implement Unchecked_Deallocation
  105. --   2- it is felt that maintaining a list will be faster than
  106. --      maintaining an entire heap (???)
  107. ----------------------------------------------------------
  108.  
  109.     procedure Free (Item : in out Link) is
  110.     Temp : List_Type;
  111.     begin
  112.     if Item /= null then
  113.         if Free_Nodes = null then
  114.         Temp := new Node_Type;
  115.         else
  116.         Temp := Free_Nodes;
  117.         Free_Nodes := Free_Nodes.all.Next;
  118.         end if;
  119.         Temp.all := (L => Item, Next => Free_Items);
  120.         Free_Items := Temp;
  121.         Item := null;
  122.     end if;
  123.     end Free;
  124.  
  125. ----------------------------------------------------------
  126.     procedure Get (New_Item : in out Link) is
  127.     Temp : List_Type;
  128.     begin
  129.     if New_Item = null then
  130.         if Free_Items = null then
  131.         New_Item := new Item;
  132.         else
  133.         Temp := Free_Items;
  134.         Free_Items := Free_Items.all.Next;
  135.         New_Item := Temp.all.L;
  136.         Temp.all := (L => null, Next => Free_Nodes);
  137.         Free_Nodes := Temp;
  138.         end if;
  139.     end if;
  140.     end Get;
  141.  
  142. ----------------------------------------------------------
  143. end Garbage_Collection;
  144.  
  145. ::::::::::
  146. garb_coll_test.ada
  147. ::::::::::
  148. with Text_Io,
  149.      Garbage_Collection;
  150. use Text_Io;
  151.  
  152. procedure Garb_Coll_Test is
  153.     type Block   is array (1 .. 1000) of Integer;
  154.     type A_Block is access Block;
  155.     package Garbage is new Garbage_Collection
  156.            (Item => Block, Link => A_Block);
  157.     A : array (1 .. 5) of A_Block;
  158.     Bug : exception;
  159. begin
  160.     for I in 1 .. 10_000 loop
  161.     for J in A'Range loop
  162.         Garbage.Get (A (J));
  163.         a(j).all := (others => 44);
  164.     end loop;
  165.     for J in A'Range loop
  166.         Garbage.Free (A (J));
  167.         if A (J) /= null then
  168.         raise Bug;
  169.         end if;
  170.     end loop;
  171.     Put ('.');
  172.     end loop;
  173. exception
  174.     when Storage_Error =>
  175.     New_Line;
  176.     Put_Line ("Storage Error !!!!!");
  177.     when others =>
  178.     New_Line;
  179.     Put_Line ("Fatal Exception. ");
  180. end Garb_Coll_Test;
  181.  
  182. pragma Main;
  183. ::::::::::
  184. garb_coll_test_2.ada
  185. ::::::::::
  186. with Text_Io;
  187. use Text_Io;
  188.  
  189. procedure Garb_Coll_Test_2 is
  190.     type Block   is array (1 .. 1000) of Integer;
  191.     type A_Block is access Block;
  192.     a : a_block;
  193.     count : natural := 0;
  194.     -- the goal of this test is simply to see how may blocks
  195.     -- we can allocate before we get a storage error.
  196. begin
  197.     loop
  198.     a := new block;
  199.     count := natural'succ (count);
  200.     end loop;
  201. exception
  202.     when others =>
  203.     New_Line;
  204.     Put_Line ("Fatal Exception.");
  205.     put_line ("Count was "& natural'image (count));
  206. end Garb_Coll_Test_2;
  207.  
  208. pragma Main;
  209.