Skip to content

Commit 2a94e5a

Browse files
Merge pull request #199 from lightpanda-io/c_alloc
setCAllocator
2 parents f0773a3 + 8e96ee3 commit 2a94e5a

File tree

19 files changed

+163
-14
lines changed

19 files changed

+163
-14
lines changed

.github/actions/install/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ runs:
1919
run: |
2020
ln -s /usr/local/lib/libiconv vendor/libiconv
2121
22+
- name: build mimalloc
23+
shell: bash
24+
run: make install-mimalloc
25+
2226
- name: build netsurf
2327
shell: bash
2428
run: make install-netsurf

.gitmodules

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
url = git@github.com:lightpanda-io/jsruntime-lib.git
44
[submodule "vendor/netsurf/libwapcaplet"]
55
path = vendor/netsurf/libwapcaplet
6-
url = https://source.netsurf-browser.org/libwapcaplet.git
6+
url = git@github.com:lightpanda-io/libwapcaplet.git
77
[submodule "vendor/netsurf/libparserutils"]
88
path = vendor/netsurf/libparserutils
9-
url = https://source.netsurf-browser.org/libparserutils.git
9+
url = git@github.com:lightpanda-io/libparserutils.git
1010
[submodule "vendor/netsurf/libdom"]
1111
path = vendor/netsurf/libdom
1212
url = git@github.com:lightpanda-io/libdom.git
@@ -15,7 +15,10 @@
1515
url = https://source.netsurf-browser.org/buildsystem.git
1616
[submodule "vendor/netsurf/libhubbub"]
1717
path = vendor/netsurf/libhubbub
18-
url = https://source.netsurf-browser.org/libhubbub.git
18+
url = git@github.com:lightpanda-io/libhubbub.git
1919
[submodule "tests/wpt"]
2020
path = tests/wpt
2121
url = https://github.com/lightpanda-io/wpt
22+
[submodule "vendor/mimalloc"]
23+
path = vendor/mimalloc
24+
url = git@github.com:microsoft/mimalloc.git

Makefile

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ test:
9595
.PHONY: install-submodule
9696
.PHONY: install-jsruntime install-jsruntime-dev install-libiconv
9797
.PHONY: _install-netsurf install-netsurf clean-netsurf test-netsurf install-netsurf-dev
98+
.PHONY: install-mimalloc install-mimalloc-dev clean-mimalloc
9899
.PHONY: install-dev install
99100

100101
## Install and build dependencies for release
101-
install: install-submodule install-jsruntime install-netsurf
102+
install: install-submodule install-jsruntime install-netsurf install-mimalloc
102103

103104
## Install and build dependencies for dev
104-
install-dev: install-submodule install-jsruntime-dev install-netsurf-dev
105+
install-dev: install-submodule install-jsruntime-dev install-netsurf-dev install-mimalloc-dev
105106

106107
install-netsurf-dev: _install-netsurf
107108
install-netsurf-dev: OPTCFLAGS := -O0 -g -DNDEBUG
@@ -185,6 +186,26 @@ install-jsruntime:
185186
@cd vendor/jsruntime-lib && \
186187
make install
187188

189+
.PHONY: _build_mimalloc
190+
_build_mimalloc:
191+
@cd vendor/mimalloc && \
192+
mkdir -p out/include && \
193+
cp include/mimalloc.h out/include/ && \
194+
cd out && \
195+
cmake -DMI_BUILD_SHARED=OFF -DMI_BUILD_OBJECT=OFF -DMI_BUILD_TESTS=OFF -DMI_OVERRIDE=OFF $(OPTS) .. && \
196+
make
197+
198+
install-mimalloc-dev: _build_mimalloc
199+
install-mimalloc-dev: OPTS=-DCMAKE_BUILD_TYPE=Debug
200+
install-mimalloc-dev:
201+
@cd vendor/mimalloc/out && \
202+
mv libmimalloc-debug.a libmimalloc.a
203+
204+
install-mimalloc: _build_mimalloc
205+
206+
clean-mimalloc:
207+
@rm -fr vendor/mimalloc/lib/*
208+
188209
## Init and update git submodule
189210
install-submodule:
190211
@git submodule init && \

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ Browsercore is written with [Zig](https://ziglang.org/) `0.12`. You have to
88
install it with the right version in order to build the project.
99

1010
Browsercore also depends on
11-
[js-runtimelib](https://github.com/francisbouvier/jsruntime-lib/) and
12-
[Netsurf libs](https://www.netsurf-browser.org/) libs.
11+
[js-runtimelib](https://github.com/francisbouvier/jsruntime-lib/),
12+
[Netsurf libs](https://www.netsurf-browser.org/) and
13+
[Mimalloc](https://microsoft.github.io/mimalloc) libs.
1314

1415
To be able to build the v8 engine for js-runtimelib, you have to install some libs:
1516

@@ -36,11 +37,26 @@ make install-submodule
3637

3738
### Build Netsurf
3839

39-
The command `make install-netsurf` will build netsurf libs used by browsercore.
40+
The command `make install-netsurf` will build Netsurf libs used by browsercore.
4041
```
4142
make install-netsurf
4243
```
4344

45+
For dev env, use `make install-netsurf-dev`.
46+
47+
### Build Mimalloc
48+
49+
The command `make install-mimalloc` will build Mimalloc lib used by browsercore.
50+
```
51+
make install-mimalloc
52+
```
53+
54+
For dev env, use `make install-mimalloc-dev`.
55+
56+
Note, when Mimalloc is built in dev mode, you can dump memory stats with the
57+
env var `MIMALLOC_SHOW_STATS=1`. See
58+
https://microsoft.github.io/mimalloc/environment.html
59+
4460
### Build jsruntime-lib
4561

4662
The command `make install-jsruntime-dev` uses jsruntime-lib's `zig-v8` dependency to build v8 engine lib.

build.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ fn common(
152152
) !void {
153153
try jsruntime_pkgs.add(step, options);
154154
linkNetSurf(step);
155+
156+
// link mimalloc
157+
step.addObjectFile(.{ .path = "vendor/mimalloc/out/libmimalloc.a" });
158+
step.addIncludePath(.{ .path = "vendor/mimalloc/out/include" });
155159
}
156160

157161
fn linkNetSurf(step: *std.build.LibExeObjStep) void {

src/browser/browser.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ pub const Page = struct {
134134
self.session.env.stop();
135135
// TODO unload document: https://html.spec.whatwg.org/#unloading-documents
136136

137+
// clear netsurf memory arena.
138+
parser.deinit();
139+
137140
_ = self.arena.reset(.free_all);
138141
}
139142

@@ -211,6 +214,9 @@ pub const Page = struct {
211214
fn loadHTMLDoc(self: *Page, reader: anytype, charset: []const u8) !void {
212215
const alloc = self.arena.allocator();
213216

217+
// start netsurf memory arena.
218+
try parser.init();
219+
214220
log.debug("parse html with charset {s}", .{charset});
215221

216222
const ccharset = try alloc.dupeZ(u8, charset);

src/main.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ pub fn main() !void {
5656
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
5757
defer arena.deinit();
5858

59+
try parser.init();
60+
defer parser.deinit();
61+
5962
// document
6063
const file = try std.fs.cwd().openFile("test.html", .{});
6164
defer file.close();

src/main_get.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ pub fn main() !void {
6565
defer browser.deinit();
6666

6767
var page = try browser.currentSession().createPage();
68-
defer page.end();
68+
defer page.deinit();
69+
6970
try page.navigate(url);
71+
defer page.end();
7072

7173
if (dump) {
7274
try page.dump(std.io.getStdOut());

src/main_shell.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub fn main() !void {
3737
var arena = std.heap.ArenaAllocator.init(gpa.allocator());
3838
defer arena.deinit();
3939

40+
try parser.init();
41+
defer parser.deinit();
42+
4043
// document
4144
const file = try std.fs.cwd().openFile("test.html", .{});
4245
defer file.close();

src/mimalloc.zig

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// This file makes the glue between mimalloc heap allocation and libdom memory
2+
// management.
3+
// We replace the libdom default usage of allocations with mimalloc heap
4+
// allocation to be able to free all memory used at once, like an arena usage.
5+
6+
const std = @import("std");
7+
const c = @cImport({
8+
@cInclude("mimalloc.h");
9+
});
10+
11+
const Error = error{
12+
HeapNotNull,
13+
HeapNull,
14+
};
15+
16+
var heap: ?*c.mi_heap_t = null;
17+
18+
pub fn create() Error!void {
19+
if (heap != null) return Error.HeapNotNull;
20+
heap = c.mi_heap_new();
21+
if (heap == null) return Error.HeapNull;
22+
}
23+
24+
pub fn destroy() void {
25+
if (heap == null) return;
26+
c.mi_heap_destroy(heap.?);
27+
heap = null;
28+
}
29+
30+
pub export fn m_alloc(size: usize) callconv(.C) ?*anyopaque {
31+
if (heap == null) return null;
32+
return c.mi_heap_malloc(heap.?, size);
33+
}
34+
35+
pub export fn re_alloc(ptr: ?*anyopaque, size: usize) callconv(.C) ?*anyopaque {
36+
if (heap == null) return null;
37+
return c.mi_heap_realloc(heap.?, ptr, size);
38+
}
39+
40+
pub export fn c_alloc(nmemb: usize, size: usize) callconv(.C) ?*anyopaque {
41+
if (heap == null) return null;
42+
return c.mi_heap_calloc(heap.?, nmemb, size);
43+
}
44+
45+
pub export fn str_dup(s: [*c]const u8) callconv(.C) [*c]u8 {
46+
if (heap == null) return null;
47+
return c.mi_heap_strdup(heap.?, s);
48+
}
49+
50+
pub export fn strn_dup(s: [*c]const u8, size: usize) callconv(.C) [*c]u8 {
51+
if (heap == null) return null;
52+
return c.mi_heap_strndup(heap.?, s, size);
53+
}
54+
55+
// NOOP, use destroy to clear all the memory allocated at once.
56+
pub export fn f_ree(_: ?*anyopaque) callconv(.C) void {
57+
return;
58+
}

0 commit comments

Comments
 (0)