rockbox/firmware/include/core_alloc.h
Aidan MacDonald f47aa584a8 buflib: add pin/unpin operation
An allocation is pinned by calling buflib_pin() to up its pin count.
The pin count is like a reference count: when above 0, buflib won't
move the allocation and won't call its move callbacks. This makes it
safe to hold the pointer returned by buflib_get_data() across yields
or allocations.

Note that pinned allocations can still shrink because there are some
use cases where this would be valid, if buffer users coordinate with
the shrink callback.

Change-Id: I0d0c2a8ac7d891d3ad6b3d0eb80c5b5a1b4b9a9d
2022-09-19 15:09:51 -04:00

46 lines
1.4 KiB
C

#ifndef __CORE_ALLOC_H__
#define __CORE_ALLOC_H__
#include <string.h>
#include <stdbool.h>
#include "config.h"
#include "buflib.h"
/* All functions below are wrappers for functions in buflib.h, except
* they have a predefined context
*/
void core_allocator_init(void) INIT_ATTR;
int core_alloc(const char* name, size_t size);
int core_alloc_ex(const char* name, size_t size, struct buflib_callbacks *ops);
int core_alloc_maximum(const char* name, size_t *size, struct buflib_callbacks *ops);
bool core_shrink(int handle, void* new_start, size_t new_size);
void core_pin(int handle);
void core_unpin(int handle);
unsigned core_pin_count(int handle);
int core_free(int handle);
size_t core_available(void);
size_t core_allocatable(void);
const char* core_get_name(int handle);
#ifdef DEBUG
void core_check_valid(void);
#endif
/* DO NOT ADD wrappers for buflib_buffer_out/in. They do not call
* the move callbacks and are therefore unsafe in the core */
#ifdef BUFLIB_DEBUG_BLOCK_SINGLE
int core_get_num_blocks(void);
void core_print_block_at(int block_num, char* buf, size_t bufsize);
#endif
/* frees the debug test alloc created at initialization,
* since this is the first any further alloc should force a compaction run */
bool core_test_free(void);
static inline void* core_get_data(int handle)
{
extern struct buflib_context core_ctx;
return buflib_get_data(&core_ctx, handle);
}
#endif /* __CORE_ALLOC_H__ */