Skip to content

Conversation

@trvon
Copy link

@trvon trvon commented Jan 6, 2026

I can clean up this pull request, but this enables windows builds based on my testing.

Summary by CodeRabbit

Release Notes

  • New Features
    • Added Windows platform support to the PDF parser library, enabling compatibility with Windows systems alongside existing POSIX system support.

✏️ Tip: You can customize this high-level summary in your review settings.

Copilot AI review requested due to automatic review settings January 6, 2026 17:51
@coderabbitai
Copy link

coderabbitai bot commented Jan 6, 2026

📝 Walkthrough

Walkthrough

The changes introduce OS-specific file handling to support both Windows and POSIX systems. A new data_is_allocated flag tracks memory ownership, enabling Windows to allocate file data into memory while POSIX systems use memory mapping. File opening and closing logic now branches based on the operating system.

Changes

Cohort / File(s) Summary
OS-aware file handling and memory management
src/root.zig
Adds is_windows constant and data_is_allocated flag to Document struct; implements Windows-specific file allocation path and POSIX mmap path; creates new openFromMemoryOwnedAlloc initialization path; updates close() deallocation logic to distinguish between allocated and mmap'd memory; modifies resolver wrapper cast style.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant FileOpen as File Opening Logic
    participant Windows as Windows Path
    participant POSIX as POSIX Path
    participant Memory as Memory Management
    participant Close as Close/Deallocation

    rect rgb(240, 248, 255)
    Note over Windows,POSIX: OS Detection & File Opening
    User->>FileOpen: openWithConfig()
    alt Windows System
        FileOpen->>Windows: Allocate memory buffer
        Windows->>Memory: allocator.alloc()
        Memory-->>Windows: Allocated memory
        Windows->>FileOpen: openFromMemoryOwnedAlloc()
        FileOpen->>FileOpen: Set data_is_allocated = true
    else POSIX System
        FileOpen->>POSIX: Memory map file
        POSIX->>Memory: mmap()
        Memory-->>POSIX: Mapped memory
        FileOpen->>FileOpen: Set data_is_allocated = false
    end
    end

    rect rgb(255, 240, 245)
    Note over Windows,POSIX: Deallocation Path
    User->>Close: close()
    alt Windows System
        Close->>Memory: allocator.free()
    else POSIX System
        alt data_is_allocated
            Close->>Memory: allocator.free()
        else mmap'd data
            Close->>Memory: munmap()
        end
    end
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Poem

🐰 Hop, hop! Cross platforms with ease,
Windows and POSIX both pleased,
Memory mapped or fresh allocated,
Each path precisely calculated,
Clean deallocation, the burden erased!

Pre-merge checks

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses the main objective of the PR: enabling Windows platform support through OS-aware file handling, memory management, and platform-specific code paths.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4381de3 and b52397c.

📒 Files selected for processing (1)
  • src/root.zig
🔇 Additional comments (6)
src/root.zig (6)

14-17: LGTM!

Clean platform detection that follows the existing is_wasm pattern.


100-103: LGTM!

The data_is_allocated field correctly distinguishes between allocated memory (Windows) and mmap'd memory (POSIX). The default value of false is appropriate for non-owning cases like openFromMemory.


163-174: LGTM!

The POSIX mmap path correctly maps the file with read-only, private semantics.


177-203: LGTM!

The function correctly sets data_is_allocated = false to indicate mmap'd memory, and includes appropriate WASM guard and error handling.


205-227: LGTM!

The new openFromMemoryOwnedAlloc function correctly handles Windows-allocated memory by setting data_is_allocated = true. The function is private, so the lack of a platform guard is acceptable since it's only called from the Windows branch.


376-380: LGTM!

The pointer cast pattern using @ptrCast(@alignCast(...)) is idiomatic Zig for safely converting an opaque pointer to a typed pointer.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Windows platform support to the ZPDF library by implementing an alternative file loading mechanism. Since Windows doesn't support POSIX mmap, the PR uses allocated memory with readAll for Windows while maintaining mmap for POSIX systems.

  • Adds platform detection for Windows and conditional compilation paths
  • Implements allocated memory approach for Windows file loading instead of mmap
  • Updates cleanup logic to handle both mmap'd and allocated memory

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return openFromMemoryOwned(allocator, data, config);
if (comptime is_windows) {
// Windows: read file into allocated memory (no mmap support)
const data = try allocator.alignedAlloc(u8, .fromByteUnits(std.heap.page_size_min), size);
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alignment parameter syntax appears incorrect. In Zig's alignedAlloc function, the second parameter should be a compile-time known alignment value (like std.mem.page_size), not an alignment enum. The correct syntax should be std.heap.page_size_min directly as the second parameter, not wrapped in .fromByteUnits(). This will likely cause a compilation error.

Suggested change
const data = try allocator.alignedAlloc(u8, .fromByteUnits(std.heap.page_size_min), size);
const data = try allocator.alignedAlloc(u8, std.heap.page_size_min, size);

Copilot uses AI. Check for mistakes.
Comment on lines +401 to +406
// POSIX: check if allocated or mmap'd
if (self.data_is_allocated) {
self.allocator.free(aligned_ptr[0..self.data.len]);
} else {
std.posix.munmap(aligned_ptr[0..self.data.len]);
}
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup logic has a potential bug. On non-Windows POSIX systems, if data_is_allocated is true, the code will call allocator.free(), but this field is only set to true in openFromMemoryOwnedAlloc which is Windows-only (lines 162, 206-226). This means the POSIX path at line 402-403 can never be taken. Either remove this unreachable code or document why POSIX might need allocated memory in the future.

Copilot uses AI. Check for mistakes.
/// Whether we own the data (mmap'd or allocated)
owns_data: bool,
/// Whether data was allocated (Windows) vs mmap'd (POSIX)
data_is_allocated: bool = false,
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new data_is_allocated field has a default value of false, but this field is not initialized in the openFromMemory method (around line 234-246). While the default will apply, it would be clearer and more consistent with the other initialization blocks to explicitly set this field to false, especially since owns_data is already being set to false.

Copilot uses AI. Check for mistakes.
Comment on lines +154 to +162
if (comptime is_windows) {
// Windows: read file into allocated memory (no mmap support)
const data = try allocator.alignedAlloc(u8, .fromByteUnits(std.heap.page_size_min), size);
errdefer allocator.free(data);
const bytes_read = try file.readAll(data);
if (bytes_read != size) {
return error.UnexpectedEof;
}
return openFromMemoryOwnedAlloc(allocator, data, config);
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new Windows-specific file loading path (lines 154-162) lacks test coverage. The repository has comprehensive test suites (integration_test.zig, etc.), but there are no tests validating that Windows file loading and cleanup work correctly. Consider adding platform-specific tests or conditional tests that exercise this path when compiling for Windows.

Copilot uses AI. Check for mistakes.
@Lulzx
Copy link
Owner

Lulzx commented Jan 12, 2026

can you take a look at the comments from copilot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants