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

+6
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

+9
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

+36
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

+13
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

+35
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

+7
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

+21
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+
)
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+
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+
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
From 8cc8e199ea6f2b9282f45b0c26e64295f5c9ec03 Mon Sep 17 00:00:00 2001
2+
From: Yuta Saito <[email protected]>
3+
Date: Tue, 15 Oct 2024 09:45:00 +0900
4+
Subject: [PATCH] Remove redundant include of string.h in memset.c
5+
6+
string.h is not included in this build setup
7+
---
8+
Sources/dlmalloc/wasm/memcpy.c | 2 --
9+
Sources/dlmalloc/wasm/memmove.c | 1 -
10+
Sources/dlmalloc/wasm/memset.c | 1 -
11+
3 file changed, 4 deletion(-)
12+
13+
diff --git a/Sources/dlmalloc/wasm/memset.c b/Sources/dlmalloc/wasm/memset.c
14+
index f64c9cf..711717a 100644
15+
--- a/Sources/dlmalloc/wasm/memset.c
16+
+++ b/Sources/dlmalloc/wasm/memset.c
17+
@@ -1,4 +1,3 @@
18+
-#include <string.h>
19+
#include <stdint.h>
20+
21+
void *memset(void *dest, int c, size_t n)
22+
diff --git a/Sources/dlmalloc/wasm/memcpy.c b/Sources/dlmalloc/wasm/memcpy.c
23+
index 3cc7e28..0422eeb 100644
24+
--- a/Sources/dlmalloc/wasm/memcpy.c
25+
+++ b/Sources/dlmalloc/wasm/memcpy.c
26+
@@ -1,6 +1,4 @@
27+
-#include <string.h>
28+
#include <stdint.h>
29+
-#include <endian.h>
30+
31+
void *memcpy(void *restrict dest, const void *restrict src, size_t n)
32+
{
33+
diff --git a/Sources/dlmalloc/wasm/memmove.c b/Sources/dlmalloc/wasm/memmove.c
34+
index 7376a52..74913a4 100644
35+
--- a/Sources/dlmalloc/wasm/memmove.c
36+
+++ b/Sources/dlmalloc/wasm/memmove.c
37+
@@ -1,4 +1,3 @@
38+
-#include <string.h>
39+
#include <stdint.h>
40+
41+
#ifdef __GNUC__
42+
--
43+
2.39.3 (Apple Git-146)
44+

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# swift-dlmalloc
2+
3+
This is a SwiftPM package for [dlmalloc](https://gee.cs.oswego.edu/dl/html/malloc.html) which is a general-purpose memory allocator written by Doug Lea.
4+
Typically, this package is helpful when you need a memory allocator implementation for Embedded Swift applications.
5+
6+
## Supported Platforms
7+
8+
| Target triplet | Supported |
9+
|----------------|:---------:|
10+
| wasm32-unknown-none-wasm ||
11+
12+
Feel free to create a PR to add support for other platforms.
13+
14+
## Usage
15+
16+
Add the following dependency to your `Package.swift` file:
17+
18+
```swift
19+
dependencies: [
20+
.package(url: "https://github.com/swiftwasm/swift-dlmalloc", from: "0.1.0"),
21+
]
22+
```
23+
24+
Then, add `dlmalloc` as a dependency for your target:
25+
26+
```swift
27+
.executableTarget(
28+
name: "Examples",
29+
dependencies: [
30+
.product(name: "dlmalloc", package: "swift-dlmalloc"),
31+
]
32+
)
33+
```
34+
35+
Finally, import `dlmalloc` module in your Swift code:
36+
37+
```swift
38+
39+
import dlmalloc
40+
41+
public func main() {
42+
let ptr = dlmalloc.malloc(8)!
43+
ptr.storeBytes(of: 42, as: Int.self)
44+
print("malloc(8) = \(ptr)\n")
45+
print("*(\(ptr)) = \(ptr.load(as: Int.self))\n")
46+
47+
dlmalloc.free(ptr)
48+
}
49+
```
50+
51+
See [Examples](Examples) directory for more working examples.
52+
53+
## License
54+
55+
This package is licensed under the MIT license. See [LICENSE](LICENSE) for more info.
56+
57+
Portions of this software are based on the work of Doug Lea and others. See [NOTICE](NOTICE) for more info.
58+

Sources/dlmalloc/dlmalloc.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#if __wasm__
2+
# define LACKS_STRING_H
3+
# include <stddef.h>
4+
void *memcpy (void *__restrict, const void *__restrict, size_t);
5+
void *memset (void *, int, size_t);
6+
void abort(void);
7+
_Thread_local int errno = 0;
8+
# include "./wasm/dlmalloc.c"
9+
# include "./wasm/sbrk.c"
10+
# include "./wasm/memcpy.c"
11+
# include "./wasm/memset.c"
12+
# include "./wasm/memmove.c"
13+
# include "./wasm/abort.c"
14+
#else
15+
# error "Unsupported target platform. Contribution for porting to the platform is more than welcome!"
16+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module dlmalloc {
2+
header "swift_dlmalloc.h"
3+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef SWIFT_DLMALLOC_H
2+
#define SWIFT_DLMALLOC_H
3+
4+
#include <stddef.h>
5+
6+
void *malloc (size_t);
7+
void *calloc (size_t, size_t);
8+
void *realloc (void *, size_t);
9+
void free (void *);
10+
11+
#endif // SWIFT_DLMALLOC_H

Sources/dlmalloc/wasm/abort.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdlib.h>
2+
3+
void abort(void) {
4+
// wasm doesn't support signals, so just trap to halt the program.
5+
__builtin_trap();
6+
}

0 commit comments

Comments
 (0)