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