home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adav313.zip / gnat-3_13p-os2-bin-20010916.zip / emx / gnatlib / s-pooloc.adb < prev    next >
Text File  |  2000-07-19  |  6KB  |  169 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                    S Y S T E M . P O O L _ L O C A L                     --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.10 $                             --
  10. --                                                                          --
  11. --          Copyright (C) 1992-1998, Free Software Foundation, Inc.         --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. with System.Storage_Elements;
  37. with System.Address_To_Access_Conversions;
  38.  
  39. package body System.Pool_Local is
  40.  
  41.    package SSE renames System.Storage_Elements;
  42.    use type SSE.Storage_Offset;
  43.  
  44.    Pointer_Size  : constant SSE.Storage_Offset := Address'Size / Storage_Unit;
  45.    Pointers_Size : constant SSE.Storage_Offset := 2 * Pointer_Size;
  46.  
  47.    type Acc_Address is access all Address;
  48.    package Addr is new Address_To_Access_Conversions (Address);
  49.  
  50.    --------------------------
  51.    -- Imported C Functions --
  52.    --------------------------
  53.  
  54.    --  It is assumed that these functions are self-protected against concurrent
  55.    --  access (should be true on a POSIX system with threads, and is part of
  56.    --  the interface requirement for the implementation of Tasking.Primitives)
  57.  
  58.    function malloc (Size : SSE.Storage_Count) return System.Address;
  59.    pragma Import (C, malloc, "__gnat_malloc");
  60.  
  61.    procedure free (Address : System.Address);
  62.    pragma Import (C, free, "__gnat_free");
  63.  
  64.    -----------------------
  65.    -- Local Subprograms --
  66.    -----------------------
  67.  
  68.    function Next (A : Address) return Acc_Address;
  69.    --  Given an address of a block, return an access to the next block
  70.  
  71.    function Prev (A : Address) return Acc_Address;
  72.    --  Given an address of a block, return an access to the previous block
  73.  
  74.    --------------
  75.    -- Allocate --
  76.    --------------
  77.  
  78.    procedure Allocate
  79.      (Pool         : in out Unbounded_Reclaim_Pool;
  80.       Address      : out System.Address;
  81.       Storage_Size : SSE.Storage_Count;
  82.       Alignment    : SSE.Storage_Count)
  83.    is
  84.       Allocated : constant System.Address :=
  85.                     malloc (Storage_Size + Pointers_Size);
  86.  
  87.    begin
  88.       --  The call to malloc returns an address whose alignment is compatible
  89.       --  with the worst case alignment requirement for the machine; thus the
  90.       --  Alignment argument can be safely ignored.
  91.  
  92.       if Allocated = Null_Address then
  93.          raise Storage_Error;
  94.       else
  95.          Address := Allocated + Pointers_Size;
  96.          Next (Allocated).all  := Pool.First;
  97.          Prev (Allocated).all  := Null_Address;
  98.  
  99.          if Pool.First /= Null_Address then
  100.             Prev (Pool.First).all := Allocated;
  101.          end if;
  102.  
  103.          Pool.First := Allocated;
  104.       end if;
  105.    end Allocate;
  106.  
  107.    ----------------
  108.    -- Deallocate --
  109.    ----------------
  110.  
  111.    procedure Deallocate
  112.      (Pool         : in out Unbounded_Reclaim_Pool;
  113.       Address      : System.Address;
  114.       Storage_Size : SSE.Storage_Count;
  115.       Alignment    : SSE.Storage_Count)
  116.    is
  117.       Allocated : constant System.Address := Address - Pointers_Size;
  118.  
  119.    begin
  120.       if Prev (Allocated).all = Null_Address then
  121.          Pool.First := Next (Allocated).all;
  122.          Prev (Pool.First).all := Null_Address;
  123.       else
  124.          Next (Prev (Allocated).all).all := Next (Allocated).all;
  125.       end if;
  126.  
  127.       if Next (Allocated).all /= Null_Address then
  128.          Prev (Next (Allocated).all).all := Prev (Allocated).all;
  129.       end if;
  130.  
  131.       free (Allocated);
  132.    end Deallocate;
  133.  
  134.    --------------
  135.    -- Finalize --
  136.    --------------
  137.  
  138.    procedure Finalize (Pool : in out Unbounded_Reclaim_Pool) is
  139.       N         : System.Address := Pool.First;
  140.       Allocated : System.Address;
  141.  
  142.    begin
  143.       while N /= Null_Address loop
  144.          Allocated := N;
  145.          N := Next (N).all;
  146.          free (Allocated);
  147.       end loop;
  148.    end Finalize;
  149.  
  150.    ----------
  151.    -- Next --
  152.    ----------
  153.  
  154.    function Next (A : Address) return Acc_Address is
  155.    begin
  156.       return Acc_Address (Addr.To_Pointer (A));
  157.    end Next;
  158.  
  159.    ----------
  160.    -- Prev --
  161.    ----------
  162.  
  163.    function Prev (A : Address) return Acc_Address is
  164.    begin
  165.       return Acc_Address (Addr.To_Pointer (A + Pointer_Size));
  166.    end Prev;
  167.  
  168. end System.Pool_Local;
  169.