Home | History | Annotate | Download | only in libcutils

Lines Matching refs:buffer

17 #define LOG_TAG "buffer"
24 #include "buffer.h"
27 Buffer* bufferCreate(size_t capacity) {
28 Buffer* buffer = malloc(sizeof(Buffer));
29 if (buffer == NULL) {
32 buffer->capacity = capacity;
33 buffer->expected = 0;
34 buffer->data = malloc(capacity);
35 if (buffer->data == NULL) {
36 free(buffer);
39 return buffer;
42 void bufferFree(Buffer* buffer) {
43 free(buffer->data);
44 free(buffer);
47 Buffer* bufferWrap(char* data, size_t capacity, size_t size) {
48 Buffer* buffer = malloc(sizeof(Buffer));
49 if (buffer == NULL) {
53 buffer->data = data;
54 buffer->capacity = capacity;
55 buffer->size = size;
56 buffer->expected = 0;
57 return buffer;
60 int bufferPrepareForRead(Buffer* buffer, size_t expected) {
61 if (expected > buffer->capacity) {
62 // Expand buffer.
63 char* expanded = realloc(buffer->data, expected);
68 buffer->capacity = expected;
69 buffer->data = expanded;
72 buffer->size = 0;
73 buffer->expected = expected;
77 ssize_t bufferRead(Buffer* buffer, int fd) {
78 assert(buffer->size < buffer->expected);
81 buffer->data + buffer->size,
82 buffer->expected - buffer->size);
85 buffer->size += bytesRead;
86 return buffer->size;
92 void bufferPrepareForWrite(Buffer* buffer) {
93 buffer->remaining = buffer->size;
96 ssize_t bufferWrite(Buffer* buffer, int fd) {
97 assert(buffer->remaining > 0);
98 assert(buffer->remaining <= buffer->size);
101 buffer->data + buffer->size - buffer->remaining,
102 buffer->remaining);
105 buffer->remaining -= bytesWritten;
107 ALOGD("Buffer bytes written: %d", (int) bytesWritten);
108 ALOGD("Buffer size: %d", (int) buffer->size);
109 ALOGD("Buffer remaining: %d", (int) buffer->remaining);
111 return buffer->remaining;