Commit | Line | Data |
---|---|---|
94d7c01a | 1 | /* Functions for memory limit warnings. |
e231fd42 | 2 | Copyright (C) 1990, 1992 Free Software Foundation, Inc. |
94d7c01a JA |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
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 | |
e231fd42 | 8 | the Free Software Foundation; either version 2, or (at your option) |
94d7c01a JA |
9 | any later version. |
10 | ||
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. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
3b672b8f | 20 | #ifdef emacs |
94d7c01a JA |
21 | #include "config.h" |
22 | #include "lisp.h" | |
3b672b8f RS |
23 | #endif |
24 | ||
25 | #ifndef emacs | |
26 | #include <stddef.h> | |
27 | typedef size_t SIZE; | |
28 | typedef void *POINTER; | |
29 | #define EXCEEDS_LISP_PTR(x) 0 | |
30 | #endif | |
31 | ||
e231fd42 | 32 | #include "mem-limits.h" |
94d7c01a JA |
33 | |
34 | /* | |
35 | Level number of warnings already issued. | |
36 | 0 -- no warnings issued. | |
37 | 1 -- 75% warning already issued. | |
38 | 2 -- 85% warning already issued. | |
3b672b8f | 39 | 3 -- 95% warning issued; keep warning frequently. |
94d7c01a JA |
40 | */ |
41 | static int warnlevel; | |
42 | ||
43 | /* Function to call to issue a warning; | |
44 | 0 means don't issue them. */ | |
45 | static void (*warnfunction) (); | |
46 | ||
47 | extern POINTER sbrk (); | |
48 | ||
49 | /* Get more memory space, complaining if we're near the end. */ | |
50 | ||
51 | static POINTER | |
52 | morecore_with_warning (size) | |
53 | register int size; | |
54 | { | |
55 | POINTER result; | |
56 | register POINTER cp; | |
3b672b8f RS |
57 | int five_percent; |
58 | int data_size; | |
94d7c01a JA |
59 | |
60 | if (lim_data == 0) | |
61 | get_lim_data (); | |
3b672b8f | 62 | five_percent = lim_data / 20; |
94d7c01a JA |
63 | |
64 | /* Find current end of memory and issue warning if getting near max */ | |
65 | cp = sbrk (0); | |
3b672b8f | 66 | data_size = cp - data_space_start; |
94d7c01a JA |
67 | |
68 | if (warnfunction) | |
69 | switch (warnlevel) | |
70 | { | |
71 | case 0: | |
3b672b8f | 72 | if (data_size > five_percent * 15) |
94d7c01a JA |
73 | { |
74 | warnlevel++; | |
3b672b8f | 75 | (*warn_function) ("Warning: past 75% of memory limit"); |
94d7c01a JA |
76 | } |
77 | break; | |
78 | ||
79 | case 1: | |
3b672b8f | 80 | if (data_size > five_percent * 17) |
94d7c01a JA |
81 | { |
82 | warnlevel++; | |
3b672b8f | 83 | (*warn_function) ("Warning: past 85% of memory limit"); |
94d7c01a JA |
84 | } |
85 | break; | |
86 | ||
87 | case 2: | |
3b672b8f | 88 | if (data_size > five_percent * 19) |
94d7c01a JA |
89 | { |
90 | warnlevel++; | |
3b672b8f | 91 | (*warn_function) ("Warning: past 95% of memory limit"); |
94d7c01a JA |
92 | } |
93 | break; | |
94 | ||
95 | default: | |
3b672b8f | 96 | (*warn_function) ("Warning: past acceptable memory limits"); |
94d7c01a JA |
97 | break; |
98 | } | |
99 | ||
3b672b8f RS |
100 | /* If we go down below 70% full, issue another 75% warning |
101 | when we go up again. */ | |
102 | if (data_size < five_percent * 14) | |
103 | warnlevel = 0; | |
104 | /* If we go down below 80% full, issue another 85% warning | |
105 | when we go up again. */ | |
106 | else if (warnlevel > 1 && data_size < five_percent * 16) | |
107 | warnlevel = 1; | |
108 | /* If we go down below 90% full, issue another 95% warning | |
109 | when we go up again. */ | |
110 | else if (warnlevel > 2 && data_size < five_percent * 18) | |
111 | warnlevel = 2; | |
112 | ||
113 | if (EXCEEDS_LISP_PTR (cp)) | |
94d7c01a JA |
114 | (*warnfunction) ("Warning: memory in use exceeds lisp pointer size"); |
115 | ||
116 | result = sbrk (size); | |
117 | if (result == (POINTER) -1) | |
118 | return NULL; | |
119 | return result; | |
120 | } | |
121 | ||
122 | /* Cause reinitialization based on job parameters; | |
123 | also declare where the end of pure storage is. */ | |
124 | ||
125 | void | |
3b672b8f | 126 | memory_warnings (start, warnfun) |
94d7c01a JA |
127 | POINTER start; |
128 | void (*warnfun) (); | |
129 | { | |
130 | extern POINTER (* __morecore) (); /* From gmalloc.c */ | |
131 | ||
132 | if (start) | |
133 | data_space_start = start; | |
3b672b8f RS |
134 | else |
135 | data_space_start = start_of_data (); | |
136 | ||
94d7c01a JA |
137 | warnfunction = warnfun; |
138 | __morecore = &morecore_with_warning; | |
139 | } |