Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkChunkAlloc_DEFINED
     18 #define SkChunkAlloc_DEFINED
     19 
     20 #include "SkTypes.h"
     21 
     22 class SkChunkAlloc : SkNoncopyable {
     23 public:
     24     SkChunkAlloc(size_t minSize);
     25     ~SkChunkAlloc();
     26 
     27     /** Free up all allocated blocks. This invalidates all returned
     28         pointers.
     29     */
     30     void reset();
     31 
     32     /** Reuse all allocated blocks. This invalidates all returned
     33         pointers (like reset) but doesn't necessarily free up all
     34         of the privately allocated blocks. This is more efficient
     35         if you plan to reuse the allocator multiple times.
     36     */
     37     void reuse();
     38 
     39     enum AllocFailType {
     40         kReturnNil_AllocFailType,
     41         kThrow_AllocFailType
     42     };
     43 
     44     void* alloc(size_t bytes, AllocFailType);
     45     void* allocThrow(size_t bytes) {
     46         return this->alloc(bytes, kThrow_AllocFailType);
     47     }
     48 
     49     /** Call this to unalloc the most-recently allocated ptr by alloc(). On
     50         success, the number of bytes freed is returned, or 0 if the block could
     51         not be unallocated. This is a hint to the underlying allocator that
     52         the previous allocation may be reused, but the implementation is free
     53         to ignore this call (and return 0).
     54      */
     55     size_t unalloc(void* ptr);
     56 
     57     size_t totalCapacity() const { return fTotalCapacity; }
     58 
     59     /**
     60      *  Returns true if the specified address is within one of the chunks, and
     61      *  has at least 1-byte following the address (i.e. if addr points to the
     62      *  end of a chunk, then contains() will return false).
     63      */
     64     bool contains(const void* addr) const;
     65 
     66 private:
     67     struct Block;
     68     Block*  fBlock;
     69     size_t  fMinSize;
     70     Block*  fPool;
     71     size_t  fTotalCapacity;
     72 
     73     Block* newBlock(size_t bytes, AllocFailType ftype);
     74 };
     75 
     76 #endif
     77