c_src: update
[nit.git] / c_src / nit.common.h
index 602c33b..ce5af57 100644 (file)
@@ -2,6 +2,12 @@
 #include <stdio.h>
 #include <string.h>
 #include "gc_chooser.h"
+#ifdef ANDROID
+       #include <android/log.h>
+       #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, "Nit", __VA_ARGS__)
+#else
+       #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)
+#endif
 typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */
 typedef union {
 void* val;
@@ -17,16 +23,49 @@ struct type { int id; const char *name; int color; short int is_nullable; const
 struct instance { const struct type *type; const struct class *class; nitattribute_t attrs[]; }; /* general C type representing a Nit instance. */
 struct types { int dummy; const struct type *types[]; }; /* a list types (used for vts, fts and unresolved lists). */
 typedef struct instance val; /* general C type representing a Nit instance. */
-struct nitni_instance {struct instance *value;};
-void show_backtrace(int);
+struct nitni_instance {
+       struct nitni_instance *next,
+               *prev; /* adjacent global references in global list */
+       int count; /* number of time this global reference has been marked */
+       struct instance *value;
+};
+
+/* Native reference to Nit objects */
+/* This structure is used to represent every Nit type in extern methods and custom C code. */
+struct nitni_ref {
+       struct nitni_ref *next,
+               *prev; /* adjacent global references in global list */
+       int count; /* number of time this global reference has been marked */
+};
+
+/* List of global references from C code to Nit objects */
+/* Instanciated empty at init of Nit system and filled explicitly by user in C code */
+struct nitni_global_ref_list_t {
+       struct nitni_ref *head, *tail;
+};
+extern struct nitni_global_ref_list_t *nitni_global_ref_list;
+
+/* Initializer of global reference list */
+extern void nitni_global_ref_list_init();
+
+/* Intern function to add a global reference to the list */
+extern void nitni_global_ref_add( struct nitni_ref *ref );
+
+/* Intern function to remove a global reference from the list */
+extern void nitni_global_ref_remove( struct nitni_ref *ref );
+
+/* Increase count on an existing global reference */
+extern void nitni_global_ref_incr( struct nitni_ref *ref );
+
+/* Decrease count on an existing global reference */
+extern void nitni_global_ref_decr( struct nitni_ref *ref );
+
+void show_backtrace(int) __attribute__ ((noreturn));
+#define likely(x)       __builtin_expect((x),1)
+#define unlikely(x)     __builtin_expect((x),0)
 extern int glob_argc;
 extern char **glob_argv;
 extern val *glob_sys;
-struct instance_string__NativeString {
-const struct type *type;
-const struct class *class;
-char* value;
-};
 struct instance_kernel__Bool {
 const struct type *type;
 const struct class *class;
@@ -55,5 +94,6 @@ void* value;
 struct instance_array__NativeArray {
 const struct type *type;
 const struct class *class;
+int length;
 val* values[0];
 };