1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001-2013 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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/>. */
20 #include <unistd.h> /* for 'environ', on AIX */
22 #include "mem-limits.h"
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.
31 enum warnlevel
{ not_warned
, warned_75
, warned_85
, warned_95
};
32 static enum warnlevel warnlevel
;
34 /* Function to call to issue a warning;
35 0 means don't issue them. */
36 static void (*warn_function
) (const char *);
38 /* Start of data space; can be changed by calling malloc_init. */
39 static void *data_space_start
;
41 /* Number of bytes of writable memory we can expect to be able to get. */
42 static size_t lim_data
;
48 # define RLIMIT_AS RLIMIT_DATA
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. */
59 = (getrlimit (RLIMIT_AS
, &rlimit
) == 0 && rlimit
.rlim_cur
<= SIZE_MAX
64 #elif defined WINDOWSNT
71 extern size_t reserved_heap_size
;
72 lim_data
= reserved_heap_size
;
80 _go32_dpmi_meminfo info
;
81 unsigned long lim1
, lim2
;
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)
93 if ((long)lim2
== -1L)
97 /* Surely, the available memory is at least what we have physically
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;
115 # error "get_lim_data not implemented on this machine"
118 /* Verify amount of memory available, complaining if we're near the end. */
121 check_memory_limits (void)
124 extern void *(*real_morecore
) (ptrdiff_t);
126 extern void *(*__morecore
) (ptrdiff_t);
131 enum warnlevel new_warnlevel
;
135 five_percent
= lim_data
/ 20;
137 /* Find current end of memory and issue warning if getting near max */
140 cp
= (char *) (*real_morecore
) (0);
143 cp
= (char *) (*__morecore
) (0);
144 data_size
= (char *) cp
- (char *) data_space_start
;
149 /* What level of warning does current memory usage demand? */
151 = (data_size
> five_percent
* 19) ? warned_95
152 : (data_size
> five_percent
* 17) ? warned_85
153 : (data_size
> five_percent
* 15) ? warned_75
156 /* If we have gone up a level, give the appropriate warning. */
157 if (new_warnlevel
> warnlevel
|| new_warnlevel
== warned_95
)
159 warnlevel
= new_warnlevel
;
163 (*warn_function
) ("Warning: past 75% of memory limit");
167 (*warn_function
) ("Warning: past 85% of memory limit");
171 (*warn_function
) ("Warning: past 95% of memory limit");
174 /* Handle going down in usage levels, with some hysteresis. */
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
;
191 if (EXCEEDS_LISP_PTR (cp
))
192 (*warn_function
) ("Warning: memory in use exceeds lisp pointer size");
195 #if !defined (CANNOT_DUMP) || !defined (SYSTEM_MALLOC)
196 /* Some systems that cannot dump also cannot implement these. */
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.
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.
216 return (void *) &etext
;
217 #elif defined DATA_START
218 return (void *) DATA_START
;
219 #elif defined ORDINARY_LINK
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.
226 return (void *) &environ
;
228 extern int data_start
;
229 return (void *) &data_start
;
232 #endif /* (not CANNOT_DUMP or not SYSTEM_MALLOC) */
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. */
239 memory_warnings (void *start
, void (*warnfun
) (const char *))
241 extern void (* __after_morecore_hook
) (void); /* From gmalloc.c */
244 data_space_start
= start
;
246 data_space_start
= start_of_data ();
248 warn_function
= warnfun
;
249 __after_morecore_hook
= check_memory_limits
;
251 /* Force data limit to be recalculated on each run. */