Commit | Line | Data |
---|---|---|
94d7c01a | 1 | /* Functions for memory limit warnings. |
0b5538bd | 2 | Copyright (C) 1990, 1992, 2002, 2003, 2004, |
aaef169d | 3 | 2005, 2006 Free Software Foundation, Inc. |
94d7c01a JA |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
7 | GNU Emacs is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
e231fd42 | 9 | the Free Software Foundation; either version 2, or (at your option) |
94d7c01a JA |
10 | any later version. |
11 | ||
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU Emacs; see the file COPYING. If not, write to | |
4fc5845f LK |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
94d7c01a | 21 | |
3b672b8f | 22 | #ifdef emacs |
18160b98 | 23 | #include <config.h> |
94d7c01a | 24 | #include "lisp.h" |
3b672b8f RS |
25 | #endif |
26 | ||
27 | #ifndef emacs | |
28 | #include <stddef.h> | |
29 | typedef size_t SIZE; | |
30 | typedef void *POINTER; | |
31 | #define EXCEEDS_LISP_PTR(x) 0 | |
32 | #endif | |
33 | ||
e231fd42 | 34 | #include "mem-limits.h" |
94d7c01a | 35 | |
933f22f4 RS |
36 | #ifdef HAVE_GETRLIMIT |
37 | #include <sys/resource.h> | |
38 | #endif | |
39 | ||
94d7c01a JA |
40 | /* |
41 | Level number of warnings already issued. | |
42 | 0 -- no warnings issued. | |
43 | 1 -- 75% warning already issued. | |
44 | 2 -- 85% warning already issued. | |
3b672b8f | 45 | 3 -- 95% warning issued; keep warning frequently. |
94d7c01a JA |
46 | */ |
47 | static int warnlevel; | |
48 | ||
49 | /* Function to call to issue a warning; | |
50 | 0 means don't issue them. */ | |
da396c5e | 51 | static void (*warn_function) (); |
94d7c01a | 52 | |
94d7c01a JA |
53 | /* Get more memory space, complaining if we're near the end. */ |
54 | ||
fd065466 RM |
55 | static void |
56 | check_memory_limits () | |
94d7c01a | 57 | { |
968e9c04 AI |
58 | #ifdef REL_ALLOC |
59 | extern POINTER (*real_morecore) (); | |
60 | #endif | |
134994ae RM |
61 | extern POINTER (*__morecore) (); |
62 | ||
968e9c04 | 63 | |
94d7c01a | 64 | register POINTER cp; |
46b3623d RS |
65 | unsigned long five_percent; |
66 | unsigned long data_size; | |
94d7c01a | 67 | |
933f22f4 RS |
68 | #ifdef HAVE_GETRLIMIT |
69 | struct rlimit { | |
70 | rlim_t rlim_cur; | |
71 | rlim_t rlim_max; | |
72 | } rlimit; | |
73 | ||
74 | getrlimit (RLIMIT_DATA, &rlimit); | |
75 | ||
76 | five_percent = rlimit.rlim_max / 20; | |
77 | data_size = rlimit.rlim_cur; | |
78 | ||
79 | #else /* not HAVE_GETRLIMIT */ | |
80 | ||
94d7c01a JA |
81 | if (lim_data == 0) |
82 | get_lim_data (); | |
3b672b8f | 83 | five_percent = lim_data / 20; |
94d7c01a JA |
84 | |
85 | /* Find current end of memory and issue warning if getting near max */ | |
968e9c04 AI |
86 | #ifdef REL_ALLOC |
87 | if (real_morecore) | |
88 | cp = (char *) (*real_morecore) (0); | |
89 | else | |
90 | #endif | |
fd065466 | 91 | cp = (char *) (*__morecore) (0); |
da396c5e | 92 | data_size = (char *) cp - (char *) data_space_start; |
94d7c01a | 93 | |
933f22f4 RS |
94 | #endif /* not HAVE_GETRLIMIT */ |
95 | ||
da396c5e | 96 | if (warn_function) |
94d7c01a JA |
97 | switch (warnlevel) |
98 | { | |
177c0ea7 | 99 | case 0: |
3b672b8f | 100 | if (data_size > five_percent * 15) |
94d7c01a JA |
101 | { |
102 | warnlevel++; | |
3b672b8f | 103 | (*warn_function) ("Warning: past 75% of memory limit"); |
94d7c01a JA |
104 | } |
105 | break; | |
106 | ||
177c0ea7 | 107 | case 1: |
3b672b8f | 108 | if (data_size > five_percent * 17) |
94d7c01a JA |
109 | { |
110 | warnlevel++; | |
3b672b8f | 111 | (*warn_function) ("Warning: past 85% of memory limit"); |
94d7c01a JA |
112 | } |
113 | break; | |
114 | ||
177c0ea7 | 115 | case 2: |
3b672b8f | 116 | if (data_size > five_percent * 19) |
94d7c01a JA |
117 | { |
118 | warnlevel++; | |
3b672b8f | 119 | (*warn_function) ("Warning: past 95% of memory limit"); |
94d7c01a JA |
120 | } |
121 | break; | |
122 | ||
123 | default: | |
3b672b8f | 124 | (*warn_function) ("Warning: past acceptable memory limits"); |
94d7c01a JA |
125 | break; |
126 | } | |
127 | ||
3b672b8f RS |
128 | /* If we go down below 70% full, issue another 75% warning |
129 | when we go up again. */ | |
130 | if (data_size < five_percent * 14) | |
131 | warnlevel = 0; | |
132 | /* If we go down below 80% full, issue another 85% warning | |
133 | when we go up again. */ | |
134 | else if (warnlevel > 1 && data_size < five_percent * 16) | |
135 | warnlevel = 1; | |
136 | /* If we go down below 90% full, issue another 95% warning | |
137 | when we go up again. */ | |
138 | else if (warnlevel > 2 && data_size < five_percent * 18) | |
139 | warnlevel = 2; | |
140 | ||
141 | if (EXCEEDS_LISP_PTR (cp)) | |
da396c5e | 142 | (*warn_function) ("Warning: memory in use exceeds lisp pointer size"); |
94d7c01a JA |
143 | } |
144 | ||
145 | /* Cause reinitialization based on job parameters; | |
146 | also declare where the end of pure storage is. */ | |
147 | ||
148 | void | |
3b672b8f | 149 | memory_warnings (start, warnfun) |
94d7c01a JA |
150 | POINTER start; |
151 | void (*warnfun) (); | |
152 | { | |
fd065466 | 153 | extern void (* __after_morecore_hook) (); /* From gmalloc.c */ |
94d7c01a JA |
154 | |
155 | if (start) | |
156 | data_space_start = start; | |
3b672b8f RS |
157 | else |
158 | data_space_start = start_of_data (); | |
159 | ||
da396c5e | 160 | warn_function = warnfun; |
fd065466 | 161 | __after_morecore_hook = check_memory_limits; |
b78e8d0a AI |
162 | |
163 | #ifdef WINDOWSNT | |
164 | /* Force data limit to be recalculated on each run. */ | |
165 | lim_data = 0; | |
166 | #endif | |
94d7c01a | 167 | } |
ab5796a9 MB |
168 | |
169 | /* arch-tag: eab04eda-1f69-447a-8d9f-95f0a3983ca5 | |
170 | (do not change this comment) */ |