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#ifndef DT_SYSTEM_MEMORY_ARENA_H
20#define DT_SYSTEM_MEMORY_ARENA_H
21
22#include "system/dtpthread.h"
23#include <glib.h>
24#include <stdint.h>
25
26/*
27 * Arena allocator for cache buffers:
28 * - We reserve one big contiguous block of virtual memory (the arena).
29 * - The arena is split into fixed-size pages (page_size).
30 * - free_runs is a sorted list of "free stretches" of pages.
31 * Each run says "from page N, K pages are free".
32 * This avoids many small malloc/free calls: we just carve out page ranges
33 * and put them back into the list when done.
34 */
35typedef struct dt_cache_arena_t
36{
37 uint8_t *base;
38 size_t size;
39
40 size_t page_size;
41 uint32_t num_pages;
42
43 GArray *free_runs; // sorted list of free page runs (start page + length in pages)
44
45 dt_pthread_mutex_t lock;
47
49 size_t size,
50 uint32_t *out_pages,
51 size_t *out_size);
52
54 size_t size,
55 size_t *out_size);
56
58 void *ptr,
59 size_t size);
60
61// Return arena free-space stats (in pages). Thread-safe.
63 uint32_t *out_total_free_pages,
64 uint32_t *out_largest_free_run_pages);
65
66// Hard-release the physical pages backing every currently-free run, so the OS
67// gets them back NOW (not lazily) while the virtual reservation stays intact.
68// Complements the lazy per-free release in dt_cache_arena_free(): call this when
69// the system is under memory pressure and other applications need the RAM.
70// Thread-safe. Returns the number of bytes released.
72
73int dt_cache_arena_init(dt_cache_arena_t *a, size_t total_size);
75
76gboolean dt_cache_arena_ptr_in(const dt_cache_arena_t *a, const void *ptr);
77#endif // DT_SYSTEM_MEMORY_ARENA_H
void dt_cache_arena_stats(dt_cache_arena_t *a, uint32_t *out_total_free_pages, uint32_t *out_largest_free_run_pages)
void dt_cache_arena_cleanup(dt_cache_arena_t *a)
gboolean dt_cache_arena_ptr_in(const dt_cache_arena_t *a, const void *ptr)
gboolean dt_cache_arena_calc(const dt_cache_arena_t *a, size_t size, uint32_t *out_pages, size_t *out_size)
int dt_cache_arena_init(dt_cache_arena_t *a, size_t total_size)
void dt_cache_arena_free(dt_cache_arena_t *a, void *ptr, size_t size)
void * dt_cache_arena_alloc(dt_cache_arena_t *a, size_t size, size_t *out_size)
size_t dt_cache_arena_trim(dt_cache_arena_t *a)
size_t size
Definition mipmap_cache.c:3
dt_pthread_mutex_t lock