File : s-memory-cert.adb


   1 ------------------------------------------------------------------------------
   2 --                                                                          --
   3 --                         GNAT RUN-TIME COMPONENTS                         --
   4 --                                                                          --
   5 --                         S Y S T E M . M E M O R Y                        --
   6 --                                                                          --
   7 --                                 B o d y                                  --
   8 --                                                                          --
   9 --          Copyright (C) 2001-2014, Free Software Foundation, Inc.         --
  10 --                                                                          --
  11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  12 -- terms of the  GNU General Public License as published  by the Free Soft- --
  13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
  14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  16 -- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
  17 --                                                                          --
  18 --                                                                          --
  19 --                                                                          --
  20 --                                                                          --
  21 --                                                                          --
  22 -- You should have received a copy of the GNU General Public License and    --
  23 -- a copy of the GCC Runtime Library Exception along with this program;     --
  24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
  25 -- <http://www.gnu.org/licenses/>.                                          --
  26 --                                                                          --
  27 -- GNAT was originally developed  by the GNAT team at  New York University. --
  28 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
  29 --                                                                          --
  30 ------------------------------------------------------------------------------
  31 
  32 --  This implementation assumes that the underlying malloc/free/realloc
  33 --  implementation is thread safe, and thus, no additional lock is required.
  34 
  35 with Ada.Exceptions;
  36 
  37 package body System.Memory is
  38 
  39    use Ada.Exceptions;
  40 
  41    function c_malloc (Size : size_t) return System.Address;
  42    pragma Import (C, c_malloc, "malloc");
  43 
  44    -----------
  45    -- Alloc --
  46    -----------
  47 
  48    function Alloc (Size : size_t) return System.Address is
  49       Result      : System.Address;
  50       Actual_Size : size_t := Size;
  51 
  52    begin
  53       if Size = size_t'Last then
  54          Raise_Exception (Storage_Error'Identity, "object too large");
  55       end if;
  56 
  57       --  Change size from zero to non-zero. We still want a proper pointer
  58       --  for the zero case because pointers to zero length objects have to
  59       --  be distinct, but we can't just go ahead and allocate zero bytes,
  60       --  since some malloc's return zero for a zero argument.
  61 
  62       if Size = 0 then
  63          Actual_Size := 1;
  64       end if;
  65 
  66       Result := c_malloc (Actual_Size);
  67 
  68       if Result = System.Null_Address then
  69          Raise_Exception (Storage_Error'Identity, "heap exhausted");
  70       end if;
  71 
  72       return Result;
  73    end Alloc;
  74 
  75    ----------
  76    -- Free --
  77    ----------
  78 
  79    procedure Free (Ptr : System.Address) is
  80    begin
  81       raise Program_Error;
  82    end Free;
  83 
  84 end System.Memory;