Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
caches/cache.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2011-2014 johannes hanika.
4 Copyright (C) 2012 Richard Wonka.
5 Copyright (C) 2012, 2014, 2016 Tobias Ellinghaus.
6 Copyright (C) 2013 Simon Spannagel.
7 Copyright (C) 2014-2016 Roman Lebedev.
8 Copyright (C) 2020 Pascal Obry.
9 Copyright (C) 2022 Martin Bařinka.
10 Copyright (C) 2026 Aurélien PIERRE.
11 Copyright (C) 2026 Guillaume Stutin.
12
13 darktable is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 darktable is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with darktable. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#ifndef DT_CACHES_CACHE_H
28#define DT_CACHES_CACHE_H
29
30#include "system/dtpthread.h"
31#include <glib.h>
32#include <inttypes.h>
33#include <stddef.h>
34
35typedef struct dt_cache_entry_t
36{
37 void *data;
38 size_t data_size;
39 size_t cost;
40 GList *link;
41 dt_pthread_rwlock_t lock;
43 uint32_t key;
44}
46
47typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry));
48typedef void((*dt_cache_cleanup_t)(void *userdata, dt_cache_entry_t *entry));
49
64
67
68typedef struct dt_cache_t
69{
70 dt_pthread_mutex_t lock; // big fat lock. we're only expecting a couple hand full of cpu threads to use this concurrently.
71
72 size_t entry_size; // cache line allocation
73 size_t cost; // user supplied cost per cache line (bytes?)
74 size_t cost_quota; // quota to try and meet. but don't use as hard limit.
75
76 GHashTable *hashtable; // stores (key, entry) pairs
77 GList *lru; // last element is most recently used, first is about to be kicked from cache.
78
79 // callback functions for cache misses/garbage collection
80 dt_cache_allocate_t allocate;
81 dt_cache_allocate_t cleanup;
84}
86
87// entry size is only used if alloc callback is 0
88void dt_cache_init(dt_cache_t *cache, size_t entry_size, size_t cost_quota);
89void dt_cache_cleanup(dt_cache_t *cache);
90
91static inline void dt_cache_set_allocate_callback(dt_cache_t *cache, dt_cache_allocate_t allocate_cb,
92 void *allocate_data)
93{
94 cache->allocate = allocate_cb;
95 cache->allocate_data = allocate_data;
96}
97static inline void dt_cache_set_cleanup_callback(dt_cache_t *cache, dt_cache_cleanup_t cleanup_cb,
98 void *cleanup_data)
99{
100 cache->cleanup = cleanup_cb;
101 cache->cleanup_data = cleanup_data;
102}
103
104// returns a slot in the cache for this key (newly allocated if need be), locked according to mode (r, w),
105// or NULL when the key was not cached and the new slot's payload could not be allocated
106#define dt_cache_get(A, B, C) dt_cache_get_with_caller(A, B, C, __FILE__, __LINE__)
107dt_cache_entry_t *dt_cache_get_with_caller(dt_cache_t *cache, const uint32_t key, char mode, const char *file, int line);
108// same but returns 0 if not allocated yet (both will block and wait for entry rw locks to be released)
109dt_cache_entry_t *dt_cache_testget(dt_cache_t *cache, const uint32_t key, char mode);
110// release a lock on a cache entry. the cache knows which one you mean (r or w).
111#define dt_cache_release(A, B) dt_cache_release_with_caller(A, B, __FILE__, __LINE__)
112void dt_cache_release_with_caller(dt_cache_t *cache, dt_cache_entry_t *entry, const char *file, int line);
113
114// 0: not contained
115int32_t dt_cache_contains(dt_cache_t *cache, const uint32_t key);
116// returns 0 on success, 1 if the key was not found.
117int32_t dt_cache_remove(dt_cache_t *cache, const uint32_t key);
118// Seed a cache entry without running the allocate callback. Returns 0 on insert, 1 if
119// already present, -1 on failure.
120//
121// The payload is always allocated with dt_alloc_align(), and that is not an implementation
122// detail a caller may vary: an entry whose cache has no cleanup callback is freed by this
123// file with dt_free_align(), in four places, whatever it was allocated with. On Windows
124// those are _aligned_malloc and _aligned_free, so a payload allocated any other way is a
125// corrupted heap on eviction -- and on Linux both sides end at free(), so nothing local
126// ever reproduces it.
127//
128// This used to take an `aligned_alloc` flag choosing between dt_alloc_align() and
129// g_malloc(). Passing FALSE was heap corruption on Windows and always had been; the one
130// caller that did was fixed, and the parameter is gone so the next one cannot.
131int dt_cache_seed(dt_cache_t *cache, const uint32_t key, const void *data, size_t data_size, size_t cost);
132// removes from the tip of the lru list, until the fill ratio of the hashtable
133// goes below the given parameter, in terms of the user defined cost measure.
134// takes cache->lock; skips the entries another thread holds rather than waiting
135// for them, so it may free less than asked. returns the cost reclaimed.
136size_t dt_cache_gc(dt_cache_t *cache, const float fill_ratio);
137
138// iterate over all currently contained data blocks.
139// not thread safe! only use this for init/cleanup!
140// returns non zero the first time process() returns non zero.
142 int (*process)(const uint32_t key, const void *data, void *user_data),
143 void *user_data);
144
145#endif // DT_CACHES_CACHE_H
146
147// clang-format off
148// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
149// vim: shiftwidth=2 expandtab tabstop=2 cindent
150// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
151// clang-format on
__DT_CLONE_TARGETS__ int process(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, const void *const ivoid, void *const ovoid)
Definition ashift.c:3179
int32_t dt_cache_remove(dt_cache_t *cache, const uint32_t key)
int32_t dt_cache_contains(dt_cache_t *cache, const uint32_t key)
void dt_cache_entry_free_detached(dt_cache_entry_t *entry)
Release an entry from dt_cache_entry_new_detached(), and its data. NULL-safe.
static void dt_cache_set_cleanup_callback(dt_cache_t *cache, dt_cache_cleanup_t cleanup_cb, void *cleanup_data)
void dt_cache_release_with_caller(dt_cache_t *cache, dt_cache_entry_t *entry, const char *file, int line)
dt_cache_entry_t * dt_cache_testget(dt_cache_t *cache, const uint32_t key, char mode)
int dt_cache_seed(dt_cache_t *cache, const uint32_t key, const void *data, size_t data_size, size_t cost)
dt_cache_entry_t * dt_cache_entry_new_detached(void)
Allocate a cache entry that belongs to NO cache, for a one-shot decode.
void dt_cache_init(dt_cache_t *cache, size_t entry_size, size_t cost_quota)
dt_cache_entry_t * dt_cache_get_with_caller(dt_cache_t *cache, const uint32_t key, char mode, const char *file, int line)
size_t dt_cache_gc(dt_cache_t *cache, const float fill_ratio)
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
static void dt_cache_set_allocate_callback(dt_cache_t *cache, dt_cache_allocate_t allocate_cb, void *allocate_data)
int dt_cache_for_all(dt_cache_t *cache, int(*process)(const uint32_t key, const void *data, void *user_data), void *user_data)
void dt_cache_cleanup(dt_cache_t *cache)
void * data
Definition geometry.h:111
char * key
uint32_t key
GList * link
size_t data_size
dt_pthread_rwlock_t lock
int _lock_demoting
void * data
size_t cost
size_t cost
GList * lru
size_t cost_quota
void * allocate_data
GHashTable * hashtable
dt_cache_allocate_t allocate
dt_pthread_mutex_t lock
size_t entry_size
dt_cache_allocate_t cleanup
void * cleanup_data