Merge from emacs-24; up to 2012-12-22T19:09:52Z!rgm@gnu.org
[bpt/emacs.git] / src / vm-limit.c
1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001-2013 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20 #include <unistd.h> /* for 'environ', on AIX */
21 #include "lisp.h"
22 #include "mem-limits.h"
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.
29 3 -- 95% warning issued; keep warning frequently.
30 */
31 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
32 static enum warnlevel warnlevel;
33
34 /* Function to call to issue a warning;
35 0 means don't issue them. */
36 static void (*warn_function) (const char *);
37
38 /* Start of data space; can be changed by calling malloc_init. */
39 static void *data_space_start;
40
41 /* Number of bytes of writable memory we can expect to be able to get. */
42 static size_t lim_data;
43 \f
44
45 #ifdef HAVE_GETRLIMIT
46
47 # ifndef RLIMIT_AS
48 # define RLIMIT_AS RLIMIT_DATA
49 # endif
50
51 static void
52 get_lim_data (void)
53 {
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);
62 }
63
64 #elif defined WINDOWSNT
65
66 #include "w32heap.h"
67
68 static void
69 get_lim_data (void)
70 {
71 extern size_t reserved_heap_size;
72 lim_data = reserved_heap_size;
73 }
74
75 #elif defined MSDOS
76
77 void
78 get_lim_data (void)
79 {
80 _go32_dpmi_meminfo info;
81 unsigned long lim1, lim2;
82
83 _go32_dpmi_get_free_memory_information (&info);
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;
89 lim2 = info.available_physical_pages;
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;
95 else
96 lim2 *= 4096;
97 /* Surely, the available memory is at least what we have physically
98 available, right? */
99 if (lim1 >= lim2)
100 lim_data = lim1;
101 else
102 lim_data = lim2;
103 /* Don't believe they will give us more that 0.5 GB. */
104 if (lim_data > 512U * 1024U * 1024U)
105 lim_data = 512U * 1024U * 1024U;
106 }
107
108 unsigned long
109 ret_lim_data (void)
110 {
111 get_lim_data ();
112 return lim_data;
113 }
114 #else
115 # error "get_lim_data not implemented on this machine"
116 #endif
117 \f
118 /* Verify amount of memory available, complaining if we're near the end. */
119
120 static void
121 check_memory_limits (void)
122 {
123 #ifdef REL_ALLOC
124 extern void *(*real_morecore) (ptrdiff_t);
125 #endif
126 extern void *(*__morecore) (ptrdiff_t);
127
128 void *cp;
129 size_t five_percent;
130 size_t data_size;
131 enum warnlevel new_warnlevel;
132
133 if (lim_data == 0)
134 get_lim_data ();
135 five_percent = lim_data / 20;
136
137 /* Find current end of memory and issue warning if getting near max */
138 #ifdef REL_ALLOC
139 if (real_morecore)
140 cp = (char *) (*real_morecore) (0);
141 else
142 #endif
143 cp = (char *) (*__morecore) (0);
144 data_size = (char *) cp - (char *) data_space_start;
145
146 if (!warn_function)
147 return;
148
149 /* What level of warning does current memory usage demand? */
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;
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 }
190
191 if (EXCEEDS_LISP_PTR (cp))
192 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
193 }
194 \f
195 #if !defined (CANNOT_DUMP) || !defined (SYSTEM_MALLOC)
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
211 char *
212 start_of_data (void)
213 {
214 #ifdef BSD_SYSTEM
215 extern char etext;
216 return (void *) &etext;
217 #elif defined DATA_START
218 return (void *) DATA_START;
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 */
226 return (void *) &environ;
227 #else
228 extern int data_start;
229 return (void *) &data_start;
230 #endif
231 }
232 #endif /* (not CANNOT_DUMP or not SYSTEM_MALLOC) */
233 \f
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. */
237
238 void
239 memory_warnings (void *start, void (*warnfun) (const char *))
240 {
241 extern void (* __after_morecore_hook) (void); /* From gmalloc.c */
242
243 if (start)
244 data_space_start = start;
245 else
246 data_space_start = start_of_data ();
247
248 warn_function = warnfun;
249 __after_morecore_hook = check_memory_limits;
250
251 /* Force data limit to be recalculated on each run. */
252 lim_data = 0;
253 }