--- SheepShaver/src/macos_util.cpp 2003/09/29 22:47:39 1.3 +++ SheepShaver/src/macos_util.cpp 2004/12/19 09:01:04 1.10 @@ -1,7 +1,7 @@ /* * macos_util.cpp - MacOS definitions/utility functions * - * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig + * SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,6 +27,7 @@ #include "xlowmem.h" #include "emul_op.h" #include "macos_util.h" +#include "thunks.h" #define DEBUG 0 #include "debug.h" @@ -41,15 +42,15 @@ static inline long CallUniversal(void *a } typedef int16 (*gsl_ptr)(char *, uint32, uint32, uint32 *, void **, char *); static uint32 gsl_tvect = 0; -static inline int16 GetSharedLibrary(char *arg1, uint32 arg2, uint32 arg3, uint32 *arg4, void **arg5, char *arg6) +static inline int16 GetSharedLibrary(uintptr arg1, uint32 arg2, uint32 arg3, uintptr arg4, uintptr arg5, uintptr arg6) { - return (int16)CallMacOS6(gsl_ptr, gsl_tvect, arg1, arg2, arg3, arg4, arg5, arg6); + return (int16)CallMacOS6(gsl_ptr, gsl_tvect, (char *)arg1, arg2, arg3, (uint32 *)arg4, (void **)arg5, (char *)arg6); } typedef int16 (*fs_ptr)(uint32, char *, void **, uint32 *); static uint32 fs_tvect = 0; -static inline int16 FindSymbol(uint32 arg1, char *arg2, void **arg3, uint32 *arg4) +static inline int16 FindSymbol(uint32 arg1, uintptr arg2, uintptr arg3, uintptr arg4) { - return (int16)CallMacOS4(fs_ptr, fs_tvect, arg1, arg2, arg3, arg4); + return (int16)CallMacOS4(fs_ptr, fs_tvect, arg1, (char *)arg2, (void **)arg3, (uint32 **)arg4); } typedef int16 (*cc_ptr)(uint32 *); static uint32 cc_tvect = 0; @@ -57,6 +58,18 @@ static inline int16 CloseConnection(uint { return (int16)CallMacOS1(cc_ptr, cc_tvect, arg1); } +typedef uint32 (*nps_ptr)(uint32); +static uint32 nps_tvect = 0; +static inline uint32 NewPtrSys(uint32 arg1) +{ + return CallMacOS1(nps_ptr, nps_tvect, arg1); +} +typedef void (*d_ptr)(uint32); +static uint32 d_tvect = 0; +static inline void DisposePtr(uint32 arg1) +{ + CallMacOS1(d_ptr, d_tvect, arg1); +} /* @@ -150,21 +163,25 @@ void FileDiskLayout(loff_t size, uint8 * * lib and sym must be Pascal strings! */ -void *FindLibSymbol(char *lib, char *sym) +uint32 FindLibSymbol(char *lib_str, char *sym_str) { - uint32 conn_id = 0; - void *main_addr = NULL; - char err[256] = ""; - uint32 *sym_addr = NULL; - uint32 sym_class = 0; + SheepVar32 conn_id = 0; + SheepVar32 main_addr = 0; + SheepArray<256> err; + WriteMacInt8(err.addr(), 0); + SheepVar32 sym_addr = 0; + SheepVar32 sym_class = 0; - D(bug("FindLibSymbol %s in %s...\n", sym+1, lib+1)); + SheepString lib(lib_str); + SheepString sym(sym_str); + + D(bug("FindLibSymbol %s in %s...\n", sym.value()+1, lib.value()+1)); if (ReadMacInt32(XLM_RUN_MODE) == MODE_EMUL_OP) { M68kRegisters r; // Find shared library - static const uint8 proc1[] = { + static const uint8 proc1_template[] = { 0x55, 0x8f, // subq.l #2,a7 0x2f, 0x08, // move.l a0,-(a7) 0x2f, 0x3c, 0x70, 0x77, 0x70, 0x63, // move.l #'pwpc',-(a7) @@ -175,19 +192,20 @@ void *FindLibSymbol(char *lib, char *sym 0x3f, 0x3c, 0x00, 0x01, // (GetSharedLibrary) 0xaa, 0x5a, // CFMDispatch 0x30, 0x1f, // move.w (a7)+,d0 - M68K_RTS >> 8, M68K_RTS & 0xff + M68K_RTS >> 8, M68K_RTS }; - r.a[0] = (uint32)lib; - r.a[1] = (uint32)&conn_id; - r.a[2] = (uint32)&main_addr; - r.a[3] = (uint32)err; - Execute68k((uint32)proc1, &r); - D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", (int16)r.d[0], ntohl(conn_id), ntohl((uintptr)main_addr))); + BUILD_SHEEPSHAVER_PROCEDURE(proc1); + r.a[0] = lib.addr(); + r.a[1] = conn_id.addr(); + r.a[2] = main_addr.addr(); + r.a[3] = err.addr(); + Execute68k(proc1, &r); + D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", (int16)r.d[0], conn_id.value(), main_addr.value())); if (r.d[0]) - return NULL; + return 0; // Find symbol - static const uint8 proc2[] = { + static const uint8 proc2_template[] = { 0x55, 0x8f, // subq.l #2,a7 0x2f, 0x00, // move.l d0,-(a7) 0x2f, 0x08, // move.l a0,-(a7) @@ -196,19 +214,20 @@ void *FindLibSymbol(char *lib, char *sym 0x3f, 0x3c, 0x00, 0x05, // (FindSymbol) 0xaa, 0x5a, // CFMDispatch 0x30, 0x1f, // move.w (a7)+,d0 - M68K_RTS >> 8, M68K_RTS & 0xff + M68K_RTS >> 8, M68K_RTS }; - r.d[0] = ntohl(conn_id); - r.a[0] = (uint32)sym; - r.a[1] = (uint32)&sym_addr; - r.a[2] = (uint32)&sym_class; - Execute68k((uint32)proc2, &r); - D(bug(" FindSymbol1: ret %d, sym_addr %p, sym_class %ld\n", (int16)r.d[0], ntohl((uintptr)sym_addr), ntohl(sym_class))); + BUILD_SHEEPSHAVER_PROCEDURE(proc2); + r.d[0] = conn_id.value(); + r.a[0] = sym.addr(); + r.a[1] = sym_addr.addr(); + r.a[2] = sym_class.addr(); + Execute68k(proc2, &r); + D(bug(" FindSymbol1: ret %d, sym_addr %p, sym_class %ld\n", (int16)r.d[0], sym_addr.value(), sym_class.value())); //!! CloseConnection()? if (r.d[0]) - return NULL; + return 0; else - return (void *)ntohl((uintptr)sym_addr); + return sym_addr.value(); } else { @@ -217,17 +236,17 @@ void *FindLibSymbol(char *lib, char *sym return 0; } int16 res; - res = GetSharedLibrary(lib, FOURCC('p','w','p','c'), 1, &conn_id, &main_addr, err); - D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", res, ntohl(conn_id), ntohl((uintptr)main_addr))); + res = GetSharedLibrary(lib.addr(), FOURCC('p','w','p','c'), 1, conn_id.addr(), main_addr.addr(), err.addr()); + D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", res, conn_id.value(), main_addr.value())); if (res) - return NULL; - res = FindSymbol(ntohl(conn_id), sym, (void **)&sym_addr, &sym_class); - D(bug(" FindSymbol: ret %d, sym_addr %p, sym_class %ld\n", res, ntohl((uintptr)sym_addr), ntohl(sym_class))); + return 0; + res = FindSymbol(conn_id.value(), sym.addr(), sym_addr.addr(), sym_class.addr()); + D(bug(" FindSymbol: ret %d, sym_addr %p, sym_class %ld\n", res, sym_addr.value(), sym_class.value())); //!!?? CloseConnection(&conn_id); if (res) - return NULL; + return 0; else - return (void *)ntohl((uintptr)sym_addr); + return sym_addr.value(); } } @@ -238,33 +257,47 @@ void *FindLibSymbol(char *lib, char *sym void InitCallUniversalProc() { - cu_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\021CallUniversalProc"); + cu_tvect = FindLibSymbol("\014InterfaceLib", "\021CallUniversalProc"); D(bug("CallUniversalProc TVECT at %08lx\n", cu_tvect)); if (cu_tvect == 0) { printf("FATAL: Can't find CallUniversalProc()\n"); QuitEmulator(); } - gsl_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\020GetSharedLibrary"); + gsl_tvect = FindLibSymbol("\014InterfaceLib", "\020GetSharedLibrary"); D(bug("GetSharedLibrary TVECT at %08lx\n", gsl_tvect)); if (gsl_tvect == 0) { printf("FATAL: Can't find GetSharedLibrary()\n"); QuitEmulator(); } - fs_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\012FindSymbol"); + fs_tvect = FindLibSymbol("\014InterfaceLib", "\012FindSymbol"); D(bug("FindSymbol TVECT at %08lx\n", fs_tvect)); if (fs_tvect == 0) { printf("FATAL: Can't find FindSymbol()\n"); QuitEmulator(); } - cc_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\017CloseConnection"); + cc_tvect = FindLibSymbol("\014InterfaceLib", "\017CloseConnection"); D(bug("CloseConnection TVECT at %08lx\n", cc_tvect)); if (cc_tvect == 0) { printf("FATAL: Can't find CloseConnection()\n"); QuitEmulator(); } + + nps_tvect = FindLibSymbol("\014InterfaceLib", "\011NewPtrSys"); + D(bug("NewPtrSys TVECT at %08lx\n", nps_tvect)); + if (nps_tvect == 0) { + printf("FATAL: Can't find NewPtrSys()\n"); + QuitEmulator(); + } + + d_tvect = FindLibSymbol("\014InterfaceLib", "\012DisposePtr"); + D(bug("DisposePtr TVECT at %08lx\n", d_tvect)); + if (d_tvect == 0) { + printf("FATAL: Can't find DisposePtr()\n"); + QuitEmulator(); + } } @@ -304,3 +337,18 @@ uint32 TimeToMacTime(time_t t) uint32 days = local->tm_yday + 365 * (local->tm_year - 4) + intervening_leap_days; return local->tm_sec + 60 * (local->tm_min + 60 * (local->tm_hour + 24 * days)); } + + +/* + * Memory allocators in MacOS system heap zone + */ + +uint32 Mac_sysalloc(uint32 size) +{ + return NewPtrSys(size); +} + +void Mac_sysfree(uint32 addr) +{ + DisposePtr(addr); +}