/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * This is a memory allocator designed to provide reasonable management of free * space and fast access to allocated data. More than one allocator can be used * at a time by initializing multiple contexts. * * Copyright (C) 2009 Andrew Mahone * Copyright (C) 2011 Thomas Martitz * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #ifndef _BUFLIB_H_ #define _BUFLIB_H_ #include #include #include /* enable single block debugging */ #define BUFLIB_DEBUG_BLOCK_SINGLE union buflib_data { intptr_t val; /* length of the block in n*sizeof(union buflib_data). Includes buflib metadata overhead. A negative value indicates block is unallocated */ volatile unsigned pincount; /* number of pins */ struct buflib_callbacks* ops; /* callback functions for move and shrink. Can be NULL */ char* alloc; /* start of allocated memory area */ union buflib_data *handle; /* pointer to entry in the handle table. Used during compaction for fast lookup */ uint32_t crc; /* checksum of this data to detect corruption */ }; struct buflib_context { union buflib_data *handle_table; union buflib_data *first_free_handle; union buflib_data *last_handle; union buflib_data *buf_start; union buflib_data *alloc_end; bool compact; }; /** * This declares the minimal overhead that is required per alloc. These * are bytes that are allocated from the context's pool in addition * to the actually requested number of bytes. * * The total number of bytes consumed by an allocation is * BUFLIB_ALLOC_OVERHEAD + requested bytes + strlen(handle_table[-handle].alloc); } #endif /** * Shrink the memory allocation associated with the given handle * Mainly intended to be used with the shrink callback, but it can also * be called outside as well, e.g. to give back buffer space allocated * with buflib_alloc_maximum(). * * Note that you must move/copy data around yourself before calling this, * buflib will not do this as part of shrinking. * * handle: The handle identifying this allocation * new_start: the new start of the allocation * new_size: the new size of the allocation * * Returns: true if shrinking was successful. Otherwise it returns false, * without having modified memory. * */ bool buflib_shrink(struct buflib_context *ctx, int handle, void* newstart, size_t new_size); /** * Increment the pin count for a handle. When pinned the handle will not * be moved and move callbacks will not be triggered, allowing a pointer * to the buffer to be kept across yields or used for I/O. * * Note that shrink callbacks can still be invoked for pinned handles. */ void buflib_pin(struct buflib_context *ctx, int handle); /** * Decrement the pin count for a handle. */ void buflib_unpin(struct buflib_context *ctx, int handle); /** * Get the current pin count of a handle. Zero means the handle is not pinned. */ unsigned buflib_pin_count(struct buflib_context *ctx, int handle); /** * Frees memory associated with the given handle * * Returns: 0 (to invalidate handles in one line, 0 is not a valid handle) */ int buflib_free(struct buflib_context *context, int handle); /** * Moves the underlying buflib buffer up by size bytes (as much as * possible for size == 0) without moving the end. This effectively * reduces the available space by taking away manageable space from the * front. This space is not available for new allocations anymore. * * To make space available in the front, everything is moved up. * It does _NOT_ call the move callbacks * * * size: size in bytes to move the buffer up (take away). The actual * bytes moved is returned in this * Returns: The new start of the underlying buflib buffer */ void* buflib_buffer_out(struct buflib_context *ctx, size_t *size); /** * Moves the underlying buflib buffer down by size bytes without * moving the end. This grows the buflib buffer by adding space to the front. * The new bytes are available for new allocations. * * Everything is moved down, and the new free space will be in the middle. * It does _NOT_ call the move callbacks. * * size: size in bytes to move the buffer down (new free space) */ void buflib_buffer_in(struct buflib_context *ctx, int size); /* debugging */ /** * Returns the name, as given to buflib_alloc() and buflib_allloc_ex(), of the * allocation associated with the given handle. As naming allocations * is optional, there might be no name associated. * * handle: The handle indicating the allocation * * Returns: A pointer to the string identifier of the allocation, or NULL * if none was specified with buflib_alloc_ex(). */ const char* buflib_get_name(struct buflib_context *ctx, int handle); /** * Gets the number of blocks in the entire buffer, allocated or unallocated * * Only available if BUFLIB_DEBUG_BLOCK_SIGNLE is defined */ int buflib_get_num_blocks(struct buflib_context *ctx); /** * Print information about a single block as indicated by block_num * into buf * * buflib_get_num_blocks() beforehand to get the total number of blocks, * as passing an block_num higher than that is undefined * * Only available if BUFLIB_DEBUG_BLOCK_SIGNLE is defined */ void buflib_print_block_at(struct buflib_context *ctx, int block_num, char* buf, size_t bufsize); /** * Check integrity of given buflib context */ void buflib_check_valid(struct buflib_context *ctx); #endif