Skip to content

Fixing realloc's edge case.#1937

Merged
nicolasnoble merged 1 commit intomainfrom
realloc-fix
May 30, 2025
Merged

Fixing realloc's edge case.#1937
nicolasnoble merged 1 commit intomainfrom
realloc-fix

Conversation

@nicolasnoble
Copy link
Copy Markdown
Member

No description provided.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 30, 2025

Walkthrough

This change corrects the logic for updating free block pointers and sizes when expanding memory allocations adjacent to the heap head in two custom realloc functions. The updates ensure that the new free block after expansion is properly linked within the free list and its size accurately reflects the remaining free memory.

Changes

Files Change Summary
src/mips/openbios/kernel/alloc.c Fixed multi_realloc to correctly set the new free block's next pointer and size when expanding blocks adjacent to the heap head.
src/mips/psyqo/src/alloc.c Corrected psyqo_realloc to properly update the new empty block's linkage and size after expanding allocation adjacent to the head block.

Possibly related PRs

  • Brand new memory allocator. #1856: Overhauls memory allocation and block management logic in alloc.c, directly related to the fixes in this PR for realloc and free block linkage.

Poem

In the warren of memory, blocks stretch and grow,
With pointers aligned in a seamless flow.
Sizes now balanced, no gaps left behind,
The heap’s little bunnies hop well-defined.
🐇🌿✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c4cd97f and 35de25b.

📒 Files selected for processing (2)
  • src/mips/openbios/kernel/alloc.c (1 hunks)
  • src/mips/psyqo/src/alloc.c (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/mips/psyqo/src/alloc.c
  • src/mips/openbios/kernel/alloc.c
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: pcsx-redux (x86_64-linux)
  • GitHub Check: pcsx-redux (aarch64-linux)
  • GitHub Check: pcsx-redux (aarch64-linux)
  • GitHub Check: pcsx-redux (x86_64-linux)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: cross-arm64
  • GitHub Check: build
  • GitHub Check: coverage
  • GitHub Check: asan
  • GitHub Check: build-openbios
  • GitHub Check: aur-build
  • GitHub Check: macos-build-and-test-toolchain
  • GitHub Check: toolchain
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@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: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 15e839b and c4cd97f.

📒 Files selected for processing (2)
  • src/mips/openbios/kernel/alloc.c (1 hunks)
  • src/mips/psyqo/src/alloc.c (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: macOS CI
src/mips/openbios/kernel/alloc.c

[error] 246-246: Compilation error: ‘delta’ undeclared (first use in this function) in function ‘multi_realloc’.

⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: pcsx-redux (aarch64-linux)
  • GitHub Check: pcsx-redux (x86_64-linux)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: pcsx-redux (x86_64-linux)
  • GitHub Check: pcsx-redux (aarch64-linux)
  • GitHub Check: cross-arm64
  • GitHub Check: toolchain
  • GitHub Check: macos-build-and-test-toolchain
  • GitHub Check: asan
  • GitHub Check: coverage
  • GitHub Check: aur-build
  • GitHub Check: build

Comment thread src/mips/openbios/kernel/alloc.c Outdated
Comment on lines +245 to +246
new_block->next = head->next;
new_block->size = head->size - delta;
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.

⚠️ Potential issue

Fix compilation error: 'delta' undeclared.

There's a compilation error due to the undeclared variable delta on line 246.

The size calculation should be the amount of memory freed (old_size - size), not head->size - delta. Apply this diff:

                new_block->next = head->next;
-                new_block->size = head->size - delta;
+                new_block->size = old_size - size;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
new_block->next = head->next;
new_block->size = head->size - delta;
new_block->next = head->next;
new_block->size = old_size - size;
🧰 Tools
🪛 GitHub Actions: macOS CI

[error] 246-246: Compilation error: ‘delta’ undeclared (first use in this function) in function ‘multi_realloc’.

🤖 Prompt for AI Agents
In src/mips/openbios/kernel/alloc.c at lines 245-246, the variable 'delta' is
undeclared causing a compilation error. Replace 'head->size - delta' with the
correct size calculation 'old_size - size' to fix the error and correctly
compute the freed memory size.

Comment thread src/mips/psyqo/src/alloc.c Outdated
Comment on lines +528 to +529
new_block->next = head->next;
new_block->size = head->size - delta;
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.

⚠️ Potential issue

Fix incorrect variable usage and scope issue.

Line 529 uses delta which is declared in a different conditional branch (line 542) and represents a different value (size - old_size for growing vs old_size - size needed for shrinking).

The size calculation should be the amount of memory freed when shrinking. Apply this diff:

                new_block->next = head->next;
-                new_block->size = head->size - delta;
+                new_block->size = old_size - size;

The fix to new_block->next = head->next correctly links the new block to skip the current head.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
new_block->next = head->next;
new_block->size = head->size - delta;
new_block->next = head->next;
new_block->size = old_size - size;
🤖 Prompt for AI Agents
In src/mips/psyqo/src/alloc.c around lines 528 to 529, the variable delta used
for calculating new_block->size is incorrectly referenced from a different
conditional branch where it has a different meaning. To fix this, replace delta
with the correct calculation for shrinking, which is old_size - size, ensuring
the new block size correctly represents the freed memory. Also, keep the
assignment new_block->next = head->next to properly link the new block in the
list.

Revert "Fixing realloc's edge case."

This reverts commit c4cd97f.

.
@nicolasnoble nicolasnoble merged commit 285c320 into main May 30, 2025
25 checks passed
@nicolasnoble nicolasnoble deleted the realloc-fix branch May 30, 2025 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant