trunk/src/osd/modules/lib/osdlib.h
| r0 | r242785 | |
| 1 | //============================================================ |
| 2 | // |
| 3 | // osdlib.h |
| 4 | // |
| 5 | // Copyright (c) 1996-2014, Nicola Salmoria and the MAME Team. |
| 6 | // Visit http://mamedev.org for licensing and usage restrictions. |
| 7 | // |
| 8 | // SDLMAME by Olivier Galibert and R. Belmont |
| 9 | // |
| 10 | // - Common low level routines |
| 11 | // - Source files also provide the following from osdcore.h |
| 12 | // |
| 13 | // - osd_ticks |
| 14 | // - osd_sleep |
| 15 | // - osd_malloc |
| 16 | // - osd_malloc_array |
| 17 | // - osd_free |
| 18 | //============================================================ |
| 19 | |
| 20 | #ifndef __OSDLIB__ |
| 21 | #define __OSDLIB__ |
| 22 | |
| 23 | /*----------------------------------------------------------------------------- |
| 24 | osd_num_processors: return the number of processors |
| 25 | |
| 26 | Parameters: |
| 27 | |
| 28 | None. |
| 29 | |
| 30 | Return value: |
| 31 | |
| 32 | Number of processors |
| 33 | -----------------------------------------------------------------------------*/ |
| 34 | int osd_get_num_processors(void); |
| 35 | |
| 36 | /*----------------------------------------------------------------------------- |
| 37 | osd_process_kill: kill the current process |
| 38 | |
| 39 | Parameters: |
| 40 | |
| 41 | None. |
| 42 | |
| 43 | Return value: |
| 44 | |
| 45 | None. |
| 46 | -----------------------------------------------------------------------------*/ |
| 47 | void osd_process_kill(void); |
| 48 | |
| 49 | /*----------------------------------------------------------------------------- |
| 50 | osd_getenv: return pointer to environment variable |
| 51 | |
| 52 | Parameters: |
| 53 | |
| 54 | name - name of environment variable |
| 55 | |
| 56 | Return value: |
| 57 | |
| 58 | pointer to value |
| 59 | -----------------------------------------------------------------------------*/ |
| 60 | char *osd_getenv(const char *name); |
| 61 | |
| 62 | /*----------------------------------------------------------------------------- |
| 63 | osd_setenv: set environment variable |
| 64 | |
| 65 | Parameters: |
| 66 | |
| 67 | name - name of environment variable |
| 68 | value - value to write |
| 69 | overwrite - overwrite if it exists |
| 70 | |
| 71 | Return value: |
| 72 | |
| 73 | 0 on success |
| 74 | -----------------------------------------------------------------------------*/ |
| 75 | |
| 76 | int osd_setenv(const char *name, const char *value, int overwrite); |
| 77 | |
| 78 | #endif /* __OSDLIB__ */ |
trunk/src/osd/sdl/sdlmain.c
| r242784 | r242785 | |
| 52 | 52 | #include "video.h" |
| 53 | 53 | #include "input.h" |
| 54 | 54 | #include "osdsdl.h" |
| 55 | | #include "sdlos.h" |
| 55 | #include "modules/lib/osdlib.h" |
| 56 | 56 | #include "modules/sound/sdl_sound.h" |
| 57 | 57 | #if defined(SDLMAME_EMSCRIPTEN) |
| 58 | 58 | #include "modules/sound/js_sound.h" |
| r242784 | r242785 | |
| 269 | 269 | |
| 270 | 270 | // we do some special sauce on Win32... |
| 271 | 271 | |
| 272 | | #if !defined(SDLMAME_WIN32) |
| 273 | | int main(int argc, char **argv) |
| 274 | | { |
| 275 | | int res = 0; |
| 276 | | |
| 277 | | #else |
| 278 | | |
| 272 | #if defined(SDLMAME_WIN32) |
| 279 | 273 | /* gee */ |
| 280 | 274 | extern "C" DECLSPEC void SDLCALL SDL_SetModuleHandle(void *hInst); |
| 275 | #endif |
| 281 | 276 | |
| 282 | 277 | // translated to utf8_main |
| 283 | 278 | int main(int argc, char *argv[]) |
| 284 | 279 | { |
| 285 | 280 | int res = 0; |
| 286 | 281 | |
| 282 | #if defined(SDLMAME_WIN32) |
| 287 | 283 | #if !(SDLMAME_SDL2) |
| 288 | 284 | /* Load SDL dynamic link library */ |
| 289 | 285 | if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) { |
| r242784 | r242785 | |
| 293 | 289 | SDL_SetModuleHandle(GetModuleHandle(NULL)); |
| 294 | 290 | #endif |
| 295 | 291 | #endif |
| 292 | |
| 296 | 293 | // disable I/O buffering |
| 297 | 294 | setvbuf(stdout, (char *) NULL, _IONBF, 0); |
| 298 | 295 | setvbuf(stderr, (char *) NULL, _IONBF, 0); |
| r242784 | r242785 | |
| 527 | 524 | { |
| 528 | 525 | osd_printf_verbose("\t%-20s\n", SDL_GetAudioDriver(i)); |
| 529 | 526 | } |
| 527 | |
| 530 | 528 | #endif |
| 531 | 529 | } |
| 532 | 530 | |
trunk/src/osd/sdl/sdlos.h
| r242784 | r242785 | |
| 1 | | //============================================================ |
| 2 | | // |
| 3 | | // sdlos.h - SDLMAME OS dependent functions |
| 4 | | // |
| 5 | | // Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team. |
| 6 | | // Visit http://mamedev.org for licensing and usage restrictions. |
| 7 | | // |
| 8 | | // SDLMAME by Olivier Galibert and R. Belmont |
| 9 | | // |
| 10 | | //============================================================ |
| 11 | | |
| 12 | | #ifndef __SDLOS__ |
| 13 | | #define __SDLOS__ |
| 14 | | |
| 15 | | /*----------------------------------------------------------------------------- |
| 16 | | osd_num_processors: return the number of processors |
| 17 | | |
| 18 | | Parameters: |
| 19 | | |
| 20 | | None. |
| 21 | | |
| 22 | | Return value: |
| 23 | | |
| 24 | | Number of processors |
| 25 | | -----------------------------------------------------------------------------*/ |
| 26 | | int osd_get_num_processors(void); |
| 27 | | |
| 28 | | |
| 29 | | /*----------------------------------------------------------------------------- |
| 30 | | osd_getenv: return pointer to environment variable |
| 31 | | |
| 32 | | Parameters: |
| 33 | | |
| 34 | | name - name of environment variable |
| 35 | | |
| 36 | | Return value: |
| 37 | | |
| 38 | | pointer to value |
| 39 | | -----------------------------------------------------------------------------*/ |
| 40 | | char *osd_getenv(const char *name); |
| 41 | | |
| 42 | | /*----------------------------------------------------------------------------- |
| 43 | | osd_setenv: set environment variable |
| 44 | | |
| 45 | | Parameters: |
| 46 | | |
| 47 | | name - name of environment variable |
| 48 | | value - value to write |
| 49 | | overwrite - overwrite if it exists |
| 50 | | |
| 51 | | Return value: |
| 52 | | |
| 53 | | 0 on success |
| 54 | | -----------------------------------------------------------------------------*/ |
| 55 | | |
| 56 | | int osd_setenv(const char *name, const char *value, int overwrite); |
| 57 | | |
| 58 | | #endif /* __SDLOS__ */ |
trunk/src/osd/sdl/sdlos_macosx.c
| r242784 | r242785 | |
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | | //============================================================ |
| 143 | | // osd_num_processors |
| 144 | | //============================================================ |
| 145 | 142 | |
| 146 | | int osd_get_num_processors(void) |
| 147 | | { |
| 148 | | int processors = 1; |
| 149 | | |
| 150 | | struct host_basic_info host_basic_info; |
| 151 | | unsigned int count; |
| 152 | | kern_return_t r; |
| 153 | | mach_port_t my_mach_host_self; |
| 154 | | |
| 155 | | count = HOST_BASIC_INFO_COUNT; |
| 156 | | my_mach_host_self = mach_host_self(); |
| 157 | | if ( ( r = host_info(my_mach_host_self, HOST_BASIC_INFO, (host_info_t)(&host_basic_info), &count)) == KERN_SUCCESS ) |
| 158 | | { |
| 159 | | processors = host_basic_info.avail_cpus; |
| 160 | | } |
| 161 | | mach_port_deallocate(mach_task_self(), my_mach_host_self); |
| 162 | | |
| 163 | | return processors; |
| 164 | | } |
| 165 | | |
| 166 | 143 | //============================================================ |
| 167 | | // osd_malloc |
| 168 | | //============================================================ |
| 169 | | |
| 170 | | void *osd_malloc(size_t size) |
| 171 | | { |
| 172 | | #ifndef MALLOC_DEBUG |
| 173 | | return malloc(size); |
| 174 | | #else |
| 175 | | #error "MALLOC_DEBUG not yet supported" |
| 176 | | #endif |
| 177 | | } |
| 178 | | |
| 179 | | |
| 180 | | //============================================================ |
| 181 | | // osd_malloc_array |
| 182 | | //============================================================ |
| 183 | | |
| 184 | | void *osd_malloc_array(size_t size) |
| 185 | | { |
| 186 | | #ifndef MALLOC_DEBUG |
| 187 | | return malloc(size); |
| 188 | | #else |
| 189 | | #error "MALLOC_DEBUG not yet supported" |
| 190 | | #endif |
| 191 | | } |
| 192 | | |
| 193 | | |
| 194 | | //============================================================ |
| 195 | | // osd_free |
| 196 | | //============================================================ |
| 197 | | |
| 198 | | void osd_free(void *ptr) |
| 199 | | { |
| 200 | | #ifndef MALLOC_DEBUG |
| 201 | | free(ptr); |
| 202 | | #else |
| 203 | | #error "MALLOC_DEBUG not yet supported" |
| 204 | | #endif |
| 205 | | } |
| 206 | | |
| 207 | | //============================================================ |
| 208 | | // osd_getenv |
| 209 | | //============================================================ |
| 210 | | |
| 211 | | char *osd_getenv(const char *name) |
| 212 | | { |
| 213 | | return getenv(name); |
| 214 | | } |
| 215 | | |
| 216 | | //============================================================ |
| 217 | | // osd_setenv |
| 218 | | //============================================================ |
| 219 | | |
| 220 | | int osd_setenv(const char *name, const char *value, int overwrite) |
| 221 | | { |
| 222 | | return setenv(name, value, overwrite); |
| 223 | | } |
| 224 | | |
| 225 | | //============================================================ |
| 226 | 144 | // osd_get_clipboard_text |
| 227 | 145 | //============================================================ |
| 228 | 146 | |
| r242784 | r242785 | |
| 365 | 283 | } |
| 366 | 284 | |
| 367 | 285 | //============================================================ |
| 368 | | // osd_get_slider_list |
| 369 | | //============================================================ |
| 370 | | |
| 371 | | const void *osd_get_slider_list() |
| 372 | | { |
| 373 | | return NULL; |
| 374 | | } |
| 375 | | |
| 376 | | //============================================================ |
| 377 | 286 | // osd_get_full_path |
| 378 | 287 | //============================================================ |
| 379 | 288 | |
trunk/src/osd/sdl/sdlos_unix.c
| r242784 | r242785 | |
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | | //============================================================ |
| 74 | | // osd_num_processors |
| 75 | | //============================================================ |
| 76 | | |
| 77 | | int osd_get_num_processors(void) |
| 78 | | { |
| 79 | | int processors = 1; |
| 80 | | |
| 81 | | #if defined(_SC_NPROCESSORS_ONLN) |
| 82 | | processors = sysconf(_SC_NPROCESSORS_ONLN); |
| 83 | | #endif |
| 84 | | return processors; |
| 85 | | } |
| 86 | | |
| 87 | | //============================================================ |
| 88 | | // osd_malloc |
| 89 | | //============================================================ |
| 90 | | |
| 91 | | void *osd_malloc(size_t size) |
| 92 | | { |
| 93 | | #ifndef MALLOC_DEBUG |
| 94 | | return malloc(size); |
| 95 | | #else |
| 96 | | #error "MALLOC_DEBUG not yet supported" |
| 97 | | #endif |
| 98 | | } |
| 99 | | |
| 100 | | |
| 101 | | //============================================================ |
| 102 | | // osd_malloc_array |
| 103 | | //============================================================ |
| 104 | | |
| 105 | | void *osd_malloc_array(size_t size) |
| 106 | | { |
| 107 | | #ifndef MALLOC_DEBUG |
| 108 | | return malloc(size); |
| 109 | | #else |
| 110 | | #error "MALLOC_DEBUG not yet supported" |
| 111 | | #endif |
| 112 | | } |
| 113 | | |
| 114 | | |
| 115 | | //============================================================ |
| 116 | | // osd_free |
| 117 | | //============================================================ |
| 118 | | |
| 119 | | void osd_free(void *ptr) |
| 120 | | { |
| 121 | | #ifndef MALLOC_DEBUG |
| 122 | | free(ptr); |
| 123 | | #else |
| 124 | | #error "MALLOC_DEBUG not yet supported" |
| 125 | | #endif |
| 126 | | } |
| 127 | | |
| 128 | | //============================================================ |
| 129 | | // osd_getenv |
| 130 | | //============================================================ |
| 131 | | |
| 132 | | char *osd_getenv(const char *name) |
| 133 | | { |
| 134 | | return getenv(name); |
| 135 | | } |
| 136 | | |
| 137 | | |
| 138 | | //============================================================ |
| 139 | | // osd_setenv |
| 140 | | //============================================================ |
| 141 | | |
| 142 | | int osd_setenv(const char *name, const char *value, int overwrite) |
| 143 | | { |
| 144 | | return setenv(name, value, overwrite); |
| 145 | | } |
| 146 | | |
| 147 | 73 | #if (SDLMAME_SDL2) |
| 148 | 74 | |
| 149 | 75 | //============================================================ |
| r242784 | r242785 | |
| 309 | 235 | } |
| 310 | 236 | |
| 311 | 237 | //============================================================ |
| 312 | | // osd_get_slider_list |
| 313 | | //============================================================ |
| 314 | | |
| 315 | | const void *osd_get_slider_list() |
| 316 | | { |
| 317 | | return NULL; |
| 318 | | } |
| 319 | | |
| 320 | | //============================================================ |
| 321 | 238 | // osd_get_full_path |
| 322 | 239 | //============================================================ |
| 323 | 240 | |
trunk/src/osd/sdl/sdlos_win32.c
| r242784 | r242785 | |
| 20 | 20 | #include "osdcore.h" |
| 21 | 21 | #include "strconv.h" |
| 22 | 22 | |
| 23 | | #include "../windows/winos.c" |
| 24 | | |
| 25 | 23 | //============================================================ |
| 26 | 24 | // PROTOTYPES |
| 27 | 25 | //============================================================ |
| r242784 | r242785 | |
| 160 | 158 | } |
| 161 | 159 | |
| 162 | 160 | //============================================================ |
| 163 | | // osd_malloc |
| 164 | | //============================================================ |
| 165 | | |
| 166 | | void *osd_malloc(size_t size) |
| 167 | | { |
| 168 | | #ifndef MALLOC_DEBUG |
| 169 | | return HeapAlloc(GetProcessHeap(), 0, size); |
| 170 | | #else |
| 171 | | // add in space for the size |
| 172 | | size += sizeof(size_t); |
| 173 | | |
| 174 | | // basic objects just come from the heap |
| 175 | | void *result = HeapAlloc(GetProcessHeap(), 0, size); |
| 176 | | |
| 177 | | // store the size and return and pointer to the data afterward |
| 178 | | *reinterpret_cast<size_t *>(result) = size; |
| 179 | | return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); |
| 180 | | #endif |
| 181 | | } |
| 182 | | |
| 183 | | |
| 184 | | //============================================================ |
| 185 | | // osd_malloc_array |
| 186 | | //============================================================ |
| 187 | | |
| 188 | | void *osd_malloc_array(size_t size) |
| 189 | | { |
| 190 | | #ifndef MALLOC_DEBUG |
| 191 | | return HeapAlloc(GetProcessHeap(), 0, size); |
| 192 | | #else |
| 193 | | // add in space for the size |
| 194 | | size += sizeof(size_t); |
| 195 | | |
| 196 | | // round the size up to a page boundary |
| 197 | | size_t rounded_size = ((size + sizeof(void *) + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; |
| 198 | | |
| 199 | | // reserve that much memory, plus two guard pages |
| 200 | | void *page_base = VirtualAlloc(NULL, rounded_size + 2 * PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS); |
| 201 | | if (page_base == NULL) |
| 202 | | return NULL; |
| 203 | | |
| 204 | | // now allow access to everything but the first and last pages |
| 205 | | page_base = VirtualAlloc(reinterpret_cast<UINT8 *>(page_base) + PAGE_SIZE, rounded_size, MEM_COMMIT, PAGE_READWRITE); |
| 206 | | if (page_base == NULL) |
| 207 | | return NULL; |
| 208 | | |
| 209 | | // work backwards from the page base to get to the block base |
| 210 | | void *result = GUARD_ALIGN_START ? page_base : (reinterpret_cast<UINT8 *>(page_base) + rounded_size - size); |
| 211 | | |
| 212 | | // store the size at the start with a flag indicating it has a guard page |
| 213 | | *reinterpret_cast<size_t *>(result) = size | 0x80000000; |
| 214 | | return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); |
| 215 | | #endif |
| 216 | | } |
| 217 | | |
| 218 | | |
| 219 | | //============================================================ |
| 220 | | // osd_free |
| 221 | | //============================================================ |
| 222 | | |
| 223 | | void osd_free(void *ptr) |
| 224 | | { |
| 225 | | #ifndef MALLOC_DEBUG |
| 226 | | HeapFree(GetProcessHeap(), 0, ptr); |
| 227 | | #else |
| 228 | | size_t size = reinterpret_cast<size_t *>(ptr)[-1]; |
| 229 | | |
| 230 | | // if no guard page, just free the pointer |
| 231 | | if ((size & 0x80000000) == 0) |
| 232 | | HeapFree(GetProcessHeap(), 0, reinterpret_cast<UINT8 *>(ptr) - sizeof(size_t)); |
| 233 | | |
| 234 | | // large items need more care |
| 235 | | else |
| 236 | | { |
| 237 | | ULONG_PTR page_base = (reinterpret_cast<ULONG_PTR>(ptr) - sizeof(size_t)) & ~(PAGE_SIZE - 1); |
| 238 | | VirtualFree(reinterpret_cast<void *>(page_base - PAGE_SIZE), 0, MEM_RELEASE); |
| 239 | | } |
| 240 | | #endif |
| 241 | | } |
| 242 | | |
| 243 | | //============================================================ |
| 244 | | // osd_setenv |
| 245 | | //============================================================ |
| 246 | | |
| 247 | | int osd_setenv(const char *name, const char *value, int overwrite) |
| 248 | | { |
| 249 | | char *buf; |
| 250 | | int result; |
| 251 | | |
| 252 | | if (!overwrite) |
| 253 | | { |
| 254 | | if (osd_getenv(name) != NULL) |
| 255 | | return 0; |
| 256 | | } |
| 257 | | buf = (char *) osd_malloc_array(strlen(name)+strlen(value)+2); |
| 258 | | sprintf(buf, "%s=%s", name, value); |
| 259 | | result = putenv(buf); |
| 260 | | |
| 261 | | /* will be referenced by environment |
| 262 | | * Therefore it is not freed here |
| 263 | | */ |
| 264 | | |
| 265 | | return result; |
| 266 | | } |
| 267 | | |
| 268 | | //============================================================ |
| 269 | 161 | // get_clipboard_text_by_format |
| 270 | 162 | //============================================================ |
| 271 | 163 | |
| r242784 | r242785 | |
| 460 | 352 | } |
| 461 | 353 | |
| 462 | 354 | //============================================================ |
| 463 | | // osd_get_slider_list |
| 464 | | //============================================================ |
| 465 | | |
| 466 | | const void *osd_get_slider_list() |
| 467 | | { |
| 468 | | return NULL; |
| 469 | | } |
| 470 | | |
| 471 | | //============================================================ |
| 472 | 355 | // win_error_to_mame_file_error |
| 473 | 356 | //============================================================ |
| 474 | 357 | |
trunk/src/osd/sdl/window.c
| r242784 | r242785 | |
| 235 | 235 | if (work_queue == NULL) |
| 236 | 236 | return false; |
| 237 | 237 | osd_work_item_queue(work_queue, &sdlwindow_thread_id, NULL, WORK_ITEM_FLAG_AUTO_RELEASE); |
| 238 | sdlwindow_sync(); |
| 238 | 239 | } |
| 239 | 240 | else |
| 240 | 241 | { |
| r242784 | r242785 | |
| 264 | 265 | return false; |
| 265 | 266 | } |
| 266 | 267 | |
| 268 | #if SDLMAME_SDL2 |
| 269 | /* We may want to set a number of the hints SDL2 provides. |
| 270 | * The code below will document which hints were set. |
| 271 | */ |
| 272 | const char * hints[] = { SDL_HINT_RENDER_DRIVER, SDL_HINT_RENDER_OPENGL_SHADERS, |
| 273 | SDL_HINT_RENDER_DIRECT3D_THREADSAFE, SDL_HINT_RENDER_SCALE_QUALITY, |
| 274 | SDL_HINT_RENDER_VSYNC, SDL_HINT_VIDEO_ALLOW_SCREENSAVER, |
| 275 | SDL_HINT_VIDEO_X11_XVIDMODE, SDL_HINT_VIDEO_X11_XINERAMA, |
| 276 | SDL_HINT_VIDEO_X11_XRANDR, SDL_HINT_GRAB_KEYBOARD, |
| 277 | SDL_HINT_MOUSE_RELATIVE_MODE_WARP, |
| 278 | SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, SDL_HINT_IDLE_TIMER_DISABLED, |
| 279 | SDL_HINT_ORIENTATIONS, SDL_HINT_ACCELEROMETER_AS_JOYSTICK, |
| 280 | SDL_HINT_XINPUT_ENABLED, SDL_HINT_GAMECONTROLLERCONFIG, |
| 281 | SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, SDL_HINT_ALLOW_TOPMOST, |
| 282 | SDL_HINT_TIMER_RESOLUTION, SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, |
| 283 | SDL_HINT_VIDEO_WIN_D3DCOMPILER, SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT, |
| 284 | SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES, |
| 285 | NULL |
| 286 | }; |
| 287 | |
| 288 | osd_printf_verbose("\nHints:\n"); |
| 289 | for (int i = 0; hints[i] != NULL; i++) |
| 290 | osd_printf_verbose("\t%-40s %s\n", hints[i], SDL_GetHint(hints[i])); |
| 291 | #endif |
| 292 | |
| 267 | 293 | // set up the window list |
| 268 | 294 | last_window_ptr = &sdl_window_list; |
| 269 | 295 | osd_printf_verbose("Leave sdlwindow_init\n"); |
trunk/src/osd/windows/winmisc.c
| r242784 | r242785 | |
| 21 | 21 | |
| 22 | 22 | |
| 23 | 23 | //============================================================ |
| 24 | | // MACROS |
| 25 | | //============================================================ |
| 26 | | |
| 27 | | // presumed size of a page of memory |
| 28 | | #define PAGE_SIZE 4096 |
| 29 | | |
| 30 | | // align allocations to start or end of the page? |
| 31 | | #define GUARD_ALIGN_START 0 |
| 32 | | |
| 33 | | |
| 34 | | |
| 35 | | //============================================================ |
| 36 | 24 | // GLOBAL VARIABLES |
| 37 | 25 | //============================================================ |
| 38 | 26 | |
| 39 | 27 | void (*s_debugger_stack_crawler)() = NULL; |
| 40 | 28 | |
| 41 | 29 | |
| 42 | | |
| 43 | 30 | //============================================================ |
| 44 | | // osd_malloc |
| 45 | | //============================================================ |
| 46 | | |
| 47 | | void *osd_malloc(size_t size) |
| 48 | | { |
| 49 | | #ifndef MALLOC_DEBUG |
| 50 | | return HeapAlloc(GetProcessHeap(), 0, size); |
| 51 | | #else |
| 52 | | // add in space for the size |
| 53 | | size += sizeof(size_t); |
| 54 | | |
| 55 | | // basic objects just come from the heap |
| 56 | | void *result = HeapAlloc(GetProcessHeap(), 0, size); |
| 57 | | |
| 58 | | // store the size and return and pointer to the data afterward |
| 59 | | *reinterpret_cast<size_t *>(result) = size; |
| 60 | | return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); |
| 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 HeapAlloc(GetProcessHeap(), 0, size); |
| 73 | | #else |
| 74 | | // add in space for the size |
| 75 | | size += sizeof(size_t); |
| 76 | | |
| 77 | | // round the size up to a page boundary |
| 78 | | size_t rounded_size = ((size + sizeof(void *) + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; |
| 79 | | |
| 80 | | // reserve that much memory, plus two guard pages |
| 81 | | void *page_base = VirtualAlloc(NULL, rounded_size + 2 * PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS); |
| 82 | | if (page_base == NULL) |
| 83 | | return NULL; |
| 84 | | |
| 85 | | // now allow access to everything but the first and last pages |
| 86 | | page_base = VirtualAlloc(reinterpret_cast<UINT8 *>(page_base) + PAGE_SIZE, rounded_size, MEM_COMMIT, PAGE_READWRITE); |
| 87 | | if (page_base == NULL) |
| 88 | | return NULL; |
| 89 | | |
| 90 | | // work backwards from the page base to get to the block base |
| 91 | | void *result = GUARD_ALIGN_START ? page_base : (reinterpret_cast<UINT8 *>(page_base) + rounded_size - size); |
| 92 | | |
| 93 | | // store the size at the start with a flag indicating it has a guard page |
| 94 | | *reinterpret_cast<size_t *>(result) = size | 0x80000000; |
| 95 | | return reinterpret_cast<UINT8 *>(result) + sizeof(size_t); |
| 96 | | #endif |
| 97 | | } |
| 98 | | |
| 99 | | |
| 100 | | //============================================================ |
| 101 | | // osd_free |
| 102 | | //============================================================ |
| 103 | | |
| 104 | | void osd_free(void *ptr) |
| 105 | | { |
| 106 | | #ifndef MALLOC_DEBUG |
| 107 | | HeapFree(GetProcessHeap(), 0, ptr); |
| 108 | | #else |
| 109 | | size_t size = reinterpret_cast<size_t *>(ptr)[-1]; |
| 110 | | |
| 111 | | // if no guard page, just free the pointer |
| 112 | | if ((size & 0x80000000) == 0) |
| 113 | | HeapFree(GetProcessHeap(), 0, reinterpret_cast<UINT8 *>(ptr) - sizeof(size_t)); |
| 114 | | |
| 115 | | // large items need more care |
| 116 | | else |
| 117 | | { |
| 118 | | ULONG_PTR page_base = (reinterpret_cast<ULONG_PTR>(ptr) - sizeof(size_t)) & ~(PAGE_SIZE - 1); |
| 119 | | VirtualFree(reinterpret_cast<void *>(page_base - PAGE_SIZE), 0, MEM_RELEASE); |
| 120 | | } |
| 121 | | #endif |
| 122 | | } |
| 123 | | |
| 124 | | |
| 125 | | //============================================================ |
| 126 | 31 | // osd_alloc_executable |
| 127 | 32 | //============================================================ |
| 128 | 33 | |
trunk/src/osd/windows/winos.c
| r242784 | r242785 | |
| 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 | | |
| 11 | | // MAME headers |
| 12 | | #include "osdcore.h" |
| 13 | | |
| 14 | | //============================================================ |
| 15 | | // osd_num_processors |
| 16 | | //============================================================ |
| 17 | | |
| 18 | | int osd_get_num_processors(void) |
| 19 | | { |
| 20 | | SYSTEM_INFO info; |
| 21 | | |
| 22 | | // otherwise, fetch the info from the system |
| 23 | | GetSystemInfo(&info); |
| 24 | | |
| 25 | | // max out at 4 for now since scaling above that seems to do poorly |
| 26 | | return MIN(info.dwNumberOfProcessors, 4); |
| 27 | | } |
| 28 | | |
| 29 | | //============================================================ |
| 30 | | // osd_getenv |
| 31 | | //============================================================ |
| 32 | | |
| 33 | | char *osd_getenv(const char *name) |
| 34 | | { |
| 35 | | return getenv(name); |
| 36 | | } |