{"id":23178358,"url":"https://github.com/peter-barrow/shared-memory-zig","last_synced_at":"2026-06-14T21:01:22.933Z","repository":{"id":267332321,"uuid":"900915436","full_name":"Peter-Barrow/shared-memory-zig","owner":"Peter-Barrow","description":"Unified interface to shared memory on Linux, macOS and Windows","archived":false,"fork":false,"pushed_at":"2025-03-19T20:42:28.000Z","size":30,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-05T02:15:36.572Z","etag":null,"topics":["shared-memory","zig","zig-package"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Peter-Barrow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-09T17:49:04.000Z","updated_at":"2025-03-28T22:01:12.000Z","dependencies_parsed_at":"2024-12-09T19:18:32.974Z","dependency_job_id":"1aed7ea6-2ba0-48d6-adfb-f8f4112f51e1","html_url":"https://github.com/Peter-Barrow/shared-memory-zig","commit_stats":null,"previous_names":["peter-barrow/shared-memory-zig"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Peter-Barrow%2Fshared-memory-zig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Peter-Barrow%2Fshared-memory-zig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Peter-Barrow%2Fshared-memory-zig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Peter-Barrow%2Fshared-memory-zig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Peter-Barrow","download_url":"https://codeload.github.com/Peter-Barrow/shared-memory-zig/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247276188,"owners_count":20912288,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["shared-memory","zig","zig-package"],"created_at":"2024-12-18T07:10:23.573Z","updated_at":"2026-06-14T21:01:22.920Z","avatar_url":"https://github.com/Peter-Barrow.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Shared Memory Zig\n\n**Overview**\n\nThis library implements a unified interface to shared memory on Linux, macOS and Windows\n\n## Features\n\n- Cross-platform support (Linux, FreeBSD, Windows)\n- Multiple implementation strategies (memfd, POSIX shm, Windows file mapping)\n- Type-safe shared memory segments\n- Automatic memory layout management with headers\n- Built-in existence checking\n\n## Installation\nAdd the following to you `build.zig.zon`, replacing the url with the latest archive, for example `https://github.com/Peter-Barrow/shared-memory-zig/archive/699c748fbb143733183760cc7e83ded098eac6d1.zip` and then replacing the hash with the latest commit hash.\n``` zig\n.{\n    .name = \"my-project\",\n    .version = \"0.0.0\",\n    .dependencies = .{\n        .shared_memory = .{\n            .url = \"\",\n            .hash = \"\",\n        },\n    },\n}\n```\nOr alternatively run:\n``` shell\nzig fetch --save git+https://github.com/Peter-Barrow/shared-memory-zig.git\n```\n\nAdd the following to your `build.zig`\n``` zig\nconst shared_memory = b.dependency(\"shared_memory\", .{}).module(\"shared_memory\");\nconst exe = b.addExecutable(...);\n// This adds the shared-memory-zig module to the executable which can then be imported with `@import(\"shared-memory-zig\")`\nexe.root_module.addImport(\"shared-memory-zig\", shared_memory);\n```\n\n## Dependencies\nFor compatibility with Windows this requires [zigwin32](https://github.com/marlersoft/zigwin32)\nThis codebase also uses the [known-folders](https://github.com/ziglibs/known-folders/tree/master) library to get the runtime directory on Linux and FreeBSD when using the `memfd` backend.\n\n## Example\n### Create a shared struct\n``` zig\nconst shm = @import(\"shared_memory\");\n\nconst TestStruct = struct {\n    x: i32,\n    y: f64,\n};\nconst SharedStruct = SharedMemory(TestStruct);\n\nconst shm_name = \"test_single_struct\";\n\nvar tmp = std.testing.tmpDir(.{});\ndefer tmp.cleanup();\n\nconst alloca = std.testing.allocator;\nconst io = std.testing.io;\n\nvar b: shm.DefaultBackend = .{};\nconst backend = b.backend();\n\nvar shm: SharedStruct = try shm.SharedStruct.create(\n    shm_name,\n    tmp.dir,\n    backend,\n    io,\n    alloca,\n);\ndefer shm.close();\n\nshm.data.* = .{ .x = 42, .y = 3.14 };\n\n// Open the shared memory in another \"process\"\nvar shm2 = try SharedStruct.open(\n    shm_name,\n    tmp.dir,\n    backend,\n);\ndefer shm2.close();\n\ntry std.testing.expectEqual(@as(i32, 42), shm2.data.x);\ntry std.testing.expectApproxEqAbs(@as(f64, 3.14), shm2.data.y, 0.001);\n```\n\n### Create a shared array of comptime known length\n``` zig\nconst shm = @import(\"shared_memory\");\n\nconst array_size = 20;\nvar expected = [_]i32{0} ** array_size;\nfor (0..array_size) |i| {\n    expected[i] = @intCast(i * 2);\n}\n\nconst alloca = std.testing.allocator;\nvar io_threaded: std.Io.Threaded = .init(alloca, .{});\nconst io = io_threaded.io();\n\nconst shm_name = \"test_array_fixed_length\";\n\nvar tmp = std.testing.tmpDir(.{});\ndefer tmp.cleanup();\n\nvar b: shm.DefaultBackend = .{};\nconst backend = b.backend();\n\nconst SharedI32 = shm.SharedMemory([array_size]i32);\n\nvar shm: SharedI32 = try SharedI32.create(\n    shm_name,\n    tmp.dir,\n    backend,\n    io,\n    alloca,\n);\ndefer shm.close();\n\nfor (shm.data, 0..) |*item, i| {\n    item.* = @intCast(i * 2);\n}\n\n// Open the shared memory in another \"process\"\nvar shm2 = try SharedI32.open(shm_name, tmp.dir, backend);\ndefer shm2.close();\n\nfor (shm2.data, 0..) |item, i| {\n    try std.testing.expectEqual(@as(i32, @intCast(i * 2)), item);\n}\ntry std.testing.expectEqualSlices(i32, \u0026expected, shm2.data);\n```\n\n### Create an array with runtime known length\n``` zig\nconst shm = @import(\"shared_memory\");\n\nconst array_size = 20;\nvar expected = [_]i32{0} ** array_size;\nfor (0..array_size) |i| {\n    expected[i] = @intCast(i * 2);\n}\n\nconst alloca = std.testing.allocator;\nvar io_threaded: std.Io.Threaded = .init(alloca, .{});\nconst io = io_threaded.io();\n\nconst shm_name = \"test_array_runtime_length\";\n\nvar tmp = std.testing.tmpDir(.{});\ndefer tmp.cleanup();\n\nif (use_shm_funcs) posixForceClose(shm_name);\n\nvar b: shm.DefaultBackend = .{};\nconst backend = b.backend();\n\nconst SharedI32 = shm.SharedMemory([]i32);\n\nvar shm: SharedI32 = try SharedI32.createCapacity(\n    shm_name,\n    tmp.dir,\n    array_size,\n    backend,\n    io,\n    alloca,\n);\ndefer shm.close();\n\nfor (shm.data, 0..) |*item, i| {\n    item.* = @intCast(i * 2);\n}\n\n// Open the shared memory in another \"process\"\nvar shm2 = try SharedI32.open(shm_name, tmp.dir, backend);\ndefer shm2.close();\n\n//for (shm2.data, 0..) |item, i| {\nfor (0..array_size) |i| {\n    try std.testing.expectEqual(@as(i32, @intCast(i * 2)), shm2.data[i]);\n}\ntry std.testing.expectEqualSlices(i32, \u0026expected, shm2.data[0..array_size]);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeter-barrow%2Fshared-memory-zig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpeter-barrow%2Fshared-memory-zig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpeter-barrow%2Fshared-memory-zig/lists"}