1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2006 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkChunkAlloc_DEFINED
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkChunkAlloc_DEFINED
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTypes.h"
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkChunkAlloc : SkNoncopyable {
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkChunkAlloc(size_t minSize);
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkChunkAlloc();
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
20ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com    /**
21ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com     *  Free up all allocated blocks. This invalidates all returned
22ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com     *  pointers.
23ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com     */
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void reset();
25a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips    /**
26a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips     *  Reset to 0 used bytes preserving as much memory as possible.
27a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips     *  This invalidates all returned pointers.
28a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips     */
29a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips    void rewind();
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum AllocFailType {
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kReturnNil_AllocFailType,
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kThrow_AllocFailType
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
35fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* alloc(size_t bytes, AllocFailType);
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* allocThrow(size_t bytes) {
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->alloc(bytes, kThrow_AllocFailType);
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
40fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
41aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com    /** Call this to unalloc the most-recently allocated ptr by alloc(). On
42aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        success, the number of bytes freed is returned, or 0 if the block could
43aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        not be unallocated. This is a hint to the underlying allocator that
44aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        the previous allocation may be reused, but the implementation is free
45aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        to ignore this call (and return 0).
46aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com     */
47aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com    size_t unalloc(void* ptr);
48fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t totalCapacity() const { return fTotalCapacity; }
506757a3c71fd6c16af6bbd76f268307f0177b17aereed@google.com    size_t totalUsed() const { return fTotalUsed; }
51a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips    SkDEBUGCODE(int blockCount() const { return fBlockCount; })
52a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips    SkDEBUGCODE(size_t totalLost() const { return fTotalLost; })
53f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
54f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    /**
55f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  Returns true if the specified address is within one of the chunks, and
56f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  has at least 1-byte following the address (i.e. if addr points to the
57f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  end of a chunk, then contains() will return false).
58f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
59f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    bool contains(const void* addr) const;
60f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    struct Block;
63ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Block*  fBlock;
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t  fMinSize;
66ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com    size_t  fChunkSize;
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t  fTotalCapacity;
686757a3c71fd6c16af6bbd76f268307f0177b17aereed@google.com    size_t  fTotalUsed;     // will be <= fTotalCapacity
69a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips    SkDEBUGCODE(int     fBlockCount;)
70a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips    SkDEBUGCODE(size_t  fTotalLost;)     // will be <= fTotalCapacity
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Block* newBlock(size_t bytes, AllocFailType ftype);
73a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips    Block* addBlockIfNecessary(size_t bytes, AllocFailType ftype);
74a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips
75a9061de9ff8d5b9545e5d1ef7a6e4054e1907c95robertphillips    SkDEBUGCODE(void validate();)
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
79