]> gitweb.factorcode.org Git - factor.git/blob - vm/os-macosx.mm
vm: add some allocates memory comments.
[factor.git] / vm / os-macosx.mm
1 #import <Cocoa/Cocoa.h>
2
3 #include <mach/mach_time.h>
4 #include <sys/utsname.h>
5
6 #include "master.hpp"
7
8 namespace factor {
9
10 void factor_vm::c_to_factor_toplevel(cell quot) { c_to_factor(quot); }
11
12 // Darwin 9 is 10.5, Darwin 10 is 10.6
13 // http://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
14 void early_init(void) {
15   struct utsname u;
16   int n;
17   uname(&u);
18   sscanf(u.release, "%d", &n);
19   if (n < 9) {
20     std::cout << "Factor requires Mac OS X 10.5 or later.\n";
21     exit(1);
22   }
23 }
24
25 // You must free() this yourself.
26 const char* vm_executable_path(void) {
27   return safe_strdup([[[NSBundle mainBundle] executablePath] UTF8String]);
28 }
29
30 const char* default_image_path(void) {
31   NSBundle* bundle = [NSBundle mainBundle];
32   NSString* path = [bundle bundlePath];
33   NSString* executablePath = [[bundle executablePath] stringByResolvingSymlinksInPath];
34   NSString* executable = [executablePath lastPathComponent];
35   NSString* image = [executable stringByAppendingString:@".image"];
36
37   NSString* returnVal;
38
39   if ([path hasSuffix:@".app"] || [path hasSuffix:@".app/"]) {
40     NSFileManager* mgr = [NSFileManager defaultManager];
41
42     NSString* imageInBundle =
43         [[path stringByAppendingPathComponent:@"Contents/Resources"]
44             stringByAppendingPathComponent:image];
45     NSString* imageAlongBundle = [[path stringByDeletingLastPathComponent]
46         stringByAppendingPathComponent:image];
47
48     returnVal = ([mgr fileExistsAtPath:imageInBundle] ? imageInBundle
49                                                       : imageAlongBundle);
50   } else if ([executablePath hasSuffix:@".app/Contents/MacOS/factor"]) {
51     returnVal = executablePath;
52     returnVal = [returnVal stringByDeletingLastPathComponent];
53     returnVal = [returnVal stringByDeletingLastPathComponent];
54     returnVal = [returnVal stringByDeletingLastPathComponent];
55     returnVal = [returnVal stringByDeletingLastPathComponent];
56     returnVal = [returnVal stringByAppendingPathComponent:image];
57   } else {
58     returnVal = [path stringByAppendingPathComponent:image];
59   }
60
61   return [returnVal UTF8String];
62 }
63
64 void factor_vm::init_signals(void) {
65   unix_init_signals();
66   mach_initialize();
67 }
68
69 // Amateurs at Apple: implement this function, properly!
70 Protocol* objc_getProtocol(char* name) {
71   if (strcmp(name, "NSTextInput") == 0)
72     return @protocol(NSTextInput);
73   else
74     return nil;
75 }
76
77 uint64_t nano_count() {
78   uint64_t time = mach_absolute_time();
79
80   static uint64_t scaling_factor = 0;
81   if (!scaling_factor) {
82     mach_timebase_info_data_t info;
83     kern_return_t ret = mach_timebase_info(&info);
84     if (ret != 0)
85       fatal_error("mach_timebase_info failed", ret);
86     scaling_factor = info.numer / info.denom;
87   }
88
89   return time * scaling_factor;
90 }
91
92 }