This commit was generated by cvs2svn to compensate for changes in r1383,
[chise/xemacs-chise.git.1] / src / vm-limit.c
1 /* Functions for memory limit warnings.
2    Copyright (C) 1990, 1992 Free Software Foundation, Inc.
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 /* Synched up with: FSF 19.30. */
22
23 #ifdef emacs
24 #include <config.h>
25 #include "lisp.h"
26 #endif
27
28 #include <stddef.h>
29 #ifndef emacs
30 typedef size_t SIZE;
31 typedef void *POINTER;
32 #define EXCEEDS_LISP_PTR(x) 0
33 #endif
34
35 #include "mem-limits.h"
36
37 /*
38   Level number of warnings already issued.
39   0 -- no warnings issued.
40   1 -- 75% warning already issued.
41   2 -- 85% warning already issued.
42   3 -- 95% warning issued; keep warning frequently.
43 */
44 static int warnlevel;
45
46 /* Function to call to issue a warning;
47    0 means don't issue them.  */
48 static void (*warn_function) (const char *);
49
50 /* Get more memory space, complaining if we're near the end. */
51
52 static void
53 check_memory_limits (void)
54 {
55   extern POINTER (*__morecore) (ptrdiff_t size);
56
57   POINTER cp;
58   unsigned long five_percent;
59   unsigned long data_size;
60
61   if (lim_data == 0)
62     get_lim_data ();
63   five_percent = lim_data / 20;
64
65   /* Find current end of memory and issue warning if getting near max */
66   cp = (char *) (*__morecore) (0);
67   data_size = (char *) cp - (char *) data_space_start;
68
69   if (warn_function)
70     switch (warnlevel)
71       {
72       case 0:
73         if (data_size > five_percent * 15)
74           {
75             warnlevel++;
76             (*warn_function) ("Warning: past 75% of memory limit");
77           }
78         break;
79
80       case 1:
81         if (data_size > five_percent * 17)
82           {
83             warnlevel++;
84             (*warn_function) ("Warning: past 85% of memory limit");
85           }
86         break;
87
88       case 2:
89         if (data_size > five_percent * 19)
90           {
91             warnlevel++;
92             (*warn_function) ("Warning: past 95% of memory limit");
93           }
94         break;
95
96       default:
97         (*warn_function) ("Warning: past acceptable memory limits");
98         break;
99       }
100
101   /* If we go down below 70% full, issue another 75% warning
102      when we go up again.  */
103   if (data_size < five_percent * 14)
104     warnlevel = 0;
105   /* If we go down below 80% full, issue another 85% warning
106      when we go up again.  */
107   else if (warnlevel > 1 && data_size < five_percent * 16)
108     warnlevel = 1;
109   /* If we go down below 90% full, issue another 95% warning
110      when we go up again.  */
111   else if (warnlevel > 2 && data_size < five_percent * 18)
112     warnlevel = 2;
113
114   if (EXCEEDS_LISP_PTR (cp))
115     (*warn_function) ("Warning: memory in use exceeds lisp pointer size");
116 }
117
118 /* Cause reinitialization based on job parameters;
119    also declare where the end of pure storage is. */
120
121 void
122 memory_warnings (void *start, void (*warnfun) (const char *))
123 {
124   extern void (* __after_morecore_hook) (void); /* From gmalloc.c */
125
126   if (start)
127     data_space_start = (char*) start;
128   else
129     data_space_start = start_of_data ();
130
131 #ifndef _NO_MALLOC_WARNING_
132   warn_function = warnfun;
133   __after_morecore_hook = check_memory_limits;
134 #endif
135 }