-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Readability enhancements in heap_1.c #1074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
if( ( xWantedSize + ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ) ) > xWantedSize )
{
xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
}
else
{
xWantedSize = 0;
} The correct way to think about the above code is that you are adding a value to xWantedSize which could result in overflow: xAdditionalRequiredSize = portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK );
if( ( xWantedSize + xAdditionalRequiredSize ) > xWantedSize )
{
xWantedSize += xAdditionalRequiredSize;
}
else
{
xWantedSize = 0;
} Therefore, removing the overflow check is not correct. However, we can certainly enhance readability here. Let me get a patch for that. |
Removing
We need to ensure that we are not allocating memory that is not part of |
… size" This reverts commit 7832a4b.
Signed-off-by: Gaurav Aggarwal <[email protected]>
|
I still don't understand. Can you give me an example? If xWantedSize is an unsigned integer, as I understand it, this is always true |
I understand this step, if not deducted, pucAlignedHeap may be smaller than the original address after the upward alignment process.But if it's a bit wasteful to directly deduct an alignment size, you can deduct less. |
We are trying to avoid integer overflow here. Take the following example:
Assuming that |
It is worse than that. The heap will end up allocating memory from a memory region which is not reserved for heap. Look at the following diagram-
|
Thanks. I see. |
I can see why need to deduct portBYTE_ALIGNMENT here. Now I think don't need to deduct for example: This is my modification. Could you please help to see if it is reasonable? |
Yes, you can save couple of bytes here. The question though is the added complexity worth it? The code you shared had a bug and I added a comment on your commit. |
If think about it that way, then there's really no need to add more code for just a few bytes of space. |
* Remove that Heap_1.c unnecessary judgment and code logic * Remove useless alignment calculations and increase heap usage size * Revert "Remove useless alignment calculations and increase heap usage size" This reverts commit 7832a4b. * Readability improvements Signed-off-by: Gaurav Aggarwal <[email protected]> --------- Signed-off-by: Gaurav Aggarwal <[email protected]> Co-authored-by: huangly <[email protected]> Co-authored-by: Gaurav Aggarwal <[email protected]>
Readability enhancements in heap_1.c
Use the macro
heapADD_WILL_OVERFLOW
as in other heap implementations to make the code more readable. These changes were introduced to other heaps in this PR.Test Steps
Built the Posix_GCC demo with heap_1.c.
Checklist:
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.