aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkVarAlloc.h
blob: 3729bad1051b6ad518aef157edc43ceb4edabc3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkVarAlloc_DEFINED
#define SkVarAlloc_DEFINED

#include "SkTypes.h"

class SkVarAlloc : SkNoncopyable {
public:
    // Smallest block we'll allocate is 2**N bytes.
    explicit SkVarAlloc(size_t minLgSize);
    // Same as above, but first uses up to len bytes from storage.
    SkVarAlloc(size_t minLgSize, char* storage, size_t len);

    ~SkVarAlloc();

    // Returns contiguous bytes aligned at least for pointers.
    char* alloc(size_t bytes) {
        bytes = SkAlignPtr(bytes);

        if (bytes > fRemaining) {
            this->makeSpace(bytes);
        }
        SkASSERT(bytes <= fRemaining);

        char* ptr = fByte;
        fByte += bytes;
        fRemaining = SkToU32(fRemaining - bytes);
        return ptr;
    }

    // Returns our best estimate of the number of bytes we've allocated.
    // (We may not track this precisely to save space.)
    size_t approxBytesAllocated() const { return fBytesAllocated; }

private:
    void makeSpace(size_t bytes);

    size_t fBytesAllocated;

    char* fByte;
    unsigned fRemaining;
    unsigned fLgSize;

    struct Block;
    Block* fBlock;
};
static_assert(sizeof(SkVarAlloc) <= 32, "SkVarAllocSize");

#endif//SkVarAlloc_DEFINED