Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
cache.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2011-2020 darktable developers.
4
5 darktable 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 darktable 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 <inttypes.h>
24#include <stddef.h>
25
26typedef struct dt_cache_entry_t
27{
28 void *data;
29 size_t data_size;
30 size_t cost;
31 GList *link;
34 uint32_t key;
35}
37
38typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry));
39typedef void((*dt_cache_cleanup_t)(void *userdata, dt_cache_entry_t *entry));
40
41typedef struct dt_cache_t
42{
43 dt_pthread_mutex_t lock; // big fat lock. we're only expecting a couple hand full of cpu threads to use this concurrently.
44
45 size_t entry_size; // cache line allocation
46 size_t cost; // user supplied cost per cache line (bytes?)
47 size_t cost_quota; // quota to try and meet. but don't use as hard limit.
48
49 GHashTable *hashtable; // stores (key, entry) pairs
50 GList *lru; // last element is most recently used, first is about to be kicked from cache.
51
52 // callback functions for cache misses/garbage collection
53 dt_cache_allocate_t allocate;
54 dt_cache_allocate_t cleanup;
57}
59
60// entry size is only used if alloc callback is 0
61void dt_cache_init(dt_cache_t *cache, size_t entry_size, size_t cost_quota);
62void dt_cache_cleanup(dt_cache_t *cache);
63
64static inline void dt_cache_set_allocate_callback(dt_cache_t *cache, dt_cache_allocate_t allocate_cb,
65 void *allocate_data)
66{
67 cache->allocate = allocate_cb;
68 cache->allocate_data = allocate_data;
69}
70static inline void dt_cache_set_cleanup_callback(dt_cache_t *cache, dt_cache_cleanup_t cleanup_cb,
71 void *cleanup_data)
72{
73 cache->cleanup = cleanup_cb;
74 cache->cleanup_data = cleanup_data;
75}
76
77// returns a slot in the cache for this key (newly allocated if need be), locked according to mode (r, w)
78#define dt_cache_get(A, B, C) dt_cache_get_with_caller(A, B, C, __FILE__, __LINE__)
79dt_cache_entry_t *dt_cache_get_with_caller(dt_cache_t *cache, const uint32_t key, char mode, const char *file, int line);
80// same but returns 0 if not allocated yet (both will block and wait for entry rw locks to be released)
81dt_cache_entry_t *dt_cache_testget(dt_cache_t *cache, const uint32_t key, char mode);
82// release a lock on a cache entry. the cache knows which one you mean (r or w).
83#define dt_cache_release(A, B) dt_cache_release_with_caller(A, B, __FILE__, __LINE__)
84void dt_cache_release_with_caller(dt_cache_t *cache, dt_cache_entry_t *entry, const char *file, int line);
85
86// 0: not contained
87int32_t dt_cache_contains(dt_cache_t *cache, const uint32_t key);
88// returns 0 on success, 1 if the key was not found.
89int32_t dt_cache_remove(dt_cache_t *cache, const uint32_t key);
90// removes from the tip of the lru list, until the fill ratio of the hashtable
91// goes below the given parameter, in terms of the user defined cost measure.
92// will never lock and never fail, but sometimes not free memory (in case all
93// is locked)
94void dt_cache_gc(dt_cache_t *cache, const float fill_ratio);
95
96// iterate over all currently contained data blocks.
97// not thread safe! only use this for init/cleanup!
98// returns non zero the first time process() returns non zero.
100 int (*process)(const uint32_t key, const void *data, void *user_data),
101 void *user_data);
102
103// clang-format off
104// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
105// vim: shiftwidth=2 expandtab tabstop=2 cindent
106// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
107// clang-format on
108
void 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:3088
int32_t dt_cache_remove(dt_cache_t *cache, const uint32_t key)
Definition common/cache.c:255
int32_t dt_cache_contains(dt_cache_t *cache, const uint32_t key)
Definition common/cache.c:73
static void dt_cache_set_cleanup_callback(dt_cache_t *cache, dt_cache_cleanup_t cleanup_cb, void *cleanup_data)
Definition cache.h:70
void dt_cache_gc(dt_cache_t *cache, const float fill_ratio)
Definition common/cache.c:315
void dt_cache_release_with_caller(dt_cache_t *cache, dt_cache_entry_t *entry, const char *file, int line)
Definition common/cache.c:356
dt_cache_entry_t * dt_cache_testget(dt_cache_t *cache, const uint32_t key, char mode)
Definition common/cache.c:107
void dt_cache_init(dt_cache_t *cache, size_t entry_size, size_t cost_quota)
Definition common/cache.c:32
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:148
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 cache.h:64
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:81
void dt_cache_cleanup(dt_cache_t *cache)
Definition common/cache.c:49
char * key
Definition common/metadata.c:40
#define dt_pthread_rwlock_t
Definition dtpthread.h:336
Definition cache.h:27
uint32_t key
Definition cache.h:34
GList * link
Definition cache.h:31
size_t data_size
Definition cache.h:29
dt_pthread_rwlock_t lock
Definition cache.h:32
int _lock_demoting
Definition cache.h:33
void * data
Definition cache.h:28
size_t cost
Definition cache.h:30
Definition cache.h:42
size_t cost
Definition cache.h:46
GList * lru
Definition cache.h:50
size_t cost_quota
Definition cache.h:47
void * allocate_data
Definition cache.h:55
GHashTable * hashtable
Definition cache.h:49
dt_cache_allocate_t allocate
Definition cache.h:53
dt_pthread_mutex_t lock
Definition cache.h:43
size_t entry_size
Definition cache.h:45
dt_cache_allocate_t cleanup
Definition cache.h:54
void * cleanup_data
Definition cache.h:56