LibC: Make fwrite() buffered.
This is a really naive implementation that makes fwrite() call fputc() internally, but it still performs a lot better due to avoiding the write() syscall every time.
This commit is contained in:
parent
52139a2392
commit
d3fb0a56ed
Notes:
sideshowbarker
2024-07-19 14:34:21 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/d3fb0a56ede
1 changed files with 7 additions and 7 deletions
|
@ -254,13 +254,13 @@ size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
|
|||
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream)
|
||||
{
|
||||
assert(stream);
|
||||
int rc = fflush(stream);
|
||||
if (rc == EOF)
|
||||
return 0;
|
||||
ssize_t nwritten = write(stream->fd, ptr, nmemb * size);
|
||||
if (nwritten < 0) {
|
||||
stream->error = errno;
|
||||
return 0;
|
||||
auto* bytes = (const byte*)ptr;
|
||||
ssize_t nwritten = 0;
|
||||
for (size_t i = 0; i < (size * nmemb); ++i) {
|
||||
int rc = fputc(bytes[i], stream);
|
||||
if (rc == EOF)
|
||||
break;
|
||||
++nwritten;
|
||||
}
|
||||
return nwritten / size;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue