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