Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
common/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
11 darktable is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 darktable is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with darktable. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#pragma once
26
27#include "common/dtpthread.h"
28#include <glib.h>
29#include <inttypes.h>
30#include <stddef.h>
31
32typedef struct dt_cache_entry_t
33{
34 void *data;
35 size_t data_size;
36 size_t cost;
37 GList *link;
40 uint32_t key;
41}
43
44typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry));
45typedef void((*dt_cache_cleanup_t)(void *userdata, dt_cache_entry_t *entry));
46
47typedef struct dt_cache_t
48{
49 dt_pthread_mutex_t lock; // big fat lock. we're only expecting a couple hand full of cpu threads to use this concurrently.
50
51 size_t entry_size; // cache line allocation
52 size_t cost; // user supplied cost per cache line (bytes?)
53 size_t cost_quota; // quota to try and meet. but don't use as hard limit.
54
55 GHashTable *hashtable; // stores (key, entry) pairs
56 GList *lru; // last element is most recently used, first is about to be kicked from cache.
57
58 // callback functions for cache misses/garbage collection
59 dt_cache_allocate_t allocate;
60 dt_cache_allocate_t cleanup;
63}
65
66// entry size is only used if alloc callback is 0
67void dt_cache_init(dt_cache_t *cache, size_t entry_size, size_t cost_quota);
68void dt_cache_cleanup(dt_cache_t *cache);
69
70static inline void dt_cache_set_allocate_callback(dt_cache_t *cache, dt_cache_allocate_t allocate_cb,
71 void *allocate_data)
72{
73 cache->allocate = allocate_cb;
74 cache->allocate_data = allocate_data;
75}
76static inline void dt_cache_set_cleanup_callback(dt_cache_t *cache, dt_cache_cleanup_t cleanup_cb,
77 void *cleanup_data)
78{
79 cache->cleanup = cleanup_cb;
80 cache->cleanup_data = cleanup_data;
81}
82
83// returns a slot in the cache for this key (newly allocated if need be), locked according to mode (r, w)
84#define dt_cache_get(A, B, C) dt_cache_get_with_caller(A, B, C, __FILE__, __LINE__)
85dt_cache_entry_t *dt_cache_get_with_caller(dt_cache_t *cache, const uint32_t key, char mode, const char *file, int line);
86// same but returns 0 if not allocated yet (both will block and wait for entry rw locks to be released)
87dt_cache_entry_t *dt_cache_testget(dt_cache_t *cache, const uint32_t key, char mode);
88// release a lock on a cache entry. the cache knows which one you mean (r or w).
89#define dt_cache_release(A, B) dt_cache_release_with_caller(A, B, __FILE__, __LINE__)
90void dt_cache_release_with_caller(dt_cache_t *cache, dt_cache_entry_t *entry, const char *file, int line);
91
92// 0: not contained
93int32_t dt_cache_contains(dt_cache_t *cache, const uint32_t key);
94// returns 0 on success, 1 if the key was not found.
95int32_t dt_cache_remove(dt_cache_t *cache, const uint32_t key);
96// seed a cache entry without running allocate callback. returns 0 on insert, 1 if already present, -1 on failure.
97int dt_cache_seed(dt_cache_t *cache, const uint32_t key, const void *data, size_t data_size, size_t cost,
98 gboolean aligned_alloc);
99// removes from the tip of the lru list, until the fill ratio of the hashtable
100// goes below the given parameter, in terms of the user defined cost measure.
101// will never lock and never fail, but sometimes not free memory (in case all
102// is locked)
103void dt_cache_gc(dt_cache_t *cache, const float fill_ratio);
104
105// iterate over all currently contained data blocks.
106// not thread safe! only use this for init/cleanup!
107// returns non zero the first time process() returns non zero.
109 int (*process)(const uint32_t key, const void *data, void *user_data),
110 void *user_data);
111
112// clang-format off
113// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
114// vim: shiftwidth=2 expandtab tabstop=2 cindent
115// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
116// clang-format on
int process(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, const void *const ivoid, void *const ovoid, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
Definition ashift.c:3119
int32_t dt_cache_remove(dt_cache_t *cache, const uint32_t key)
Definition common/cache.c:271
int32_t dt_cache_contains(dt_cache_t *cache, const uint32_t key)
Definition common/cache.c:89
int dt_cache_seed(dt_cache_t *cache, const uint32_t key, const void *data, size_t data_size, size_t cost, gboolean aligned_alloc)
Definition common/cache.c:413
static void dt_cache_set_cleanup_callback(dt_cache_t *cache, dt_cache_cleanup_t cleanup_cb, void *cleanup_data)
Definition common/cache.h:76
void dt_cache_gc(dt_cache_t *cache, const float fill_ratio)
Definition common/cache.c:334
void dt_cache_release_with_caller(dt_cache_t *cache, dt_cache_entry_t *entry, const char *file, int line)
Definition common/cache.c:378
dt_cache_entry_t * dt_cache_testget(dt_cache_t *cache, const uint32_t key, char mode)
Definition common/cache.c:123
void dt_cache_init(dt_cache_t *cache, size_t entry_size, size_t cost_quota)
Definition common/cache.c:44
dt_cache_entry_t * dt_cache_get_with_caller(dt_cache_t *cache, const uint32_t key, char mode, const char *file, int line)
Definition common/cache.c:164
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)
Definition common/cache.h:70
int dt_cache_for_all(dt_cache_t *cache, int(*process)(const uint32_t key, const void *data, void *user_data), void *user_data)
Definition common/cache.c:97
void dt_cache_cleanup(dt_cache_t *cache)
Definition common/cache.c:61
char * key
Definition common/metadata.c:60
#define dt_pthread_rwlock_t
Definition dtpthread.h:389
Definition common/cache.h:33
uint32_t key
Definition common/cache.h:40
GList * link
Definition common/cache.h:37
size_t data_size
Definition common/cache.h:35
dt_pthread_rwlock_t lock
Definition common/cache.h:38
int _lock_demoting
Definition common/cache.h:39
void * data
Definition common/cache.h:34
size_t cost
Definition common/cache.h:36
Definition common/cache.h:48
size_t cost
Definition common/cache.h:52
GList * lru
Definition common/cache.h:56
size_t cost_quota
Definition common/cache.h:53
void * allocate_data
Definition common/cache.h:61
GHashTable * hashtable
Definition common/cache.h:55
dt_cache_allocate_t allocate
Definition common/cache.h:59
dt_pthread_mutex_t lock
Definition common/cache.h:49
size_t entry_size
Definition common/cache.h:51
dt_cache_allocate_t cleanup
Definition common/cache.h:60
void * cleanup_data
Definition common/cache.h:62