Skip to content
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

LinuxFile reused scratch buffers ensuring size was usable. But the … #331

Merged
merged 5 commits into from
Mar 3, 2024
Merged

LinuxFile reused scratch buffers ensuring size was usable. But the … #331

merged 5 commits into from
Mar 3, 2024

Conversation

taartspi
Copy link
Collaborator

@taartspi taartspi commented Mar 1, 2024

…limit value cannot be modified so later usage failed as an intended overwrite

Modified both methods that return a buffer. The methods ensured the an existing buffer was large enough for the new request and returned it. But its new usage may containa greater limit value. I could not alter the limit value of an existing buffer, instead if an old buffer exists I remove it an allocate a new buffer.

This is an answer to a post. If you agree with this, can you spin a 2.5.1 snap shot so this user can progress.
I did not research how many attributes we needed to compare if we wanted to reuse an existing buffer
@eitch @FDelporte

…imit value cannot be modified so later usage failed as an intended overwrite
Copy link
Member

@eitch eitch left a comment

Choose a reason for hiding this comment

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

@taartspi

You test if the buffer is not null, and then remove it, creating a new buffer. Further you set the byte ordering, but since it's a new byte buffer, i think that is not necessary.

I think we could reduce the code to this:

final int byteSize = size * 4;
if (byteSize > localBufferSize)
    throw new ScratchBufferOverrun();
IntBuffer buf = ByteBuffer.allocateDirect(localBufferSize).asIntBuffer();
localOffsetsBuffer.set(buf);
return buf;

Do you agree?

@eitch
Copy link
Member

eitch commented Mar 1, 2024

Further if i look at the old code, then we reuse the buffer... Are we sure this is ok, to just always write a new buffer? Are we not using the buffers in multiple calls in some way?

@taartspi
Copy link
Collaborator Author

taartspi commented Mar 1, 2024

My last sentence in the pr about further validation for reuse. We could test if the limit is the same or now less and i think reuse is ok. I would need to decide if there are other attributes that require similar validation. And your code suggestion. I must retest but i think this will not handle the new limit being greater. Will be late my daytime before i can do this work

taartspi added 4 commits March 1, 2024 20:04
…t the limit value cannot be modified so later usage failed as an intended overwrite
…t the buffer limit value cannot be modified so later usage failed as an intended overwrite when the new limitvalue was greater than the old limt value
…t the buffer limit value cannot be modified so later usage failed as an intended overwrite when the new limitvalue was greater than the old limt value
@@ -214,7 +214,8 @@ protected synchronized IntBuffer getOffsetsBuffer(int size) {

if (byteSize > localBufferSize)
throw new ScratchBufferOverrun();

// if no buffer currently exists, new one always created.
// if buffer exists and limit is not equal to this getOffsetsBuffer, allocate new
if (buf == null) {
ByteBuffer bb = ByteBuffer.allocateDirect(localBufferSize);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Robert. U asked about reducing this code. The nativeOrder does alter the buffer and should remain. I did not challenge the comment that nativeOrder be set prior to making the buffer an IntBuff

Suggested change

@taartspi
Copy link
Collaborator Author

taartspi commented Mar 3, 2024

-Git repeated saying that the push failed for my local being behind. Seems it did accomplish something each attempt until I figured out how to fix this without using some dangerous force. That left these extra commits, sorry about that.

Copy link
Member

@eitch eitch left a comment

Choose a reason for hiding this comment

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

Ok, since you tested, then i am sure it is ok

@eitch eitch merged commit 48f8fb0 into Pi4J:develop Mar 3, 2024
1 check passed
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