rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rmemallocator.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2016 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
53#ifndef __R_MEM_ALLOCATOR_H__
54#define __R_MEM_ALLOCATOR_H__
55
56#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
57#error "#include <rlib.h> only please."
58#endif
59
60#include <rlib/rtypes.h>
61#include <rlib/rmem.h>
62#include <rlib/rref.h>
63
64#include <stdarg.h>
65
67
82
85
93typedef enum {
94 R_MEM_MAP_READ = (1 << 0),
95 R_MEM_MAP_WRITE = (1 << 1),
96 R_MEM_MAP_FLAG_LAST = (1 << 8)
98
100#define R_MEM_MAP_RW (R_MEM_MAP_READ | R_MEM_MAP_WRITE)
101
104
106#define R_MEM_ALLOCATOR_SYSTEM "system"
107
109typedef struct RMemAllocator RMemAllocator;
111typedef struct RMem RMem;
112
127
129#define R_MEM_MAP_INFO_INIT { NULL, 0, 0, 0, NULL }
130
147
148
149/******************************************************************************/
150/* RMem - Memory chunks */
151/******************************************************************************/
152
177 rsize size, rsize offset, rpointer user, RDestroyNotify usernotify) R_ATTR_WARN_UNUSED_RESULT;
178
191static inline RMem * r_mem_new_take(RMemFlags flags, rpointer data,
192 rsize allocsize, rsize size, rsize offset)
193{
194 return r_mem_new_wrapped (flags, data, allocsize, size, offset, data, r_free);
195}
196
197/* Abstract RMem functionality */
198
200#define r_mem_ref r_ref_ref
201
203#define r_mem_unref r_ref_unref
204
222
240
253
267
280
296
299 RMem * a, va_list args) R_ATTR_WARN_UNUSED_RESULT;
300
303 RMem ** mems, ruint count) R_ATTR_WARN_UNUSED_RESULT;
304
320
323 RMem * a, va_list args) R_ATTR_WARN_UNUSED_RESULT;
324
327 RMem ** mems, ruint count) R_ATTR_WARN_UNUSED_RESULT;
328
330#define r_mem_is_readonly(mem) (mem->flags & R_MEM_FLAG_READONLY)
331
333#define r_mem_is_writable(mem) ((mem->flags & R_MEM_FLAG_READONLY) == 0)
334
336#define r_mem_is_zero_prefixed(mem) (mem->flags & R_MEM_FLAG_ZERO_PREFIXED)
337
339#define r_mem_is_zero_padded(mem) (mem->flags & R_MEM_FLAG_ZERO_PADDED)
340
359
378 RMemFlags flags, RMemAllocator * allocator, RMem * parent,
379 rsize allocsize, rsize size, rsize alignmask, rsize offset);
380
390R_API void r_mem_clear (RMem * mem);
391
392
393/******************************************************************************/
394/* RMemAllocator - Memory allocator */
395/******************************************************************************/
396
402#define r_mem_allocator_default() r_mem_allocator_find (R_MEM_ALLOCATOR_SYSTEM)
403
415
428
429/* Abstract RMemAllocator functionality */
430
432#define r_mem_allocator_ref r_ref_ref
433
435#define r_mem_allocator_unref r_ref_unref
436
448
453static inline RMem *
455 rsize prefix, rsize padding, rsize alignmask)
456{
457 RMemAllocationParams params = { flags, alignmask, prefix, padding };
458 return r_mem_allocator_alloc_full (allocator, size, &params);
459}
460
470 const rchar * mem_type;
473 RMem * (*alloc) (RMemAllocator * allocator, rsize size, const RMemAllocationParams * params);
474 rboolean (*free) (RMemAllocator * allocator, RMem * mem);
475 rpointer (*map) (RMem * mem, const RMemMapInfo * info);
476 rboolean (*unmap) (RMem * mem, const RMemMapInfo * info);
477 RMem * (*merge) (const RMemAllocationParams * params, RMem ** mems, ruint count);
478 RMem * (*copy) (RMem * mem, rssize offset, rssize size);
479 RMem * (*view) (RMem * mem, rssize offset, rssize size);
480};
481
482
483R_END_DECLS
484
487#endif /* __R_MEM_ALLOCATOR_H__ */
488
#define R_ATTR_WARN_UNUSED_RESULT
Warn if a function's return value is ignored.
Definition rmacros.h:278
#define R_ATTR_NULL_TERMINATED
Mark a variadic function as requiring a NULL sentinel.
Definition rmacros.h:336
#define R_API
Public-API decoration: resolves to R_API_EXPORT while building rlib and R_API_IMPORT for consumers.
Definition rmacros.h:115
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
void r_mem_init(RMem *mem, RDestroyNotify notify, RMemFlags flags, RMemAllocator *allocator, RMem *parent, rsize allocsize, rsize size, rsize alignmask, rsize offset)
Allocator-implementation helper: populate an RMem's common fields and arm its destructor.
RMem * r_mem_allocator_alloc_full(RMemAllocator *allocator, rsize size, const RMemAllocationParams *params) R_ATTR_WARN_UNUSED_RESULT
Allocate a new RMem chunk via allocator.
RMemAllocator * r_mem_allocator_find(const rchar *name) R_ATTR_WARN_UNUSED_RESULT
Look up a registered allocator by name.
ruint32 RMemFlags
Bitmask of RMemFlag values.
Definition rmemallocator.h:84
RMem * r_mem_take_array(const RMemAllocationParams *params, RMem **mems, ruint count) R_ATTR_WARN_UNUSED_RESULT
Array variant of r_mem_take - consumes references in mems on success.
static RMem * r_mem_new_take(RMemFlags flags, rpointer data, rsize allocsize, rsize size, rsize offset)
Wrap an existing heap allocation in an RMem and transfer ownership.
Definition rmemallocator.h:191
RMem * r_mem_view(RMem *mem, rssize offset, rssize size) R_ATTR_WARN_UNUSED_RESULT
Create an aliased view onto a byte range of mem.
RMem * r_mem_mergev(const RMemAllocationParams *params, RMem *a, va_list args) R_ATTR_WARN_UNUSED_RESULT
va_list variant of r_mem_merge.
ruint32 RMemMapFlags
Bitmask of RMemMapFlag values.
Definition rmemallocator.h:103
rboolean r_mem_map(RMem *mem, RMemMapInfo *info, RMemMapFlags flags)
Open a scoped mapping over mem's bytes.
RMem * r_mem_merge_array(const RMemAllocationParams *params, RMem **mems, ruint count) R_ATTR_WARN_UNUSED_RESULT
Array variant of r_mem_merge - mems is a count-long array of chunk pointers.
void r_mem_clear(RMem *mem)
Allocator-implementation helper: release the references an RMem holds (parent, allocator).
RMemFlag
Per-chunk flags carried on every RMem.
Definition rmemallocator.h:75
rboolean r_mem_resize(RMem *mem, rsize offset, rsize size)
Adjust the visible window inside mem.
RMem * r_mem_copy(RMem *mem, rssize offset, rssize size) R_ATTR_WARN_UNUSED_RESULT
Deep-copy a byte range from mem into a fresh RMem.
RMemMapFlag
Mapping-access flags passed to r_mem_map.
Definition rmemallocator.h:93
RMem * r_mem_new_wrapped(RMemFlags flags, rpointer data, rsize allocsize, rsize size, rsize offset, rpointer user, RDestroyNotify usernotify) R_ATTR_WARN_UNUSED_RESULT
Wrap an existing pointer in an RMem without copying.
rboolean r_mem_unmap(RMem *mem, RMemMapInfo *info)
Close a mapping previously opened with r_mem_map.
RMem * r_mem_takev(const RMemAllocationParams *params, RMem *a, va_list args) R_ATTR_WARN_UNUSED_RESULT
va_list variant of r_mem_take.
void r_mem_allocator_register(RMemAllocator *allocator)
Register an allocator so it can be looked up by name.
RMem * r_mem_take(const RMemAllocationParams *params, RMem *a,...) R_ATTR_WARN_UNUSED_RESULT
Concatenate chunks (as r_mem_merge) but unref the inputs.
static RMem * r_mem_allocator_alloc(RMemAllocator *allocator, RMemFlags flags, rsize size, rsize prefix, rsize padding, rsize alignmask)
Convenience for r_mem_allocator_alloc_full with the params spelled out as individual arguments.
Definition rmemallocator.h:454
RMem * r_mem_merge(const RMemAllocationParams *params, RMem *a,...) R_ATTR_WARN_UNUSED_RESULT
Concatenate two or more chunks into a fresh RMem.
@ R_MEM_FLAG_NO_VIEWS
Definition rmemallocator.h:78
@ R_MEM_FLAG_READONLY
Definition rmemallocator.h:77
@ R_MEM_FLAG_ZERO_PADDED
Definition rmemallocator.h:80
@ R_MEM_FLAG_ZERO_PREFIXED
Definition rmemallocator.h:79
@ R_MEM_FLAG_NONE
Definition rmemallocator.h:76
@ R_MEM_MAP_WRITE
Definition rmemallocator.h:95
@ R_MEM_MAP_READ
Definition rmemallocator.h:94
@ R_MEM_MAP_FLAG_LAST
Definition rmemallocator.h:96
void r_free(rpointer ptr)
Release a heap allocation obtained from r_malloc / r_malloc0 / r_calloc / r_realloc.
char rchar
Default character type (char).
Definition rtypes.h:137
int rboolean
Boolean type (typedef'd to int).
Definition rtypes.h:59
unsigned int ruint
Unsigned int.
Definition rtypes.h:157
void(* RDestroyNotify)(rpointer ptr)
Destructor callback: free / release the value at ptr.
Definition rtypes.h:401
unsigned int ruint32
Unsigned 32-bit integer.
Definition rtypes.h:192
void * rpointer
Generic pointer (alias for void *).
Definition rtypes.h:327
unsigned char ruint8
Unsigned 8-bit integer.
Definition rtypes.h:190
unsigned long rsize
Unsigned pointer-sized size type (like size_t).
Definition rtypes.h:290
signed long rssize
Signed pointer-sized size type (like ssize_t).
Definition rtypes.h:291
Memory allocation, byte-buffer operations, and pattern scanning.
Refcount base struct shared by every refcounted type in rlib.
Foundational type aliases used by every rlib header.
Allocation-time constraints passed to r_mem_allocator_alloc and the merge / take helpers.
Definition rmemallocator.h:121
rsize prefix
Definition rmemallocator.h:124
RMemFlags flags
Definition rmemallocator.h:122
rsize alignmask
Definition rmemallocator.h:123
rsize padding
Definition rmemallocator.h:125
Concrete layout of an RMemAllocator.
Definition rmemallocator.h:468
rboolean(* unmap)(RMem *mem, const RMemMapInfo *info)
Definition rmemallocator.h:476
rsize alignmask
Definition rmemallocator.h:471
RRef ref
Definition rmemallocator.h:469
const rchar * mem_type
Definition rmemallocator.h:470
rpointer(* map)(RMem *mem, const RMemMapInfo *info)
Definition rmemallocator.h:475
rboolean(* free)(RMemAllocator *allocator, RMem *mem)
Definition rmemallocator.h:474
Scope object carried by an active mapping.
Definition rmemallocator.h:140
ruint8 * data
Definition rmemallocator.h:141
RMem * mem
Definition rmemallocator.h:145
rsize allocsize
Definition rmemallocator.h:143
rsize size
Definition rmemallocator.h:142
RMemMapFlags flags
Definition rmemallocator.h:144
Concrete layout of an RMem chunk.
Definition rmemallocator.h:348
RMemFlags flags
Definition rmemallocator.h:350
rsize size
Definition rmemallocator.h:355
rsize alignmask
Definition rmemallocator.h:356
RMem * parent
Definition rmemallocator.h:352
RMemAllocator * allocator
Definition rmemallocator.h:351
rsize offset
Definition rmemallocator.h:357
RRef ref
Definition rmemallocator.h:349
rsize allocsize
Definition rmemallocator.h:354
Refcount base struct.
Definition rref.h:58