Add accounting allocator and native memory pressure C-API#244
Open
ramezgerges wants to merge 1 commit into
Open
Add accounting allocator and native memory pressure C-API#244ramezgerges wants to merge 1 commit into
ramezgerges wants to merge 1 commit into
Conversation
36cb7d0 to
90bf5a1
Compare
5 tasks
Add a process-wide allocation counter and threshold-crossing notification
that the managed SkiaSharp layer uses to drive GC.AddMemoryPressure /
RemoveMemoryPressure without polling.
The bookkeeping (atomic counter, threshold watermark, CAS-claimed
crossing logic, malloc_usable_size dispatching, public C-API) lives in
a dedicated src/c/sk_memory.cpp / include/c/sk_memory.h pair in the
SkiaSharp C shim. The upstream allocator
(src/ports/SkMemory_malloc.cpp) gains only a forward declaration of
two extern "C" hook functions and a single call site in each of
sk_malloc_flags / sk_free / sk_realloc_throw -- ~25 added lines total,
no upstream lines removed.
The new public C-API:
uint64_t sk_memory_get_native_allocated(void);
void sk_memory_set_threshold_callback(
sk_memory_threshold_proc callback,
uint64_t threshold_bytes);
A NULL callback or zero threshold disables notifications; the counter
keeps updating.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
90bf5a1 to
34d86c7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of Change
Add src/c/sk_memory_skiasharp.cpp, an accounting variant of src/ports/SkMemory_malloc.cpp. Behaviorally identical to the upstream allocator, but every successful sk_malloc/sk_realloc/sk_free updates an atomic int64 counter (measured via malloc_usable_size / _msize / malloc_size). A registered callback fires whenever the counter drifts +/- threshold bytes since the last notification, claimed via CAS so only one thread fires per crossing.
Expose two new functions in include/c/sk_graphics.h:
uint64_t sk_graphics_get_native_memory_allocated(void);
void sk_graphics_set_native_memory_threshold_callback(
sk_native_memory_threshold_proc callback,
uint64_t threshold_bytes);
BUILD.gn: link the SkiaSharp variant instead of the upstream allocator. The sksl tool's separate build (gn/sksl.gni) keeps the default malloc.
Lets the managed layer report Skia's native byte usage to the GC via GC.AddMemoryPressure / RemoveMemoryPressure without polling.
SkiaSharp Issue
Related to mono/SkiaSharp#1288
API Changes
uint64_t sk_graphics_get_native_memory_allocated(void);
void sk_graphics_set_native_memory_threshold_callback(
sk_native_memory_threshold_proc callback,
uint64_t threshold_bytes);
Behavioral Changes
None.
Required SkiaSharp PR
Requires mono/SkiaSharp#4067
PR Checklist
skiasharpat time of PR