The Support Kit Table of Contents | The Support Kit Index

# BBlockCache

Derived from: none

Declared in: be/support/BlockCache.h

Library: libbe.so

###### Summary

The BBlockCache class pre-allocates a pool of fixed-length blocks of memory. You call `Get()` to retrieve a block of memory from the pool, and `Save()` to return it. BBlockCache objects are useful in applications that frequently allocate and deallocate fixed-sized objects, and in the implementations of a class' `new` and `delete` operators.

## Constructor and Destructor

### BBlockCache()

`BBlockCache(size_t` count, `size_t` size, `uint32` type`)`

Creates a new memory block pool, allocating memory for count blocks, each controlling size bytes of memory. type is either:

- `B_OBJECT_CACHE`; memory is managed through `new` and `delete`.

- `B_MALLOC_CACHE`; memory is managed through `malloc()` and `free()`.

### ~BBlockCache()

`~BBlockCache()`

Frees any unused memory in the object's block pool. Memory that was retrieved through `Get()` (and that hasn't been returned through `Save()`) is not deallocated.

## Member Functions

### Get()

void *`Get(`size_t size`)`

Retrieves a block of memory of the given size and returns it directly. If size is the same as the size argument you passed to the constructor, the memory returned will be taken from the object's cache. Otherwise, it's allocated using either `new` or `malloc()` as requested in the constructor. When you're done with the memory, you can either deallocate it yourself, or return it to the BBlockCache object by calling `Save()`.

### Save()

void `Save(`void *pointer, size_t size`)`

Returns, to the BBlockCache object, size bytes of memory pointed to by pointer. If the memory was taken from the object's pool, the memory is returned to the pool. Otherwise, it's deallocated. In either case, the caller is freed of responsibility for deallocating the memory.

The Support Kit Table of Contents | The Support Kit Index

The B e Book,
...in lovely HTML...
for BeOS Release 5.

Copyright © 2000 Be, Inc. All rights reserved..