Skip to content

Add allocator functions to c-api#8168

Open
bschoenmaeckers wants to merge 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-mem
Open

Add allocator functions to c-api#8168
bschoenmaeckers wants to merge 1 commit into
RustPython:mainfrom
bschoenmaeckers:c-api-mem

Conversation

@bschoenmaeckers

@bschoenmaeckers bschoenmaeckers commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Added Python-compatible C API memory allocation/free entry points, including both standard and “raw” variants.
    • Standard variants normalize zero-size allocation requests, while raw variants pass sizes through directly.
  • Chores

    • Updated the spelling dictionary to recognize the new term introduced by these changes.

@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 8dbf6876-848a-4092-8bc7-492a90c527dd

📥 Commits

Reviewing files that changed from the base of the PR and between ea59686 and 5e7a490.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (4)
  • .cspell.dict/cpython.txt
  • crates/capi/Cargo.toml
  • crates/capi/src/lib.rs
  • crates/capi/src/pymem.rs
✅ Files skipped from review due to trivial changes (3)
  • .cspell.dict/cpython.txt
  • crates/capi/Cargo.toml
  • crates/capi/src/lib.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/capi/src/pymem.rs

📝 Walkthrough

Walkthrough

The crate adds a new public pymem module and exports PyMem_* and PyMem_Raw* C-ABI allocation functions backed by libc.

Changes

C API pymem exports

Layer / File(s) Summary
Crate wiring
crates/capi/Cargo.toml, crates/capi/src/lib.rs, .cspell.dict/cpython.txt
libc is added as a workspace dependency, pymem is publicly exported from the crate module list, and pymem is added to the CPython cspell word list.
PyMem FFI shims
crates/capi/src/pymem.rs
Eight exported allocation and free functions are added, with PyMem_* normalizing zero sizes before calling libc and PyMem_Raw* forwarding arguments directly.

Sequence Diagram(s)

sequenceDiagram
  participant CCaller
  participant PymemRs
  participant Libc
  CCaller->>PymemRs: call PyMem_* or PyMem_Raw*
  PymemRs->>Libc: malloc/calloc/realloc/free
  Libc-->>PymemRs: allocation result or completion
  PymemRs-->>CCaller: return pointer or void
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • youknowone

Poem

A bunny hopped through pymem night,
With libc hops, all neat and right.
PyMem_* sprang out with care,
Raw memory juggling in the air.
🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding allocator functions to the c-api.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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 unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/capi/src/pymem.rs`:
- Around line 4-15: Normalize zero-size inputs in the CPython memory wrappers so
they satisfy the C-API contract: update PyMem_Malloc, PyMem_Calloc, and
PyMem_Realloc to treat zero-byte requests as 1-byte allocations and ensure
realloc with a zero new_size does not free the block. Apply the same zero-size
normalization consistently to PyMem_RawMalloc, PyMem_RawCalloc, and
PyMem_RawRealloc so all memory entry points in pymem.rs behave the same way
instead of forwarding raw zero values directly to libc.
🪄 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: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: a41f31dc-588b-4ce4-9d78-2a6eb0c7ed5b

📥 Commits

Reviewing files that changed from the base of the PR and between 870ad2d and bc43086.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • crates/capi/Cargo.toml
  • crates/capi/src/lib.rs
  • crates/capi/src/pymem.rs

Comment thread crates/capi/src/pymem.rs Outdated

@ShaharNaveh ShaharNaveh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

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