Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / src / vm-limit.c
blobbad1d61e6119b6c6e1ebb6454c0e8f23dde58789
1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001-2014 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/>. */
19 #include <config.h>
20 #include <unistd.h> /* for 'environ', on AIX */
21 #include "lisp.h"
23 #ifdef MSDOS
24 #include <dpmi.h>
25 extern int etext;
26 #endif
28 /* Some systems need this before <sys/resource.h>. */
29 #include <sys/types.h>
31 #ifdef HAVE_SYS_RESOURCE_H
32 # include <sys/time.h>
33 # include <sys/resource.h>
34 #else
35 # if HAVE_SYS_VLIMIT_H
36 # include <sys/vlimit.h> /* Obsolete, says glibc */
37 # endif
38 #endif
40 /* Start of data. It is OK if this is approximate; it's used only as
41 a heuristic. */
42 #ifdef DATA_START
43 # define data_start ((char *) DATA_START)
44 #else
45 extern char data_start[];
46 # ifndef HAVE_DATA_START
47 /* Initialize to nonzero, so that it's put into data and not bss.
48 Link this file's object code first, so that this symbol is near the
49 start of data. */
50 char data_start[1] = { 1 };
51 # endif
52 #endif
55 Level number of warnings already issued.
56 0 -- no warnings issued.
57 1 -- 75% warning already issued.
58 2 -- 85% warning already issued.
59 3 -- 95% warning issued; keep warning frequently.
61 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
62 static enum warnlevel warnlevel;
64 /* Function to call to issue a warning;
65 0 means don't issue them. */
66 static void (*warn_function) (const char *);
68 /* Start of data space; can be changed by calling memory_warnings. */
69 static char *data_space_start;
71 /* Number of bytes of writable memory we can expect to be able to get. */
72 static size_t lim_data;
74 /* Return true if PTR cannot be represented as an Emacs Lisp object. */
75 static bool
76 exceeds_lisp_ptr (void *ptr)
78 return (! USE_LSB_TAG
79 && VAL_MAX < UINTPTR_MAX
80 && ((uintptr_t) ptr & ~DATA_SEG_BITS) >> VALBITS != 0);
83 #ifdef HAVE_GETRLIMIT
85 # ifndef RLIMIT_AS
86 # define RLIMIT_AS RLIMIT_DATA
87 # endif
89 static void
90 get_lim_data (void)
92 /* Set LIM_DATA to the minimum of the maximum object size and the
93 maximum address space. Don't bother to check for values like
94 RLIM_INFINITY since in practice they are not much less than SIZE_MAX. */
95 struct rlimit rlimit;
96 lim_data
97 = (getrlimit (RLIMIT_AS, &rlimit) == 0 && rlimit.rlim_cur <= SIZE_MAX
98 ? rlimit.rlim_cur
99 : SIZE_MAX);
102 #elif defined WINDOWSNT
104 #include "w32heap.h"
106 static void
107 get_lim_data (void)
109 extern size_t reserved_heap_size;
110 lim_data = reserved_heap_size;
113 #elif defined MSDOS
115 void
116 get_lim_data (void)
118 _go32_dpmi_meminfo info;
119 unsigned long lim1, lim2;
121 _go32_dpmi_get_free_memory_information (&info);
122 /* DPMI server of Windows NT and its descendants reports in
123 info.available_memory a much lower amount that is really
124 available, which causes bogus "past 95% of memory limit"
125 warnings. Try to overcome that via circumstantial evidence. */
126 lim1 = info.available_memory;
127 lim2 = info.available_physical_pages;
128 /* DPMI Spec: "Fields that are unavailable will hold -1." */
129 if ((long)lim1 == -1L)
130 lim1 = 0;
131 if ((long)lim2 == -1L)
132 lim2 = 0;
133 else
134 lim2 *= 4096;
135 /* Surely, the available memory is at least what we have physically
136 available, right? */
137 if (lim1 >= lim2)
138 lim_data = lim1;
139 else
140 lim_data = lim2;
141 /* Don't believe they will give us more that 0.5 GB. */
142 if (lim_data > 512U * 1024U * 1024U)
143 lim_data = 512U * 1024U * 1024U;
146 unsigned long
147 ret_lim_data (void)
149 get_lim_data ();
150 return lim_data;
152 #else
153 # error "get_lim_data not implemented on this machine"
154 #endif
156 /* Verify amount of memory available, complaining if we're near the end. */
158 static void
159 check_memory_limits (void)
161 #ifdef REL_ALLOC
162 extern void *(*real_morecore) (ptrdiff_t);
163 #else
164 void *(*real_morecore) (ptrdiff_t) = 0;
165 #endif
166 extern void *(*__morecore) (ptrdiff_t);
168 char *cp;
169 size_t five_percent;
170 size_t data_size;
171 enum warnlevel new_warnlevel;
173 if (lim_data == 0)
174 get_lim_data ();
175 five_percent = lim_data / 20;
177 /* Find current end of memory and issue warning if getting near max */
178 cp = (real_morecore ? real_morecore : __morecore) (0);
179 data_size = cp - data_space_start;
181 if (!warn_function)
182 return;
184 /* What level of warning does current memory usage demand? */
185 new_warnlevel
186 = (data_size > five_percent * 19) ? warned_95
187 : (data_size > five_percent * 17) ? warned_85
188 : (data_size > five_percent * 15) ? warned_75
189 : not_warned;
191 /* If we have gone up a level, give the appropriate warning. */
192 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
194 warnlevel = new_warnlevel;
195 switch (warnlevel)
197 case warned_75:
198 (*warn_function) ("Warning: past 75% of memory limit");
199 break;
201 case warned_85:
202 (*warn_function) ("Warning: past 85% of memory limit");
203 break;
205 case warned_95:
206 (*warn_function) ("Warning: past 95% of memory limit");
209 /* Handle going down in usage levels, with some hysteresis. */
210 else
212 /* If we go down below 70% full, issue another 75% warning
213 when we go up again. */
214 if (data_size < five_percent * 14)
215 warnlevel = not_warned;
216 /* If we go down below 80% full, issue another 85% warning
217 when we go up again. */
218 else if (warnlevel > warned_75 && data_size < five_percent * 16)
219 warnlevel = warned_75;
220 /* If we go down below 90% full, issue another 95% warning
221 when we go up again. */
222 else if (warnlevel > warned_85 && data_size < five_percent * 18)
223 warnlevel = warned_85;
226 if (exceeds_lisp_ptr (cp))
227 (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
230 /* Enable memory usage warnings.
231 START says where the end of pure storage is.
232 WARNFUN specifies the function to call to issue a warning. */
234 void
235 memory_warnings (void *start, void (*warnfun) (const char *))
237 extern void (* __after_morecore_hook) (void); /* From gmalloc.c */
239 data_space_start = start ? start : data_start;
241 warn_function = warnfun;
242 __after_morecore_hook = check_memory_limits;
244 /* Force data limit to be recalculated on each run. */
245 lim_data = 0;