diff options
author | 2022-06-07 22:32:46 -0700 | |
---|---|---|
committer | 2022-06-07 22:32:46 -0700 | |
commit | 43de33afc7fcc4cab25f578566e225ba9e4d4258 (patch) | |
tree | 141676095981741c3a5740093fee79ed12d4edcd /src/memory_allocator.zig | |
parent | 958fc3d4f5ba2a1fb5b5e1e2b9fe3a4500dbefc6 (diff) | |
download | bun-43de33afc7fcc4cab25f578566e225ba9e4d4258.tar.gz bun-43de33afc7fcc4cab25f578566e225ba9e4d4258.tar.zst bun-43de33afc7fcc4cab25f578566e225ba9e4d4258.zip |
Web Streams API (#176)
* [bun.js] `WritableStream`, `ReadableStream`, `TransformStream`, `WritableStreamDefaultController`, `ReadableStreamDefaultController` & more
* Implement `Blob.stream()`
* Update streams.test.js
* Fix sourcemaps crash
* [TextEncoder] 3x faster in hot loops
* reading almost works
* start to implement native streams
* Implement `Blob.stream()`
* Implement `Bun.file(pathOrFd).stream()`
* Add an extra function
* [fs.readFile] Improve performance
* make jsc bindings a little easier to work with
* fix segfault
* faster async/await + readablestream optimizations
* WebKit updates
* More WebKit updates
* Add releaseWEakrefs binding
* `bun:jsc`
* More streams
* Update streams.test.js
* Update Makefile
* Update mimalloc
* Update WebKit
* Create bun-jsc.test.js
* Faster ReadableStream
* Fix off by one & exceptions
* Handle empty files/blobs
* Update streams.test.js
* Move streams to it's own file
* temp
* impl #1
* take two
* good enough for now
* Implement `readableStreamToArray`, `readableStreamToArrayBuffer`, `concatArrayBuffers`
* jsxOptimizationInlining
* Fix crash
* Add `jsxOptimizationInline` to Bun.Transpiler
* Update Transpiler types
* Update js_ast.zig
* Automatically choose production mode when NODE_ENV="production"
* Update cli.zig
* [jsx] Handle defaultProps when inlining
* Update transpiler.test.js
* uncomment some tests
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/memory_allocator.zig')
-rw-r--r-- | src/memory_allocator.zig | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/memory_allocator.zig b/src/memory_allocator.zig index 19b738add..a8fbd116a 100644 --- a/src/memory_allocator.zig +++ b/src/memory_allocator.zig @@ -214,3 +214,129 @@ const z_allocator_vtable = Allocator.VTable{ .resize = ZAllocator.resize, .free = ZAllocator.free, }; +const HugeAllocator = struct { + fn alloc( + _: *anyopaque, + len: usize, + alignment: u29, + len_align: u29, + return_address: usize, + ) error{OutOfMemory}![]u8 { + _ = return_address; + assert(len > 0); + assert(std.math.isPowerOfTwo(alignment)); + + var slice = std.os.mmap( + null, + len, + std.os.PROT.READ | std.os.PROT.WRITE, + std.os.MAP.ANONYMOUS | std.os.MAP.PRIVATE, + -1, + 0, + ) catch + return error.OutOfMemory; + + _ = len_align; + return slice; + } + + fn resize( + _: *anyopaque, + _: []u8, + _: u29, + _: usize, + _: u29, + _: usize, + ) ?usize { + return null; + } + + fn free( + _: *anyopaque, + buf: []u8, + _: u29, + _: usize, + ) void { + std.os.munmap(@alignCast(std.meta.alignment([]align(std.mem.page_size) u8), buf)); + } +}; + +pub const huge_allocator = Allocator{ + .ptr = undefined, + .vtable = &huge_allocator_vtable, +}; +const huge_allocator_vtable = Allocator.VTable{ + .alloc = HugeAllocator.alloc, + .resize = HugeAllocator.resize, + .free = HugeAllocator.free, +}; + +pub const huge_threshold = 1024 * 256; + +const AutoSizeAllocator = struct { + fn alloc( + _: *anyopaque, + len: usize, + alignment: u29, + len_align: u29, + return_address: usize, + ) error{OutOfMemory}![]u8 { + if (len >= huge_threshold) { + return huge_allocator.rawAlloc( + len, + alignment, + len_align, + return_address, + ); + } + + return c_allocator.rawAlloc( + len, + alignment, + len_align, + return_address, + ); + } + + fn resize( + _: *anyopaque, + _: []u8, + _: u29, + _: usize, + _: u29, + _: usize, + ) ?usize { + return null; + } + + fn free( + _: *anyopaque, + buf: []u8, + a: u29, + b: usize, + ) void { + if (buf.len >= huge_threshold) { + return huge_allocator.rawFree( + buf, + a, + b, + ); + } + + return c_allocator.rawFree( + buf, + a, + b, + ); + } +}; + +pub const auto_allocator = Allocator{ + .ptr = undefined, + .vtable = &auto_allocator_vtable, +}; +const auto_allocator_vtable = Allocator.VTable{ + .alloc = AutoSizeAllocator.alloc, + .resize = AutoSizeAllocator.resize, + .free = AutoSizeAllocator.free, +}; |