unix: Allow to set heap size using "-X heapsize=N" option.

This commit is contained in:
Paul Sokolovsky 2014-03-01 12:21:53 +02:00
parent 25f5a30e73
commit a374d9c860

View File

@ -24,9 +24,10 @@
#include <readline/history.h> #include <readline/history.h>
#endif #endif
#if MICROPY_ENABLE_GC
// Heap size of GC heap (if enabled) // Heap size of GC heap (if enabled)
// TODO: allow to specify on command line long heap_size = 128*1024;
#define HEAP_SIZE 128*1024 #endif
// Stack top at the start of program // Stack top at the start of program
void *stack_top; void *stack_top;
@ -233,13 +234,34 @@ static mp_obj_t pyb_gc(void) {
MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc); MP_DEFINE_CONST_FUN_OBJ_0(pyb_gc_obj, pyb_gc);
#endif #endif
// Process options which set interpreter init options
void pre_process_options(int argc, char **argv) {
for (int a = 1; a < argc; a++) {
if (argv[a][0] == '-') {
if (strcmp(argv[a], "-X") == 0) {
if (a + 1 >= argc) {
exit(usage());
}
#if MICROPY_ENABLE_GC
if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
}
#endif
a++;
}
}
}
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
volatile int stack_dummy; volatile int stack_dummy;
stack_top = (void*)&stack_dummy; stack_top = (void*)&stack_dummy;
pre_process_options(argc, argv);
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
char *heap = malloc(HEAP_SIZE); char *heap = malloc(heap_size);
gc_init(heap, heap + HEAP_SIZE); gc_init(heap, heap + heap_size);
#endif #endif
qstr_init(); qstr_init();
@ -319,9 +341,7 @@ int main(int argc, char **argv) {
printf(" peak %d\n", m_get_peak_bytes_allocated()); printf(" peak %d\n", m_get_peak_bytes_allocated());
*/ */
if (argc == 1) { bool executed = false;
do_repl();
} else {
for (int a = 1; a < argc; a++) { for (int a = 1; a < argc; a++) {
if (argv[a][0] == '-') { if (argv[a][0] == '-') {
if (strcmp(argv[a], "-c") == 0) { if (strcmp(argv[a], "-c") == 0) {
@ -329,6 +349,9 @@ int main(int argc, char **argv) {
return usage(); return usage();
} }
do_str(argv[a + 1]); do_str(argv[a + 1]);
executed = true;
a += 1;
} else if (strcmp(argv[a], "-X") == 0) {
a += 1; a += 1;
} else { } else {
return usage(); return usage();
@ -345,9 +368,13 @@ int main(int argc, char **argv) {
rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i]))); rt_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
} }
do_file(argv[a]); do_file(argv[a]);
executed = true;
break; break;
} }
} }
if (!executed) {
do_repl();
} }
rt_deinit(); rt_deinit();