1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #ifndef __IO_STREAM_H__
17 #define __IO_STREAM_H__
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 
22 #include "ErrorLog.h"
23 
24 class IOStream {
25 public:
26 
IOStream(size_t bufSize)27     IOStream(size_t bufSize) {
28         m_buf = NULL;
29         m_bufsize = bufSize;
30         m_free = 0;
31     }
32 
33     virtual void *allocBuffer(size_t minSize) = 0;
34     virtual int commitBuffer(size_t size) = 0;
35     virtual const unsigned char *readFully( void *buf, size_t len) = 0;
36     virtual const unsigned char *read( void *buf, size_t *inout_len) = 0;
37     virtual int writeFully(const void* buf, size_t len) = 0;
38 
~IOStream()39     virtual ~IOStream() {
40 
41         // NOTE: m_buf is 'owned' by the child class thus we expect it to be released by it
42     }
43 
alloc(size_t len)44     unsigned char *alloc(size_t len) {
45 
46         if (m_buf && len > m_free) {
47             if (flush() < 0) {
48                 ERR("Failed to flush in alloc\n");
49                 return NULL; // we failed to flush so something is wrong
50             }
51         }
52 
53         if (!m_buf || len > m_bufsize) {
54             int allocLen = m_bufsize < len ? len : m_bufsize;
55             m_buf = (unsigned char *)allocBuffer(allocLen);
56             if (!m_buf) {
57                 ERR("Alloc (%u bytes) failed\n", allocLen);
58                 return NULL;
59             }
60             m_bufsize = m_free = allocLen;
61         }
62 
63         unsigned char *ptr;
64 
65         ptr = m_buf + (m_bufsize - m_free);
66         m_free -= len;
67 
68         return ptr;
69     }
70 
flush()71     int flush() {
72 
73         if (!m_buf || m_free == m_bufsize) return 0;
74 
75         int stat = commitBuffer(m_bufsize - m_free);
76         m_buf = NULL;
77         m_free = 0;
78         return stat;
79     }
80 
readback(void * buf,size_t len)81     const unsigned char *readback(void *buf, size_t len) {
82         flush();
83         return readFully(buf, len);
84     }
85 
86 
87 private:
88     unsigned char *m_buf;
89     size_t m_bufsize;
90     size_t m_free;
91 };
92 
93 //
94 // When a client opens a connection to the renderer, it should
95 // send unsigned int value indicating the "clientFlags".
96 // The following are the bitmask of the clientFlags.
97 // currently only one bit is used which flags the server
98 // it should exit.
99 //
100 #define IOSTREAM_CLIENT_EXIT_SERVER      1
101 
102 #endif
103