trunk/src/osd/modules/lib/osdlib_macosx.c
| r0 | r242794 | |
| 1 | // This file is a placeholder. |
| 2 | |
| 3 | #include <sys/types.h> |
| 4 | #include <signal.h> |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #include <mach/mach.h> |
| 8 | #include <mach/mach_time.h> |
| 9 | #include <Carbon/Carbon.h> |
| 10 | |
| 11 | // MAME headers |
| 12 | #include "osdlib.h" |
| 13 | |
| 14 | //============================================================ |
| 15 | // osd_process_kill |
| 16 | //============================================================ |
| 17 | |
| 18 | void osd_process_kill(void) |
| 19 | { |
| 20 | kill(getpid(), SIGKILL); |
| 21 | } |
| 22 | |
| 23 | //============================================================ |
| 24 | // osd_num_processors |
| 25 | //============================================================ |
| 26 | |
| 27 | int osd_get_num_processors(void) |
| 28 | { |
| 29 | int processors = 1; |
| 30 | |
| 31 | struct host_basic_info host_basic_info; |
| 32 | unsigned int count; |
| 33 | kern_return_t r; |
| 34 | mach_port_t my_mach_host_self; |
| 35 | |
| 36 | count = HOST_BASIC_INFO_COUNT; |
| 37 | my_mach_host_self = mach_host_self(); |
| 38 | if ( ( r = host_info(my_mach_host_self, HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count)) == KERN_SUCCESS ) |
| 39 | { |
| 40 | processors = host_basic_info.avail_cpus; |
| 41 | } |
| 42 | mach_port_deallocate(mach_task_self(), my_mach_host_self); |
| 43 | |
| 44 | return processors; |
| 45 | } |
| 46 | |
| 47 | //============================================================ |
| 48 | // osd_malloc |
| 49 | //============================================================ |
| 50 | |
| 51 | void *osd_malloc(size_t size) |
| 52 | { |
| 53 | #ifndef MALLOC_DEBUG |
| 54 | return malloc(size); |
| 55 | #else |
| 56 | #error "MALLOC_DEBUG not yet supported" |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | |
| 61 | //============================================================ |
| 62 | // osd_malloc_array |
| 63 | //============================================================ |
| 64 | |
| 65 | void *osd_malloc_array(size_t size) |
| 66 | { |
| 67 | #ifndef MALLOC_DEBUG |
| 68 | return malloc(size); |
| 69 | #else |
| 70 | #error "MALLOC_DEBUG not yet supported" |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | |
| 75 | //============================================================ |
| 76 | // osd_free |
| 77 | //============================================================ |
| 78 | |
| 79 | void osd_free(void *ptr) |
| 80 | { |
| 81 | #ifndef MALLOC_DEBUG |
| 82 | free(ptr); |
| 83 | #else |
| 84 | #error "MALLOC_DEBUG not yet supported" |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | //============================================================ |
| 89 | // osd_getenv |
| 90 | //============================================================ |
| 91 | |
| 92 | char *osd_getenv(const char *name) |
| 93 | { |
| 94 | return getenv(name); |
| 95 | } |
| 96 | |
| 97 | //============================================================ |
| 98 | // osd_setenv |
| 99 | //============================================================ |
| 100 | |
| 101 | int osd_setenv(const char *name, const char *value, int overwrite) |
| 102 | { |
| 103 | return setenv(name, value, overwrite); |
| 104 | } |
trunk/src/osd/modules/lib/osdlib_unix.c
| r0 | r242794 | |
| 1 | |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <sys/mman.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <signal.h> |
| 7 | |
| 8 | #include "osdlib.h" |
| 9 | |
| 10 | //============================================================ |
| 11 | // osd_getenv |
| 12 | //============================================================ |
| 13 | |
| 14 | char *osd_getenv(const char *name) |
| 15 | { |
| 16 | return getenv(name); |
| 17 | } |
| 18 | |
| 19 | //============================================================ |
| 20 | // osd_setenv |
| 21 | //============================================================ |
| 22 | |
| 23 | int osd_setenv(const char *name, const char *value, int overwrite) |
| 24 | { |
| 25 | return setenv(name, value, overwrite); |
| 26 | } |
| 27 | |
| 28 | //============================================================ |
| 29 | // osd_num_processors |
| 30 | //============================================================ |
| 31 | |
| 32 | int osd_get_num_processors(void) |
| 33 | { |
| 34 | int processors = 1; |
| 35 | |
| 36 | #if defined(_SC_NPROCESSORS_ONLN) |
| 37 | processors = sysconf(_SC_NPROCESSORS_ONLN); |
| 38 | #endif |
| 39 | return processors; |
| 40 | } |
| 41 | |
| 42 | //============================================================ |
| 43 | // osd_process_kill |
| 44 | //============================================================ |
| 45 | |
| 46 | void osd_process_kill(void) |
| 47 | { |
| 48 | kill(getpid(), SIGKILL); |
| 49 | } |
| 50 | |
| 51 | //============================================================ |
| 52 | // osd_malloc |
| 53 | //============================================================ |
| 54 | |
| 55 | void *osd_malloc(size_t size) |
| 56 | { |
| 57 | #ifndef MALLOC_DEBUG |
| 58 | return malloc(size); |
| 59 | #else |
| 60 | #error "MALLOC_DEBUG not yet supported" |
| 61 | #endif |
| 62 | } |
| 63 | |
| 64 | |
| 65 | //============================================================ |
| 66 | // osd_malloc_array |
| 67 | //============================================================ |
| 68 | |
| 69 | void *osd_malloc_array(size_t size) |
| 70 | { |
| 71 | #ifndef MALLOC_DEBUG |
| 72 | return malloc(size); |
| 73 | #else |
| 74 | #error "MALLOC_DEBUG not yet supported" |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | |
| 79 | //============================================================ |
| 80 | // osd_free |
| 81 | //============================================================ |
| 82 | |
| 83 | void osd_free(void *ptr) |
| 84 | { |
| 85 | #ifndef MALLOC_DEBUG |
| 86 | free(ptr); |
| 87 | #else |
| 88 | #error "MALLOC_DEBUG not yet supported" |
| 89 | #endif |
| 90 | } |
trunk/src/osd/modules/lib/osdlib_win32.c
| r0 | r242794 | |
| 1 | //============================================================ |
| 2 | // |
| 3 | // winos.c - Win32 OS specific low level code |
| 4 | // |
| 5 | //============================================================ |
| 6 | |
| 7 | #define WIN32_LEAN_AND_MEAN |
| 8 | #include <windows.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | // MAME headers |
| 13 | #include "osdlib.h" |
| 14 | #include "osdcomm.h" |
| 15 | #include "osdcore.h" |
| 16 | |
| 17 | //============================================================ |
| 18 | // MACROS |
| 19 | //============================================================ |
| 20 | |
| 21 | // presumed size of a page of memory |
| 22 | #define PAGE_SIZE 4096 |
| 23 | |
| 24 | // align allocations to start or end of the page? |
| 25 | #define GUARD_ALIGN_START 0 |
| 26 | |
| 27 | //============================================================ |
| 28 | // osd_num_processors |
| 29 | //============================================================ |
| 30 | |
| 31 | int osd_get_num_processors(void) |
| 32 | { |
| 33 | SYSTEM_INFO info; |
| 34 | |
| 35 | // otherwise, fetch the info from the system |
| 36 | GetSystemInfo(&info); |
| 37 | |
| 38 | // max out at 4 for now since scaling above that seems to do poorly |
| 39 | return MIN(info.dwNumberOfProcessors, 4); |
| 40 | } |
| 41 | |
| 42 | //============================================================ |
| 43 | // osd_getenv |
| 44 | //============================================================ |
| 45 | |
| 46 | char *osd_getenv(const char *name) |
| 47 | { |
| 48 | return getenv(name); |
| 49 | } |
| 50 | |
| 51 | //============================================================ |
| 52 | // osd_setenv |
| 53 | //============================================================ |
| 54 | |
| 55 | int osd_setenv(const char *name, const char *value, int overwrite) |
| 56 | { |
| 57 | char *buf; |
| 58 | int result; |
| 59 | |
| 60 | if (!overwrite) |
| 61 | { |
| 62 | if (osd_getenv(name) != NULL) |
| 63 | return 0; |
| 64 | } |
| 65 | buf = (char *) osd_malloc_array(strlen(name)+strlen(value)+2); |
| 66 | sprintf(buf, "%s=%s", name, value); |
| 67 | result = putenv(buf); |
| 68 | |
| 69 | /* will be referenced by environment |
| 70 | * Therefore it is not freed here |
| 71 | */ |
| 72 | |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | //============================================================ |
| 77 | // osd_process_kill |
| 78 | //============================================================ |
| 79 | |
| 80 | void osd_process_kill(void) |
| 81 | { |
| 82 | TerminateProcess(GetCurrentProcess(), -1); |
| 83 | } |
| 84 | |
| 85 | //============================================================ |
| 86 | // osd_malloc |
| 87 | //============================================================ |
| 88 | |
| 89 | void *osd_malloc(size_t size) |
| 90 | { |
| 91 | #ifndef MALLOC_DEBUG |
| 92 | return HeapAlloc(GetProcessHeap(), 0, size); |
| 93 | #else |
| 94 | // add in space for the size |
| 95 | size += sizeof(size_t); |
| 96 | |
| 97 | // basic objects just come from the heap |
| 98 | void *result = HeapAlloc(GetProcessHeap(), 0, size); |
| 99 | |
| 100 | // store the size and return and pointer to the data afterward |
| 101 | *reinterpret_cast<size_t *>(result) = size; |
| 102 | return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); |
| 103 | #endif |
| 104 | } |
| 105 | |
| 106 | |
| 107 | //============================================================ |
| 108 | // osd_malloc_array |
| 109 | //============================================================ |
| 110 | |
| 111 | void *osd_malloc_array(size_t size) |
| 112 | { |
| 113 | #ifndef MALLOC_DEBUG |
| 114 | return HeapAlloc(GetProcessHeap(), 0, size); |
| 115 | #else |
| 116 | // add in space for the size |
| 117 | size += sizeof(size_t); |
| 118 | |
| 119 | // round the size up to a page boundary |
| 120 | size_t rounded_size = ((size + sizeof(void *) + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; |
| 121 | |
| 122 | // reserve that much memory, plus two guard pages |
| 123 | void *page_base = VirtualAlloc(NULL, rounded_size + 2 * PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS); |
| 124 | if (page_base == NULL) |
| 125 | return NULL; |
| 126 | |
| 127 | // now allow access to everything but the first and last pages |
| 128 | page_base = VirtualAlloc(reinterpret_cast<UINT8 *>(page_base) + PAGE_SIZE, rounded_size, MEM_COMMIT, PAGE_READWRITE); |
| 129 | if (page_base == NULL) |
| 130 | return NULL; |
| 131 | |
| 132 | // work backwards from the page base to get to the block base |
| 133 | void *result = GUARD_ALIGN_START ? page_base : (reinterpret_cast<UINT8 *>(page_base) + rounded_size - size); |
| 134 | |
| 135 | // store the size at the start with a flag indicating it has a guard page |
| 136 | *reinterpret_cast<size_t *>(result) = size | 0x80000000; |
| 137 | return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); |
| 138 | #endif |
| 139 | } |
| 140 | |
| 141 | |
| 142 | //============================================================ |
| 143 | // osd_free |
| 144 | //============================================================ |
| 145 | |
| 146 | void osd_free(void *ptr) |
| 147 | { |
| 148 | #ifndef MALLOC_DEBUG |
| 149 | HeapFree(GetProcessHeap(), 0, ptr); |
| 150 | #else |
| 151 | size_t size = reinterpret_cast<size_t *>(ptr)[-1]; |
| 152 | |
| 153 | // if no guard page, just free the pointer |
| 154 | if ((size & 0x80000000) == 0) |
| 155 | HeapFree(GetProcessHeap(), 0, reinterpret_cast<UINT8 *>(ptr) - sizeof(size_t)); |
| 156 | |
| 157 | // large items need more care |
| 158 | else |
| 159 | { |
| 160 | ULONG_PTR page_base = (reinterpret_cast<ULONG_PTR>(ptr) - sizeof(size_t)) & ~(PAGE_SIZE - 1); |
| 161 | VirtualFree(reinterpret_cast<void *>(page_base - PAGE_SIZE), 0, MEM_RELEASE); |
| 162 | } |
| 163 | #endif |
| 164 | } |