diff options
author | Pixel <Pixel> | 2001-10-29 15:44:12 +0000 |
---|---|---|
committer | Pixel <Pixel> | 2001-10-29 15:44:12 +0000 |
commit | 2e5bed84841f33ff28dd95b77b555720c875a286 (patch) | |
tree | de6cbcbf5fa65e90e1888d52a9426373a1638c61 /lib/Buffer.cc | |
parent | 0a6190d0269392e875a479df01b152d47ea4143c (diff) |
Beginning task manager
Diffstat (limited to 'lib/Buffer.cc')
-rw-r--r-- | lib/Buffer.cc | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/Buffer.cc b/lib/Buffer.cc new file mode 100644 index 0000000..164c961 --- /dev/null +++ b/lib/Buffer.cc @@ -0,0 +1,55 @@ +#include <string.h> +#include "Buffer.h" +#include "General.h" +#include "config.h" + +Buffer::Buffer() : Handle(-1), buffer(NULL), realsiz(0), bufsiz(0), ptr(0) { } + +Buffer::~Buffer() { + free(buffer); +} + +ssize_t Buffer::write(const void *buf, size_t count) { + if (count + realsiz > bufsiz) { + int numblocks = (count + realsiz) / realloc_threshold; + int remains = (count + realsiz) % realloc_threshold; + buffer = (char *) realloc(buffer, bufsiz = ((numblocks + (remains ? 1 : 0)) * realloc_threshold)); + } + memcpy(buffer + realsiz, buf, count); + realsiz += count; + + return count; +} + +ssize_t Buffer::read(void *buf, size_t count) { + count = MIN(count, realsiz - ptr); + + if (!count) { + return 0; + } + + memcpy(buf, buffer + ptr, count); + ptr += count; + + if (ptr >= realloc_threshold) { + int numblocks = (ptr / realloc_threshold) - (bufsiz / realloc_threshold); + memmove(buffer, buffer + numblocks * realloc_threshold, numblocks * realloc_threshold); + buffer = (char *) realloc(buffer, bufsiz = (numblocks * realloc_threshold)); + ptr -= numblocks * realloc_threshold; + realsiz -= numblocks * realloc_threshold; + } + + return count; +} + +bool Buffer::CanRead() { + return true; +} + +bool Buffer::CanWrite() { + return true; +} + +String Buffer::GetName() { + return "Buffer"; +} |