Skip to content

Commit

Permalink
feat(libstore): add page size system feature
Browse files Browse the repository at this point in the history
  • Loading branch information
RossComputerGuy committed Feb 11, 2025
1 parent c5621b9 commit 3d61f26
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ static bool hasVirt() {
}
#endif

extern "C" const char** nix_libstore_get_default_system_features();

StringSet Settings::getDefaultSystemFeatures()
{
/* For backwards compatibility, accept some "features" that are
Expand All @@ -196,6 +198,14 @@ StringSet Settings::getDefaultSystemFeatures()
features.insert("apple-virt");
#endif

const char** value = nix_libstore_get_default_system_features();
if (value != nullptr) {
for (size_t i = 0; value[i] != nullptr; i++) {
features.insert(value[i]);
}
}
free(value);

return features;
}

Expand Down
23 changes: 23 additions & 0 deletions src/libstore/globals.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const std = @import("std");

pub fn getDefaultSystemFeatures() callconv(.C) ?[*:null]const ?[*:0]const u8 {
var list = std.ArrayList(?[*:0]const u8).init(std.heap.c_allocator);
defer list.deinit();

const pgsize = std.heap.pageSize();

if (pgsize == std.heap.page_size_max) {
list.append(std.fmt.allocPrintZ(std.heap.c_allocator, "pages-{d}k", .{pgsize / 1024}) catch return null) catch return null;
} else {
var i = pgsize;
while (i < std.heap.page_size_max) : (i *= 2) {
list.append(std.fmt.allocPrintZ(std.heap.c_allocator, "pages-{d}k", .{i / 1024}) catch return null) catch return null;
}
}

return list.toOwnedSliceSentinel(null) catch return null;
}

comptime {
@export(&getDefaultSystemFeatures, .{ .name = "nix_libstore_get_default_system_features" });
}
5 changes: 5 additions & 0 deletions src/libstore/libstore.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub const globals = @import("globals.zig");

comptime {
_ = globals;
}
16 changes: 15 additions & 1 deletion src/libstore/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project('nix-store', 'cpp',
project('nix-store', 'cpp', 'c',
version : files('.zix-version'),
default_options : [
'cpp_std=c++2a',
Expand Down Expand Up @@ -132,6 +132,8 @@ if aws_s3.found()
endif
deps_other += aws_s3

subdir('nix-meson-build-support/zig')

subdir('nix-meson-build-support/generate-header')

generated_headers = []
Expand Down Expand Up @@ -420,6 +422,18 @@ endforeach
subdir('nix-meson-build-support/export-all-symbols')
subdir('nix-meson-build-support/windows-version')

sources += custom_target(
'zig build-lib',
command: [zig, 'build-lib', '-femit-h=@OUTDIR@/libstore-zig.h', '-femit-bin=@OUTPUT@', '-ofmt=c', '-lc', zig_args, '@INPUT@'],
output: 'libstore-zig.c',
input: 'libstore.zig',
depend_files: [
'globals.zig',
],
)

include_dirs += fs.parent(fs.parent(zig.full_path())) / 'lib' / 'zig'

this_library = library(
'nixstore',
generated_headers,
Expand Down
1 change: 1 addition & 0 deletions src/libstore/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mkMesonLibrary (finalAttrs: {
(fileset.fileFilter (file: file.hasExt "sb") ./.)
(fileset.fileFilter (file: file.hasExt "md") ./.)
(fileset.fileFilter (file: file.hasExt "sql") ./.)
(fileset.fileFilter (file: file.hasExt "zig") ./.)
];

nativeBuildInputs = lib.optional embeddedSandboxShell unixtools.hexdump;
Expand Down

0 comments on commit 3d61f26

Please sign in to comment.