mtklein | 29b1afc | 2015-04-09 07:46:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 8 | #ifndef SkVarAlloc_DEFINED |
| 9 | #define SkVarAlloc_DEFINED |
| 10 | |
| 11 | #include "SkTypes.h" |
| 12 | |
| 13 | class SkVarAlloc : SkNoncopyable { |
| 14 | public: |
mtklein | 59dba14 | 2014-12-12 08:41:23 -0800 | [diff] [blame] | 15 | // Smallest block we'll allocate is 2**N bytes. |
| 16 | explicit SkVarAlloc(size_t minLgSize); |
mtklein | 29b1afc | 2015-04-09 07:46:41 -0700 | [diff] [blame] | 17 | // Same as above, but first uses up to len bytes from storage. |
| 18 | SkVarAlloc(size_t minLgSize, char* storage, size_t len); |
| 19 | |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 20 | ~SkVarAlloc(); |
| 21 | |
mtklein | 5a744b7 | 2015-09-14 11:11:17 -0700 | [diff] [blame] | 22 | // Returns contiguous bytes aligned at least for pointers. |
| 23 | char* alloc(size_t bytes) { |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 24 | bytes = SkAlignPtr(bytes); |
| 25 | |
mtklein | f2950b1 | 2014-11-13 12:41:14 -0800 | [diff] [blame] | 26 | if (bytes > fRemaining) { |
mtklein | 5a744b7 | 2015-09-14 11:11:17 -0700 | [diff] [blame] | 27 | this->makeSpace(bytes); |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 28 | } |
mtklein | f2950b1 | 2014-11-13 12:41:14 -0800 | [diff] [blame] | 29 | SkASSERT(bytes <= fRemaining); |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 30 | |
| 31 | char* ptr = fByte; |
| 32 | fByte += bytes; |
bsalomon | ccb328d | 2014-12-11 13:31:06 -0800 | [diff] [blame] | 33 | fRemaining = SkToU32(fRemaining - bytes); |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 34 | return ptr; |
| 35 | } |
| 36 | |
mtklein | 0bd57b2 | 2014-11-18 09:32:36 -0800 | [diff] [blame] | 37 | // Returns our best estimate of the number of bytes we've allocated. |
mtklein | 98b8485 | 2015-04-21 15:23:59 -0700 | [diff] [blame] | 38 | // (We may not track this precisely to save space.) |
| 39 | size_t approxBytesAllocated() const { return fBytesAllocated; } |
mtklein | 0bd57b2 | 2014-11-18 09:32:36 -0800 | [diff] [blame] | 40 | |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 41 | private: |
mtklein | 5a744b7 | 2015-09-14 11:11:17 -0700 | [diff] [blame] | 42 | void makeSpace(size_t bytes); |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 43 | |
mtklein | 98b8485 | 2015-04-21 15:23:59 -0700 | [diff] [blame] | 44 | size_t fBytesAllocated; |
| 45 | |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 46 | char* fByte; |
mtklein | f2950b1 | 2014-11-13 12:41:14 -0800 | [diff] [blame] | 47 | unsigned fRemaining; |
mtklein | 59dba14 | 2014-12-12 08:41:23 -0800 | [diff] [blame] | 48 | unsigned fLgSize; |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 49 | |
| 50 | struct Block; |
| 51 | Block* fBlock; |
| 52 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 53 | static_assert(sizeof(SkVarAlloc) <= 32, "SkVarAllocSize"); |
mtklein | 8113dd1 | 2014-11-12 15:15:28 -0800 | [diff] [blame] | 54 | |
| 55 | #endif//SkVarAlloc_DEFINED |