Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
memory_arena.h
Go to the documentation of this file.
1/*
2 This file is part of Ansel
3 Copyright (C) 2026 - Aurélien PIERRE
4
5 Ansel is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Ansel is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with darktable. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#include "common/dtpthread.h"
22#include <glib.h>
23#include <stdint.h>
24
25/*
26 * Arena allocator for cache buffers:
27 * - We reserve one big contiguous block of virtual memory (the arena).
28 * - The arena is split into fixed-size pages (page_size).
29 * - free_runs is a sorted list of "free stretches" of pages.
30 * Each run says "from page N, K pages are free".
31 * This avoids many small malloc/free calls: we just carve out page ranges
32 * and put them back into the list when done.
33 */
34typedef struct dt_cache_arena_t
35{
36 uint8_t *base;
37 size_t size;
38
39 size_t page_size;
40 uint32_t num_pages;
41
42 GArray *free_runs; // sorted list of free page runs (start page + length in pages)
43
44 dt_pthread_mutex_t lock;
46
48 size_t size,
49 uint32_t *out_pages,
50 size_t *out_size);
51
53 size_t size,
54 size_t *out_size);
55
57 void *ptr,
58 size_t size);
59
60// Return arena free-space stats (in pages). Thread-safe.
62 uint32_t *out_total_free_pages,
63 uint32_t *out_largest_free_run_pages);
64
65int dt_cache_arena_init(dt_cache_arena_t *a, size_t total_size);
67
68gboolean dt_cache_arena_ptr_in(const dt_cache_arena_t *a, const void *ptr);
void dt_cache_arena_stats(dt_cache_arena_t *a, uint32_t *out_total_free_pages, uint32_t *out_largest_free_run_pages)
Definition memory_arena.c:217
void dt_cache_arena_cleanup(dt_cache_arena_t *a)
Definition memory_arena.c:240
gboolean dt_cache_arena_ptr_in(const dt_cache_arena_t *a, const void *ptr)
Definition memory_arena.c:329
gboolean dt_cache_arena_calc(const dt_cache_arena_t *a, size_t size, uint32_t *out_pages, size_t *out_size)
Definition memory_arena.c:39
int dt_cache_arena_init(dt_cache_arena_t *a, size_t total_size)
Definition memory_arena.c:268
void dt_cache_arena_free(dt_cache_arena_t *a, void *ptr, size_t size)
Definition memory_arena.c:121
void * dt_cache_arena_alloc(dt_cache_arena_t *a, size_t size, size_t *out_size)
Definition memory_arena.c:66
size_t size
Definition mipmap_cache.c:3
Definition memory_arena.h:35
uint8_t * base
Definition memory_arena.h:36
size_t size
Definition memory_arena.h:37
GArray * free_runs
Definition memory_arena.h:42
size_t page_size
Definition memory_arena.h:39
dt_pthread_mutex_t lock
Definition memory_arena.h:44
uint32_t num_pages
Definition memory_arena.h:40