1515#include " java_lang_Runnable.h"
1616#include " java_lang_System.h"
1717#include " java_lang_ArrayIndexOutOfBoundsException.h"
18+ #if defined(__APPLE__) && defined(__OBJC__)
1819#import < mach/mach.h>
1920#import < mach/mach_host.h>
21+ #else
22+ #include < time.h>
23+ #define NSLog (...) printf(__VA_ARGS__); printf(" \n " )
24+ #endif
2025
2126// The amount of memory allocated between GC cycle checks (generally 30 seconds)
2227// that triggers "High-frequency" GC mode. When "High-frequency" mode is triggered,
@@ -70,8 +75,9 @@ static JAVA_BOOLEAN isEdt(long threadId) {
7075}
7176
7277// Gets the amount of free memory in the system.
73- static natural_t get_free_memory (void )
78+ static long get_free_memory (void )
7479 {
80+ #if defined(__APPLE__) && defined(__OBJC__)
7581 mach_port_t host_port;
7682 mach_msg_type_number_t host_size;
7783 vm_size_t pagesize;
@@ -85,8 +91,11 @@ static natural_t get_free_memory(void)
8591 return 0 ;
8692 }
8793 /* Stats in bytes */
88- natural_t mem_free = vm_stat.free_count * pagesize;
94+ long mem_free = vm_stat.free_count * pagesize;
8995 return mem_free;
96+ #else
97+ return 1024 * 1024 * 100 ; // Stub: 100MB
98+ #endif
9099 }
91100
92101// Initializes the GC thresholds based on the free memory on the device.
@@ -674,7 +683,9 @@ void preSweepCount(CODENAME_ONE_THREAD_STATE) {
674683}
675684
676685void printObjectsPostSweep (CODENAME_ONE_THREAD_STATE) {
686+ #if defined(__APPLE__) && defined(__OBJC__)
677687 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc ] init ];
688+ #endif
678689
679690 // this should be the last class used
680691 int classTypeCount[cn1_array_3_id_java_util_Vector + 1 ];
@@ -706,10 +717,14 @@ void printObjectsPostSweep(CODENAME_ONE_THREAD_STATE) {
706717 if (classTypeCount[iter] > 0 ) {
707718 if (classTypeCountPreSweep[iter] - classTypeCount[iter] > 0 ) {
708719 if (iter > cn1_array_start_offset) {
720+ #if defined(__APPLE__) && defined(__OBJC__)
709721 NSLog (@" There are %i instances of %@ taking up %i bytes, %i were cleaned which saved %i bytes" , classTypeCount[iter], [NSString stringWithUTF8String: arrayOfNames[iter]], sizeInHeapForType[iter], classTypeCountPreSweep[iter] - classTypeCount[iter], sizeInHeapForTypePreSweep[iter] - sizeInHeapForType[iter]);
722+ #endif
710723 } else {
711724 JAVA_OBJECT str = STRING_FROM_CONSTANT_POOL_OFFSET (classNameLookup[iter]);
725+ #if defined(__APPLE__) && defined(__OBJC__)
712726 NSLog (@" There are %i instances of %@ taking up %i bytes, %i were cleaned which saved %i bytes" , classTypeCount[iter], toNSString (threadStateData, str), sizeInHeapForType[iter], classTypeCountPreSweep[iter] - classTypeCount[iter], sizeInHeapForTypePreSweep[iter] - sizeInHeapForType[iter]);
727+ #endif
713728 }
714729 }
715730 actualTotalMemory += sizeInHeapForType[iter];
@@ -719,11 +734,15 @@ void printObjectsPostSweep(CODENAME_ONE_THREAD_STATE) {
719734 NSLog (@" **** GC cycle complete ****" );
720735
721736 free (arrayOfNames);
737+ #if defined(__APPLE__) && defined(__OBJC__)
722738 [pool release ];
739+ #endif
723740}
724741
725742void printObjectTypesInHeap (CODENAME_ONE_THREAD_STATE) {
743+ #if defined(__APPLE__) && defined(__OBJC__)
726744 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc ] init ];
745+ #endif
727746
728747 // this should be the last class used
729748 int classTypeCount[cn1_array_3_id_java_util_Vector + 1 ];
@@ -756,18 +775,24 @@ void printObjectTypesInHeap(CODENAME_ONE_THREAD_STATE) {
756775 float f = ((float )classTypeCount[iter]) / ((float )t) * 100 .0f ;
757776 float f2 = ((float )sizeInHeapForType[iter]) / ((float )totalAllocatedHeap) * 100 .0f ;
758777 if (iter > cn1_array_start_offset) {
778+ #if defined(__APPLE__) && defined(__OBJC__)
759779 NSLog (@" There are %i instances of %@ which is %i percent its %i bytes which is %i mem percent" , classTypeCount[iter], [NSString stringWithUTF8String: arrayOfNames[iter]], (int )f, sizeInHeapForType[iter], (int )f2);
780+ #endif
760781 } else {
761782 JAVA_OBJECT str = STRING_FROM_CONSTANT_POOL_OFFSET (classNameLookup[iter]);
783+ #if defined(__APPLE__) && defined(__OBJC__)
762784 NSLog (@" There are %i instances of %@ which is %i percent its %i bytes which is %i mem percent" , classTypeCount[iter], toNSString (threadStateData, str), (int )f, sizeInHeapForType[iter], (int )f2);
785+ #endif
763786 }
764787 actualTotalMemory += sizeInHeapForType[iter];
765788 }
766789 }
767790 NSLog (@" Actual ram = %i vs total mallocs = %i " , actualTotalMemory, totalAllocatedHeap);
768791
769792 free (arrayOfNames);
793+ #if defined(__APPLE__) && defined(__OBJC__)
770794 [pool release ];
795+ #endif
771796}
772797#endif
773798
@@ -796,6 +821,7 @@ void codenameOneGCSweep() {
796821
797822#ifdef DEBUG_GC_ALLOCATIONS
798823 int classId = o->className ;
824+ #if defined(__APPLE__) && defined(__OBJC__)
799825 NSString * whereIs;
800826 if (classId > 0 ) {
801827 whereIs = (NSString *)((struct obj__java_lang_String*)STRING_FROM_CONSTANT_POOL_OFFSET (classId))->java_lang_String_nsString ;
@@ -824,6 +850,7 @@ void codenameOneGCSweep() {
824850 }
825851 NSLog (@" Sweeping: %X , Mark: %i , Allocated: %@ %i , type: %@ , toString: '%@ '" , (int )o, o->__codenameOneGcMark , whereIs, o->line , [NSString stringWithUTF8String: o->__codenameOneParentClsReference->clsName], ns);
826852 }
853+ #endif
827854#endif
828855
829856 removeObjectFromHeapCollection (threadStateData, o);
@@ -1249,6 +1276,7 @@ void initConstantPool() {
12491276
12501277JAVA_OBJECT utf8String = NULL ;
12511278
1279+ #if defined(__APPLE__) && defined(__OBJC__)
12521280JAVA_OBJECT fromNSString (CODENAME_ONE_THREAD_STATE, NSString * str) {
12531281 if (str == nil ) {
12541282 return JAVA_NULL;
@@ -1274,6 +1302,7 @@ JAVA_OBJECT fromNSString(CODENAME_ONE_THREAD_STATE, NSString* str) {
12741302 finishedNativeAllocations ();
12751303 return s;
12761304}
1305+ #endif
12771306
12781307const char * stringToUTF8 (CODENAME_ONE_THREAD_STATE, JAVA_OBJECT str) {
12791308 if (str == NULL ) {
@@ -1308,6 +1337,7 @@ JAVA_OBJECT fromNSString(CODENAME_ONE_THREAD_STATE, NSString* str) {
13081337 return cs;
13091338}
13101339
1340+ #if defined(__APPLE__) && defined(__OBJC__)
13111341NSString * toNSString (CODENAME_ONE_THREAD_STATE, JAVA_OBJECT o) {
13121342 if (o == JAVA_NULL) {
13131343 return 0 ;
@@ -1323,6 +1353,7 @@ JAVA_OBJECT fromNSString(CODENAME_ONE_THREAD_STATE, NSString* str) {
13231353 str->java_lang_String_nsString = (JAVA_LONG)x;
13241354 return st;
13251355}
1356+ #endif
13261357
13271358JAVA_OBJECT __NEW_ARRAY_JAVA_BOOLEAN (CODENAME_ONE_THREAD_STATE, JAVA_INT size) {
13281359 JAVA_OBJECT o = allocArray (threadStateData, size, &class_array1__JAVA_BOOLEAN, sizeof (JAVA_ARRAY_BOOLEAN), 1 );
0 commit comments