SkChunkAlloc.h revision f2b98d67dcb6fcb3120feede9c72016fc7b3ead8
18a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Copyright (C) 2006 The Android Open Source Project
38a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Licensed under the Apache License, Version 2.0 (the "License");
58a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * you may not use this file except in compliance with the License.
68a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * You may obtain a copy of the License at
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *      http://www.apache.org/licenses/LICENSE-2.0
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * Unless required by applicable law or agreed to in writing, software
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * distributed under the License is distributed on an "AS IS" BASIS,
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * See the License for the specific language governing permissions and
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com * limitations under the License.
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkChunkAlloc_DEFINED
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkChunkAlloc_DEFINED
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTypes.h"
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comclass SkChunkAlloc : SkNoncopyable {
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkChunkAlloc(size_t minSize);
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    ~SkChunkAlloc();
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Free up all allocated blocks. This invalidates all returned
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pointers.
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void reset();
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Reuse all allocated blocks. This invalidates all returned
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        pointers (like reset) but doesn't necessarily free up all
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        of the privately allocated blocks. This is more efficient
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        if you plan to reuse the allocator multiple times.
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void reuse();
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    enum AllocFailType {
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kReturnNil_AllocFailType,
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        kThrow_AllocFailType
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    };
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* alloc(size_t bytes, AllocFailType);
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    void* allocThrow(size_t bytes) {
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return this->alloc(bytes, kThrow_AllocFailType);
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
49aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com    /** Call this to unalloc the most-recently allocated ptr by alloc(). On
50aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        success, the number of bytes freed is returned, or 0 if the block could
51aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        not be unallocated. This is a hint to the underlying allocator that
52aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        the previous allocation may be reused, but the implementation is free
53aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com        to ignore this call (and return 0).
54aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com     */
55aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com    size_t unalloc(void* ptr);
56aefd2bc75738963b9b6579897be32bfbc8fb00afreed@android.com
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t totalCapacity() const { return fTotalCapacity; }
58f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
59f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    /**
60f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  Returns true if the specified address is within one of the chunks, and
61f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  has at least 1-byte following the address (i.e. if addr points to the
62f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     *  end of a chunk, then contains() will return false).
63f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com     */
64f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com    bool contains(const void* addr) const;
65f2b98d67dcb6fcb3120feede9c72016fc7b3ead8reed@android.com
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    struct Block;
688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Block*  fBlock;
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t  fMinSize;
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Block*  fPool;
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    size_t  fTotalCapacity;
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
738a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    Block* newBlock(size_t bytes, AllocFailType ftype);
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
77