Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zig: deallocate a varargs function after applying it #591

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions impls/zig/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:bionic
FROM ubuntu:24.04
MAINTAINER Joel Martin <[email protected]>

##########################################################
Expand All @@ -9,25 +9,18 @@ MAINTAINER Joel Martin <[email protected]>
RUN apt-get -y update

# Required for running tests
RUN apt-get -y install make python

# Some typical implementation and test requirements
RUN apt-get -y install curl libreadline-dev libedit-dev libpcre3-dev
RUN apt-get -y install make python3
RUN ln -fs /usr/bin/python3 /usr/local/bin/python

RUN mkdir -p /mal
WORKDIR /mal

##########################################################
# Specific implementation requirements
##########################################################
RUN apt-get -y install ca-certificates curl gcc libc6-dev libpcre3-dev libreadline-dev xz-utils

RUN apt-get -y install gcc gdc ldc gpg wget

RUN wget https://ziglang.org/download/0.5.0/zig-linux-x86_64-0.5.0.tar.xz && \
echo `pwd` && \
tar -xf zig-linux-x86_64-0.5.0.tar.xz && \
cp -r zig-linux-x86_64-0.5.0 /usr/local/bin && \
ln -sf /usr/local/bin/zig-linux-x86_64-0.5.0/zig /usr/local/bin/zig && \
chmod +x /usr/local/bin/zig
RUN curl https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz | tar -xJC/opt
RUN ln -fst/usr/local/bin /opt/zig-linux-x86_64-0.13.0/zig

ENV HOME /mal
15 changes: 7 additions & 8 deletions impls/zig/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ STEPS = step0_repl step1_read_print step2_eval step3_env step4_if_fn_do step5_tc

all: $(STEPS)

dist: mal
zig_opts += --release=safe
zig_opts += -Doptimize=Debug
$(STEPS):
zig build $(zig_opts) -Dname=$@ [email protected]


%: %.zig
zig build -Drelease-fast=true
ln -sf zig-cache/bin/$* .

.PHONY: clean
.PHONY: all $(STEPS) clean

clean:
rm -f $(STEPS)
rm -fr .zig-cache/ zig-out/
rm -f *~
24 changes: 24 additions & 0 deletions impls/zig/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
debug_alloc in types.zig may help with reference counting.


TODO Simplify the printer with the new reader functions in the zig
library.


NOTE Before implementing any optimization or optional fix that would
increase the complexity, please take into account that someone has to
maintain the code, and the zig language evolves quickly.

Some memory leaks are probably already present, especially when an
error interrupts the normal execution flow.

Examples of things that are deliberately not implemented...
* TCO for try*
* preallocate integers between 0 and 100 at startup
* use ArrayList.ensureTotalCapacityPrecise/HashMap.ensureTotalCapacity
after most calls to new_list/vector/map.
* store symbols in a global hash map,
* implement lists/vectors as slices/cons cells/whatever
* deallocate cyclic structures not detected by reference counting like
(let* (f (fn* () nil)))
(def! a (atom 2)) (def! v [a]) (reset! a v)
51 changes: 21 additions & 30 deletions impls/zig/build.zig
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
const LibExeObjStep = @import("std").build.LibExeObjStep;
const Builder = @import("std").build.Builder;
const builtin = @import("builtin");

const warn = @import("std").debug.warn;
const Builder = @import("std").Build;

pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();

const exes = [_] *LibExeObjStep {
b.addExecutable("step0_repl", "step0_repl.zig"),
b.addExecutable("step1_read_print", "step1_read_print.zig"),
b.addExecutable("step2_eval", "step2_eval.zig"),
b.addExecutable("step3_env", "step3_env.zig"),
b.addExecutable("step4_if_fn_do", "step4_if_fn_do.zig"),
b.addExecutable("step5_tco", "step5_tco.zig"),
b.addExecutable("step6_file", "step6_file.zig"),
b.addExecutable("step7_quote", "step7_quote.zig"),
b.addExecutable("step8_macros", "step8_macros.zig"),
b.addExecutable("step9_try", "step9_try.zig"),
b.addExecutable("stepA_mal", "stepA_mal.zig"),
};
// Two options select the built step.

const name = b.option([]const u8, "name", "step name (without .zig)")
orelse "stepA_mal";

const root_source_file = b.path(
b.option([]const u8, "root_source_file", "step name (with .zig)")
orelse "stepA_mal.zig");

const exe = b.addExecutable(.{
.name = name,
.root_source_file = root_source_file,
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
});

for(exes) |exe| {
exe.setBuildMode(mode);
exe.linkSystemLibrary("c");
exe.linkSystemLibrary("pcre");
exe.linkSystemLibrary("readline");
const run_cmd = exe.run();
const step = b.step(exe.name, exe.name);
step.dependOn(&run_cmd.step);
b.default_step.dependOn(&exe.step);
b.installArtifact(exe);
}
exe.linkSystemLibrary("c");
exe.linkSystemLibrary("pcre");
exe.linkSystemLibrary("readline");
b.default_step.dependOn(&exe.step);
b.installArtifact(exe);
}
Loading