From cb1694f1244b9c3e3d5895752e0d209dad7ca0a3 Mon Sep 17 00:00:00 2001 From: Nathaniel Filardo Date: Fri, 4 Sep 2020 15:29:51 +0100 Subject: [PATCH] pal_windows: defer registration for low-mem until use If we're going to explore fully-static PALs, then we shouldn't need a constructor. --- src/pal/pal_windows.h | 49 ++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/pal/pal_windows.h b/src/pal/pal_windows.h index b4cec09..1e6215f 100644 --- a/src/pal/pal_windows.h +++ b/src/pal/pal_windows.h @@ -46,32 +46,6 @@ namespace snmalloc } public: - PALWindows() - { - // No error handling here - if this doesn't work, then we will just - // consume more memory. There's nothing sensible that we could do in - // error handling. We also leak both the low memory notification object - // handle and the wait object handle. We'll need them until the program - // exits, so there's little point doing anything else. - // - // We only try to register once. If this fails, give up. Even if we - // create multiple PAL objects, we don't want to get more than one - // callback. - if (!registered_for_notifications.exchange(true)) - { - lowMemoryObject = - CreateMemoryResourceNotification(LowMemoryResourceNotification); - HANDLE waitObject; - RegisterWaitForSingleObject( - &waitObject, - lowMemoryObject, - low_memory, - nullptr, - INFINITE, - WT_EXECUTEDEFAULT); - } - } - /** * Bitmap of PalFeatures flags indicating the optional features that this * PAL supports. This PAL supports low-memory notifications. @@ -105,6 +79,29 @@ namespace snmalloc static void register_for_low_memory_callback(PalNotificationObject* callback) { + // No error handling here - if this doesn't work, then we will just + // consume more memory. There's nothing sensible that we could do in + // error handling. We also leak both the low memory notification object + // handle and the wait object handle. We'll need them until the program + // exits, so there's little point doing anything else. + // + // We only try to register once. If this fails, give up. Even if we + // create multiple PAL objects, we don't want to get more than one + // callback. + if (!registered_for_notifications.exchange(true)) + { + lowMemoryObject = + CreateMemoryResourceNotification(LowMemoryResourceNotification); + HANDLE waitObject; + RegisterWaitForSingleObject( + &waitObject, + lowMemoryObject, + low_memory, + nullptr, + INFINITE, + WT_EXECUTEDEFAULT); + } + low_memory_callbacks.register_notification(callback); }