diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a3f52f..15cee55 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,4 +23,5 @@ jobs: - name: Run run: | zig build run-example1 - zig build run-example2 \ No newline at end of file + zig build run-example2 + zig build run-example3 \ No newline at end of file diff --git a/build.zig b/build.zig index 2b26259..3a18cd2 100644 --- a/build.zig +++ b/build.zig @@ -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, @@ -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); diff --git a/examples/example3.d b/examples/example3.d new file mode 100644 index 0000000..e0fbd26 --- /dev/null +++ b/examples/example3.d @@ -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(); + +}