vkoskiv
/MacNTP
/amendments
/15
Purge resources when done to save 8K of system heap
We now no longer need to persist our resources in the system heap for
later trap access, so we now let the system purge them after MacNTP
has run. This saves 8K of system heap.
If MacNTP did end up updating the clock, it sets up the MacTCP driver,
which stays resident in the system heap until the next reboot, bloating
the system heap by 16K. Nothing we can do about that, AFAIK.
I also yanked out two globals we no longer need.
vkoskiv made amendment 15 about 1 year ago
--- main.c Fri Sep 8 20:35:25 2023
+++ main.c Fri Sep 8 21:47:23 2023
@@ -31,7 +31,7 @@ TODO:
//#define DEBUGGING
void dump_error(enum MacNTPError error, OSErr oserror);
-int TryMacNTP(void);
+int TryMacNTP(Str255 url, Str255 utc);
#define SECONDS_TO_02_09_1996 2924510400ul
#define MACNTP_ICN_ID_PENDING 128
@@ -41,10 +41,7 @@ int TryMacNTP(void);
Handle g_mtcp_init_handle;
#define NTP_URL_STR_ID 128
-StringHandle g_url;
-
#define UTC_OFFSET_STR_ID 129
-StringHandle g_utc; // Offset in minutes,
#ifdef DEBUGGING
void dump_error(enum MacNTPError error, OSErr oserror) {
@@ -98,24 +95,18 @@ void dump_error(enum MacNTPError error, OSErr oserror)
}
#endif
-int TryMacNTP(void) {
+int TryMacNTP(Str255 url, Str255 utc) {
struct ntp_packet payload;
enum MacNTPError error;
OSErr oserr = 0;
minutes_t utc_offset;
- if (!g_utc) return 1;
- StringToNum(*g_utc, &utc_offset);
- HUnlock(g_utc);
- DisposHandle(g_utc);
- g_utc = nil;
+ if (!utc) return 1;
+ StringToNum(utc, &utc_offset);
- if (!g_url) return 1;
- PtoCstr(*g_url);
- error = MacNTPFetchTime((char *)(*g_url), &payload, &oserr, utc_offset);
- HUnlock(g_url);
- DisposHandle(g_url);
- g_url = nil;
+ if (!url) return 1;
+ PtoCstr(url);
+ error = MacNTPFetchTime((char *)(url), &payload, &oserr, utc_offset);
if (error) {
#ifdef DEBUGGING
@@ -137,30 +128,28 @@ int TryMacNTP(void) {
void main() {
Ptr our_init_ptr;
unsigned long systime;
+ StringHandle url;
+ StringHandle utc; // Offset in minutes
asm {
move.L A0, our_init_ptr;
}
RememberA0();
SetUpA4();
- g_mtcp_init_handle = RecoverHandle(our_init_ptr);
- DetachResource(g_mtcp_init_handle);
+ // This is already locked in the resource attrs
+ g_mtcp_init_handle = RecoverHandle(our_init_ptr);
- g_url = GetString(NTP_URL_STR_ID);
- g_utc = GetString(UTC_OFFSET_STR_ID);
- if (!g_url || !g_utc) {
+ url = GetString(NTP_URL_STR_ID);
+ utc = GetString(UTC_OFFSET_STR_ID);
+ if (!url || !utc) {
#ifdef DEBUGGING
DebugStr("\pSTR rsrcs missing");
#endif
+ ShowInitIcon(MACNTP_ICN_ID_FAILURE, true);
+ goto skip;
}
- DetachResource(g_url);
- MoveHHi(g_url);
- HLock(g_url);
- DetachResource(g_utc);
- MoveHHi(g_utc);
- HLock(g_utc);
+ HLock(url);
+ HLock(utc);
- ShowInitIcon(MACNTP_ICN_ID_PENDING, false);
-
#ifndef DEBUGGING
// We only try to update the clock if we're well into the 20th century
ReadDateTime((unsigned long *)&systime);
@@ -174,12 +163,17 @@ void main() {
ShowInitIcon(MACNTP_ICN_ID_PENDING, false);
#endif
- if (!TryMacNTP()) {
+ if (!TryMacNTP(*url, *utc)) {
ShowInitIcon(MACNTP_ICN_ID_SUCCESS, true);
} else {
ShowInitIcon(MACNTP_ICN_ID_FAILURE, true);
}
+ HUnlock(url); // I have no idea if unlock is needed here. Maybe not.
+ DisposHandle(url);
+ HUnlock(utc);
+ DisposHandle(utc);
+ HUnlock(g_mtcp_init_handle);
skip:
error:
RestoreA4();
--- README Thu Sep 7 21:05:05 2023
+++ README Sat Sep 9 01:55:51 2023
@@ -1,6 +1,6 @@
MacNTP is the premier network time solution for System 6 users running MacTCP on their 68k Macs. It pretty much just does what it says on the tin - seamlessly updates the system clock of your Mac via NTP at boot time, so you don't need to put a leak-prone clock battery in your precious computer.
-MacNTP is © Valtteri Koskivuori (vkoskiv), and MIT-licensed.
+MacNTP is © Valtteri Koskivuori (vkoskiv), and ISC-licensed.
If you find it useful, please don't hesitate to reach out with feedback or feature suggestions. You can reach me by mailing a letter to:
- Valtteri Koskivuori, P.O. Box 347, Helsinki, Finland
@@ -8,7 +8,7 @@ If you find it useful, please don't hesitate to reach
More details:
-MacNTP works by patching the InitGraf trap. The patched trap (in main.c) checks to see if MacNTP has already run, and if it hasn't, it then checks the system time. If the system time is obviously incorrect, it will only then try to fetch the current time from the NTP server.
+During boot, MacNTP will check the system clock, and if it detects a time in the 20th century, it will attempt to query the configured NTP server for the current time. The INIT will display the MacNTP icon in the "pending" state. If the NTP request succeeds or fails, the icon is updated to reflect the result.
If you want to compile it yourself:
--- ShowInitIcon.c Fri Sep 8 20:25:45 2023
+++ ShowInitIcon.c Fri Sep 8 21:32:05 2023
@@ -164,5 +164,6 @@ static void DrawBWIcon (short iconID, Rect *iconRect)
// Then the icon.
source.baseAddr = *icon;
CopyBits(&source, &destination, &source.bounds, iconRect, srcOr, nil);
+ HUnlock(icon);
}
}