Permalink
Browse files

Added Win32 mutex implementation, made os/mmap portable, made closure…

…s work under Win32
  • Loading branch information...
1 parent f07565d commit c5980c2aae2792ee69ac849db30a2fcaf2ac4570 nddrylliog committed Jun 13, 2010
@@ -6,60 +6,80 @@ errno: extern Int
strerror: extern func (Int) -> String
BinarySeq: class {
-
+
data : UChar*
size : SizeT
index := 0
transTable := HashMap<String, Int> new()
-
+
init: func ~withData (=size, d: UChar*) {
init(size)
index = size
memcpy(data, d, size * UChar size)
}
-
+
init: func ~withSize (=size) {
memsize := size * UChar size
// at least 4096, and a multiple of 4096 that is bigger than memsize
realsize := memsize + 4096 - (memsize % 4096)
data = gc_malloc(realsize)
- result := mprotect(data, realsize, PROT_READ | PROT_WRITE | PROT_EXEC)
- if(result != 0) {
- printf("mprotect(%p, %zd) failed with code %d. Message = %s\n", data, realsize, result, strerror(errno))
+
+ version (linux || apple) {
+ result := mprotect(data, realsize, PROT_READ | PROT_WRITE | PROT_EXEC)
+ if(result != 0) {
+ Exception new(This, "mprotect(%p, %zd) failed with code %d. Message = %s\n" format(data, realsize, result, strerror(errno))) throw()
+ }
}
+
+ version (windows) {
+ // TODO: it seems that it works fine under Windows XP+mingw+GCC4.5 without it
+ // on the other hand, it returns false with it and throws the exception.
+ // Does anyone have any clue what's going on? We can't use VirtualAlloc because
+ // it would leak :x
+
+ /*
+ result := VirtualProtect(data, realsize, PAGE_READWRITE, null)
+ if(!result) {
+ Exception new(This, "VirtualProtect(%p, %zd) failed !\n" format(data, realsize)) throw()
+ }
+ */
+ }
+
+ version (!(linux || apple || windows)) {
+ Exception new(This, "Closures aren't supported on your platform (yet) !") throw()
+ }
+
initTransTable()
- // mmap is leaking (cause we don't know when to free), and apparently not needed, but just in case, here's the correct call
- //data = mmap(null, memsize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS, -1, 0)
}
-
+
initTransTable: func {
transTable["c"] = Char size
transTable["d"] = Double size
- transTable["f"] = Float size
- transTable["h"] = Short size
- transTable["i"] = Int size
+ transTable["f"] = Float size
+ transTable["h"] = Short size
+ transTable["i"] = Int size
transTable["l"] = Long size
transTable["P"] = Pointer size
}
append: func ~other (other: BinarySeq) -> BinarySeq {
append(other data, other size)
}
-
+
append: func ~withLength (ptr: Pointer, ptrLength: SizeT) -> BinarySeq {
memcpy(data + index, ptr, ptrLength)
index += ptrLength
return this
}
-
+
reset: func { index = 0 }
-
+
print: func {
for(i : Int in 0..index)
printf("%.2x ", data[i])
- println()
+ println()
}
-
+
}
operator += (b1, b2 : BinarySeq) -> BinarySeq {
View
@@ -10,154 +10,156 @@ version(!linux) {
}
version(windows) {
- include windows
+ include windows
}
/* covers & functions */
version(windows) {
- SystemTime: cover from SYSTEMTIME {
- wHour, wMinute, wSecond, wMilliseconds : extern UShort
- }
+ SystemTime: cover from SYSTEMTIME {
+ wHour, wMinute, wSecond, wMilliseconds : extern UShort
+ }
timeGetTime: extern func -> UInt32
- GetLocalTime: extern func (SystemTime*)
- Sleep: extern func (UInt)
+ GetLocalTime: extern func (SystemTime*)
+ Sleep: extern func (UInt)
}
version(!windows) {
- TimeT: cover from time_t
- TimeZone: cover from struct timezone
- TMStruct: cover from struct tm {
- tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst : extern Int
- }
- TimeVal: cover from struct timeval {
- tv_sec: extern TimeT
- tv_usec: extern Int
- }
-
- time: extern proto func (TimeT*) -> TimeT
- localtime: extern func (TimeT*) -> TMStruct*
- gettimeofday: extern func (TimeVal*, TimeZone*) -> Int
- usleep: extern func (UInt)
+ TimeT: cover from time_t
+ TimeZone: cover from struct timezone
+ TMStruct: cover from struct tm {
+ tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst : extern Int
+ }
+ TimeVal: cover from struct timeval {
+ tv_sec: extern TimeT
+ tv_usec: extern Int
+ }
+
+ time: extern proto func (TimeT*) -> TimeT
+ localtime: extern func (TimeT*) -> TMStruct*
+ gettimeofday: extern func (TimeVal*, TimeZone*) -> Int
+ usleep: extern func (UInt)
}
/* implementation */
Time: class {
__time_millisec_base := static This runTime
-
- /**
- Returns the microseconds that have elapsed in the current minute.
- */
- microtime: static func -> LLong {
- return microsec() as LLong + (sec() as LLong) * 1_000_000
- }
-
- /**
- Returns the microseconds that have elapsed in the current second.
- */
- microsec: static func -> UInt {
- version(windows) {
- st: SystemTime
- GetLocalTime(st&)
- return st wMilliseconds * 1000
- }
- version(!windows) {
- tv : TimeVal
- gettimeofday(tv&, null)
- return tv tv_usec
- }
- return -1
- }
-
- /**
- Gets the number of milliseconds elapsed since program start.
- */
- runTime: static UInt {
- get {
- t: ULLong
- version(windows) {
- // NOTE: timeGetTime only returns a 32-bit integer. the upside is
- // that it's accurate to 1ms, but unfortunately rollover is very
- // possible
- timeGetTime() as UInt - __time_millisec_base
- }
+
+ /**
+ Returns the microseconds that have elapsed in the current minute.
+ */
+ microtime: static func -> LLong {
+ return microsec() as LLong + (sec() as LLong) * 1_000_000
+ }
+
+ /**
+ Returns the microseconds that have elapsed in the current second.
+ */
+ microsec: static func -> UInt {
+ version(windows) {
+ st: SystemTime
+ GetLocalTime(st&)
+ return st wMilliseconds * 1000
+ }
+ version(!windows) {
+ tv : TimeVal
+ gettimeofday(tv&, null)
+ return tv tv_usec
+ }
+ return -1
+ }
+
+ /**
+ Gets the number of milliseconds elapsed since program start.
+ */
+ runTime: static UInt {
+ get {
+ t: ULLong
+ version(windows) {
+ // NOTE: timeGetTime only returns a 32-bit integer. the upside is
+ // that it's accurate to 1ms, but unfortunately rollover is very
+ // possible
+
+ // FIXME: getting undefined reference for now :x
+ //timeGetTime() as UInt - __time_millisec_base
+ }
version(!windows) {
tv : TimeVal
gettimeofday(tv&, null)
return ((tv tv_usec / 1000 + tv tv_sec * 1000) - __time_millisec_base) as UInt
- }
- return -1
- }
- }
-
- /**
- Returns the seconds that have elapsed in the current minute.
- */
- sec: static func -> UInt {
- version(windows) {
- st: SystemTime
- GetLocalTime(st&)
- return st wSecond
- }
- version(!windows) {
- tt := time(null)
- val := localtime(tt&)
- return val@ tm_sec
- }
- return -1
- }
-
- /**
- Returns the minutes that have elapsed in the current hour.
- */
- min: static func -> UInt {
- version(windows) {
- st: SystemTime
- GetLocalTime(st&)
- return st wMinute
- }
- version(!windows) {
- tt := time(null)
- val := localtime(tt&)
- return val@ tm_min
- }
- return -1
- }
-
- /**
- Returns the hours that have elapsed in the current day.
- */
- hour: static func -> UInt {
- version(windows) {
- st: SystemTime
- GetLocalTime(st&)
- return st wHour
- }
- version(!windows) {
- tt := time(null)
- val := localtime(tt&)
- return val@ tm_hour
- }
- return -1
- }
-
- sleepSec: static func (duration: Float) {
- sleepMicro(duration * 1_000_000)
- }
-
- sleepMilli: static func (duration: UInt) {
- sleepMicro(duration * 1_000)
- }
-
- sleepMicro: static func (duration: UInt) {
- version(windows) {
- Sleep(duration / 1_000)
- }
- version(!windows) {
- usleep(duration)
- }
- }
+ }
+ return -1
+ }
+ }
+
+ /**
+ Returns the seconds that have elapsed in the current minute.
+ */
+ sec: static func -> UInt {
+ version(windows) {
+ st: SystemTime
+ GetLocalTime(st&)
+ return st wSecond
+ }
+ version(!windows) {
+ tt := time(null)
+ val := localtime(tt&)
+ return val@ tm_sec
+ }
+ return -1
+ }
+
+ /**
+ Returns the minutes that have elapsed in the current hour.
+ */
+ min: static func -> UInt {
+ version(windows) {
+ st: SystemTime
+ GetLocalTime(st&)
+ return st wMinute
+ }
+ version(!windows) {
+ tt := time(null)
+ val := localtime(tt&)
+ return val@ tm_min
+ }
+ return -1
+ }
+
+ /**
+ Returns the hours that have elapsed in the current day.
+ */
+ hour: static func -> UInt {
+ version(windows) {
+ st: SystemTime
+ GetLocalTime(st&)
+ return st wHour
+ }
+ version(!windows) {
+ tt := time(null)
+ val := localtime(tt&)
+ return val@ tm_hour
+ }
+ return -1
+ }
+
+ sleepSec: static func (duration: Float) {
+ sleepMicro(duration * 1_000_000)
+ }
+
+ sleepMilli: static func (duration: UInt) {
+ sleepMicro(duration * 1_000)
+ }
+
+ sleepMicro: static func (duration: UInt) {
+ version(windows) {
+ Sleep(duration / 1_000)
+ }
+ version(!windows) {
+ usleep(duration)
+ }
+ }
}
Oops, something went wrong.

0 comments on commit c5980c2

Please sign in to comment.