reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SkWriter32.h" |
| 2 | |
| 3 | struct SkWriter32::Block { |
| 4 | Block* fNext; |
| 5 | size_t fSize; |
| 6 | size_t fAllocated; |
| 7 | |
| 8 | size_t available() const { return fSize - fAllocated; } |
| 9 | char* base() { return (char*)(this + 1); } |
| 10 | const char* base() const { return (const char*)(this + 1); } |
| 11 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 12 | uint32_t* alloc(size_t size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | SkASSERT(SkAlign4(size) == size); |
| 14 | SkASSERT(this->available() >= size); |
| 15 | void* ptr = this->base() + fAllocated; |
| 16 | fAllocated += size; |
| 17 | SkASSERT(fAllocated <= fSize); |
| 18 | return (uint32_t*)ptr; |
| 19 | } |
| 20 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 21 | uint32_t* peek32(size_t offset) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | SkASSERT(offset <= fAllocated + 4); |
| 23 | void* ptr = this->base() + offset; |
| 24 | return (uint32_t*)ptr; |
| 25 | } |
| 26 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 27 | static Block* Create(size_t size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | SkASSERT(SkAlign4(size) == size); |
| 29 | Block* block = (Block*)sk_malloc_throw(sizeof(Block) + size); |
| 30 | block->fNext = NULL; |
| 31 | block->fSize = size; |
| 32 | block->fAllocated = 0; |
| 33 | return block; |
| 34 | } |
| 35 | }; |
| 36 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 39 | SkWriter32::~SkWriter32() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | this->reset(); |
| 41 | } |
| 42 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 43 | void SkWriter32::reset() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | Block* block = fHead; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 45 | while (block) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | Block* next = block->fNext; |
| 47 | sk_free(block); |
| 48 | block = next; |
| 49 | } |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 50 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | fSize = 0; |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 52 | fHead = fTail = NULL; |
| 53 | fSingleBlock = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 56 | void SkWriter32::reset(void* block, size_t size) { |
| 57 | this->reset(); |
reed@google.com | c925451 | 2011-05-04 14:43:40 +0000 | [diff] [blame] | 58 | SkASSERT(0 == ((fSingleBlock - (char*)0) & 3)); // need 4-byte alignment |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 59 | fSingleBlock = (char*)block; |
| 60 | fSingleBlockSize = (size & ~3); |
| 61 | } |
| 62 | |
| 63 | uint32_t* SkWriter32::reserve(size_t size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | SkASSERT(SkAlign4(size) == size); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 65 | |
| 66 | if (fSingleBlock) { |
| 67 | uint32_t* ptr = (uint32_t*)(fSingleBlock + fSize); |
| 68 | fSize += size; |
| 69 | SkASSERT(fSize <= fSingleBlockSize); |
| 70 | return ptr; |
| 71 | } |
| 72 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | Block* block = fTail; |
| 74 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 75 | if (NULL == block) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | SkASSERT(NULL == fHead); |
| 77 | fHead = fTail = block = Block::Create(SkMax32(size, fMinSize)); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 78 | } else if (block->available() < size) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | fTail = Block::Create(SkMax32(size, fMinSize)); |
| 80 | block->fNext = fTail; |
| 81 | block = fTail; |
| 82 | } |
| 83 | |
| 84 | fSize += size; |
| 85 | |
| 86 | return block->alloc(size); |
| 87 | } |
| 88 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 89 | uint32_t* SkWriter32::peek32(size_t offset) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | SkASSERT(SkAlign4(offset) == offset); |
| 91 | SkASSERT(offset <= fSize); |
| 92 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 93 | if (fSingleBlock) { |
| 94 | return (uint32_t*)(fSingleBlock + offset); |
| 95 | } |
| 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | Block* block = fHead; |
| 98 | SkASSERT(NULL != block); |
| 99 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 100 | while (offset >= block->fAllocated) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | offset -= block->fAllocated; |
| 102 | block = block->fNext; |
| 103 | SkASSERT(NULL != block); |
| 104 | } |
| 105 | return block->peek32(offset); |
| 106 | } |
| 107 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 108 | void SkWriter32::flatten(void* dst) const { |
| 109 | if (fSingleBlock) { |
| 110 | memcpy(dst, fSingleBlock, fSize); |
| 111 | return; |
| 112 | } |
| 113 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | const Block* block = fHead; |
| 115 | SkDEBUGCODE(size_t total = 0;) |
| 116 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 117 | while (block) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | size_t allocated = block->fAllocated; |
| 119 | memcpy(dst, block->base(), allocated); |
| 120 | dst = (char*)dst + allocated; |
| 121 | block = block->fNext; |
| 122 | |
| 123 | SkDEBUGCODE(total += allocated;) |
| 124 | SkASSERT(total <= fSize); |
| 125 | } |
| 126 | SkASSERT(total == fSize); |
| 127 | } |
| 128 | |
| 129 | void SkWriter32::writePad(const void* src, size_t size) { |
| 130 | size_t alignedSize = SkAlign4(size); |
| 131 | char* dst = (char*)this->reserve(alignedSize); |
| 132 | memcpy(dst, src, size); |
| 133 | dst += size; |
| 134 | int n = alignedSize - size; |
| 135 | while (--n >= 0) { |
| 136 | *dst++ = 0; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #include "SkStream.h" |
| 141 | |
| 142 | size_t SkWriter32::readFromStream(SkStream* stream, size_t length) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 143 | if (fSingleBlock) { |
| 144 | SkASSERT(fSingleBlockSize >= fSize); |
| 145 | size_t remaining = fSingleBlockSize - fSize; |
| 146 | if (length > remaining) { |
| 147 | length = remaining; |
| 148 | } |
| 149 | stream->read(fSingleBlock + fSize, length); |
| 150 | fSize += length; |
| 151 | return length; |
| 152 | } |
| 153 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | char scratch[1024]; |
| 155 | const size_t MAX = sizeof(scratch); |
| 156 | size_t remaining = length; |
| 157 | |
| 158 | while (remaining != 0) { |
| 159 | size_t n = remaining; |
| 160 | if (n > MAX) { |
| 161 | n = MAX; |
| 162 | } |
| 163 | size_t bytes = stream->read(scratch, n); |
| 164 | this->writePad(scratch, bytes); |
| 165 | remaining -= bytes; |
| 166 | if (bytes != n) { |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | return length - remaining; |
| 171 | } |
| 172 | |
| 173 | bool SkWriter32::writeToStream(SkWStream* stream) { |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 174 | if (fSingleBlock) { |
| 175 | return stream->write(fSingleBlock, fSize); |
| 176 | } |
| 177 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | const Block* block = fHead; |
| 179 | while (block) { |
| 180 | if (!stream->write(block->base(), block->fAllocated)) { |
| 181 | return false; |
| 182 | } |
| 183 | block = block->fNext; |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 188 | /////////////////////////////////////////////////////////////////////////////// |
| 189 | |
| 190 | #include "SkReader32.h" |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame^] | 191 | #include "SkString.h" |
| 192 | |
| 193 | /* |
| 194 | * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4 |
| 195 | */ |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 196 | |
| 197 | const char* SkReader32::readString(size_t* outLen) { |
| 198 | // we need to read at least 1-4 bytes |
| 199 | SkASSERT(this->isAvailable(4)); |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame^] | 200 | size_t len = this->readInt(); |
| 201 | const void* ptr = this->peek(); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 202 | |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame^] | 203 | // skip over teh string + '\0' and then pad to a multiple of 4 |
| 204 | size_t alignedSize = SkAlign4(len + 1); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 205 | this->skip(alignedSize); |
| 206 | |
| 207 | if (outLen) { |
| 208 | *outLen = len; |
| 209 | } |
| 210 | return (const char*)ptr; |
| 211 | } |
| 212 | |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame^] | 213 | size_t SkReader32::readIntoString(SkString* copy) { |
| 214 | size_t len; |
| 215 | const char* ptr = this->readString(&len); |
| 216 | if (copy) { |
| 217 | copy->set(ptr, len); |
| 218 | } |
| 219 | return len; |
| 220 | } |
| 221 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 222 | void SkWriter32::writeString(const char str[], size_t len) { |
| 223 | if ((long)len < 0) { |
| 224 | SkASSERT(str); |
| 225 | len = strlen(str); |
| 226 | } |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame^] | 227 | this->write32(len); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 228 | // add 1 since we also write a terminating 0 |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame^] | 229 | size_t alignedLen = SkAlign4(len + 1); |
| 230 | char* ptr = (char*)this->reserve(alignedLen); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 231 | memcpy(ptr, str, len); |
| 232 | ptr[len] = 0; |
| 233 | // we may have left 0,1,2,3 bytes uninitialized, since we reserved align4 |
| 234 | // number of bytes. That's ok, since the reader will know to skip those |
| 235 | } |
| 236 | |
| 237 | size_t SkWriter32::WriteStringSize(const char* str, size_t len) { |
| 238 | if ((long)len < 0) { |
| 239 | SkASSERT(str); |
| 240 | len = strlen(str); |
| 241 | } |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame^] | 242 | const size_t lenBytes = 4; // we use 4 bytes to record the length |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 243 | // add 1 since we also write a terminating 0 |
| 244 | return SkAlign4(lenBytes + len + 1); |
| 245 | } |
| 246 | |
| 247 | |