* lisp/emacs-lisp/easy-mmode.el (define-minor-mode): Use mode function
[emacs.git] / src / vm-limit.c
blob308613f7eb40ba9f51ecf8b62f555cf641aa7ae6
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 "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
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 #ifdef HAVE_GETRLIMIT
76 # ifndef RLIMIT_AS
77 # define RLIMIT_AS RLIMIT_DATA
78 # endif
80 static void
81 get_lim_data (void)
83 /* Set LIM_DATA to the minimum of the maximum object size and the
84 maximum address space. Don't bother to check for values like
85 RLIM_INFINITY since in practice they are not much less than SIZE_MAX. */
86 struct rlimit rlimit;
87 lim_data
88 = (getrlimit (RLIMIT_AS, &rlimit) == 0 && rlimit.rlim_cur <= SIZE_MAX
89 ? rlimit.rlim_cur
90 : SIZE_MAX);
93 #elif defined WINDOWSNT
95 #include "w32heap.h"
97 static void
98 get_lim_data (void)
100 extern size_t reserved_heap_size;
101 lim_data = reserved_heap_size;
104 #elif defined MSDOS
106 void
107 get_lim_data (void)
109 unsigned long totalram, freeram, totalswap, freeswap;
111 dos_memory_info (&totalram, &freeram, &totalswap, &freeswap);
112 lim_data = freeram;
113 /* Don't believe they will give us more that 0.5 GB. */
114 if (lim_data > 512U * 1024U * 1024U)
115 lim_data = 512U * 1024U * 1024U;
118 unsigned long
119 ret_lim_data (void)
121 get_lim_data ();
122 return lim_data;
124 #else
125 # error "get_lim_data not implemented on this machine"
126 #endif
128 /* Verify amount of memory available, complaining if we're near the end. */
130 static void
131 check_memory_limits (void)
133 #ifdef REL_ALLOC
134 extern void *(*real_morecore) (ptrdiff_t);
135 #else
136 void *(*real_morecore) (ptrdiff_t) = 0;
137 #endif
138 extern void *(*__morecore) (ptrdiff_t);
140 char *cp;
141 size_t five_percent;
142 size_t data_size;
143 enum warnlevel new_warnlevel;
145 if (lim_data == 0)
146 get_lim_data ();
147 five_percent = lim_data / 20;
149 /* Find current end of memory and issue warning if getting near max */
150 cp = (real_morecore ? real_morecore : __morecore) (0);
151 data_size = cp - data_space_start;
153 if (!warn_function)
154 return;
156 /* What level of warning does current memory usage demand? */
157 new_warnlevel
158 = (data_size > five_percent * 19) ? warned_95
159 : (data_size > five_percent * 17) ? warned_85
160 : (data_size > five_percent * 15) ? warned_75
161 : not_warned;
163 /* If we have gone up a level, give the appropriate warning. */
164 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
166 warnlevel = new_warnlevel;
167 switch (warnlevel)
169 case warned_75:
170 (*warn_function) ("Warning: past 75% of memory limit");
171 break;
173 case warned_85:
174 (*warn_function) ("Warning: past 85% of memory limit");
175 break;
177 case warned_95:
178 (*warn_function) ("Warning: past 95% of memory limit");
181 /* Handle going down in usage levels, with some hysteresis. */
182 else
184 /* If we go down below 70% full, issue another 75% warning
185 when we go up again. */
186 if (data_size < five_percent * 14)
187 warnlevel = not_warned;
188 /* If we go down below 80% full, issue another 85% warning
189 when we go up again. */
190 else if (warnlevel > warned_75 && data_size < five_percent * 16)
191 warnlevel = warned_75;
192 /* If we go down below 90% full, issue another 95% warning
193 when we go up again. */
194 else if (warnlevel > warned_85 && data_size < five_percent * 18)
195 warnlevel = warned_85;
199 /* Enable memory usage warnings.
200 START says where the end of pure storage is.
201 WARNFUN specifies the function to call to issue a warning. */
203 void
204 memory_warnings (void *start, void (*warnfun) (const char *))
206 extern void (* __after_morecore_hook) (void); /* From gmalloc.c */
208 data_space_start = start ? start : data_start;
210 warn_function = warnfun;
211 __after_morecore_hook = check_memory_limits;
213 /* Force data limit to be recalculated on each run. */
214 lim_data = 0;