summaryrefslogtreecommitdiffstats
path: root/libcutils/buffer.h
blob: d8bc108ee7c6ffde0a1d19608eea4ec324f5550d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Byte buffer utilities.
 */

#ifndef __BUFFER_H
#define __BUFFER_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>

/** 
 * Byte buffer of known size. Keeps track of how much data has been read
 * into or written out of the buffer.
 */
typedef struct {
    /** Buffered data. */
    char* data;

    union {
        /** For reading. # of bytes we expect. */
        size_t expected;

        /** For writing. # of bytes to write. */
        size_t remaining;
    };

    /** Actual # of bytes in the buffer. */
    size_t size;

    /** Amount of memory allocated for this buffer. */
    size_t capacity;
} Buffer;

/**
 * Returns true if all data has been read into the buffer.
 */
#define bufferReadComplete(buffer) (buffer->expected == buffer->size)

/**
 * Returns true if the buffer has been completely written.
 */
#define bufferWriteComplete(buffer) (buffer->remaining == 0)

/**
 * Creates a new buffer with the given initial capacity.
 */
Buffer* bufferCreate(size_t initialCapacity);

/**
 * Wraps an existing byte array.
 */
Buffer* bufferWrap(char* data, size_t capacity, size_t size);

/**
 * Frees and its data.
 */
void bufferFree(Buffer* buffer);

/**
 * Prepares buffer to read 'expected' number of bytes. Expands capacity if
 * necessary. Returns 0 if successful or -1 if an error occurs allocating
 * memory.
 */
int bufferPrepareForRead(Buffer* buffer, size_t expected);

/**
 * Reads some data into a buffer. Returns -1 in case of an error and sets 
 * errno (see read()). Returns 0 for EOF. Updates buffer->size and returns
 * the new size after a succesful read. 
 *
 * Precondition: buffer->size < buffer->expected
 */
ssize_t bufferRead(Buffer* buffer, int fd);

/**
 * Prepares a buffer to be written out.
 */
void bufferPrepareForWrite(Buffer* buffer);

/**
 * Writes data from buffer to the given fd. Returns -1 and sets errno in case
 * of an error. Updates buffer->remaining and returns the number of remaining
 * bytes to be written after a successful write. 
 *
 * Precondition: buffer->remaining > 0
 */
ssize_t bufferWrite(Buffer* buffer, int fd);

#ifdef __cplusplus
}
#endif

#endif /* __BUFFER_H */