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 / a-raise.c < prev    next >
C/C++ Source or Header  |  2000-07-19  |  9KB  |  284 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                         GNAT COMPILER COMPONENTS                         */
  4. /*                                                                          */
  5. /*                              A - R A I S E                               */
  6. /*                                                                          */
  7. /*                          C Implementation File                           */
  8. /*                                                                          */
  9. /*                            $Revision: 1.52 $                             */
  10. /*                                                                          */
  11. /*          Copyright (C) 1992-1999, 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 you  link  this file  with other  files to */
  25. /* produce an executable,  this file does not by itself cause the resulting */
  26. /* executable to be covered by the GNU General Public License. This except- */
  27. /* ion does not  however invalidate  any other reasons  why the  executable */
  28. /* file might be covered by the  GNU Public License.                        */
  29. /*                                                                          */
  30. /* GNAT was originally developed  by the GNAT team at  New York University. */
  31. /* It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). */
  32. /*                                                                          */
  33. /****************************************************************************/
  34.  
  35. /* Routines to support runtime exception handling */
  36.  
  37. /* ??? We need this to define malloc on those machines that need it, but
  38.    this is the wrong file when this is built for libgnat.a.  */
  39.  
  40. #ifdef __alpha_vxworks
  41. #include "vxWorks.h"
  42. #endif
  43.  
  44. #include "config.h"
  45. #include "a-ada.h"
  46. #include "a-types.h"
  47. #include "a-raise.h"
  48. #include <stdio.h>
  49.  
  50. #define Lock_Task system__soft_links__lock_task
  51. extern void (*Lock_Task) (void);
  52.  
  53. #define Unlock_Task system__soft_links__unlock_task
  54. extern void (*Unlock_Task) (void);
  55.  
  56. #define Print_Unhandled_Exception ada__exceptions__print_unhandled_exception
  57. extern void Print_Unhandled_Exception (void);
  58.  
  59. #define Raise_With_C_Msg ada__exceptions__raise_with_c_msg
  60. extern void Raise_With_C_Msg (Exception_Id, char *);
  61.  
  62. extern struct Exception_Data constraint_error;
  63. extern struct Exception_Data program_error;
  64. extern struct Exception_Data storage_error;
  65.  
  66. /*  We have not yet figured out how to import this directly */
  67.  
  68. void
  69. _gnat_builtin_longjmp (ptr, flag)
  70.      void *ptr;
  71.      int flag;
  72. {
  73.    __builtin_longjmp (ptr, flag);
  74. }
  75.  
  76. /* When an exception is raised for which no handler exists, the procedure
  77.    Ada.Exceptions.Unhandled_Exception is called, which performs the call
  78.    to adafinal to complete finalization, and then prints out the error
  79.    messages for the unhandled exception. The final step is to call this
  80.    routine, which performs any system dependent cleanup required */
  81.  
  82. void
  83. __gnat_unhandled_terminate ()
  84. {
  85. /* Special termination handling for VMS */
  86.  
  87. #ifdef VMS
  88.     {
  89.       long prvhnd;
  90.  
  91.       /* Remove the exception vector so it won't intercept any errors
  92.      in the call to exit, and go into and endless loop */
  93.  
  94.       SYS$SETEXV (1, 0, 3, &prvhnd);
  95.       os_exit (1);
  96.     }
  97.  
  98. /* Termination handling for all other systems. */
  99.  
  100. #elif !defined (__RT__)
  101.     os_exit (1);
  102. #endif
  103. }
  104.  
  105. #if defined (WINNT) || defined (__INTERIX)
  106. /* The following defines functions to limit the amount
  107.    of used memory for systems that do not have OS support
  108.    for that. The amount of available memory available for
  109.    dynamic allocation is limited by setting the environment
  110.    variable GNAT_MEMORY_LIMIT to the number of kilobytes
  111.    that can be used.
  112. */
  113.  
  114. static __SIZE_TYPE__ available_memory = 0;
  115. /*  Amount of memory that is available for heap allocations.
  116.     A value of 0 means that the amount is not yet initialized */
  117.  
  118. #define MSIZE_ACCURACY 4096
  119. /* This defines the amount of memory to add to requested allocation
  120.    sizes, because malloc may return a bigger block than requested.
  121.    As msize is used when free-ing, it must be used on allocation as
  122.    well. To prevent underflow of available_memory we need to use
  123.    a reserve.
  124. */
  125.  
  126. /* This routine must be called while having the task lock.
  127.    When the memory limit is not yet initialized, it will
  128.    be set to the value of the GNAT_MEMORY_LIMIT environment
  129.    variable or to unlimited if that does not exist.
  130.  
  131.    If the size is larger than the amount of available memory,
  132.    the task lock will be freed and a storage_error exception will
  133.    be raised.
  134. */
  135.  
  136. static void check_available_memory (size)
  137.    __SIZE_TYPE__ size;
  138. {
  139.    if (available_memory == 0)
  140.    {
  141.       /* The amount of available memory hasn't been initialized yet */
  142.       char *gnat_memory_limit;
  143.       gnat_memory_limit = (char *) getenv ("GNAT_MEMORY_LIMIT");
  144.       if (gnat_memory_limit != 0) {
  145.          available_memory = atoi (gnat_memory_limit) * 1024 + MSIZE_ACCURACY;
  146.       } else {
  147.          available_memory = (__SIZE_TYPE__) -1;
  148.       }
  149.    }
  150.  
  151.    if (size >= available_memory)
  152.    {
  153.       /* There is a memory overflow */
  154.       (*Unlock_Task) ();
  155.       Raise_With_C_Msg (&storage_error, "heap memory limit exceeded");
  156.    }
  157. }
  158.  
  159. #endif
  160.  
  161. /* Linux modules don't have malloc/free but provide a similar interface with
  162.    kmalloc/kfree. */
  163.  
  164. #ifdef __RT__
  165.  
  166. void *
  167. malloc (size)
  168.      __SIZE_TYPE__ size;
  169. {
  170.   extern void *kmalloc();
  171.  
  172.   return kmalloc (size);
  173. }
  174.  
  175. void
  176. free (void *ptr);
  177. {
  178.   kfree (ptr);
  179. }
  180.  
  181. void *
  182. realloc (void *ptr, __SIZE_TYPE__ size)
  183. {
  184.   /* Currently do not support realloc. */
  185.   return 0;
  186. }
  187.  
  188. #endif
  189.  
  190. /* malloc for use by GNAT, with error checking and task lockout. */
  191.  
  192. void *
  193. __gnat_malloc (size)
  194.      __SIZE_TYPE__ size;
  195. {
  196.   void *result;
  197.  
  198.   if (size == (__SIZE_TYPE__) -1)
  199.     Raise_With_C_Msg (&storage_error, "object too large");
  200.  
  201.   /* Change size from zero to non-zero. We still want a proper pointer
  202.      for the zero case because pointers to zero length objects have to
  203.      be distinct, but we can't just go ahead and allocate zero bytes,
  204.      since some malloc's return zero for a zero argument. */
  205.  
  206.   if (size == 0)
  207.     size = 1;
  208.  
  209.   (*Lock_Task) ();
  210.  
  211. #if defined (WINNT) || defined (__INTERIX)
  212.   if (size + MSIZE_ACCURACY >= available_memory)
  213.      check_available_memory (size + MSIZE_ACCURACY);
  214. #endif
  215.  
  216.   result = (char *) malloc (size);
  217.  
  218. #if defined (WINNT) || defined (__INTERIX)
  219.   if (result != 0)
  220.      available_memory = available_memory - _msize (result);
  221. #endif
  222.  
  223.   (*Unlock_Task) ();
  224.  
  225.   if (result == 0)
  226.     Raise_With_C_Msg (&storage_error, "heap exhausted");
  227.  
  228.   return result;
  229. }
  230.  
  231. /* free for use by GNAT, with task lockout */
  232.  
  233. void
  234. __gnat_free (void *ptr)
  235. {
  236.   (*Lock_Task) ();
  237.  
  238. #if defined (WINNT) || defined (__INTERIX)
  239.   if (ptr != 0)
  240.      available_memory = available_memory + _msize (ptr);
  241. #endif
  242.  
  243.   free (ptr);
  244.   (*Unlock_Task) ();
  245. }
  246.  
  247. /* realloc for use by GNAT, with error checking and task lockout. */
  248.  
  249. void *
  250. __gnat_realloc (void *ptr, __SIZE_TYPE__ size)
  251. {
  252.   void *result;
  253. #if defined (WINNT) || defined (__INTERIX)
  254.   __SIZE_TYPE__ old_size;
  255. #endif
  256.  
  257.   if (size == (__SIZE_TYPE__) -1)
  258.     Raise_With_C_Msg (&storage_error, "object too large");
  259.  
  260.   (*Lock_Task) ();
  261.  
  262. #if defined (WINNT) || defined (__INTERIX)
  263.   old_size = _msize (ptr);
  264.  
  265.   /* conservative check - no need to try to be precise here */
  266.   if (size + MSIZE_ACCURACY >= available_memory)
  267.      check_available_memory (size + MSIZE_ACCURACY);
  268. #endif
  269.  
  270.   result = (void *) realloc (ptr, size);
  271.  
  272. #if defined (WINNT) || defined (__INTERIX)
  273.     if (result != 0)
  274.        available_memory = available_memory + old_size - _msize (ptr);
  275. #endif
  276.  
  277.   (*Unlock_Task) ();
  278.  
  279.   if (result == 0)
  280.     Raise_With_C_Msg (&storage_error, "heap exhausted");
  281.  
  282.   return result;
  283. }
  284.