Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ let package = Package(
),
.library(name: "GraphiteCacaoHost", targets: ["GraphiteCacaoHost"]),
.library(name: "GraphiteCacaoRenderer", targets: ["GraphiteCacaoRenderer"]),
.library(name: "GraphiteEvents", targets: ["GraphiteEvents"])
.library(name: "GraphiteEvents", targets: ["GraphiteEvents"]),
.library(name: "GraphiteMemory", targets: ["GraphiteMemory"])
],
// FreeType reads outline fonts; builds without it keep the bitmap font.
traits: [
Expand Down Expand Up @@ -174,6 +175,7 @@ let package = Package(
.target(name: "GraphiteCacaoHost", dependencies: ["GraphiteCore", "GraphiteWindows", "GraphiteDialogs", "GraphiteEvents", "GraphiteCacaoRenderer", .product(name: "CacaoHost", package: "Cacao"), .product(name: "CacaoRender", package: "Cacao")]),
.target(name: "GraphiteCacaoRenderer", dependencies: ["GraphiteCore", "Graphite2D", "GraphiteWindows", "GraphiteControls", "GraphiteDialogs", .product(name: "CacaoRender", package: "Cacao")]),
.target(name: "GraphiteEvents", dependencies: ["GraphiteCore"]),
.target(name: "GraphiteMemory", dependencies: ["GraphiteCore"]),
.target(
name: "Graphite",
dependencies: ["GraphiteCore", "GraphiteTasks", "GraphiteFiles", "GraphiteResources", "GraphiteAutomation", "Graphite2D", "GraphiteText", "GraphiteSound", "GraphiteWindows", "GraphiteMenus", "GraphiteControls", "GraphiteDialogs", "GraphiteEvents"]
Expand Down Expand Up @@ -227,6 +229,7 @@ let package = Package(
dependencies: ["GraphiteDialogs"]
),
.testTarget(name: "GraphiteCacaoHostTests", dependencies: ["GraphiteCacaoHost", "GraphiteCore", "GraphiteDialogs", "GraphiteWindows", "Graphite2D", .product(name: "CacaoHost", package: "Cacao")]),
.testTarget(name: "GraphiteEventsTests", dependencies: ["GraphiteEvents"])
.testTarget(name: "GraphiteEventsTests", dependencies: ["GraphiteEvents"]),
.testTarget(name: "GraphiteMemoryTests", dependencies: ["GraphiteMemory"])
]
)
1 change: 1 addition & 0 deletions ReferenceTests/Retro68/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ add_application(FileManagerCapture FileManagerCapture.c)
add_application(RelStringCapture RelStringCapture.c)
add_application(CatalogOrderCapture CatalogOrderCapture.c)
add_application(HighLevelEventCapture HighLevelEventCapture.c HighLevelEventCapture.r)
add_application(MemoryManagerCapture MemoryManagerCapture.c)
1 change: 1 addition & 0 deletions ReferenceTests/Retro68/MemoryManager-macos-9.2.2-ppc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# SizesNewHandle(0) -> 522248032 (1F20DF60) MemError 0 NewHandle(0): master set, HGetState 00, size 0NewHandle(-1) -> 0 (00000000) MemError -116NewHandle(0x7FFFFFFF) -> 0 (00000000) MemError -108NewHandle(16 MB) -> 0 (00000000) MemError -108NewHandleClear(7) -> 522248032 (1F20DF60) MemError 0 bytes zero -> 1 (00000001) MemError 0 GetHandleSize -> 7 (00000007) MemError 0SetHandleSize(h, -1): MemError 0; GetHandleSize -> 0SetHandleSize(h, 5): MemError 0; GetHandleSize -> 5SetHandleSize(h, -2): MemError 0; GetHandleSize -> 0SetHandleSize(h, 0): MemError 0; GetHandleSize -> 0 master pointer set -> 1 (00000001) MemError 0SetHandleSize(h, 0x7FFFFFFF): MemError -108; GetHandleSize -> 0DisposeHandle(h) -> 0 (00000000) MemError 0# Statenew: master set, HGetState 00, size 16HLock: master set, HGetState 80, size 16HPurge: master set, HGetState C0, size 16HSetRBit: master set, HGetState E0, size 16HUnlock: master set, HGetState 60, size 16HNoPurge: master set, HGetState 20, size 16HClrRBit: master set, HGetState 00, size 16HSetState(FF): master set, HGetState E0, size 16 MemError -> 0 (00000000) MemError 0HSetState(00): master set, HGetState 00, size 16RecoverHandle(*h) == h -> 1 (00000001) MemError 0# Empty and purgedNewEmptyHandle -> 522248028 (1F20DF5C) MemError 0 empty: master nil, HGetState 93, size 0 GetHandleSize -> 0 (00000000) MemError -109 HLock -> 0 (00000000) MemError -109 locked empty: master nil, HGetState 93, size 0 HUnlock -> 0 (00000000) MemError -109 HPurge -> 0 (00000000) MemError -109 HGetState -> 0 (00000000) MemError -109 SetHandleSize(empty, 10): MemError -109; GetHandleSize -> 0 after: master nil, HGetState 93, size 0EmptyHandle(locked, purgeable) -> 0 (00000000) MemError -112 after: master set, HGetState C0, size 16EmptyHandle(unlocked, purgeable) -> 0 (00000000) MemError 0 after: master nil, HGetState 93, size 0ReallocateHandle(empty, 12) -> 0 (00000000) MemError 0 after: master set, HGetState 00, size 12EmptyHandle(unpurgeable) -> 0 (00000000) MemError 0 after: master nil, HGetState 93, size 0ReallocateHandle(locked, 20) -> 0 (00000000) MemError -112 after: master set, HGetState 80, size 12ReallocateHandle(unlocked, 20) -> 0 (00000000) MemError 0 after: master set, HGetState 00, size 20DisposeHandle(empty) -> 0 (00000000) MemError 0# PurgingPurgeMem(max) -> 0 (00000000) MemError -108 purgeable master pointer nil -> 1 (00000001) MemError -108 GetHandleSize(purged) -> 0 (00000000) MemError -109# Growing a locked blockSetHandleSize(locked, 200000): MemError -108; GetHandleSize -> 16 moved -> 0 (00000000) MemError 0SetHandleSize(unlocked, 200000): MemError 0; GetHandleSize -> 200000 moved -> 1 (00000001) MemError 0SetHandleSize(shrink, 8): MemError 0; GetHandleSize -> 8# Master pointer reuseNewHandle after DisposeHandle reuses the master pointer -> 1 (00000001) MemError 0 again -> 0 (00000000) MemError 0# PointersNewPtr(0) -> 522269872 (1F2134B0) MemError 0 GetPtrSize -> 0 (00000000) MemError 0NewPtr(-1) -> 0 (00000000) MemError -116NewPtrClear(9) GetPtrSize -> 9 (00000009) MemError 0SetPtrSize(3): MemError 0; GetPtrSize -> 3SetPtrSize(-1): MemError 0; GetPtrSize -> 0SetPtrSize(200000) with a pointer after: MemError -108; GetPtrSize -> 5DisposePtr -> 0 (00000000) MemError 0# NilGetHandleSize(nil) -> 0 (00000000) MemError -109HLock(nil) -> 0 (00000000) MemError -109HUnlock(nil) -> 0 (00000000) MemError -109HPurge(nil) -> 0 (00000000) MemError -109HGetState(nil) -> 147 (00000093) MemError -109SetHandleSize(nil, 4) -> 0 (00000000) MemError -109EmptyHandle(nil) -> 0 (00000000) MemError 0DisposeHandle(nil) -> 0 (00000000) MemError 0DisposePtr(nil) -> 0 (00000000) MemError 0GetPtrSize(nil) -> 0 (00000000) MemError -113# Done
Expand Down
223 changes: 223 additions & 0 deletions ReferenceTests/Retro68/MemoryManagerCapture.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
* Captures the Memory Manager's rules for handles and pointers in the
* application heap, writing "MemoryManager.txt" next to itself. It refuses
* to run if that file is already there. Each line is a call, what it
* returned and MemError after it; the file is flushed after every line, so
* a call that crashes leaves the lines before it.
*
* It records sizes and their errors, empty and purged handles, the state
* bits HGetState gives, locking and purging, growing a locked block that
* another block follows, master pointer reuse, and the same calls on nil.
*/
#include <MacMemory.h>
#include <Files.h>
#include <stdio.h>
#include <string.h>

static short out;

static void line(const char *text)
{
long count = strlen(text);
FSWrite(out, &count, text);
count = 1;
FSWrite(out, &count, "\r");
FlushVol(0, 0);
}

static void note(const char *call, long value)
{
char text[160];
snprintf(text, sizeof text, "%s -> %ld (%08lX) MemError %d", call, value, (unsigned long)value, MemError());
line(text);
}

/* A call's own MemError, then the handle's size and GetHandleSize's. */
static void sized(const char *call, Handle h)
{
char text[160];
short error = MemError();
long size = GetHandleSize(h);
snprintf(text, sizeof text, "%s: MemError %d; GetHandleSize -> %ld", call, error, size);
line(text);
}

static void pointerSized(const char *call, Ptr p)
{
char text[160];
short error = MemError();
long size = GetPtrSize(p);
snprintf(text, sizeof text, "%s: MemError %d; GetPtrSize -> %ld", call, error, size);
line(text);
}

static void state(const char *label, Handle h)
{
char text[160];
snprintf(text, sizeof text, "%s: master %s, HGetState %02X, size %ld",
label, *h ? "set" : "nil", (unsigned char)HGetState(h), (long)GetHandleSize(h));
line(text);
}

int main(void)
{
static const unsigned char name[] = { 17, 'M','e','m','o','r','y','M','a','n','a','g','e','r','.','t','x','t' };
Handle h, g, blocker, empty, a, b;
Ptr p;
long before;
int i;

if (Create(name, 0, 'Grph', 'TEXT') != noErr) return 1;
if (FSOpen(name, 0, &out) != noErr) return 2;

line("# Sizes");
h = NewHandle(0);
note("NewHandle(0)", (long)h);
if (h) { state(" NewHandle(0)", h); DisposeHandle(h); }
h = NewHandle(-1);
note("NewHandle(-1)", (long)h);
h = NewHandle(0x7FFFFFFF);
note("NewHandle(0x7FFFFFFF)", (long)h);
h = NewHandle(0x00FFFFFF);
note("NewHandle(16 MB)", (long)h);
if (h) DisposeHandle(h);
h = NewHandleClear(7);
note("NewHandleClear(7)", (long)h);
note(" bytes zero", h && (*h)[0] == 0 && (*h)[6] == 0);
note(" GetHandleSize", GetHandleSize(h));
SetHandleSize(h, -1);
sized("SetHandleSize(h, -1)", h);
SetHandleSize(h, 5);
sized("SetHandleSize(h, 5)", h);
SetHandleSize(h, -2);
sized("SetHandleSize(h, -2)", h);
SetHandleSize(h, 0);
sized("SetHandleSize(h, 0)", h);
note(" master pointer set", *h != 0);
SetHandleSize(h, 0x7FFFFFFF);
sized("SetHandleSize(h, 0x7FFFFFFF)", h);
DisposeHandle(h);
note("DisposeHandle(h)", 0);

line("# State");
h = NewHandle(16);
state("new", h);
HLock(h); state("HLock", h);
HPurge(h); state("HPurge", h);
HSetRBit(h); state("HSetRBit", h);
HUnlock(h); state("HUnlock", h);
HNoPurge(h); state("HNoPurge", h);
HClrRBit(h); state("HClrRBit", h);
HSetState(h, (SignedByte)0xFF); state("HSetState(FF)", h);
note(" MemError", 0);
HSetState(h, 0); state("HSetState(00)", h);
note("RecoverHandle(*h) == h", RecoverHandle(*h) == h);

line("# Empty and purged");
empty = NewEmptyHandle();
note("NewEmptyHandle", (long)empty);
state(" empty", empty);
note(" GetHandleSize", GetHandleSize(empty));
HLock(empty); note(" HLock", 0); state(" locked empty", empty);
HUnlock(empty);
HUnlock(empty); note(" HUnlock", 0);
HPurge(empty); note(" HPurge", 0);
HGetState(empty); note(" HGetState", 0);
SetHandleSize(empty, 10);
sized(" SetHandleSize(empty, 10)", empty);
state(" after", empty);
HPurge(h); HLock(h);
EmptyHandle(h); note("EmptyHandle(locked, purgeable)", 0); state(" after", h);
HUnlock(h);
EmptyHandle(h); note("EmptyHandle(unlocked, purgeable)", 0); state(" after", h);
ReallocateHandle(h, 12); note("ReallocateHandle(empty, 12)", 0); state(" after", h);
HNoPurge(h);
EmptyHandle(h); note("EmptyHandle(unpurgeable)", 0); state(" after", h);
ReallocateHandle(h, 12);
HLock(h);
ReallocateHandle(h, 20); note("ReallocateHandle(locked, 20)", 0); state(" after", h);
HUnlock(h);
ReallocateHandle(h, 20); note("ReallocateHandle(unlocked, 20)", 0); state(" after", h);
DisposeHandle(h);
DisposeHandle(empty);
note("DisposeHandle(empty)", 0);

line("# Purging");
a = NewHandle(1000);
HPurge(a);
before = (long)*a;
PurgeMem(0x7FFFFFFF);
note("PurgeMem(max)", 0);
note(" purgeable master pointer nil", *a == 0);
note(" GetHandleSize(purged)", GetHandleSize(a));
DisposeHandle(a);

line("# Growing a locked block");
h = NewHandle(16);
blocker = NewHandle(16);
before = (long)*h;
HLock(h);
HLock(blocker);
SetHandleSize(h, 200000);
sized("SetHandleSize(locked, 200000)", h);
note(" moved", (long)*h != before);
HUnlock(h);
SetHandleSize(h, 200000);
sized("SetHandleSize(unlocked, 200000)", h);
note(" moved", (long)*h != before);
SetHandleSize(h, 8);
sized("SetHandleSize(shrink, 8)", h);
DisposeHandle(h);
HUnlock(blocker);
DisposeHandle(blocker);

line("# Master pointer reuse");
a = NewHandle(4);
DisposeHandle(a);
b = NewHandle(4);
note("NewHandle after DisposeHandle reuses the master pointer", a == b);
g = NewHandle(4);
for (i = 0; i < 3; ++i) {
Handle c = NewHandle(4);
DisposeHandle(c);
}
note(" again", 0);
DisposeHandle(b);
DisposeHandle(g);

line("# Pointers");
p = NewPtr(0);
note("NewPtr(0)", (long)p);
if (p) { note(" GetPtrSize", GetPtrSize(p)); DisposePtr(p); }
p = NewPtr(-1);
note("NewPtr(-1)", (long)p);
p = NewPtrClear(9);
note("NewPtrClear(9) GetPtrSize", GetPtrSize(p));
SetPtrSize(p, 3);
pointerSized("SetPtrSize(3)", p);
SetPtrSize(p, -1);
pointerSized("SetPtrSize(-1)", p);
SetPtrSize(p, 5);
blocker = (Handle)NewPtr(16);
SetPtrSize(p, 200000);
pointerSized("SetPtrSize(200000) with a pointer after", p);
DisposePtr((Ptr)blocker);
DisposePtr(p);
note("DisposePtr", 0);

line("# Nil");
note("GetHandleSize(nil)", GetHandleSize(0));
HLock(0); note("HLock(nil)", 0);
HUnlock(0); note("HUnlock(nil)", 0);
HPurge(0); note("HPurge(nil)", 0);
note("HGetState(nil)", (unsigned char)HGetState(0));
SetHandleSize(0, 4); note("SetHandleSize(nil, 4)", 0);
EmptyHandle(0); note("EmptyHandle(nil)", 0);
DisposeHandle(0); note("DisposeHandle(nil)", 0);
DisposePtr(0); note("DisposePtr(nil)", 0);
note("GetPtrSize(nil)", GetPtrSize(0));

line("# Done");
FSClose(out);
return 0;
}
28 changes: 28 additions & 0 deletions ReferenceTests/Retro68/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,31 @@ OS 9.2.2 is `HighLevelEvents-macos-9.2.2-ppc.txt` (CR line endings).

`EventTests.classicHighLevelEventsStayApart` and `AppleEventInboxTests` check
`ClassicEventQueue` and `AppleEventInbox` against these.

## Memory Manager

`MemoryManagerCapture.c` calls the Memory Manager in its application heap
and writes each call's result and its own MemError, flushing the volume after
every line. Its output on Mac OS 9.2.2 is
`MemoryManager-macos-9.2.2-ppc.txt` (CR line endings).

- Sizes: `NewHandle(-1)` and `NewPtr(-1)` gave memSCErr (-116); one too
large, memFullErr (-108). `SetHandleSize` and `SetPtrSize` took a negative
size as 0, without an error. `NewHandle(0)` and `NewPtr(0)` gave blocks of
no bytes, and `SetHandleSize(h, 0)` kept the master pointer set.
- State: `HGetState` gave the locked (0x80), purgeable (0x40) and resource
(0x20) bits, and `HSetState(0xFF)` set only those.
- A handle's state went with its block. For a handle with no block, an empty
or purged one, `HGetState`, `HLock`, `HUnlock`, `HPurge`, `GetHandleSize`
and `SetHandleSize` gave nilHandleErr (-109); `HGetState` returned 0x93,
that error's low byte. `ReallocateHandle` gave the handle a block with no
bits set.
- `EmptyHandle` and `ReallocateHandle` of a locked handle gave memPurErr
(-112) and left it as it was. A locked block that couldn't grow where it
was gave memFullErr and kept its size; an unlocked one moved.
- `PurgeMem` purged a purgeable block, its master pointer then nil.
- A disposed handle's master pointer was the next `NewHandle` gave.
- Nil: `DisposeHandle`, `EmptyHandle` and `DisposePtr` gave noErr;
`GetPtrSize(nil)` gave memAZErr (-113).

`HeapZoneTests` checks `HeapZone` against these.
14 changes: 13 additions & 1 deletion Sources/GraphiteCore/MacError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public extension MacError {
static var memoryFull: MacError { MacError(-108) }
/// nilHandleErr: a handle that is nil.
static var nilHandle: MacError { MacError(-109) }
/// memAdrErr: an address outside the heap, or not a block's.
static var memoryAddress: MacError { MacError(-110) }
/// memWZErr: a block that is free.
static var memoryFreeBlock: MacError { MacError(-111) }
/// memPurErr: purging a locked block.
static var memoryPurgeLocked: MacError { MacError(-112) }
/// memAZErr: an address not in the zone.
static var memoryZone: MacError { MacError(-113) }
/// memSCErr: a size that can't be.
static var memorySize: MacError { MacError(-116) }
/// memLockedErr: moving a locked block.
static var memoryLocked: MacError { MacError(-117) }

// Gestalt

Expand Down Expand Up @@ -180,7 +192,7 @@ extension MacError: CustomStringConvertible {
/// "paramErr (-50)".
public var description: String {
let names: [Int32: String] = [
0: "noErr", -1: "qErr", -4: "unimpErr", -50: "paramErr", -108: "memFullErr", -109: "nilHandleErr",
0: "noErr", -1: "qErr", -4: "unimpErr", -50: "paramErr", -108: "memFullErr", -109: "nilHandleErr", -110: "memAdrErr", -111: "memWZErr", -112: "memPurErr", -113: "memAZErr", -116: "memSCErr", -117: "memLockedErr",
-128: "userCanceledErr", -5551: "gestaltUndefSelectorErr", -5552: "gestaltDupSelectorErr",
-5553: "gestaltLocationErr", -29299: "kMPInvalidIDErr", -29298: "kMPInsufficientResourcesErr",
-29297: "kMPTaskAbortedErr", -29296: "kMPTimeoutErr", -29295: "kMPDeletedErr",
Expand Down
75 changes: 75 additions & 0 deletions Sources/GraphiteMemory/GuestMemory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// The guest's memory, as the host gives it to the Memory Manager: checked
// reads and writes of bytes at guest addresses. A heap zone keeps its
// blocks and master pointers in it, so that the guest's own code can follow
// a handle to its block, and keeps everything else about them itself.

import GraphiteCore

public protocol GuestMemory: AnyObject {

/// The bytes at an address: a fault for addresses the guest can't use.
func read(_ address: UInt32, count: Int) throws(MacError) -> [UInt8]

/// Writes bytes at an address: a fault for addresses the guest can't
/// use.
func write(_ bytes: [UInt8], at address: UInt32) throws(MacError)

/// Copies bytes from one place to another, the two maybe overlapping.
func move(from source: UInt32, to destination: UInt32, count: Int) throws(MacError)
}

public extension GuestMemory {

func move(from source: UInt32, to destination: UInt32, count: Int) throws(MacError) {
guard count > 0, source != destination else { return }
try write(read(source, count: count), at: destination)
}

/// Writes zeros.
func zero(_ address: UInt32, count: Int) throws(MacError) {
guard count > 0 else { return }
try write([UInt8](repeating: 0, count: count), at: address)
}

/// A big-endian 32-bit value, as a master pointer is.
func load32(_ address: UInt32) throws(MacError) -> UInt32 {
try read(address, count: 4).reduce(0) { $0 << 8 | UInt32($1) }
}

/// Stores a big-endian 32-bit value.
func store32(_ value: UInt32, at address: UInt32) throws(MacError) {
try write([UInt8(value >> 24), UInt8(truncatingIfNeeded: value >> 16),
UInt8(truncatingIfNeeded: value >> 8), UInt8(truncatingIfNeeded: value)], at: address)
}
}

/// Guest memory in a byte array, from a base address: for tests, and hosts
/// that keep the guest's memory so. Addresses outside it are memAdrErr.
public final class ByteGuestMemory: GuestMemory {

public let base: UInt32

public private(set) var bytes: [UInt8]

public init(base: UInt32, count: Int) {
self.base = base
self.bytes = [UInt8](repeating: 0, count: count)
}

private func offset(_ address: UInt32, _ count: Int) throws(MacError) -> Int {
guard address >= base, count >= 0 else { throw .memoryAddress }
let offset = Int(address - base)
guard offset + count <= bytes.count else { throw .memoryAddress }
return offset
}

public func read(_ address: UInt32, count: Int) throws(MacError) -> [UInt8] {
let start = try offset(address, count)
return Array(bytes[start ..< start + count])
}

public func write(_ bytes: [UInt8], at address: UInt32) throws(MacError) {
let start = try offset(address, bytes.count)
self.bytes.replaceSubrange(start ..< start + bytes.count, with: bytes)
}
}
Loading
Loading