diff options
Diffstat (limited to 'src/osd/modules/lib/osdlib_win32.c')
-rw-r--r-- | src/osd/modules/lib/osdlib_win32.c | 268 |
1 files changed, 134 insertions, 134 deletions
diff --git a/src/osd/modules/lib/osdlib_win32.c b/src/osd/modules/lib/osdlib_win32.c index 4d78820f7d6..3150cdafc97 100644 --- a/src/osd/modules/lib/osdlib_win32.c +++ b/src/osd/modules/lib/osdlib_win32.c @@ -53,7 +53,7 @@ void (*s_debugger_stack_crawler)() = NULL; const char *osd_getenv(const char *name) { - return getenv(name); + return getenv(name); } @@ -63,23 +63,23 @@ const char *osd_getenv(const char *name) int osd_setenv(const char *name, const char *value, int overwrite) { - char *buf; - int result; - - if (!overwrite) - { - if (osd_getenv(name) != NULL) - return 0; - } - buf = (char *) osd_malloc_array(strlen(name)+strlen(value)+2); - sprintf(buf, "%s=%s", name, value); - result = putenv(buf); - - /* will be referenced by environment - * Therefore it is not freed here - */ - - return result; + char *buf; + int result; + + if (!overwrite) + { + if (osd_getenv(name) != NULL) + return 0; + } + buf = (char *) osd_malloc_array(strlen(name)+strlen(value)+2); + sprintf(buf, "%s=%s", name, value); + result = putenv(buf); + + /* will be referenced by environment + * Therefore it is not freed here + */ + + return result; } //============================================================ @@ -88,7 +88,7 @@ int osd_setenv(const char *name, const char *value, int overwrite) void osd_process_kill(void) { - TerminateProcess(GetCurrentProcess(), -1); + TerminateProcess(GetCurrentProcess(), -1); } //============================================================ @@ -97,13 +97,13 @@ void osd_process_kill(void) int osd_get_num_processors(void) { - SYSTEM_INFO info; + SYSTEM_INFO info; - // otherwise, fetch the info from the system - GetSystemInfo(&info); + // otherwise, fetch the info from the system + GetSystemInfo(&info); - // max out at 4 for now since scaling above that seems to do poorly - return MIN(info.dwNumberOfProcessors, 4); + // max out at 4 for now since scaling above that seems to do poorly + return MIN(info.dwNumberOfProcessors, 4); } //============================================================ @@ -113,17 +113,17 @@ int osd_get_num_processors(void) void *osd_malloc(size_t size) { #ifndef MALLOC_DEBUG - return HeapAlloc(GetProcessHeap(), 0, size); + return HeapAlloc(GetProcessHeap(), 0, size); #else - // add in space for the size - size += sizeof(size_t); + // add in space for the size + size += sizeof(size_t); - // basic objects just come from the heap - void *result = HeapAlloc(GetProcessHeap(), 0, size); + // basic objects just come from the heap + void *result = HeapAlloc(GetProcessHeap(), 0, size); - // store the size and return and pointer to the data afterward - *reinterpret_cast<size_t *>(result) = size; - return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); + // store the size and return and pointer to the data afterward + *reinterpret_cast<size_t *>(result) = size; + return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); #endif } @@ -135,30 +135,30 @@ void *osd_malloc(size_t size) void *osd_malloc_array(size_t size) { #ifndef MALLOC_DEBUG - return HeapAlloc(GetProcessHeap(), 0, size); + return HeapAlloc(GetProcessHeap(), 0, size); #else - // add in space for the size - size += sizeof(size_t); + // add in space for the size + size += sizeof(size_t); - // round the size up to a page boundary - size_t rounded_size = ((size + sizeof(void *) + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; + // round the size up to a page boundary + size_t rounded_size = ((size + sizeof(void *) + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; - // reserve that much memory, plus two guard pages - void *page_base = VirtualAlloc(NULL, rounded_size + 2 * PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS); - if (page_base == NULL) - return NULL; + // reserve that much memory, plus two guard pages + void *page_base = VirtualAlloc(NULL, rounded_size + 2 * PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS); + if (page_base == NULL) + return NULL; - // now allow access to everything but the first and last pages - page_base = VirtualAlloc(reinterpret_cast<UINT8 *>(page_base) + PAGE_SIZE, rounded_size, MEM_COMMIT, PAGE_READWRITE); - if (page_base == NULL) - return NULL; + // now allow access to everything but the first and last pages + page_base = VirtualAlloc(reinterpret_cast<UINT8 *>(page_base) + PAGE_SIZE, rounded_size, MEM_COMMIT, PAGE_READWRITE); + if (page_base == NULL) + return NULL; - // work backwards from the page base to get to the block base - void *result = GUARD_ALIGN_START ? page_base : (reinterpret_cast<UINT8 *>(page_base) + rounded_size - size); + // work backwards from the page base to get to the block base + void *result = GUARD_ALIGN_START ? page_base : (reinterpret_cast<UINT8 *>(page_base) + rounded_size - size); - // store the size at the start with a flag indicating it has a guard page - *reinterpret_cast<size_t *>(result) = size | 0x80000000; - return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); + // store the size at the start with a flag indicating it has a guard page + *reinterpret_cast<size_t *>(result) = size | 0x80000000; + return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); #endif } @@ -170,20 +170,20 @@ void *osd_malloc_array(size_t size) void osd_free(void *ptr) { #ifndef MALLOC_DEBUG - HeapFree(GetProcessHeap(), 0, ptr); + HeapFree(GetProcessHeap(), 0, ptr); #else - size_t size = reinterpret_cast<size_t *>(ptr)[-1]; - - // if no guard page, just free the pointer - if ((size & 0x80000000) == 0) - HeapFree(GetProcessHeap(), 0, reinterpret_cast<UINT8 *>(ptr) - sizeof(size_t)); - - // large items need more care - else - { - ULONG_PTR page_base = (reinterpret_cast<ULONG_PTR>(ptr) - sizeof(size_t)) & ~(PAGE_SIZE - 1); - VirtualFree(reinterpret_cast<void *>(page_base - PAGE_SIZE), 0, MEM_RELEASE); - } + size_t size = reinterpret_cast<size_t *>(ptr)[-1]; + + // if no guard page, just free the pointer + if ((size & 0x80000000) == 0) + HeapFree(GetProcessHeap(), 0, reinterpret_cast<UINT8 *>(ptr) - sizeof(size_t)); + + // large items need more care + else + { + ULONG_PTR page_base = (reinterpret_cast<ULONG_PTR>(ptr) - sizeof(size_t)) & ~(PAGE_SIZE - 1); + VirtualFree(reinterpret_cast<void *>(page_base - PAGE_SIZE), 0, MEM_RELEASE); + } #endif } @@ -197,7 +197,7 @@ void osd_free(void *ptr) void *osd_alloc_executable(size_t size) { - return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); + return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); } @@ -209,7 +209,7 @@ void *osd_alloc_executable(size_t size) void osd_free_executable(void *ptr, size_t size) { - VirtualFree(ptr, 0, MEM_RELEASE); + VirtualFree(ptr, 0, MEM_RELEASE); } @@ -220,19 +220,19 @@ void osd_free_executable(void *ptr, size_t size) void osd_break_into_debugger(const char *message) { #ifdef OSD_WINDOWS - if (IsDebuggerPresent()) - { - win_output_debug_string_utf8(message); - DebugBreak(); - } - else if (s_debugger_stack_crawler != NULL) - (*s_debugger_stack_crawler)(); + if (IsDebuggerPresent()) + { + win_output_debug_string_utf8(message); + DebugBreak(); + } + else if (s_debugger_stack_crawler != NULL) + (*s_debugger_stack_crawler)(); #else - if (IsDebuggerPresent()) - { - OutputDebugStringA(message); - DebugBreak(); - } + if (IsDebuggerPresent()) + { + OutputDebugStringA(message); + DebugBreak(); + } #endif } @@ -252,36 +252,36 @@ static BOOL using_qpc = TRUE; osd_ticks_t osd_ticks(void) { - LARGE_INTEGER performance_count; - - // if we're suspended, just return that - if (suspend_ticks != 0) - return suspend_ticks; - - // if we have a per second count, just go for it - if (ticks_per_second != 0) - { - // QueryPerformanceCounter if we can - if (using_qpc) - { - QueryPerformanceCounter(&performance_count); - return (osd_ticks_t)performance_count.QuadPart - suspend_ticks; - } - - // otherwise, fall back to timeGetTime - else - return (osd_ticks_t)timeGetTime() - suspend_ticks; - } - - // if not, we have to determine it - using_qpc = QueryPerformanceFrequency(&performance_count) && (performance_count.QuadPart != 0); - if (using_qpc) - ticks_per_second = (osd_ticks_t)performance_count.QuadPart; - else - ticks_per_second = 1000; - - // call ourselves to get the first value - return osd_ticks(); + LARGE_INTEGER performance_count; + + // if we're suspended, just return that + if (suspend_ticks != 0) + return suspend_ticks; + + // if we have a per second count, just go for it + if (ticks_per_second != 0) + { + // QueryPerformanceCounter if we can + if (using_qpc) + { + QueryPerformanceCounter(&performance_count); + return (osd_ticks_t)performance_count.QuadPart - suspend_ticks; + } + + // otherwise, fall back to timeGetTime + else + return (osd_ticks_t)timeGetTime() - suspend_ticks; + } + + // if not, we have to determine it + using_qpc = QueryPerformanceFrequency(&performance_count) && (performance_count.QuadPart != 0); + if (using_qpc) + ticks_per_second = (osd_ticks_t)performance_count.QuadPart; + else + ticks_per_second = 1000; + + // call ourselves to get the first value + return osd_ticks(); } @@ -291,9 +291,9 @@ osd_ticks_t osd_ticks(void) osd_ticks_t osd_ticks_per_second(void) { - if (ticks_per_second == 0) - osd_ticks(); - return ticks_per_second; + if (ticks_per_second == 0) + osd_ticks(); + return ticks_per_second; } //============================================================ @@ -302,28 +302,28 @@ osd_ticks_t osd_ticks_per_second(void) void osd_sleep(osd_ticks_t duration) { - DWORD msec; - - // make sure we've computed ticks_per_second - if (ticks_per_second == 0) - (void)osd_ticks(); - - // convert to milliseconds, rounding down - msec = (DWORD)(duration * 1000 / ticks_per_second); - - // only sleep if at least 2 full milliseconds - if (msec >= 2) - { - HANDLE current_thread = GetCurrentThread(); - int old_priority = GetThreadPriority(current_thread); - - // take a couple of msecs off the top for good measure - msec -= 2; - - // bump our thread priority super high so that we get - // priority when we need it - SetThreadPriority(current_thread, THREAD_PRIORITY_TIME_CRITICAL); - Sleep(msec); - SetThreadPriority(current_thread, old_priority); - } + DWORD msec; + + // make sure we've computed ticks_per_second + if (ticks_per_second == 0) + (void)osd_ticks(); + + // convert to milliseconds, rounding down + msec = (DWORD)(duration * 1000 / ticks_per_second); + + // only sleep if at least 2 full milliseconds + if (msec >= 2) + { + HANDLE current_thread = GetCurrentThread(); + int old_priority = GetThreadPriority(current_thread); + + // take a couple of msecs off the top for good measure + msec -= 2; + + // bump our thread priority super high so that we get + // priority when we need it + SetThreadPriority(current_thread, THREAD_PRIORITY_TIME_CRITICAL); + Sleep(msec); + SetThreadPriority(current_thread, old_priority); + } } |