Fix URL cookie expiration bug
[emacs.git] / src / vm-limit.c
blob703238bf6ce8d5fbb7074fd605d88d4acfa476a5
1 /* Functions for memory limit warnings.
2 Copyright (C) 1990, 1992, 2001-2017 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 (at
9 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 <https://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 #ifdef HAVE_MALLOC_H
55 # include <malloc.h>
56 #endif
57 #ifndef DOUG_LEA_MALLOC
58 # ifndef __MALLOC_HOOK_VOLATILE
59 # define __MALLOC_HOOK_VOLATILE volatile
60 # endif
61 extern void *(*__morecore) (ptrdiff_t);
62 extern void (*__MALLOC_HOOK_VOLATILE __after_morecore_hook) (void);
63 #endif
65 /* From ralloc.c. */
66 #ifdef REL_ALLOC
67 extern void *(*real_morecore) (ptrdiff_t);
68 #endif
71 Level number of warnings already issued.
72 0 -- no warnings issued.
73 1 -- 75% warning already issued.
74 2 -- 85% warning already issued.
75 3 -- 95% warning issued; keep warning frequently.
77 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
78 static enum warnlevel warnlevel;
80 /* Function to call to issue a warning;
81 0 means don't issue them. */
82 static void (*warn_function) (const char *);
84 /* Start of data space; can be changed by calling memory_warnings. */
85 static char *data_space_start;
87 /* Number of bytes of writable memory we can expect to be able to get. */
88 static size_t lim_data;
90 #ifdef HAVE_GETRLIMIT
92 # ifndef RLIMIT_AS
93 # define RLIMIT_AS RLIMIT_DATA
94 # endif
96 static void
97 get_lim_data (void)
99 /* Set LIM_DATA to the minimum of the maximum object size and the
100 maximum address space. Don't bother to check for values like
101 RLIM_INFINITY since in practice they are not much less than SIZE_MAX. */
102 struct rlimit rlimit;
103 lim_data
104 = (getrlimit (RLIMIT_AS, &rlimit) == 0 && rlimit.rlim_cur <= SIZE_MAX
105 ? rlimit.rlim_cur
106 : SIZE_MAX);
109 #elif defined WINDOWSNT
111 #include "w32heap.h"
113 static void
114 get_lim_data (void)
116 extern size_t reserved_heap_size;
117 lim_data = reserved_heap_size;
120 #elif defined MSDOS
122 void
123 get_lim_data (void)
125 unsigned long totalram, freeram, totalswap, freeswap;
127 dos_memory_info (&totalram, &freeram, &totalswap, &freeswap);
128 lim_data = freeram;
129 /* Don't believe they will give us more that 0.5 GB. */
130 if (lim_data > 512U * 1024U * 1024U)
131 lim_data = 512U * 1024U * 1024U;
134 unsigned long
135 ret_lim_data (void)
137 get_lim_data ();
138 return lim_data;
140 #else
141 # error "get_lim_data not implemented on this machine"
142 #endif
144 /* Verify amount of memory available, complaining if we're near the end. */
146 static void
147 check_memory_limits (void)
149 #ifndef REL_ALLOC
150 void *(*real_morecore) (ptrdiff_t) = 0;
151 #endif
153 char *cp;
154 size_t five_percent;
155 size_t data_size;
156 enum warnlevel new_warnlevel;
158 if (lim_data == 0)
159 get_lim_data ();
160 five_percent = lim_data / 20;
162 /* Find current end of memory and issue warning if getting near max */
163 cp = (real_morecore ? real_morecore : __morecore) (0);
164 data_size = cp - data_space_start;
166 if (!warn_function)
167 return;
169 /* What level of warning does current memory usage demand? */
170 new_warnlevel
171 = (data_size > five_percent * 19) ? warned_95
172 : (data_size > five_percent * 17) ? warned_85
173 : (data_size > five_percent * 15) ? warned_75
174 : not_warned;
176 /* If we have gone up a level, give the appropriate warning. */
177 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
179 warnlevel = new_warnlevel;
180 static char const *const warn_diagnostic[] =
182 "Warning: past 75% of memory limit",
183 "Warning: past 85% of memory limit",
184 "Warning: past 95% of memory limit"
186 warn_function (warn_diagnostic[warnlevel - 1]);
188 /* Handle going down in usage levels, with some hysteresis. */
189 else
191 /* If we go down below 70% full, issue another 75% warning
192 when we go up again. */
193 if (data_size < five_percent * 14)
194 warnlevel = not_warned;
195 /* If we go down below 80% full, issue another 85% warning
196 when we go up again. */
197 else if (warnlevel > warned_75 && data_size < five_percent * 16)
198 warnlevel = warned_75;
199 /* If we go down below 90% full, issue another 95% warning
200 when we go up again. */
201 else if (warnlevel > warned_85 && data_size < five_percent * 18)
202 warnlevel = warned_85;
206 /* Enable memory usage warnings.
207 START says where the end of pure storage is.
208 WARNFUN specifies the function to call to issue a warning. */
210 void
211 memory_warnings (void *start, void (*warnfun) (const char *))
213 data_space_start = start ? start : data_start;
215 warn_function = warnfun;
216 __after_morecore_hook = check_memory_limits;
218 /* Force data limit to be recalculated on each run. */
219 lim_data = 0;