Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support std.WasmPageAllocator #9

Open
fengb opened this issue Jan 9, 2020 · 1 comment
Open

Support std.WasmPageAllocator #9

fengb opened this issue Jan 9, 2020 · 1 comment

Comments

@fengb
Copy link
Owner

fengb commented Jan 9, 2020

Now that we have a built-in page-based allocator in std, we should start consuming it.

Benefits:

  • standardize around std — properly coordinate with any wasm allocator
  • really freeing memory — this should be identical to ArenaAllocator.deinit
  • potentially ignore jumbo allocations entirely

Drawbacks:

  • the std allocator requires an additional 1-2 KB
  • need new metadata architecture for C-like malloc()

Questions:

  1. Is it possible to detect using "free()"? In theory, wasm_page_allocator can be tiny if it optimizes away the free/reuse bookkeeping, so it'd be amazing if it could detect at comptime whether free is needed.
  2. Should we keep around the current dumb page allocator and only enable the new path on "deinit()"? std.WasmPageAllocator was designed to handle freeing any memory page (including ones it didn't originally allocate) so this works around question 1.
@fengb fengb changed the title Support wasm_page_allocator Support std.WasmPageAllocator Jan 9, 2020
@fengb
Copy link
Owner Author

fengb commented Jan 9, 2020

Brain dump of metadata for C-malloc:

Each page pointer (ptr % 65536) translates to an index. This should match the page index stored internally by std.WasmPageAllocator.

const Allocation = enum(u2) {
    None,
    Start,
    Continue,
};

const MallocCompatibility = struct {
    page_meta: PackedIntArray(),

    fn pageCount(self: Self, page_idx: usize) usize {
        assert(self.get(page_idx) == .Start);
        var count = 1;
        while (page_idx + count < self.size and self.get(page_idx + count) == .Continue) {
            count += 1;
        }
    }

    pub fn free(self: Self, ptr: [*]u8) void {
        if (ptr % 65536 == 0) {
            const count = self.pageCount(ptr / 65536);
            if (self.count > 1) {
                const slice = ptr[0 .. self.count * 65536];
                return allocator.free(slice);
            }
        }
        const slice = self.restore(ptr);
        return allocator.free(slice);
    }
};

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

No branches or pull requests

1 participant