aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkFixedAlloc.cpp
blob: cb2f798d4fdb2a8a63cf42b52c7a0350dbbec4ec (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
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkFixedAlloc.h"

SkFixedAlloc::SkFixedAlloc(void* ptr, size_t len)
    : fStorage((char*)ptr), fCursor(fStorage), fEnd(fStorage + len) {}

void SkFixedAlloc::undo() {
    // This function is essentially make() in reverse.

    // First, read the Footer we stamped at the end.
    Footer footer;
    memcpy(&footer, fCursor - sizeof(Footer), sizeof(Footer));

    Releaser releaser = (Releaser)((char*)Base + (footer >> 5));
    ptrdiff_t padding = footer & 31;

    fCursor = releaser(fCursor);
    fCursor -= padding;
}

void SkFixedAlloc::reset() {
    while (fCursor > fStorage) {
        this->undo();
    }
}

void SkFixedAlloc::Base() { }

SkFallbackAlloc::SkFallbackAlloc(SkFixedAlloc* fixed) : fFixedAlloc(fixed) {}

void SkFallbackAlloc::undo() {
    if (fHeapAllocs.empty()) {
        return fFixedAlloc->undo();
    }
    HeapAlloc alloc = fHeapAllocs.back();
    alloc.deleter(alloc.ptr);
    fHeapAllocs.pop_back();
}

void SkFallbackAlloc::reset() {
    while (!fHeapAllocs.empty()) {
        this->undo();
    }
    fFixedAlloc->reset();
}