diff --git a/libraries/wutdevoptab/devoptab_fs_read.c b/libraries/wutdevoptab/devoptab_fs_read.c index 52e1e58..c340268 100644 --- a/libraries/wutdevoptab/devoptab_fs_read.c +++ b/libraries/wutdevoptab/devoptab_fs_read.c @@ -27,34 +27,49 @@ __wut_fs_read(struct _reent *r, return -1; } - // Copy to internal buffer due to alignment requirement and read in chunks. - alignedReadBuffer = memalign(0x40, 8192); - while (len > 0) { - size_t toRead = len > 8192 ? 8192 : len; - - // Write the data - status = FSReadFile(__wut_devoptab_fs_client, &cmd, alignedReadBuffer, 1, - toRead, file->fd, 0, -1); - if (status <= 0) { - break; + 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; } - - // 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; + } 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; + alignedReadBuffer = memalign(0x40, buffer_size); + if(!alignedReadBuffer){ + r->_errno = ENOMEM; + return -1; } + 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 if (bytesRead > 0) { diff --git a/libraries/wutdevoptab/devoptab_fs_write.c b/libraries/wutdevoptab/devoptab_fs_write.c index 93b8ef2..1d356a2 100644 --- a/libraries/wutdevoptab/devoptab_fs_write.c +++ b/libraries/wutdevoptab/devoptab_fs_write.c @@ -27,32 +27,47 @@ __wut_fs_write(struct _reent *r, return -1; } - // Copy to internal buffer due to alignment requirement and write in chunks. - alignedWriteBuffer = memalign(0x40, 8192); - while (len > 0) { - size_t toWrite = len > 8192 ? 8192 : 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; + 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; } - - bytes = (uint32_t)status; - file->offset += bytes; - bytesWritten += bytes; - ptr += bytes; - len -= bytes; - - if (bytes < toWrite) { - break; + } 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 > 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 if (bytesWritten > 0) {