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();
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum AllocFailType {
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kReturnNil_AllocFailType,
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kThrow_AllocFailType
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
30fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* alloc(size_t bytes, AllocFailType);
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* allocThrow(size_t bytes) {
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->alloc(bytes, kThrow_AllocFailType);
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
35fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
36aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com    /** Call this to unalloc the most-recently allocated ptr by alloc(). On
37aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        success, the number of bytes freed is returned, or 0 if the block could
38aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        not be unallocated. This is a hint to the underlying allocator that
39aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        the previous allocation may be reused, but the implementation is free
40aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        to ignore this call (and return 0).
41aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com     */
42aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com    size_t unalloc(void* ptr);
43fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t totalCapacity() const { return fTotalCapacity; }
456757a3c71fd6c16af6bbd76f268307f0177b17aereed@google.com    size_t totalUsed() const { return fTotalUsed; }
46ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com    int blockCount() const { return fBlockCount; }
47f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
48f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    /**
49f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  Returns true if the specified address is within one of the chunks, and
50f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  has at least 1-byte following the address (i.e. if addr points to the
51f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  end of a chunk, then contains() will return false).
52f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
53f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    bool contains(const void* addr) const;
54f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    struct Block;
57ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Block*  fBlock;
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t  fMinSize;
60ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com    size_t  fChunkSize;
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t  fTotalCapacity;
626757a3c71fd6c16af6bbd76f268307f0177b17aereed@google.com    size_t  fTotalUsed;     // will be <= fTotalCapacity
63ebd24962dfdb7a62cf97c0e3938851d56cabd10freed@google.com    int     fBlockCount;
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Block* newBlock(size_t bytes, AllocFailType ftype);
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
69