summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/debugger/debugimgui.cpp
diff options
context:
space:
mode:
author angelosa <lordkale4@gmail.com>2024-09-25 12:00:36 +0200
committer angelosa <lordkale4@gmail.com>2024-09-25 12:00:36 +0200
commitad580b750889a2388fd33d9e9cc4b950fe4959ac (patch)
treee2aefdaa255fc4f0084841f6e3d91cdc2613938c /src/osd/modules/debugger/debugimgui.cpp
parent27703e71540f1843ff4f61666f6bda17bdb992da (diff)
hash/supracan.xml: hookup NVRAM for rebelst and standardize description
Diffstat (limited to 'src/osd/modules/debugger/debugimgui.cpp')
0 files changed, 0 insertions, 0 deletions
f='#n108'>108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
//============================================================
//
//  sdlos_*.c - OS specific low level code
//
//  Copyright (c) 1996-2010, Nicola Salmoria and the MAME Team.
//  Visit http://mamedev.org for licensing and usage restrictions.
//
//  SDLMAME by Olivier Galibert and R. Belmont
//
//============================================================

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>

#include <stdlib.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif

// MAME headers
#include "osdlib.h"
#include "osdcomm.h"
#include "osdcore.h"

#include "winutf8.h"

//============================================================
//  MACROS
//============================================================

// presumed size of a page of memory
#define PAGE_SIZE           4096

// align allocations to start or end of the page?
#define GUARD_ALIGN_START   0


//============================================================
//  GLOBAL VARIABLES
//============================================================

#ifdef OSD_WINDOWS
void (*s_debugger_stack_crawler)() = NULL;
#endif


//============================================================
//  osd_getenv
//============================================================

char *osd_getenv(const char *name)
{
    return getenv(name);
}


//============================================================
//  osd_setenv
//============================================================

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;
}

//============================================================
//  osd_process_kill
//============================================================

void osd_process_kill(void)
{
    TerminateProcess(GetCurrentProcess(), -1);
}

//============================================================
//  osd_num_processors
//============================================================

int osd_get_num_processors(void)
{
    SYSTEM_INFO 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);
}

//============================================================
//  osd_malloc
//============================================================

void *osd_malloc(size_t size)
{
#ifndef MALLOC_DEBUG
    return HeapAlloc(GetProcessHeap(), 0, size);
#else
    // add in space for the size
    size += sizeof(size_t);

    // 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);
#endif
}


//============================================================
//  osd_malloc_array
//============================================================

void *osd_malloc_array(size_t size)
{
#ifndef MALLOC_DEBUG
    return HeapAlloc(GetProcessHeap(), 0, size);
#else
    // 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;

    // 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;

    // 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);
#endif
}


//============================================================
//  osd_free
//============================================================

void osd_free(void *ptr)
{
#ifndef MALLOC_DEBUG
    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);
    }
#endif
}


//============================================================
//  osd_alloc_executable
//
//  allocates "size" bytes of executable memory.  this must take
//  things like NX support into account.
//============================================================

void *osd_alloc_executable(size_t size)
{
    return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
}


//============================================================
//  osd_free_executable
//
//  frees memory allocated with osd_alloc_executable
//============================================================

void osd_free_executable(void *ptr, size_t size)
{
    VirtualFree(ptr, 0, MEM_RELEASE);
}


//============================================================
//  osd_break_into_debugger
//============================================================

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)();
#else
    if (IsDebuggerPresent())
    {
        OutputDebugStringA(message);
        DebugBreak();
    }
#endif
}

//============================================================
//  GLOBAL VARIABLES
//============================================================

static osd_ticks_t ticks_per_second = 0;
static osd_ticks_t suspend_ticks = 0;
static BOOL using_qpc = TRUE;



//============================================================
//  osd_ticks
//============================================================

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();
}


//============================================================
//  osd_ticks_per_second
//============================================================

osd_ticks_t osd_ticks_per_second(void)
{
    if (ticks_per_second == 0)
        osd_ticks();
    return ticks_per_second;
}

//============================================================
//  osd_sleep
//============================================================

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);
    }
}