This source file includes following definitions.
- get_lim_data
- get_lim_data
- get_lim_data
- ret_lim_data
- check_memory_limits
- memory_warnings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20 #include <unistd.h>
21 #include "lisp.h"
22
23 #ifdef MSDOS
24 #include "dosfns.h"
25 extern int etext;
26 #endif
27
28
29 #include <sys/types.h>
30
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>
37 # endif
38 #endif
39
40
41
42 #ifdef DATA_START
43 # define data_start ((char *) DATA_START)
44 #else
45 extern char data_start[];
46 # ifndef HAVE_DATA_START
47
48
49
50 char data_start[1] = { 1 };
51 # endif
52 #endif
53
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
64
65
66 #ifdef REL_ALLOC
67 extern void *(*real_morecore) (ptrdiff_t);
68 #endif
69
70
71
72
73
74
75
76
77 enum warnlevel { not_warned, warned_75, warned_85, warned_95 };
78 static enum warnlevel warnlevel;
79
80
81
82 static void (*warn_function) (const char *);
83
84
85 static char *data_space_start;
86
87
88 static size_t lim_data;
89
90 #ifdef HAVE_GETRLIMIT
91
92 # ifndef RLIMIT_AS
93 # define RLIMIT_AS RLIMIT_DATA
94 # endif
95
96 static void
97 get_lim_data (void)
98 {
99
100
101
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);
107 }
108
109 #elif defined WINDOWSNT
110
111 #include "w32heap.h"
112
113 static void
114 get_lim_data (void)
115 {
116 extern size_t reserved_heap_size;
117 lim_data = reserved_heap_size;
118 }
119
120 #elif defined MSDOS
121
122 void
123 get_lim_data (void)
124 {
125 unsigned long totalram, freeram, totalswap, freeswap;
126
127 dos_memory_info (&totalram, &freeram, &totalswap, &freeswap);
128 lim_data = freeram;
129
130 if (lim_data > 512U * 1024U * 1024U)
131 lim_data = 512U * 1024U * 1024U;
132 }
133
134 unsigned long
135 ret_lim_data (void)
136 {
137 get_lim_data ();
138 return lim_data;
139 }
140 #else
141 # error "get_lim_data not implemented on this machine"
142 #endif
143
144
145
146 static void
147 check_memory_limits (void)
148 {
149 #ifndef REL_ALLOC
150 void *(*real_morecore) (ptrdiff_t) = 0;
151 #endif
152
153 char *cp;
154 size_t five_percent;
155 size_t data_size;
156 enum warnlevel new_warnlevel;
157
158 if (lim_data == 0)
159 get_lim_data ();
160 five_percent = lim_data / 20;
161
162
163 cp = (real_morecore ? real_morecore : __morecore) (0);
164 data_size = cp - data_space_start;
165
166 if (!warn_function)
167 return;
168
169
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;
175
176
177 if (new_warnlevel > warnlevel || new_warnlevel == warned_95)
178 {
179 warnlevel = new_warnlevel;
180 static char const *const warn_diagnostic[] =
181 {
182 "Warning: past 75% of memory limit",
183 "Warning: past 85% of memory limit",
184 "Warning: past 95% of memory limit"
185 };
186 warn_function (warn_diagnostic[warnlevel - 1]);
187 }
188
189 else
190 {
191
192
193 if (data_size < five_percent * 14)
194 warnlevel = not_warned;
195
196
197 else if (warnlevel > warned_75 && data_size < five_percent * 16)
198 warnlevel = warned_75;
199
200
201 else if (warnlevel > warned_85 && data_size < five_percent * 18)
202 warnlevel = warned_85;
203 }
204 }
205
206
207
208
209
210 void
211 memory_warnings (void *start, void (*warnfun) (const char *))
212 {
213 data_space_start = start ? start : data_start;
214
215 warn_function = warnfun;
216 __after_morecore_hook = check_memory_limits;
217
218
219 lim_data = 0;
220 }