wutdevoptab: Only allocate a new buffer if the given buffer is not aligned properly (#159)

This commit is contained in:
Maschell 2021-01-01 17:16:40 +01:00 committed by GitHub
parent 81d689ed76
commit 79b8597a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 48 deletions

View File

@ -27,10 +27,24 @@ __wut_fs_read(struct _reent *r,
return -1;
}
if((((uintptr_t) ptr) & 0x3F) == 0){
status = FSReadFile(__wut_devoptab_fs_client, &cmd, (uint8_t *) ptr, 1,
len, file->fd, 0, -1);
if(status > 0){
bytesRead = (uint32_t) status;
file->offset += bytesRead;
}
} else {
// Copy to internal buffer due to alignment requirement and read in chunks.
alignedReadBuffer = memalign(0x40, 8192);
// Using a buffer smaller than 128KiB takes a performance hit.
int buffer_size = len < 128*1024 ? len : 128*1024;
alignedReadBuffer = memalign(0x40, buffer_size);
if(!alignedReadBuffer){
r->_errno = ENOMEM;
return -1;
}
while (len > 0) {
size_t toRead = len > 8192 ? 8192 : len;
size_t toRead = len > buffer_size ? buffer_size : len;
// Write the data
status = FSReadFile(__wut_devoptab_fs_client, &cmd, alignedReadBuffer, 1,
@ -55,6 +69,7 @@ __wut_fs_read(struct _reent *r,
}
}
free(alignedReadBuffer);
}
// Return partial read
if (bytesRead > 0) {

View File

@ -27,10 +27,24 @@ __wut_fs_write(struct _reent *r,
return -1;
}
// Copy to internal buffer due to alignment requirement and write in chunks.
alignedWriteBuffer = memalign(0x40, 8192);
if((((uintptr_t) ptr) & 0x3F) == 0){
status = FSWriteFile(__wut_devoptab_fs_client, &cmd, (uint8_t *) ptr,
1, len, file->fd, 0, -1);
if(status > 0){
bytesWritten = (uint32_t) status;
file->offset += bytesWritten;
}
} else {
// Copy to internal buffer due to alignment requirement and read in chunks.
// Using a buffer smaller than 128KiB takes a performance hit.
int buffer_size = len < 128*1024 ? len : 128*1024;
alignedWriteBuffer = memalign(0x40, buffer_size);
if(!alignedWriteBuffer){
r->_errno = ENOMEM;
return -1;
}
while (len > 0) {
size_t toWrite = len > 8192 ? 8192 : len;
size_t toWrite = len > buffer_size ? buffer_size : len;
// Copy to internal buffer
memcpy(alignedWriteBuffer, ptr, toWrite);
@ -53,6 +67,7 @@ __wut_fs_write(struct _reent *r,
}
}
free(alignedWriteBuffer);
}
// Return partial write
if (bytesWritten > 0) {