summaryrefslogtreecommitdiff
path: root/apps/plugins/lib/buflib.h
blob: d4ef4af9ff7c78fad8c4ab6d5fc8b2d8ebe8ac19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/***************************************************************************
*             __________               __   ___.
*   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
*
* 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_
#include <plugin.h>

union buflib_data
{
    intptr_t val;
    union buflib_data *ptr;
};

struct buflib_context
{
    union buflib_data *handle_table;
    union buflib_data *first_free_handle;
    union buflib_data *last_handle;
    union buflib_data *first_free_block;
    union buflib_data *buf_start;
    union buflib_data *alloc_end;
    bool compact;
};

void buflib_init(struct buflib_context *context, void *buf, size_t size);
int buflib_alloc(struct buflib_context *context, size_t size);
void buflib_free(struct buflib_context *context, int handle);
void* buflib_buffer_out(struct buflib_context *ctx, size_t *size);
void buflib_buffer_in(struct buflib_context *ctx, int size);



/* always_inline is due to this not getting inlined when not optimizing, which
 * leads to an unresolved reference since it doesn't exist as a non-inline
 * function
 */
extern inline __attribute__((always_inline))
void* buflib_get_data(struct buflib_context *context, int handle)
{
    return (void*)(context->handle_table[-handle].ptr);
}
#endif