Skip to content

Fix NativeStringContext: fixed 8MB pool, malloc fallback, and proper free - #1264

Merged
uNetworkingAB merged 2 commits into
masterfrom
copilot/add-native-string-context-free
Mar 29, 2026
Merged

Fix NativeStringContext: fixed 8MB pool, malloc fallback, and proper free#1264
uNetworkingAB merged 2 commits into
masterfrom
copilot/add-native-string-context-free

Conversation

Copilot AI commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

The pool in NativeStringContext could resize() (reallocate), leaving dangling pointers in any live NativeString. Additionally, no mechanism existed to release dynamically allocated memory.

Changes

  • Fixed pool size: Pool is permanently 8MB; alloc() no longer calls resize(). This prevents vector reallocation from invalidating pointers held by NativeString.
  • malloc fallback: When the pool is exhausted, alloc() falls back to malloc() instead of growing the pool.
  • NativeStringContext::free(char *): Checks if pointer is within pool range — no-op if so, calls ::free() otherwise. This correctly handles both the pool and malloc'd cases with a single call.
  • NativeString destructor: Tracks an allocated flag (set only on the UTF-8 string copy path) and calls ctx->free(data) on destruction. Copy constructor and assignment operator are deleted to prevent double-free.
// alloc: no more resize, malloc fallback
char *alloc(size_t size) {
    if (pool_offset + size > pool.size()) {
        return (char *) malloc(size);  // freed via free() below
    }
    char *ptr = pool.data() + pool_offset;
    pool_offset += size;
    return ptr;
}

// free: pool pointers are no-ops, malloc pointers are released
void free(char *ptr) {
    if (ptr >= pool.data() && ptr < pool.data() + pool.size()) return;
    ::free(ptr);
}

📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Copilot AI changed the title [WIP] Add NativeStringContext::free to manage memory pointers Fix NativeStringContext: fixed 8MB pool, malloc fallback, and proper free Mar 29, 2026
Copilot AI requested a review from uNetworkingAB March 29, 2026 20:13
@uNetworkingAB
uNetworkingAB marked this pull request as ready for review March 29, 2026 20:15
@uNetworkingAB
uNetworkingAB merged commit 71cac73 into master Mar 29, 2026
1 check was pending
Copilot AI added a commit that referenced this pull request Apr 2, 2026
… proper free (#1264)"

This reverts commit 71cac73.

Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
uNetworkingAB added a commit that referenced this pull request Apr 2, 2026
* Revert "Fix NativeStringContext: fixed 8MB pool, malloc fallback, and proper free (#1264)"

This reverts commit 71cac73.

Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>

* Revert "Replace NativeString heap allocations with thread-local pool allocator (#1263)"

This reverts commit 3e2ea85.

Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>

* Revert "Update NativeString to use better copy mechanism and ValueView for utf8 string data direct access (#1262)"

This reverts commit 6b4968f.

Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>

* Revert "Add DataView support, update RecognizedString type (#1258)"

This reverts commit 0b0c8f9.

Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: uNetworkingAB <110806833+uNetworkingAB@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants