1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * This is a memory allocator designed to provide reasonable management of free
11 * space and fast access to allocated data. More than one allocator can be used
12 * at a time by initializing multiple contexts.
14 * Copyright (C) 2009 Andrew Mahone
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
32 union buflib_data
*ptr
;
37 union buflib_data
*handle_table
;
38 union buflib_data
*first_free_handle
;
39 union buflib_data
*last_handle
;
40 union buflib_data
*first_free_block
;
41 union buflib_data
*buf_start
;
42 union buflib_data
*alloc_end
;
46 void buflib_init(struct buflib_context
*context
, void *buf
, size_t size
);
47 int buflib_alloc(struct buflib_context
*context
, size_t size
);
48 void buflib_free(struct buflib_context
*context
, int handle
);
49 void* buflib_buffer_out(struct buflib_context
*ctx
, size_t *size
);
50 void buflib_buffer_in(struct buflib_context
*ctx
, int size
);
54 /* always_inline is due to this not getting inlined when not optimizing, which
55 * leads to an unresolved reference since it doesn't exist as a non-inline
58 extern inline __attribute__((always_inline
))
59 void* buflib_get_data(struct buflib_context
*context
, int handle
)
61 return (void*)(context
->handle_table
[-handle
].ptr
);