# The Support Kit: BBlockCache

Derived from: none

Declared in: be/support/BlockCache.h

Library: libbe.so

## Overview

The BBlockCache class provides a simple mechanism for caching fixed-length blocks of memory. Typically, this is useful when a program is frequently allocating and deallocating fixed-sized objects. BBlockCache maintains a pool of memory blocks, doling them out as needed by the application. The program can also return memory blocks to the pool for reuse later.

## Constructor and Destructor

### BBlockCache()

          BBlockCache(size_t cache_size, size_t block_size, uint32 type)

Creates a new memory block cache, allocating memory for cache_size blocks, each controlling block_size bytes of memory. type is either `B_OBJECT_CACHE` or `B_MALLOC_CACHE`. Specifying type `B_OBJECT_CACHE` will cause the memory blocks to be managed by `new`/`delete` while type `B_MALLOC_CACHE` memory blocks are managed by `malloc`/`free`.

### ~BBlockCache()

          ~BBlockCache()

Frees all memory in the free pool of the memory cache.

## Member Functions

### Get()

          void *Get(size_t block_size)

Retrieves a memory block of size block_size from the cache. If block_size is the same as the value given to the constructor, the memory returned will be taken from the cache. Otherwise, it is allocated by either `new` or `malloc` as appropriate. The caller assumes responsibility for deallocating the memory block, although it should in general return the memory block to the BBlockCache with the `Save()` method, described below.

### Save()

          void Save(void *pointer, size_t block_size)

Returns a memory block of size block_size to the cache. If block_size is the same as the value passed in the constructor, the memory block is returned to the cache free block list. Otherwise, it is `free`d or `delete`d, as appropriate. In either case, the caller is freed of responsibility for deallocating the memory.

The Be Book, in lovely HTML, for BeOS Release 3.

Copyright © 1998 Be, Inc. All rights reserved.

Last modified March 26, 1998.