Skip to content

trim alloc#9

Merged
youknowone merged 2 commits intomainfrom
alloc
Apr 28, 2026
Merged

trim alloc#9
youknowone merged 2 commits intomainfrom
alloc

Conversation

@youknowone
Copy link
Copy Markdown
Owner

@youknowone youknowone commented Apr 28, 2026

Summary by CodeRabbit

  • New Features

    • Added configurable allocator selection with a new --allocator command-line option.
    • Added optional xz-core-custom-allocator feature flag for extended allocator support.
    • Startup banner now displays the selected allocator.
  • Chores

    • Refactored internal memory management and deallocation routines for improved type safety.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

Walkthrough

This PR refactors the LZMA allocator infrastructure to support custom allocators. It introduces a new raw_alloc.rs module for header-based memory management, removes the legacy rust_only.rs module, restructures allocator dispatch to use type-aware free operations (internal_free_bytes, internal_free_array, internal_free<T>), and adds a --allocator CLI option to perf-probe for testing allocator configurations via a new xz-core-custom-allocator feature flag.

Changes

Cohort / File(s) Summary
Core Allocator Refactoring
xz-core/src/raw_alloc.rs, xz-core/src/alloc.rs, xz-core/src/alloc/custom.rs, xz-core/src/alloc/rust.rs
Introduces new raw_alloc.rs with header-based allocation scheme (200+ lines). Restructures allocator dispatch: alloc.rs now conditions rust_allocator export on custom_allocator feature, and re-exports internal_free_bytes/internal_free_array alongside internal_free. custom.rs swaps fallback impl and refactors single free to generic/typed/array variants. rust.rs delegates to raw_alloc and removes local allocation logic.
Removed Allocator Module
xz-core/src/alloc/rust_only.rs
Removes entirely (~58 lines); functionality migrated to raw_alloc.rs.
Perf-probe CLI Integration
perf-probe/Cargo.toml, perf-probe/src/main.rs
Adds xz-core-custom-allocator feature flag. Introduces AllocatorPolicy enum, Config.allocator field, --allocator CLI option with validation, and allocator printing in banner. Passes allocator policy through all backend_encode calls.
Module Declaration
xz-core/src/lib.rs
Adds private raw_alloc module.
Deallocation API Updates
xz-core/src/common/{alone_decoder,alone_encoder,auto_decoder,block_decoder,block_encoder,file_info,index_decoder,index_encoder,lzip_decoder,microlzma_decoder,microlzma_encoder,outqueue,stream_decoder_mt,stream_encoder_mt}.rs, xz-core/src/{delta/delta_common,lz/lz_decoder,lz/lz_encoder,lzma/lzma2_decoder,lzma2_encoder,lzma_decoder,lzma_encoder},simple/simple_coder.rs
Updates free call sites to use precise APIs: removes *mut c_void casts where applicable, replaces internal_free with internal_free_bytes for byte buffers, and uses internal_free_array with element counts for arrays. Most changes are single-line updates; stream_encoder_mt.rs and lz_encoder.rs have more involved updates across multiple error paths.

Possibly Related PRs

Poem

🐰 Headers now track every byte,
Raw allocations burn so bright,
Type-aware frees hop with glee—
No more c_void cast, so free!
Alloc's grand refactoring flight! ✨

Estimated Code Review Effort

🎯 4 (Complex) | ⏱️ ~75 minutes

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 3.09% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'trim alloc' is vague and does not clearly convey the actual changes made in this PR, which involve substantial refactoring of memory allocation APIs, introducing custom allocator support, and migrating allocation code across multiple modules. Use a more descriptive title that captures the main purpose, such as 'Refactor allocator APIs to support custom allocation policies' or 'Extract and consolidate raw allocation implementation'.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch alloc

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
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@xz-core/src/raw_alloc.rs`:
- Around line 180-209: Add unit tests that exercise the typed allocation/free
helpers by calling internal_alloc_object and internal_alloc_array (with a
repr(align) test type), asserting the returned pointers are non-null and
properly aligned, then freeing them with the corresponding
internal_free_object/internal_free_array functions to ensure the direct
alloc/dealloc path is exercised; locate these helpers (internal_alloc_object,
internal_alloc_array, internal_free_object, internal_free_array) in raw_alloc.rs
and mirror the existing raw byte tests' structure (unsafe block, size/alignment
checks, then call the matching free) so mismatched free/alloc paths are caught.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f421664f-6285-41be-b360-15b5f27774b3

📥 Commits

Reviewing files that changed from the base of the PR and between 36be6fe and b8ac043.

📒 Files selected for processing (30)
  • perf-probe/Cargo.toml
  • perf-probe/src/main.rs
  • xz-core/src/alloc.rs
  • xz-core/src/alloc/custom.rs
  • xz-core/src/alloc/rust.rs
  • xz-core/src/alloc/rust_only.rs
  • xz-core/src/common/alone_decoder.rs
  • xz-core/src/common/alone_encoder.rs
  • xz-core/src/common/auto_decoder.rs
  • xz-core/src/common/block_decoder.rs
  • xz-core/src/common/block_encoder.rs
  • xz-core/src/common/file_info.rs
  • xz-core/src/common/index_decoder.rs
  • xz-core/src/common/index_encoder.rs
  • xz-core/src/common/lzip_decoder.rs
  • xz-core/src/common/microlzma_decoder.rs
  • xz-core/src/common/microlzma_encoder.rs
  • xz-core/src/common/outqueue.rs
  • xz-core/src/common/stream_decoder_mt.rs
  • xz-core/src/common/stream_encoder_mt.rs
  • xz-core/src/delta/delta_common.rs
  • xz-core/src/lib.rs
  • xz-core/src/lz/lz_decoder.rs
  • xz-core/src/lz/lz_encoder.rs
  • xz-core/src/lzma/lzma2_decoder.rs
  • xz-core/src/lzma/lzma2_encoder.rs
  • xz-core/src/lzma/lzma_decoder.rs
  • xz-core/src/lzma/lzma_encoder.rs
  • xz-core/src/raw_alloc.rs
  • xz-core/src/simple/simple_coder.rs
💤 Files with no reviewable changes (1)
  • xz-core/src/alloc/rust_only.rs

Comment thread xz-core/src/raw_alloc.rs
Comment on lines +180 to +209
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn raw_allocator_round_trip() {
unsafe {
let ptr = alloc_bytes(32);
assert!(!ptr.is_null());
free_ptr(ptr);
}
}

#[test]
fn raw_allocation_respects_alignment() {
#[repr(align(32))]
struct Align32([u8; 32]);

unsafe {
let ptr = alloc_impl(
core::mem::size_of::<Align32>(),
core::mem::align_of::<Align32>(),
false,
);
assert!(!ptr.is_null());
assert_eq!((ptr as usize) % core::mem::align_of::<Align32>(), 0);
free_impl(ptr);
}
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial

Add coverage for the typed alloc/free helpers.

These tests only exercise the header-backed byte path. The new internal_alloc_object/internal_alloc_array + internal_free* path uses direct alloc/dealloc, so a small round-trip test there would catch free-family mismatches before they turn into UB.

Test sketch
 #[test]
 fn raw_allocation_respects_alignment() {
     #[repr(align(32))]
     struct Align32([u8; 32]);

     unsafe {
         let ptr = alloc_impl(
             core::mem::size_of::<Align32>(),
             core::mem::align_of::<Align32>(),
             false,
         );
         assert!(!ptr.is_null());
         assert_eq!((ptr as usize) % core::mem::align_of::<Align32>(), 0);
         free_impl(ptr);
     }
 }
+
+#[test]
+fn typed_allocator_round_trip() {
+    unsafe {
+        let object = internal_alloc_object::<u64>(core::ptr::null());
+        assert!(!object.is_null());
+        internal_free(object, core::ptr::null());
+
+        let array = internal_alloc_array::<u32>(4, core::ptr::null());
+        assert!(!array.is_null());
+        internal_free_array(array, 4, core::ptr::null());
+    }
+}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@xz-core/src/raw_alloc.rs` around lines 180 - 209, Add unit tests that
exercise the typed allocation/free helpers by calling internal_alloc_object and
internal_alloc_array (with a repr(align) test type), asserting the returned
pointers are non-null and properly aligned, then freeing them with the
corresponding internal_free_object/internal_free_array functions to ensure the
direct alloc/dealloc path is exercised; locate these helpers
(internal_alloc_object, internal_alloc_array, internal_free_object,
internal_free_array) in raw_alloc.rs and mirror the existing raw byte tests'
structure (unsafe block, size/alignment checks, then call the matching free) so
mismatched free/alloc paths are caught.

@youknowone youknowone merged commit dab9f65 into main Apr 28, 2026
47 checks passed
@coderabbitai coderabbitai Bot mentioned this pull request May 4, 2026
Merged
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.

1 participant