Skip to content

Commit

Permalink
Remove mprotect
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed Feb 4, 2024
1 parent ecf1fa3 commit 23f309d
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 41 deletions.
2 changes: 1 addition & 1 deletion bench/bench-with-builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ kiviBenchmark();
if (isDeno() || isBun()) {
kiviBenchmark();
logRatio(0, 1);
logRatio(0, 2);
// logRatio(0, 2);
} else {
logRatio(0, 1);
}
2 changes: 1 addition & 1 deletion bench/faker/generate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Buffer } from "node:buffer";

const count = 1_000_000;
const count = 500_000;

const genRandomNum = (min, max) => {
return Math.random() * (max - min) + min;
Expand Down
5 changes: 2 additions & 3 deletions src/core/Kivi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const ByteMap = @import("ByteMap.zig");
pub const Config = extern struct {
// (2 ** 18) * 16 = 4194304
group_size: usize = 262144,
mem_size: usize = 2 * 1024 * 1024 * 1024,
page_size: usize = 100 * 1024 * 1024,
mem_size: usize = 1 * 1024 * 1024 * 1024,
};

mem: MMap,
Expand All @@ -21,7 +20,7 @@ fn stringcpy(dest: []u8, src: []const u8) !void {
}

pub fn init(self: *Kivi, config: *const Config) !usize {
self.mem = try MMap.init(config.mem_size, config.page_size);
self.mem = try MMap.init(config.mem_size);

try self.map.init(&self.mem, config.group_size);

Expand Down
38 changes: 5 additions & 33 deletions src/core/Mmap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,17 @@ var empty_freelist = FreelistNode{ .len = 0, .next = null };
cursor: usize,
freelist: *FreelistNode,
mem: []align(Syscall.page_size) u8,
mprotect_size: usize,
protected_mem_cursor: usize,

const Mmap = @This();

pub fn byte_slice_cast(comptime T: type, value: []u8) []T {
var new_slice: []T = undefined;
new_slice.ptr = @as([*]T, @alignCast(@ptrCast(value.ptr)));
new_slice.len = value.len;
return new_slice;
}

fn mprotect(self: *Mmap) !void {
const protected_mem_cursor = self.protected_mem_cursor + self.mprotect_size;

if (!is_windows) {
try Syscall.mprotect(@alignCast(self.mem[self.protected_mem_cursor..protected_mem_cursor]), Syscall.PROT.READ | Syscall.PROT.WRITE);
} else {
const std = @import("std");
_ = try std.os.windows.VirtualAlloc(@ptrCast(@alignCast(self.mem)), protected_mem_cursor, std.os.windows.MEM_COMMIT, std.os.windows.PAGE_READWRITE);
}

self.protected_mem_cursor = protected_mem_cursor;
}

pub fn init(total_size: usize, mprotect_size: usize) !Mmap {
pub fn init(total_size: usize) !Mmap {
var mem: []align(Syscall.page_size) u8 = undefined;
const size = Math.alignForward(usize, total_size, Syscall.page_size);
if (!is_windows) {
mem = try Syscall.mmap(
null,
size,
Syscall.PROT.NONE,
Syscall.PROT.READ | Syscall.PROT.WRITE,
Syscall.MAP.ANONYMOUS | Syscall.MAP.PRIVATE,
-1,
0,
Expand All @@ -58,10 +36,7 @@ pub fn init(total_size: usize, mprotect_size: usize) !Mmap {
mem.ptr = @alignCast(@ptrCast(lpvoid));
}

var mmap = Mmap{ .mem = mem, .cursor = 0, .protected_mem_cursor = 0, .freelist = &empty_freelist, .mprotect_size = Math.alignForward(usize, mprotect_size, Syscall.page_size) };
mmap.mprotect() catch unreachable;

return mmap;
return Mmap{ .mem = mem, .cursor = 0, .freelist = &empty_freelist };
}

pub fn alloc_byte(self: *Mmap, size: usize) ![]u8 {
Expand Down Expand Up @@ -91,11 +66,8 @@ pub fn alloc_byte(self: *Mmap, size: usize) ![]u8 {
ending_pos += Math.alignForward(usize, size - 16, 8);
}

if (ending_pos > self.protected_mem_cursor) {
try self.mprotect();
while (ending_pos > self.protected_mem_cursor) {
try self.mprotect();
}
if (ending_pos >= self.mem.len) {
return error.OutOfMemory;
}

self.cursor = ending_pos;
Expand Down
3 changes: 1 addition & 2 deletions src/core/include/kivi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
#include <stddef.h>

struct __attribute__((aligned(8))) Kivi {
char __opaque[88];
char __opaque[72];
};

struct Config {
size_t group_size;
size_t mem_size;
size_t page_size;
};

// TODO: Behavior documented in these comments
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/js/codegen-generated.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This is generated by the codegen. Please don't touch this file.
export const kiviInstanceSize = 88;
export const kiviInstanceSize = 72;

0 comments on commit 23f309d

Please sign in to comment.