Skip to content

Commit 8323885

Browse files
committed
added panic, hello world and allocate
1 parent b226e57 commit 8323885

File tree

6 files changed

+65
-4
lines changed

6 files changed

+65
-4
lines changed

allocate.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from cffi import FFI
2+
3+
cffi = FFI()
4+
cffi.cdef(
5+
"""
6+
char* allocByteArrayZ(long long size);
7+
void printZ(char* messageZ);
8+
void freeByteArrayZ(char* array);
9+
bool detectLeaks();
10+
"""
11+
)
12+
13+
import os
14+
15+
allocate = cffi.dlopen(os.path.abspath("allocate.dll"))
16+
17+
messageZ = allocate.allocByteArrayZ(5)
18+
messageZ[0:5] = b"hello"
19+
20+
allocate.printZ(messageZ)
21+
22+
allocate.freeByteArrayZ(messageZ)
23+
24+
print("leaked: {leaked}".format(leaked=allocate.detectLeaks()))

allocate.zig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const std = @import("std");
2+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
3+
4+
export fn allocByteArrayZ(size: u64) [*:0]u8 {
5+
const arrayZ = gpa.allocator().allocSentinel(u8, size, 0) catch unreachable;
6+
return arrayZ.ptr;
7+
}
8+
9+
export fn printZ(message: [*:0]u8) callconv(.C) void {
10+
std.debug.print("{s}\n", .{message});
11+
}
12+
13+
export fn freeByteArrayZ(array: [*:0]u8) callconv(.C) void {
14+
const len = std.mem.len(array);
15+
gpa.allocator().free(array[0..len]);
16+
}
17+
18+
export fn detectLeaks() bool {
19+
return gpa.detectLeaks();
20+
}

hello-world.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
cffi = FFI()
44
cffi.cdef(
55
"""
6-
void print(char* x);
6+
void printZ(char* messageZ);
77
"""
88
)
99

@@ -12,4 +12,4 @@
1212
helloWorld = cffi.dlopen(os.path.abspath("hello-world.dll"))
1313

1414
# pass in null-terminated array
15-
helloWorld.print(b"Hello world\x00")
15+
helloWorld.printZ(b"Hello world\x00")

hello-world.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const std = @import("std");
22

33
// messageZ must be null-terminated
4-
export fn print(messageZ: [*]u8) void {
4+
export fn printZ(messageZ: [*]u8) void {
55
// [*:0]u8 is not allowed by C ABI, cast to right type
66
const message = @ptrCast([*:0]u8, messageZ);
77

8-
std.debug.print("{s}", .{message});
8+
std.debug.print("{s}\n", .{message});
99
}

panic.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from cffi import FFI
2+
3+
cffi = FFI()
4+
cffi.cdef(
5+
"""
6+
void panic();
7+
"""
8+
)
9+
10+
import os
11+
12+
panic = cffi.dlopen(os.path.abspath("panic.dll"))
13+
14+
print(panic.panic())

panic.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export fn panic() void {
2+
@panic("uh oh");
3+
}

0 commit comments

Comments
 (0)