summaryrefslogtreecommitdiff
blob: 0701363cf38a120d89d215b4d3e2ec3565f9e3c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
diff -Naur cacao/cacao/src/vm/options.cpp cacao/cacao/src/vm/options.cpp
--- cacao/cacao/src/vm/options.cpp	2014-12-12 21:14:45.000000000 +0000
+++ cacao/cacao/src/vm/options.cpp	2015-12-23 21:01:37.644275263 +0000
@@ -26,6 +26,7 @@
 #include "config.h"
 
 #include <limits.h>
+#include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -60,9 +61,9 @@
 
 bool opt_run = true;
 
-s4   opt_heapmaxsize   = 0;     /* maximum heap size                          */
-s4   opt_heapstartsize = 0;     /* initial heap size                          */
-s4   opt_stacksize     = 0;     /* thread stack size                          */
+size_t opt_heapmaxsize   = 0;   /* maximum heap size                          */
+size_t opt_heapstartsize = 0;   /* initial heap size                          */
+size_t opt_stacksize     = 0;   /* thread stack size                          */
 
 bool opt_verbose = false;
 bool opt_debugcolor = false;	/* use ANSI terminal sequences 		      */
diff -Naur cacao/cacao/src/vm/options.hpp cacao/cacao/src/vm/options.hpp
--- cacao/cacao/src/vm/options.hpp	2014-12-12 21:14:45.000000000 +0000
+++ cacao/cacao/src/vm/options.hpp	2015-12-23 21:01:37.645275246 +0000
@@ -26,6 +26,7 @@
 #ifndef OPTIONS_HPP_
 #define OPTIONS_HPP_ 1
 
+#include <stddef.h>                     // for size_t
 #include <stdint.h>                     // for int64_t
 #include <stdio.h>                      // for FILE
 #include "config.h"                     // for ENABLE_DEBUG_FILTER, etc
@@ -77,9 +78,9 @@
 extern bool opt_jar;
 extern bool opt_run;
 
-extern s4   opt_heapmaxsize;
-extern s4   opt_heapstartsize;
-extern s4   opt_stacksize;
+extern size_t opt_heapmaxsize;
+extern size_t opt_heapstartsize;
+extern size_t opt_stacksize;
 
 extern bool opt_verbose;
 extern bool opt_debugcolor;
diff -Naur cacao/cacao/src/vm/vm.cpp cacao/cacao/src/vm/vm.cpp
--- cacao/cacao/src/vm/vm.cpp	2014-12-12 21:14:45.000000000 +0000
+++ cacao/cacao/src/vm/vm.cpp	2015-12-23 21:01:38.046268504 +0000
@@ -29,9 +29,14 @@
 #include <cerrno>
 #include <cstdlib>
 #include <exception>
+#include <stddef.h>
 #include <stdint.h>
 #include <inttypes.h>
 
+#if defined(__LINUX__)
+#include <unistd.h>
+#endif
+
 #include "md-abi.hpp"
 
 #include "mm/codememory.hpp"
@@ -690,6 +695,19 @@
 	opt_heapstartsize = HEAP_STARTSIZE;
 	opt_stacksize     = STACK_SIZE;
 
+#if defined(__LINUX__)
+	// Calculate 1/4 of the physical memory.
+	size_t qmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / 4;
+
+	if (qmem > INT32_MAX) {
+		// Allocate no more than 2GB.
+		opt_heapmaxsize = INT32_MAX;
+	} else if (qmem > HEAP_MAXSIZE) {
+		// Otherwise use this if greater than default (128MB).
+		opt_heapmaxsize = qmem;
+	}
+#endif
+
 	// First of all, parse the -XX options.
 	options_xx(vm_args);
 
@@ -896,18 +914,33 @@
 		case OPT_SS:
 			{
 				char c;
-				int j;
+				size_t j;
 
+				errno = 0;
 				c = opt_arg[strlen(opt_arg) - 1];
+				j = strtoul(opt_arg, NULL, 10);
+
+				if (errno)
+					break; // Invalid.
 
 				if ((c == 'k') || (c == 'K')) {
-					j = atoi(opt_arg) * 1024;
+					if (j > SIZE_MAX / 1024)
+						break; // Overflow.
+					else
+						j *= 1024;
 
 				} else if ((c == 'm') || (c == 'M')) {
-					j = atoi(opt_arg) * 1024 * 1024;
-
-				} else
-					j = atoi(opt_arg);
+					if (j > SIZE_MAX / 1024 / 1024)
+						break; // Overflow.
+					else
+						j *= 1024 * 1024;
+
+				} else if ((c == 'g') || (c == 'G')) {
+					if (j > SIZE_MAX / 1024 / 1024 / 1024)
+						break; // Overflow.
+					else
+						j *= 1024 * 1024 * 1024;
+				}
 
 				if (opt == OPT_MX)
 					opt_heapmaxsize = j;
@@ -1498,9 +1531,9 @@
 void VM::print_run_time_config()
 {
 	puts("Run-time variables:\n");
-	printf("  maximum heap size              : %d\n", opt_heapmaxsize);
-	printf("  initial heap size              : %d\n", opt_heapstartsize);
-	printf("  stack size                     : %d\n", opt_stacksize);
+	printf("  maximum heap size              : %lu\n", opt_heapmaxsize);
+	printf("  initial heap size              : %lu\n", opt_heapstartsize);
+	printf("  stack size                     : %lu\n", opt_stacksize);
 
 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
 	printf("  gnu.classpath.boot.library.path: %s\n", _properties.get("gnu.classpath.boot.library.path"));