gnus-registry.el: Silence XEmacs byte compiler
[bpt/emacs.git] / src / vm-limit.c
CommitLineData
94d7c01a 1/* Functions for memory limit warnings.
ab422c4d 2 Copyright (C) 1990, 1992, 2001-2013 Free Software Foundation, Inc.
94d7c01a
JA
3
4This file is part of GNU Emacs.
5
9ec0b715 6GNU Emacs is free software: you can redistribute it and/or modify
94d7c01a 7it under the terms of the GNU General Public License as published by
9ec0b715
GM
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
94d7c01a
JA
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9ec0b715 17along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
94d7c01a 18
18160b98 19#include <config.h>
b69a6d22 20#include <unistd.h> /* for 'environ', on AIX */
94d7c01a 21#include "lisp.h"
e231fd42 22#include "mem-limits.h"
94d7c01a
JA
23
24/*
25 Level number of warnings already issued.
26 0 -- no warnings issued.
27 1 -- 75% warning already issued.
28 2 -- 85% warning already issued.
3b672b8f 29 3 -- 95% warning issued; keep warning frequently.
94d7c01a 30*/
395d3972 31enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
395d3972 32static enum warnlevel warnlevel;
94d7c01a
JA
33
34/* Function to call to issue a warning;
35 0 means don't issue them. */
a8fe7202 36static void (*warn_function) (const char *);
94d7c01a 37
395d3972 38/* Start of data space; can be changed by calling malloc_init. */
fcee5028 39static void *data_space_start;
395d3972
RS
40
41/* Number of bytes of writable memory we can expect to be able to get. */
62aba0d4 42static size_t lim_data;
395d3972
RS
43\f
44
7be78020 45#ifdef HAVE_GETRLIMIT
51757187 46
7be78020
PE
47# ifndef RLIMIT_AS
48# define RLIMIT_AS RLIMIT_DATA
49# endif
395d3972
RS
50
51static void
7c3320d8 52get_lim_data (void)
395d3972 53{
7be78020
PE
54 /* Set LIM_DATA to the minimum of the maximum object size and the
55 maximum address space. Don't bother to check for values like
56 RLIM_INFINITY since in practice they are not much less than SIZE_MAX. */
57 struct rlimit rlimit;
58 lim_data
59 = (getrlimit (RLIMIT_AS, &rlimit) == 0 && rlimit.rlim_cur <= SIZE_MAX
60 ? rlimit.rlim_cur
61 : SIZE_MAX);
395d3972
RS
62}
63
7be78020 64#elif defined WINDOWSNT
395d3972 65
a68089e4
EZ
66#include "w32heap.h"
67
395d3972 68static void
7c3320d8 69get_lim_data (void)
395d3972 70{
62aba0d4 71 extern size_t reserved_heap_size;
395d3972
RS
72 lim_data = reserved_heap_size;
73}
74
7be78020 75#elif defined MSDOS
395d3972 76
395d3972 77void
7c3320d8 78get_lim_data (void)
395d3972
RS
79{
80 _go32_dpmi_meminfo info;
8a445f76 81 unsigned long lim1, lim2;
395d3972
RS
82
83 _go32_dpmi_get_free_memory_information (&info);
8a445f76
EZ
84 /* DPMI server of Windows NT and its descendants reports in
85 info.available_memory a much lower amount that is really
86 available, which causes bogus "past 95% of memory limit"
87 warnings. Try to overcome that via circumstantial evidence. */
88 lim1 = info.available_memory;
7cf94eac 89 lim2 = info.available_physical_pages;
8a445f76
EZ
90 /* DPMI Spec: "Fields that are unavailable will hold -1." */
91 if ((long)lim1 == -1L)
92 lim1 = 0;
93 if ((long)lim2 == -1L)
94 lim2 = 0;
7cf94eac
EZ
95 else
96 lim2 *= 4096;
8a445f76
EZ
97 /* Surely, the available memory is at least what we have physically
98 available, right? */
7cf94eac 99 if (lim1 >= lim2)
8a445f76
EZ
100 lim_data = lim1;
101 else
102 lim_data = lim2;
103 /* Don't believe they will give us more that 0.5 GB. */
7cf94eac
EZ
104 if (lim_data > 512U * 1024U * 1024U)
105 lim_data = 512U * 1024U * 1024U;
395d3972 106}
ec06ec19
EZ
107
108unsigned long
7c3320d8 109ret_lim_data (void)
ec06ec19
EZ
110{
111 get_lim_data ();
112 return lim_data;
113}
395d3972 114#else
7be78020 115# error "get_lim_data not implemented on this machine"
395d3972 116#endif
395d3972
RS
117\f
118/* Verify amount of memory available, complaining if we're near the end. */
94d7c01a 119
fd065466 120static void
d3da34e0 121check_memory_limits (void)
94d7c01a 122{
968e9c04 123#ifdef REL_ALLOC
fcee5028 124 extern void *(*real_morecore) (ptrdiff_t);
968e9c04 125#endif
fcee5028 126 extern void *(*__morecore) (ptrdiff_t);
134994ae 127
fcee5028 128 void *cp;
62aba0d4
FP
129 size_t five_percent;
130 size_t data_size;
395d3972 131 enum warnlevel new_warnlevel;
94d7c01a
JA
132
133 if (lim_data == 0)
134 get_lim_data ();
3b672b8f 135 five_percent = lim_data / 20;
94d7c01a
JA
136
137 /* Find current end of memory and issue warning if getting near max */
968e9c04
AI
138#ifdef REL_ALLOC
139 if (real_morecore)
140 cp = (char *) (*real_morecore) (0);
141 else
142#endif
fd065466 143 cp = (char *) (*__morecore) (0);
da396c5e 144 data_size = (char *) cp - (char *) data_space_start;
94d7c01a 145
395d3972
RS
146 if (!warn_function)
147 return;
148
149 /* What level of warning does current memory usage demand? */
21da04c4
CY
150 new_warnlevel
151 = (data_size > five_percent * 19) ? warned_95
152 : (data_size > five_percent * 17) ? warned_85
153 : (data_size > five_percent * 15) ? warned_75
154 : not_warned;
395d3972
RS
155
156 /* If we have gone up a level, give the appropriate warning. */
157 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
158 {
159 warnlevel = new_warnlevel;
160 switch (warnlevel)
161 {
162 case warned_75:
163 (*warn_function) ("Warning: past 75% of memory limit");
164 break;
165
166 case warned_85:
167 (*warn_function) ("Warning: past 85% of memory limit");
168 break;
169
170 case warned_95:
171 (*warn_function) ("Warning: past 95% of memory limit");
172 }
173 }
174 /* Handle going down in usage levels, with some hysteresis. */
175 else
176 {
177 /* If we go down below 70% full, issue another 75% warning
178 when we go up again. */
179 if (data_size < five_percent * 14)
180 warnlevel = not_warned;
181 /* If we go down below 80% full, issue another 85% warning
182 when we go up again. */
183 else if (warnlevel > warned_75 && data_size < five_percent * 16)
184 warnlevel = warned_75;
185 /* If we go down below 90% full, issue another 95% warning
186 when we go up again. */
187 else if (warnlevel > warned_85 && data_size < five_percent * 18)
188 warnlevel = warned_85;
189 }
3b672b8f
RS
190
191 if (EXCEEDS_LISP_PTR (cp))
da396c5e 192 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
94d7c01a 193}
395d3972 194\f
5e617bc2 195#if !defined (CANNOT_DUMP) || !defined (SYSTEM_MALLOC)
313d9eb2
DN
196/* Some systems that cannot dump also cannot implement these. */
197
198/*
199 * Return the address of the start of the data segment prior to
200 * doing an unexec. After unexec the return value is undefined.
201 * See crt0.c for further information and definition of data_start.
202 *
203 * Apparently, on BSD systems this is etext at startup. On
204 * USG systems (swapping) this is highly mmu dependent and
205 * is also dependent on whether or not the program is running
206 * with shared text. Generally there is a (possibly large)
207 * gap between end of text and start of data with shared text.
208 *
209 */
210
8848b728 211char *
313d9eb2
DN
212start_of_data (void)
213{
214#ifdef BSD_SYSTEM
215 extern char etext;
fcee5028 216 return (void *) &etext;
313d9eb2 217#elif defined DATA_START
fcee5028 218 return (void *) DATA_START;
313d9eb2
DN
219#elif defined ORDINARY_LINK
220 /*
221 * This is a hack. Since we're not linking crt0.c or pre_crt0.c,
222 * data_start isn't defined. We take the address of environ, which
223 * is known to live at or near the start of the system crt0.c, and
224 * we don't sweat the handful of bytes that might lose.
225 */
fcee5028 226 return (void *) &environ;
313d9eb2
DN
227#else
228 extern int data_start;
fcee5028 229 return (void *) &data_start;
313d9eb2
DN
230#endif
231}
232#endif /* (not CANNOT_DUMP or not SYSTEM_MALLOC) */
233\f
395d3972
RS
234/* Enable memory usage warnings.
235 START says where the end of pure storage is.
236 WARNFUN specifies the function to call to issue a warning. */
94d7c01a
JA
237
238void
fcee5028 239memory_warnings (void *start, void (*warnfun) (const char *))
94d7c01a 240{
d2aa42f8 241 extern void (* __after_morecore_hook) (void); /* From gmalloc.c */
94d7c01a
JA
242
243 if (start)
244 data_space_start = start;
3b672b8f
RS
245 else
246 data_space_start = start_of_data ();
247
da396c5e 248 warn_function = warnfun;
fd065466 249 __after_morecore_hook = check_memory_limits;
b78e8d0a 250
b78e8d0a
AI
251 /* Force data limit to be recalculated on each run. */
252 lim_data = 0;
94d7c01a 253}