vm-limit.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Functions for memory limit warnings.
  2. Copyright (C) 1990, 1992 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library. Its master source is NOT part of
  4. the C library, however. The master source lives in /gd/gnu/lib.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16. Cambridge, MA 02139, USA. */
  17. #ifdef emacs
  18. #include <config.h>
  19. #include "lisp.h"
  20. #endif
  21. #ifndef emacs
  22. #include <stddef.h>
  23. typedef size_t SIZE;
  24. typedef void *POINTER;
  25. #define EXCEEDS_LISP_PTR(x) 0
  26. #endif
  27. #include "mem-limits.h"
  28. /*
  29. Level number of warnings already issued.
  30. 0 -- no warnings issued.
  31. 1 -- 75% warning already issued.
  32. 2 -- 85% warning already issued.
  33. 3 -- 95% warning issued; keep warning frequently.
  34. */
  35. static int warnlevel;
  36. /* Function to call to issue a warning;
  37. 0 means don't issue them. */
  38. static void (*warn_function) ();
  39. /* Get more memory space, complaining if we're near the end. */
  40. static void
  41. check_memory_limits ()
  42. {
  43. extern POINTER (*__morecore) ();
  44. register POINTER cp;
  45. unsigned long five_percent;
  46. unsigned long data_size;
  47. if (lim_data == 0)
  48. get_lim_data ();
  49. five_percent = lim_data / 20;
  50. /* Find current end of memory and issue warning if getting near max */
  51. cp = (char *) (*__morecore) (0);
  52. data_size = (char *) cp - (char *) data_space_start;
  53. if (warn_function)
  54. switch (warnlevel)
  55. {
  56. case 0:
  57. if (data_size > five_percent * 15)
  58. {
  59. warnlevel++;
  60. (*warn_function) ("Warning: past 75% of memory limit");
  61. }
  62. break;
  63. case 1:
  64. if (data_size > five_percent * 17)
  65. {
  66. warnlevel++;
  67. (*warn_function) ("Warning: past 85% of memory limit");
  68. }
  69. break;
  70. case 2:
  71. if (data_size > five_percent * 19)
  72. {
  73. warnlevel++;
  74. (*warn_function) ("Warning: past 95% of memory limit");
  75. }
  76. break;
  77. default:
  78. (*warn_function) ("Warning: past acceptable memory limits");
  79. break;
  80. }
  81. /* If we go down below 70% full, issue another 75% warning
  82. when we go up again. */
  83. if (data_size < five_percent * 14)
  84. warnlevel = 0;
  85. /* If we go down below 80% full, issue another 85% warning
  86. when we go up again. */
  87. else if (warnlevel > 1 && data_size < five_percent * 16)
  88. warnlevel = 1;
  89. /* If we go down below 90% full, issue another 95% warning
  90. when we go up again. */
  91. else if (warnlevel > 2 && data_size < five_percent * 18)
  92. warnlevel = 2;
  93. if (EXCEEDS_LISP_PTR (cp))
  94. (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
  95. }
  96. /* Cause reinitialization based on job parameters;
  97. also declare where the end of pure storage is. */
  98. void
  99. memory_warnings (start, warnfun)
  100. POINTER start;
  101. void (*warnfun) ();
  102. {
  103. extern void (* __after_morecore_hook) (); /* From gmalloc.c */
  104. if (start)
  105. data_space_start = start;
  106. else
  107. data_space_start = start_of_data ();
  108. warn_function = warnfun;
  109. __after_morecore_hook = check_memory_limits;
  110. }