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,34 +27,49 @@ __wut_fs_read(struct _reent *r,
return -1; return -1;
} }
// Copy to internal buffer due to alignment requirement and read in chunks. if((((uintptr_t) ptr) & 0x3F) == 0){
alignedReadBuffer = memalign(0x40, 8192); status = FSReadFile(__wut_devoptab_fs_client, &cmd, (uint8_t *) ptr, 1,
while (len > 0) { len, file->fd, 0, -1);
size_t toRead = len > 8192 ? 8192 : len; if(status > 0){
bytesRead = (uint32_t) status;
// Write the data file->offset += bytesRead;
status = FSReadFile(__wut_devoptab_fs_client, &cmd, alignedReadBuffer, 1,
toRead, file->fd, 0, -1);
if (status <= 0) {
break;
} }
} else {
// Copy to internal buffer // Copy to internal buffer due to alignment requirement and read in chunks.
bytes = (uint32_t)status; // Using a buffer smaller than 128KiB takes a performance hit.
memcpy(ptr, alignedReadBuffer, bytes); int buffer_size = len < 128*1024 ? len : 128*1024;
alignedReadBuffer = memalign(0x40, buffer_size);
file->offset += bytes; if(!alignedReadBuffer){
bytesRead += bytes; r->_errno = ENOMEM;
ptr += bytes; return -1;
len -= bytes;
if (bytes < toRead) {
// If we did not read the full requested toRead bytes then we reached
// the end of the file.
break;
} }
while (len > 0) {
size_t toRead = len > buffer_size ? buffer_size : len;
// Write the data
status = FSReadFile(__wut_devoptab_fs_client, &cmd, alignedReadBuffer, 1,
toRead, file->fd, 0, -1);
if (status <= 0) {
break;
}
// Copy to internal buffer
bytes = (uint32_t)status;
memcpy(ptr, alignedReadBuffer, bytes);
file->offset += bytes;
bytesRead += bytes;
ptr += bytes;
len -= bytes;
if (bytes < toRead) {
// If we did not read the full requested toRead bytes then we reached
// the end of the file.
break;
}
}
free(alignedReadBuffer);
} }
free(alignedReadBuffer);
// Return partial read // Return partial read
if (bytesRead > 0) { if (bytesRead > 0) {

View File

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