blob: 9eac6588280213d2d7063da547aecf2fa0b370e6 [file] [log] [blame]
mtklein8113dd12014-11-12 15:15:28 -08001#ifndef SkVarAlloc_DEFINED
2#define SkVarAlloc_DEFINED
3
4#include "SkTypes.h"
5
6class SkVarAlloc : SkNoncopyable {
7public:
mtkleinf2950b12014-11-13 12:41:14 -08008 SkVarAlloc();
mtklein8113dd12014-11-12 15:15:28 -08009 ~SkVarAlloc();
10
11 // Returns contiguous bytes aligned at least for pointers. You may pass SK_MALLOC_THROW, etc.
12 char* alloc(size_t bytes, unsigned sk_malloc_flags) {
13 bytes = SkAlignPtr(bytes);
14
mtkleinf2950b12014-11-13 12:41:14 -080015 if (bytes > fRemaining) {
mtklein8113dd12014-11-12 15:15:28 -080016 this->makeSpace(bytes, sk_malloc_flags);
17 }
mtkleinf2950b12014-11-13 12:41:14 -080018 SkASSERT(bytes <= fRemaining);
mtklein8113dd12014-11-12 15:15:28 -080019
20 char* ptr = fByte;
21 fByte += bytes;
mtkleinf2950b12014-11-13 12:41:14 -080022 fRemaining -= bytes;
mtklein8113dd12014-11-12 15:15:28 -080023 return ptr;
24 }
25
mtklein0bd57b22014-11-18 09:32:36 -080026 // Returns our best estimate of the number of bytes we've allocated.
27 // (We intentionally do not track this precisely to save space.)
28 size_t approxBytesAllocated() const;
29
mtklein8113dd12014-11-12 15:15:28 -080030private:
31 void makeSpace(size_t bytes, unsigned flags);
32
33 char* fByte;
mtkleinf2950b12014-11-13 12:41:14 -080034 unsigned fRemaining;
mtklein975ae5e2014-11-13 13:55:22 -080035 unsigned fLgSize; // This is always in the range [4, 16], so it really only needs 4 bits.
mtklein8113dd12014-11-12 15:15:28 -080036
37 struct Block;
38 Block* fBlock;
39};
40
41#endif//SkVarAlloc_DEFINED