Coverity: Reverse Negative: CID 210
[sxemacs] / src / mem-limits.h
1 /* Includes for memory limit warnings.
2    Copyright (C) 1990, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of SXEmacs
5
6 SXEmacs 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
9 (at your option) any later version.
10
11 SXEmacs 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 this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19
20 /* Synched up with: FSF 19.30. */
21
22 /* #### This ancient code really sucks.
23    configure should check for:
24    HAVE_SYS_RESOURCE_H, HAVE_ULIMIT_H, HAVE_GETRLIMIT, HAVE_ULIMIT,
25    and select action based on those values.
26    getrlimit() should be preferred to ulimit().
27    On Linux, ulimit() is deprecated and always returns -1. */
28
29 #ifndef INCLUDED_mem_limits_h_
30 #define INCLUDED_mem_limits_h_
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef HAVE_ULIMIT_H
37 #include <ulimit.h>
38 #endif
39
40 /* Some systems need this before <sys/resource.h>.  */
41 #include <sys/types.h>
42
43 #ifdef _LIBC
44
45 #include <sys/resource.h>
46 #define BSD4_2                  /* Tell code below to use getrlimit.  */
47
48 /* Old Linux startup code won't define __data_start.  */
49 extern int etext, __data_start;
50 weak_symbol(__data_start)
51 #define start_of_data() (&__data_start ?: &etext)
52 #else                           /* not GNU libc */
53
54 #if defined (__osf__) && (defined (__mips) || defined (mips) || defined (__alpha))
55 #include <sys/time.h>
56 #include <sys/resource.h>
57 #endif
58
59 #if defined(__bsdi__) || defined(__NetBSD__) || defined(__linux__) || defined(__OpenBSD__)
60 #define BSD4_2
61 #endif
62
63 #ifndef BSD4_2
64 #ifndef USG
65 #include <sys/vlimit.h>
66 #endif                          /* not USG */
67 #else                           /* if BSD4_2 */
68 #include <sys/time.h>
69 #include <sys/resource.h>
70 #endif                          /* BSD4_2 */
71
72 #ifdef emacs
73 /* The important properties of this type are that 1) it's a pointer, and
74    2) arithmetic on it should work as if the size of the object pointed
75    to has a size of 1.  */
76 #ifdef __STDC__
77 typedef void *POINTER;
78 #else
79 typedef char *POINTER;
80 #endif
81
82 typedef unsigned long SIZE;
83
84 extern POINTER start_of_data(void);
85 #define EXCEEDS_LISP_PTR(ptr) 0
86
87 #ifdef BSD
88 extern int etext;
89 #define start_of_data() &etext
90 #endif
91
92 #else                           /* not emacs */
93 extern char etext;
94 #define start_of_data() &etext
95 #endif                          /* not emacs */
96
97 #endif                          /* not _LIBC */
98
99 /* start of data space; can be changed by calling malloc_init */
100 static POINTER data_space_start;
101
102 /* Number of bytes of writable memory we can expect to be able to get */
103 #ifdef _RLIM_T_DECLARED
104 extern rlim_t lim_data;
105 #else
106 extern unsigned long lim_data;
107 #endif
108
109 #if defined (HEAP_IN_DATA) && !defined(PDUMP)
110 extern unsigned long static_heap_size;
111 extern int initialized;
112 static inline void get_lim_data(void)
113 {
114         if (!initialized) {
115                 lim_data = (unsigned long)-1;   /* static_heap_size; */
116         } else {
117                 lim_data = (unsigned long)-1;
118         }
119 }
120 #else
121 #ifdef NO_LIM_DATA
122 static inline void get_lim_data(void)
123 {
124         lim_data = (unsigned long)-1;
125 }
126 #else                           /* not NO_LIM_DATA */
127
128 #if defined(USG) && !defined(LINUX)
129
130 static inline void get_lim_data(void)
131 {
132         lim_data = (unsigned long)-1;
133
134         /* Use the ulimit call, if we seem to have it.  */
135 #if !defined (ULIMIT_BREAK_VALUE)
136         lim_data = ulimit(3, 0);
137 #endif
138
139         /* If that didn't work, just use the macro's value.  */
140 #ifdef ULIMIT_BREAK_VALUE
141         if (lim_data == (unsigned long)-1)
142                 lim_data = ULIMIT_BREAK_VALUE;
143 #endif
144
145         lim_data -= (long)data_space_start;
146 }
147
148 #else                           /* not USG */
149 #if !defined (BSD4_2) && !defined (__osf__) && !defined(LINUX)
150
151 static inline void get_lim_data(void)
152 {
153         lim_data = vlimit(LIM_DATA, -1);
154 }
155
156 #else                           /* BSD4_2 */
157
158 static inline void get_lim_data(void)
159 {
160         struct rlimit XXrlimit;
161
162         getrlimit(RLIMIT_DATA, &XXrlimit);
163 #ifdef RLIM_INFINITY
164         lim_data = XXrlimit.rlim_cur & RLIM_INFINITY;   /* soft limit */
165 #else
166         lim_data = XXrlimit.rlim_cur;   /* soft limit */
167 #endif
168 }
169 #endif                          /* BSD4_2 */
170 #endif                          /* not USG */
171 #endif                          /* not NO_LIM_DATA */
172 #endif                          /* not HEAP_IN_DATA */
173
174 #endif                          /* INCLUDED_mem_limits_h_ */