Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
Steven Moreland | c4dd210 | 2017-02-23 13:57:21 -0800 | [diff] [blame] | 17 | #define LOG_TAG "hw-BufferedTextOutput" |
| 18 | |
Martijn Coenen | 4080edc | 2016-05-04 14:17:02 +0200 | [diff] [blame] | 19 | #include <hwbinder/BufferedTextOutput.h> |
| 20 | #include <hwbinder/Debug.h> |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 21 | |
Steven Moreland | 5e67788 | 2018-02-23 14:59:21 -0800 | [diff] [blame] | 22 | #include <cutils/atomic.h> |
| 23 | #include <cutils/threads.h> |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | #include <utils/RefBase.h> |
| 26 | #include <utils/Vector.h> |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 27 | |
Martijn Coenen | e01f4f2 | 2016-05-12 12:33:28 +0200 | [diff] [blame] | 28 | #include <hwbinder/Static.h> |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | f729b64 | 2018-07-13 11:10:22 -0700 | [diff] [blame] | 30 | #include <pthread.h> |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | namespace android { |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 37 | namespace hardware { |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 38 | |
| 39 | struct BufferedTextOutput::BufferState : public RefBase |
| 40 | { |
Chih-Hung Hsieh | 1f555e9 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 41 | explicit BufferState(int32_t _seq) |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 42 | : seq(_seq) |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 43 | , buffer(nullptr) |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 44 | , bufferPos(0) |
| 45 | , bufferSize(0) |
| 46 | , atFront(true) |
| 47 | , indent(0) |
| 48 | , bundle(0) { |
| 49 | } |
| 50 | ~BufferState() { |
| 51 | free(buffer); |
| 52 | } |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 53 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 54 | status_t append(const char* txt, size_t len) { |
Martijn Coenen | b022196 | 2020-01-22 10:41:03 +0100 | [diff] [blame] | 55 | if (len > SIZE_MAX - bufferPos) return NO_MEMORY; // overflow |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 56 | if ((len+bufferPos) > bufferSize) { |
Martijn Coenen | b022196 | 2020-01-22 10:41:03 +0100 | [diff] [blame] | 57 | if ((len + bufferPos) > SIZE_MAX / 3) return NO_MEMORY; // overflow |
Christopher Tate | abaa762 | 2015-06-08 14:45:14 -0700 | [diff] [blame] | 58 | size_t newSize = ((len+bufferPos)*3)/2; |
Christopher Tate | abaa762 | 2015-06-08 14:45:14 -0700 | [diff] [blame] | 59 | void* b = realloc(buffer, newSize); |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 60 | if (!b) return NO_MEMORY; |
| 61 | buffer = (char*)b; |
Christopher Tate | abaa762 | 2015-06-08 14:45:14 -0700 | [diff] [blame] | 62 | bufferSize = newSize; |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 63 | } |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 64 | memcpy(buffer+bufferPos, txt, len); |
| 65 | bufferPos += len; |
| 66 | return NO_ERROR; |
| 67 | } |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 68 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 69 | void restart() { |
| 70 | bufferPos = 0; |
| 71 | atFront = true; |
| 72 | if (bufferSize > 256) { |
| 73 | void* b = realloc(buffer, 256); |
| 74 | if (b) { |
| 75 | buffer = (char*)b; |
| 76 | bufferSize = 256; |
| 77 | } |
| 78 | } |
| 79 | } |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 80 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 81 | const int32_t seq; |
| 82 | char* buffer; |
| 83 | size_t bufferPos; |
| 84 | size_t bufferSize; |
| 85 | bool atFront; |
| 86 | int32_t indent; |
| 87 | int32_t bundle; |
| 88 | }; |
| 89 | |
| 90 | struct BufferedTextOutput::ThreadState |
| 91 | { |
| 92 | Vector<sp<BufferedTextOutput::BufferState> > states; |
| 93 | }; |
| 94 | |
Elliott Hughes | f729b64 | 2018-07-13 11:10:22 -0700 | [diff] [blame] | 95 | static pthread_mutex_t gMutex = PTHREAD_MUTEX_INITIALIZER; |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 96 | |
| 97 | static thread_store_t tls; |
| 98 | |
| 99 | BufferedTextOutput::ThreadState* BufferedTextOutput::getThreadState() |
| 100 | { |
| 101 | ThreadState* ts = (ThreadState*) thread_store_get( &tls ); |
| 102 | if (ts) return ts; |
| 103 | ts = new ThreadState; |
| 104 | thread_store_set( &tls, ts, threadDestructor ); |
| 105 | return ts; |
| 106 | } |
| 107 | |
| 108 | void BufferedTextOutput::threadDestructor(void *st) |
| 109 | { |
| 110 | delete ((ThreadState*)st); |
| 111 | } |
| 112 | |
| 113 | static volatile int32_t gSequence = 0; |
| 114 | |
| 115 | static volatile int32_t gFreeBufferIndex = -1; |
| 116 | |
| 117 | static int32_t allocBufferIndex() |
| 118 | { |
| 119 | int32_t res = -1; |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 120 | |
Elliott Hughes | f729b64 | 2018-07-13 11:10:22 -0700 | [diff] [blame] | 121 | pthread_mutex_lock(&gMutex); |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 122 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 123 | if (gFreeBufferIndex >= 0) { |
| 124 | res = gFreeBufferIndex; |
| 125 | gFreeBufferIndex = gTextBuffers[res]; |
| 126 | gTextBuffers.editItemAt(res) = -1; |
| 127 | |
| 128 | } else { |
| 129 | res = gTextBuffers.size(); |
| 130 | gTextBuffers.add(-1); |
| 131 | } |
| 132 | |
Elliott Hughes | f729b64 | 2018-07-13 11:10:22 -0700 | [diff] [blame] | 133 | pthread_mutex_unlock(&gMutex); |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 134 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 135 | return res; |
| 136 | } |
| 137 | |
| 138 | static void freeBufferIndex(int32_t idx) |
| 139 | { |
Elliott Hughes | f729b64 | 2018-07-13 11:10:22 -0700 | [diff] [blame] | 140 | pthread_mutex_lock(&gMutex); |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 141 | gTextBuffers.editItemAt(idx) = gFreeBufferIndex; |
| 142 | gFreeBufferIndex = idx; |
Elliott Hughes | f729b64 | 2018-07-13 11:10:22 -0700 | [diff] [blame] | 143 | pthread_mutex_unlock(&gMutex); |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // --------------------------------------------------------------------------- |
| 147 | |
| 148 | BufferedTextOutput::BufferedTextOutput(uint32_t flags) |
| 149 | : mFlags(flags) |
| 150 | , mSeq(android_atomic_inc(&gSequence)) |
| 151 | , mIndex(allocBufferIndex()) |
| 152 | { |
| 153 | mGlobalState = new BufferState(mSeq); |
| 154 | if (mGlobalState) mGlobalState->incStrong(this); |
| 155 | } |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 156 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 157 | BufferedTextOutput::~BufferedTextOutput() |
| 158 | { |
| 159 | if (mGlobalState) mGlobalState->decStrong(this); |
| 160 | freeBufferIndex(mIndex); |
| 161 | } |
| 162 | |
| 163 | status_t BufferedTextOutput::print(const char* txt, size_t len) |
| 164 | { |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 165 | AutoMutex _l(mLock); |
| 166 | BufferState* b = getBuffer(); |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 167 | const char* const end = txt+len; |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 168 | status_t err; |
| 169 | |
| 170 | while (txt < end) { |
| 171 | // Find the next line. |
| 172 | const char* first = txt; |
| 173 | while (txt < end && *txt != '\n') txt++; |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 174 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 175 | // Include this and all following empty lines. |
| 176 | while (txt < end && *txt == '\n') txt++; |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 177 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 178 | // Special cases for first data on a line. |
| 179 | if (b->atFront) { |
| 180 | if (b->indent > 0) { |
| 181 | // If this is the start of a line, add the indent. |
| 182 | const char* prefix = stringForIndent(b->indent); |
| 183 | err = b->append(prefix, strlen(prefix)); |
| 184 | if (err != NO_ERROR) return err; |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 185 | } else if (*(txt-1) == '\n' && !b->bundle) { |
| 186 | // Fast path: if we are not indenting or bundling, and |
| 187 | // have been given one or more complete lines, just write |
| 188 | // them out without going through the buffer. |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 189 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 190 | // Slurp up all of the lines. |
| 191 | const char* lastLine = txt+1; |
| 192 | while (txt < end) { |
| 193 | if (*txt++ == '\n') lastLine = txt; |
| 194 | } |
| 195 | struct iovec vec; |
| 196 | vec.iov_base = (void*)first; |
| 197 | vec.iov_len = lastLine-first; |
| 198 | //printf("Writing %d bytes of data!\n", vec.iov_len); |
| 199 | writeLines(vec, 1); |
| 200 | txt = lastLine; |
| 201 | continue; |
| 202 | } |
| 203 | } |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 204 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 205 | // Append the new text to the buffer. |
| 206 | err = b->append(first, txt-first); |
| 207 | if (err != NO_ERROR) return err; |
| 208 | b->atFront = *(txt-1) == '\n'; |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 209 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 210 | // If we have finished a line and are not bundling, write |
| 211 | // it out. |
| 212 | //printf("Buffer is now %d bytes\n", b->bufferPos); |
| 213 | if (b->atFront && !b->bundle) { |
| 214 | struct iovec vec; |
| 215 | vec.iov_base = b->buffer; |
| 216 | vec.iov_len = b->bufferPos; |
| 217 | //printf("Writing %d bytes of data!\n", vec.iov_len); |
| 218 | writeLines(vec, 1); |
| 219 | b->restart(); |
| 220 | } |
| 221 | } |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 222 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 223 | return NO_ERROR; |
| 224 | } |
| 225 | |
| 226 | void BufferedTextOutput::moveIndent(int delta) |
| 227 | { |
| 228 | AutoMutex _l(mLock); |
| 229 | BufferState* b = getBuffer(); |
| 230 | b->indent += delta; |
| 231 | if (b->indent < 0) b->indent = 0; |
| 232 | } |
| 233 | |
| 234 | void BufferedTextOutput::pushBundle() |
| 235 | { |
| 236 | AutoMutex _l(mLock); |
| 237 | BufferState* b = getBuffer(); |
| 238 | b->bundle++; |
| 239 | } |
| 240 | |
| 241 | void BufferedTextOutput::popBundle() |
| 242 | { |
| 243 | AutoMutex _l(mLock); |
| 244 | BufferState* b = getBuffer(); |
| 245 | b->bundle--; |
| 246 | LOG_FATAL_IF(b->bundle < 0, |
| 247 | "TextOutput::popBundle() called more times than pushBundle()"); |
| 248 | if (b->bundle < 0) b->bundle = 0; |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 249 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 250 | if (b->bundle == 0) { |
| 251 | // Last bundle, write out data if it is complete. If it is not |
| 252 | // complete, don't write until the last line is done... this may |
| 253 | // or may not be the write thing to do, but it's the easiest. |
| 254 | if (b->bufferPos > 0 && b->atFront) { |
| 255 | struct iovec vec; |
| 256 | vec.iov_base = b->buffer; |
| 257 | vec.iov_len = b->bufferPos; |
| 258 | writeLines(vec, 1); |
| 259 | b->restart(); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | BufferedTextOutput::BufferState* BufferedTextOutput::getBuffer() const |
| 265 | { |
| 266 | if ((mFlags&MULTITHREADED) != 0) { |
| 267 | ThreadState* ts = getThreadState(); |
| 268 | if (ts) { |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 269 | while (ts->states.size() <= (size_t)mIndex) ts->states.add(nullptr); |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 270 | BufferState* bs = ts->states[mIndex].get(); |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 271 | if (bs != nullptr && bs->seq == mSeq) return bs; |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 272 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 273 | ts->states.editItemAt(mIndex) = new BufferState(mIndex); |
| 274 | bs = ts->states[mIndex].get(); |
Yi Kong | 55d4107 | 2018-07-23 14:55:39 -0700 | [diff] [blame] | 275 | if (bs != nullptr) return bs; |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
Keun Soo Yim | e585572 | 2017-01-07 17:18:17 -0800 | [diff] [blame] | 278 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 279 | return mGlobalState; |
| 280 | } |
| 281 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 282 | }; // namespace hardware |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 283 | }; // namespace android |