1 /* This file is part of NIT ( http://www.nitlanguage.org ).
3 * Copyright 2006-2009 Jean Privat <jean@pryen.org>
5 * This file is free software, which comes along with NIT. This software is
6 * distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 * PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 * is kept unaltered, and a notification of the changes is added.
10 * You are allowed to redistribute it and sell it, alone or is a part of
14 #include "gc_chooser.h"
19 #include <android/log.h>
20 #define PRINT_ERROR(...) ((void)__android_log_print(ANDROID_LOG_WARN, "nit", __VA_ARGS__))
22 // FIXME bring back when the GC is fixed in Android
25 #define PRINT_ERROR(...) ((void)fprintf(stderr, __VA_ARGS__))
28 enum gc_option
{ gc_opt_large
, gc_opt_malloc
, gc_opt_boehm
} gc_option
;
34 void *nit_raw_alloc(size_t s0
)
37 case gc_opt_malloc
: return malloc(s0
);
39 case gc_opt_boehm
: return GC_MALLOC_ATOMIC(s0
);
42 default: return nit_alloc(s0
);
46 static void *large_alloc(size_t s0
)
48 static char * alloc_pos
= NULL
;
49 static size_t alloc_size
= 0;
51 size_t s
= ((s0
+3)/4)*4;
53 alloc_size
= s
+ 1024*1024;
54 alloc_pos
= (char *)calloc(alloc_size
, 1);
62 void nit_gcollect(void) {
65 case gc_opt_boehm
: GC_gcollect(); break;
67 default: break; /* nothing can be done */
71 void *nit_alloc(size_t s0
)
75 case gc_opt_boehm
: return GC_MALLOC(s0
);
77 case gc_opt_malloc
: return calloc(1, s0
);
79 default: return large_alloc(s0
);
83 void initialize_gc_option(void) {
87 gc_option
= gc_opt_boehm
;
90 gc_option
= gc_opt_malloc
;
94 /* Process GC runtime selection */
95 if (getenv("NIT_GC_OPTION") != NULL
) {
96 char *opt
=getenv("NIT_GC_OPTION");
97 if (strcmp(opt
, "boehm")==0) {
99 gc_option
= gc_opt_boehm
;
101 PRINT_ERROR( "Compiled without Boehm GC support. Using default '%s'.\n", def
);
103 } else if (strcmp(opt
, "malloc")==0) {
104 gc_option
= gc_opt_malloc
;
105 } else if (strcmp(opt
, "large")==0) {
106 gc_option
= gc_opt_large
;
107 } else if (strcmp(opt
, "help")==0) {
108 PRINT_ERROR( "NIT_GC_OPTION accepts 'malloc', 'large'"
112 ". Default is '%s'.\n", def
);
115 PRINT_ERROR( "Invalid GC option in NIT_GC_OPTION environment variable. Using default '%s'.\n", def
);
119 /* Initialize GC (if needed) */
122 case gc_opt_boehm
: GC_INIT(); break;
124 default: break; /* Nothing */
128 void gc_register_finalizer(void* obj
) {
130 GC_register_finalizer(obj
, &gc_finalize
, NULL
, NULL
, NULL
);
134 void __attribute__((weak
)) gc_finalize(void *obj
, void* client_data
) {}