A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/***************************************************************************
2* __________ __ ___.
3* Open \______ \ ____ ____ | | _\_ |__ _______ ___
4* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7* \/ \/ \/ \/ \/
8* $Id$
9*
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.
13*
14* Copyright (C) 2009 Andrew Mahone
15* Copyright (C) 2011 Thomas Martitz
16*
17* This program is free software; you can redistribute it and/or
18* modify it under the terms of the GNU General Public License
19* as published by the Free Software Foundation; either version 2
20* of the License, or (at your option) any later version.
21*
22* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23* KIND, either express or implied.
24*
25****************************************************************************/
26#ifndef _BUFLIB_MEMPOOL_H_
27#define _BUFLIB_MEMPOOL_H_
28
29#ifndef _BUFLIB_H_
30# error "include buflib.h instead"
31#endif
32
33#include "system.h"
34
35/* Indices used to access block fields as block[BUFLIB_IDX_XXX] */
36enum {
37 BUFLIB_IDX_LEN, /* length of the block, must come first */
38 BUFLIB_IDX_HANDLE, /* pointer to entry in the handle table */
39 BUFLIB_IDX_OPS, /* pointer to an ops struct */
40 BUFLIB_IDX_PIN, /* pin count */
41 BUFLIB_NUM_FIELDS,
42};
43
44union buflib_data
45{
46 intptr_t val; /* length of the block in n*sizeof(union buflib_data).
47 Includes buflib metadata overhead. A negative value
48 indicates block is unallocated */
49 volatile unsigned pincount; /* number of pins */
50 struct buflib_callbacks* ops; /* callback functions for move and shrink. Can be NULL */
51 char* alloc; /* start of allocated memory area */
52 union buflib_data *handle; /* pointer to entry in the handle table.
53 Used during compaction for fast lookup */
54};
55
56struct buflib_context
57{
58 union buflib_data *handle_table;
59 union buflib_data *first_free_handle;
60 union buflib_data *last_handle;
61 union buflib_data *buf_start;
62 union buflib_data *alloc_end;
63 bool compact;
64};
65
66#define BUFLIB_ALLOC_OVERHEAD (BUFLIB_NUM_FIELDS * sizeof(union buflib_data))
67
68#ifndef BUFLIB_DEBUG_GET_DATA
69static inline void *buflib_get_data(struct buflib_context *ctx, int handle)
70{
71 return (void *)ctx->handle_table[-handle].alloc;
72}
73#endif
74
75static inline union buflib_data *_buflib_get_block_header(void *data)
76{
77 union buflib_data *bd = ALIGN_DOWN(data, sizeof(*bd));
78 return bd - BUFLIB_NUM_FIELDS;
79}
80
81static inline void *buflib_get_data_pinned(struct buflib_context *ctx, int handle)
82{
83 void *data = buflib_get_data(ctx, handle);
84 union buflib_data *bd = _buflib_get_block_header(data);
85
86 bd[BUFLIB_IDX_PIN].pincount++;
87 return data;
88}
89
90static inline void buflib_put_data_pinned(struct buflib_context *ctx, void *data)
91{
92 (void)ctx;
93 union buflib_data *bd = _buflib_get_block_header(data);
94 bd[BUFLIB_IDX_PIN].pincount--;
95}
96
97#endif /* _BUFLIB_MEMPOOL_H_ */