Skip to content

Commit

Permalink
example3 added
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Apr 18, 2024
1 parent 179c543 commit fcbbfe1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ jobs:
- name: Run
run: |
zig build run-example1
zig build run-example2
zig build run-example2
zig build run-example3
32 changes: 32 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ pub fn build(b: *std.Build) !void {
.BUILD_SHARED_LIBS = false,
});

// generate di file (like, zig-translate-c from D-importC)
// note: ImportC just gives no-mangling C code,
// but does not suppress D features exception and DruntimeGC.

// try buildExe(b, .{
// .name = "bdwgcd",
// .target = target,
// .optimize = optimize,
// .betterC = true, // disable D runtimeGC
// .kind = .obj,
// .artifact = bdwgc.artifact("gc"),
// .sources = &.{"src/gc.c"},
// .dflags = &.{
// "-w",
// "-Isrc",
// // zig-out/module/cimport.di
// b.fmt("-Hf={s}/module/cimport.di", .{b.install_path}),
// },
// });

try buildExe(b, .{
.name = "example1",
.target = target,
Expand All @@ -39,6 +59,18 @@ pub fn build(b: *std.Build) !void {
"-Isrc",
},
});
try buildExe(b, .{
.name = "example3",
.target = target,
.optimize = optimize,
.betterC = false, // need D runtimeGC
.artifact = bdwgc.artifact("gc"),
.sources = &.{"examples/example3.d"},
.dflags = &.{
"-w",
"-Isrc",
},
});
}
fn buildExe(b: *std.Build, options: abs.DCompileStep) !void {
const exe = try abs.ldcBuildStep(b, options);
Expand Down
32 changes: 32 additions & 0 deletions examples/example3.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import bdwgc;
import core.memory;
import core.thread;
import core.stdc.stdio;

void main()
{
GC_init(); // Initialize GC
GC_enable_incremental();

GC_start_incremental_collection();

auto t = new Thread(() {
int* numbers = cast(int*) GC_malloc(100 * int.sizeof);

// Populate array
foreach (i; 0 .. 100)
{
numbers[i] = i;
}

// Print elements
foreach (n; numbers[0 .. 100])
{
printf("%d ", n);
}
printf("\n");
});
t.start();
t.join();

}

0 comments on commit fcbbfe1

Please sign in to comment.