Fix a minor problem with mouse-face on mode line
[emacs.git] / src / vm-limit.c
blobab102e32623321ca3d2a12bf36eb4a51d61635f1
1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001-2015 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 "dosfns.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
54 /* From gmalloc.c. */
55 extern void (* __after_morecore_hook) (void);
56 extern void *(*__morecore) (ptrdiff_t);
58 /* From ralloc.c. */
59 #ifdef REL_ALLOC
60 extern void *(*real_morecore) (ptrdiff_t);
61 #endif
64 Level number of warnings already issued.
65 0 -- no warnings issued.
66 1 -- 75% warning already issued.
67 2 -- 85% warning already issued.
68 3 -- 95% warning issued; keep warning frequently.
70 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
71 static enum warnlevel warnlevel;
73 /* Function to call to issue a warning;
74 0 means don't issue them. */
75 static void (*warn_function) (const char *);
77 /* Start of data space; can be changed by calling memory_warnings. */
78 static char *data_space_start;
80 /* Number of bytes of writable memory we can expect to be able to get. */
81 static size_t lim_data;
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 unsigned long totalram, freeram, totalswap, freeswap;
120 dos_memory_info (&totalram, &freeram, &totalswap, &freeswap);
121 lim_data = freeram;
122 /* Don't believe they will give us more that 0.5 GB. */
123 if (lim_data > 512U * 1024U * 1024U)
124 lim_data = 512U * 1024U * 1024U;
127 unsigned long
128 ret_lim_data (void)
130 get_lim_data ();
131 return lim_data;
133 #else
134 # error "get_lim_data not implemented on this machine"
135 #endif
137 /* Verify amount of memory available, complaining if we're near the end. */
139 static void
140 check_memory_limits (void)
142 #ifndef REL_ALLOC
143 void *(*real_morecore) (ptrdiff_t) = 0;
144 #endif
146 char *cp;
147 size_t five_percent;
148 size_t data_size;
149 enum warnlevel new_warnlevel;
151 if (lim_data == 0)
152 get_lim_data ();
153 five_percent = lim_data / 20;
155 /* Find current end of memory and issue warning if getting near max */
156 cp = (real_morecore ? real_morecore : __morecore) (0);
157 data_size = cp - data_space_start;
159 if (!warn_function)
160 return;
162 /* What level of warning does current memory usage demand? */
163 new_warnlevel
164 = (data_size > five_percent * 19) ? warned_95
165 : (data_size > five_percent * 17) ? warned_85
166 : (data_size > five_percent * 15) ? warned_75
167 : not_warned;
169 /* If we have gone up a level, give the appropriate warning. */
170 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
172 warnlevel = new_warnlevel;
173 switch (warnlevel)
175 case warned_75:
176 (*warn_function) ("Warning: past 75% of memory limit");
177 break;
179 case warned_85:
180 (*warn_function) ("Warning: past 85% of memory limit");
181 break;
183 case warned_95:
184 (*warn_function) ("Warning: past 95% of memory limit");
187 /* Handle going down in usage levels, with some hysteresis. */
188 else
190 /* If we go down below 70% full, issue another 75% warning
191 when we go up again. */
192 if (data_size < five_percent * 14)
193 warnlevel = not_warned;
194 /* If we go down below 80% full, issue another 85% warning
195 when we go up again. */
196 else if (warnlevel > warned_75 && data_size < five_percent * 16)
197 warnlevel = warned_75;
198 /* If we go down below 90% full, issue another 95% warning
199 when we go up again. */
200 else if (warnlevel > warned_85 && data_size < five_percent * 18)
201 warnlevel = warned_85;
205 /* Enable memory usage warnings.
206 START says where the end of pure storage is.
207 WARNFUN specifies the function to call to issue a warning. */
209 void
210 memory_warnings (void *start, void (*warnfun) (const char *))
212 data_space_start = start ? start : data_start;
214 warn_function = warnfun;
215 __after_morecore_hook = check_memory_limits;
217 /* Force data limit to be recalculated on each run. */
218 lim_data = 0;