Skip to content

Commit 52949b1

Browse files
Init
0 parents  commit 52949b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+9808
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
Package.resolved

Examples/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
/.build
3+
/.index-build
4+
/Packages
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/configuration/registries.json
8+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
9+
.netrc

Examples/Package.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// swift-tools-version: 6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "Examples",
7+
dependencies: [
8+
.package(path: "../"),
9+
],
10+
targets: [
11+
.executableTarget(
12+
name: "Examples",
13+
dependencies: [
14+
.product(name: "dlmalloc", package: "swift-dlmalloc"),
15+
],
16+
cSettings: [
17+
.unsafeFlags(["-fdeclspec"])
18+
],
19+
swiftSettings: [
20+
.enableExperimentalFeature("Embedded"),
21+
.enableExperimentalFeature("Extern"),
22+
.unsafeFlags([
23+
"-wmo", "-disable-cmo",
24+
"-Xfrontend", "-gnone",
25+
"-Xfrontend", "-disable-stack-protector",
26+
]),
27+
],
28+
linkerSettings: [
29+
.unsafeFlags([
30+
"-Xclang-linker", "-nostdlib",
31+
"-Xlinker", "--no-entry"
32+
])
33+
]
34+
),
35+
]
36+
)

Examples/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Example of swift-dlmalloc usage
2+
3+
This is a simple example of how to use the `swift-dlmalloc` package.
4+
5+
## Build and run
6+
7+
```console
8+
$ swift build -c release --triple wasm32-unknown-none-wasm -debug-info-format=none
9+
$ wasmtime run .build/release/Examples.wasm
10+
Hello, world!
11+
malloc(8) = 0x106d0
12+
*(0x106d0) = 42
13+
```

Examples/Sources/main.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import dlmalloc
2+
3+
@_extern(wasm, module: "wasi_snapshot_preview1", name: "fd_write")
4+
@_extern(c)
5+
func fd_write(fd: Int32, iovs: UnsafeRawPointer, iovs_len: Int32, nwritten: UnsafeMutablePointer<Int32>) -> Int32
6+
7+
func _print(_ string: String) {
8+
var string = string
9+
string.withUTF8 { string in
10+
withUnsafeTemporaryAllocation(byteCount: 8, alignment: 4) { iov in
11+
let iov = iov.baseAddress!
12+
iov.advanced(by: 0).storeBytes(of: string.baseAddress!, as: UnsafeRawPointer.self)
13+
iov.advanced(by: 4).storeBytes(of: Int32(string.count), as: Int32.self)
14+
var nwritten: Int32 = 0
15+
_ = fd_write(fd: 1, iovs: iov, iovs_len: 1, nwritten: &nwritten)
16+
}
17+
}
18+
}
19+
20+
func hex(_ ptr: UnsafeRawPointer) -> String {
21+
return "0x\(String(UInt(bitPattern: ptr), radix: 16))"
22+
}
23+
24+
@_expose(wasm)
25+
@_cdecl("_start")
26+
public func main() {
27+
_print("Hello, world!\n")
28+
29+
let ptr = dlmalloc.malloc(8)!
30+
ptr.storeBytes(of: 42, as: Int.self)
31+
_print("malloc(8) = \(hex(ptr))\n")
32+
_print("*(\(hex(ptr))) = \(ptr.load(as: Int.self))\n")
33+
34+
dlmalloc.free(ptr)
35+
}

NOTICE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This package includes a modified version of dlmalloc derived from the wasi-libc
2+
project. The original dlmalloc is licensed under CC0-1.0, and the modifications
3+
by the wasi-libc project are licensed under the Apache License 2.0 with LLVM
4+
Exceptions, Apache License 2.0, and MIT License.
5+
6+
The original dlmalloc is available at: ftp://gee.cs.oswego.edu/pub/misc/malloc.c
7+
The wasi-libc project is available at: https://github.com/WebAssembly/wasi-libc

Package.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version: 5.10
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "dlmalloc",
7+
products: [
8+
.library(
9+
name: "dlmalloc",
10+
targets: ["dlmalloc"]),
11+
],
12+
targets: [
13+
.target(
14+
name: "dlmalloc",
15+
exclude: ["wasm/"],
16+
cSettings: [
17+
.headerSearchPath("wasm/internal-headers")
18+
]
19+
),
20+
]
21+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
From 27226ddab08b358ddbb961dd9564915c89caf6f0 Mon Sep 17 00:00:00 2001
2+
From: Yuta Saito <[email protected]>
3+
Date: Tue, 15 Oct 2024 08:28:43 +0900
4+
Subject: [PATCH] Remove `__wasi__` check to allow building with
5+
`wasm32-unknown-unknown` target
6+
7+
---
8+
Sources/dlmalloc/wasm/internal-headers/wasi/api.h | 4 ----
9+
1 file changed, 4 deletions(-)
10+
11+
diff --git a/Sources/dlmalloc/wasm/internal-headers/wasi/api.h b/Sources/dlmalloc/wasm/internal-headers/wasi/api.h
12+
index 45a6506..6ac9b72 100644
13+
--- a/Sources/dlmalloc/wasm/internal-headers/wasi/api.h
14+
+++ b/Sources/dlmalloc/wasm/internal-headers/wasi/api.h
15+
@@ -19,10 +19,6 @@
16+
#ifndef __wasi_api_h
17+
#define __wasi_api_h
18+
19+
-#ifndef __wasi__
20+
-#error <wasi/api.h> is only supported on WASI platforms.
21+
-#endif
22+
-
23+
#ifndef __wasm32__
24+
#error <wasi/api.h> only supports wasm32; doesn't yet support wasm64
25+
#endif
26+
--
27+
2.39.3 (Apple Git-146)
28+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
From c09abe907526966f4e679559d229d556e4e71dbe Mon Sep 17 00:00:00 2001
2+
From: Yuta Saito <[email protected]>
3+
Date: Tue, 15 Oct 2024 08:38:39 +0900
4+
Subject: [PATCH] Remove `<bits/alltypes.h>` include
5+
6+
To allow building dlmalloc outside of the wasi-libc source tree, remove
7+
the include of `<bits/alltypes.h>` and replace it with `<stddef.h>`.
8+
---
9+
Sources/dlmalloc/wasm/internal-headers/malloc.h | 4 +---
10+
1 file changed, 1 insertion(+), 3 deletions(-)
11+
12+
diff --git a/Sources/dlmalloc/wasm/internal-headers/malloc.h b/Sources/dlmalloc/wasm/internal-headers/malloc.h
13+
index bc17b10..79d3a2e 100644
14+
--- a/Sources/dlmalloc/wasm/internal-headers/malloc.h
15+
+++ b/Sources/dlmalloc/wasm/internal-headers/malloc.h
16+
@@ -9,9 +9,7 @@
17+
extern "C" {
18+
#endif
19+
20+
-#define __NEED_size_t
21+
-
22+
-#include <bits/alltypes.h>
23+
+#include <stddef.h>
24+
25+
#ifdef __wasilibc_unmodified_upstream /* Use alternate WASI libc headers */
26+
void *malloc (size_t);
27+
--
28+
2.39.3 (Apple Git-146)
29+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
From a3eec3642bbd31610ad52c35b3725e4e832ede10 Mon Sep 17 00:00:00 2001
2+
From: Yuta Saito <[email protected]>
3+
Date: Tue, 15 Oct 2024 08:59:13 +0900
4+
Subject: [PATCH] Remove extern errno values
5+
6+
They increase the size of the data segment, and are not needed since we
7+
have errno.h in this build setup.
8+
---
9+
Sources/dlmalloc/wasm/dlmalloc.c | 7 -------
10+
1 file changed, 7 deletions(-)
11+
12+
diff --git a/Sources/dlmalloc/wasm/dlmalloc.c b/Sources/dlmalloc/wasm/dlmalloc.c
13+
index 331536b..c443d67 100644
14+
--- a/Sources/dlmalloc/wasm/dlmalloc.c
15+
+++ b/Sources/dlmalloc/wasm/dlmalloc.c
16+
@@ -30,13 +30,6 @@
17+
// Align malloc regions to 16, to avoid unaligned SIMD accesses.
18+
#define MALLOC_ALIGNMENT 16
19+
20+
-// Declare errno values used by dlmalloc. We define them like this to avoid
21+
-// putting specific errno values in the ABI.
22+
-extern const int __ENOMEM;
23+
-#define ENOMEM __ENOMEM
24+
-extern const int __EINVAL;
25+
-#define EINVAL __EINVAL
26+
-
27+
// Define USE_DL_PREFIX so that we leave dlmalloc's names prefixed with 'dl'.
28+
// We define them as "static", and we wrap them with public names below. This
29+
// serves two purposes:
30+
--
31+
2.39.3 (Apple Git-146)
32+

0 commit comments

Comments
 (0)