1/* 2 * Copyright 2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Niels Sascha Reedijk, niels.reedijk@gmail.com 7 * 8 * Proofreading: 9 * David Weizades, ddewbofh@hotmail.com 10 * Thom Holwerda, slakje@quicknet.nl 11 * 12 * Corresponds to: 13 * /trunk/headers/os/support/BlockCache.h rev 19972 14 * /trunk/src/kits/support/BlockCache.cpp rev 4568 15 */ 16 17 18 /*! 19 \file BlockCache.h 20 \brief Implements a mechanism to store and retrieve memory blocks. 21*/ 22 23 24/*! 25 \var B_OBJECT_CACHE 26 \brief Used in the constructor of BBlockCache. Determines that objects will 27 be created using \c new[] and \c delete[]. 28*/ 29 30 31/*! 32 \var B_MALLOC_CACHE 33 \brief Used in the constructor of BBlockCache. Determines that objects will 34 be created using \c malloc() and \c free(). 35*/ 36 37 38/*! 39 \class BBlockCache 40 \ingroup support 41 \ingroup libbe 42 \brief A class that creates and maintains a pool of memory blocks. 43 44 In some performance critical code there might come a time where you require 45 a lot of little blocks of memory that you want to access and dispose of 46 continuously. Since allocating and freeing memory are 'expensive' 47 operations, it is better to have a pool of memory blocks at your disposal. 48 Luckily, the Haiku API provides a class that will act as the administrator 49 of your memory pool, so you will not have to reinvent the wheel every time. 50 51 The principle is easy. The constructor takes the number of blocks you 52 want to create beforehand, the size of the blocks, and the method of 53 allocation. This can either be #B_OBJECT_CACHE or #B_MALLOC_CACHE. 54 The first one uses C++ operators \c new[] and \c delete[], while the second 55 one uses \c malloc() and \c free(). Unless you have specific demands on 56 performance or you want to take care of freeing the objects yourself, either 57 way works fine. 58 59 As soon as you have the memory pool, you can Get() blocks. If the 60 pre-allocated memory blocks run out, BBlockCache will allocate new ones, so 61 you will not have to worry about availability. As soon as you are done you 62 can Save() the memory back into the pool. BBlockCache will make sure that no 63 more blocks will be saved than the initial number you requested when you 64 created the object, so be aware of that. 65 66 As soon as you got a pointer from the Get() method, you own that block of 67 memory; this means that you have the liberty to dispose of it yourself. It 68 also means that when you delete your BBlockCache instance, any blocks of 69 memory that are checked out will not be destroyed. In case you might want to 70 delete your objects yourself, make sure you free the memory the right way. 71 If you created the object as #B_OBJECT_CACHE, use \c delete[] to free your 72 object. If you created the object as #B_MALLOC_CACHE, use \c free(). Please 73 note that it defeats the purpose of this class if your are going to free all 74 the objects yourself since it basically means that when the pool runs out, 75 Get() will be allocating the objects by itself. 76 77 \note BBlockCache is thread-safe. 78*/ 79 80 81/*! 82 \fn BBlockCache::BBlockCache(uint32 blockCount, size_t blockSize, uint32 83 allocationType) 84 \brief Allocate a new memory pool. 85 86 \param blockCount The number of free memory blocks you want to allocate 87 initially. This number is also used as the maximum number of free blocks 88 that will be kept. 89 \param blockSize The size of the blocks. 90 \param allocationType Either #B_OBJECT_CACHE for using \c new[] and 91 \c delete[] or #B_MALLOC_CACHE for \c malloc() and \c free(). 92*/ 93 94 95/*! 96 \fn BBlockCache::~BBlockCache() 97 \brief Destroy the empty blocks in the free list. 98 99 Note that the blocks you checked out with Get() and not checked back in with 100 Save() will not be freed, since ownership belongs to you. Make sure you 101 clean up after yourself. 102*/ 103 104 105/*! 106 \fn void *BBlockCache::Get(size_t blockSize) 107 \brief Get a block from the pool of free blocks. 108 109 If the pool runs out of free blocks, a new one will be allocated. Please 110 note that if the size given in the \c blockSize parameter is different from 111 the size given in the constructor, a new block of memory will be created. 112 Only sizes that match the blocks in the memory pool will come from the pool. 113 114 \param blockSize The required size of the memory block. 115 \return Returns a pointer to a memory block, or \c NULL if locking the 116 object failed. 117*/ 118 119 120/*! 121 \fn void BBlockCache::Save(void *pointer, size_t blockSize) 122 \brief Save a block of memory to the memory pool. 123 124 The block of memory will only be added to the pool if the \c blockSize is 125 equal to the size the object was created with and if the maximum number of 126 free blocks in the list will not be exceeded. If not, the memory will be 127 freed. 128 129 Note that it is perfectly valid to pass objects other than those you got 130 from Get(), but please note that the way it was created conforms to the way 131 memory is allocated and freed in this pool. Therefore, only feed blocks that 132 were created with \c new[] if the allocation type is #B_OBJECT_CACHE. 133 Likewise, you should only use objects allocated with \c malloc() when the 134 allocation type is #B_MALLOC_CACHE. 135*/ 136