Skip to content

Commit

Permalink
mipmap: store cost in size_t
Browse files Browse the repository at this point in the history
E.g. in scratchmem_allocate(void *data, const uint32_t key, size_t *cost, void **buf):

dt_mipmap_cache_one_t *c = (dt_mipmap_cache_one_t *)data;
*cost = c->buffer_size;

Where c->buffer_size is size_t already.
  • Loading branch information
LebedevRI committed Sep 16, 2014
1 parent 515cef1 commit 70626dd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions src/common/cache.c
Expand Up @@ -48,7 +48,7 @@ typedef struct dt_cache_bucket_t
int16_t write; // number of writers (0 or 1)
int32_t lru; // for garbage collection: lru list
int32_t mru;
int32_t cost; // cost associated with this entry (such as byte size)
size_t cost; // cost associated with this entry (such as byte size)
uint32_t hash; // hash of the element
uint32_t key; // key of the element
// due to alignment, we waste 32 bits here
Expand Down Expand Up @@ -120,7 +120,7 @@ get_start_cacheline_bucket(const dt_cache_t *const cache, dt_cache_bucket_t *con

static void
add_cost(dt_cache_t *cache,
const int32_t cost)
const size_t cost)
{
__sync_fetch_and_add(&cache->cost, cost);
}
Expand Down Expand Up @@ -223,7 +223,7 @@ add_key_to_beginning_of_list(
const uint32_t hash,
const uint32_t key)
{
int32_t cost = 1;
size_t cost = 1;
if(cache->allocate)
{
// upgrade to a write lock in case the user requests it:
Expand Down Expand Up @@ -263,7 +263,7 @@ add_key_to_end_of_list(
const uint32_t key,
dt_cache_bucket_t *const last_bucket)
{
int32_t cost = 1;
size_t cost = 1;
if(cache->allocate)
{
if(cache->allocate(cache->allocate_data, key, &cost, &free_bucket->data))
Expand Down Expand Up @@ -1096,7 +1096,7 @@ dt_cache_write_get(dt_cache_t *cache, const uint32_t key)
}

void
dt_cache_realloc(dt_cache_t *cache, const uint32_t key, const int32_t cost, void *data)
dt_cache_realloc(dt_cache_t *cache, const uint32_t key, const size_t cost, void *data)
{
// just to support different keys:
const uint32_t hash = key;
Expand All @@ -1118,7 +1118,7 @@ dt_cache_realloc(dt_cache_t *cache, const uint32_t key, const int32_t cost, void
assert(compare_bucket->write == 1);
assert(compare_bucket->read == 1);
compare_bucket->data = data;
const int32_t cost_diff = cost - compare_bucket->cost;
const size_t cost_diff = cost - compare_bucket->cost;
compare_bucket->cost = cost;
add_cost(cache, cost_diff);
dt_cache_unlock(&segment->lock);
Expand Down
6 changes: 3 additions & 3 deletions src/common/cache.h
Expand Up @@ -45,7 +45,7 @@ typedef struct dt_cache_t
// allocate should return != 0 if a write lock on alloc is needed.
// this might be useful for cases where the allocation takes a lot of time and you don't want
// the hashtable spinlocks to block and wait for it.
int32_t (*allocate)(void *userdata, const uint32_t key, int32_t *cost, void **payload);
int32_t (*allocate)(void *userdata, const uint32_t key, size_t *cost, void **payload);
void (*cleanup) (void *userdata, const uint32_t key, void *payload);
void *allocate_data;
void *cleanup_data;
Expand All @@ -64,7 +64,7 @@ void dt_cache_static_allocation(dt_cache_t *cache, uint8_t *buf, const uint32_t
static inline void
dt_cache_set_allocate_callback(
dt_cache_t *cache,
int32_t (*allocate)(void*, const uint32_t, int32_t*, void**),
int32_t (*allocate)(void*, const uint32_t, size_t*, void**),
void *allocate_data)
{
cache->allocate = allocate;
Expand Down Expand Up @@ -115,7 +115,7 @@ void dt_cache_print(dt_cache_t *cache);
void dt_cache_print_locked(dt_cache_t *cache);

// replace data pointer, cleanup has to be done by the user.
void dt_cache_realloc(dt_cache_t *cache, const uint32_t key, const int32_t cost, void *data);
void dt_cache_realloc(dt_cache_t *cache, const uint32_t key, const size_t cost, void *data);

// iterate over all currently contained data blocks.
// not thread safe! only use this for init/cleanup!
Expand Down
2 changes: 1 addition & 1 deletion src/common/image_cache.c
Expand Up @@ -27,7 +27,7 @@
#include <sqlite3.h>

int32_t
dt_image_cache_allocate(void *data, const uint32_t key, int32_t *cost, void **buf)
dt_image_cache_allocate(void *data, const uint32_t key, size_t *cost, void **buf)
{
dt_image_cache_t *c = (dt_image_cache_t *)data;
const uint32_t hash = key; // == image id
Expand Down
6 changes: 3 additions & 3 deletions src/common/mipmap_cache.c
Expand Up @@ -485,7 +485,7 @@ static void _init_f(float *buf, uint32_t *width, uint32_t *height, const uint3
static void _init_8(uint8_t *buf, uint32_t *width, uint32_t *height, const uint32_t imgid, const dt_mipmap_size_t size);

static int32_t
scratchmem_allocate(void *data, const uint32_t key, int32_t *cost, void **buf)
scratchmem_allocate(void *data, const uint32_t key, size_t *cost, void **buf)
{
dt_mipmap_cache_one_t *c = (dt_mipmap_cache_one_t *)data;
// slot is exactly aligned with encapsulated cache's position and already allocated
Expand All @@ -494,7 +494,7 @@ scratchmem_allocate(void *data, const uint32_t key, int32_t *cost, void **buf)
}

int32_t
dt_mipmap_cache_allocate(void *data, const uint32_t key, int32_t *cost, void **buf)
dt_mipmap_cache_allocate(void *data, const uint32_t key, size_t *cost, void **buf)
{
dt_mipmap_cache_one_t *c = (dt_mipmap_cache_one_t *)data;
// slot is exactly aligned with encapsulated cache's position and already allocated
Expand Down Expand Up @@ -565,7 +565,7 @@ dt_mipmap_cache_alloc(dt_image_t *img, dt_mipmap_size_t size, dt_mipmap_cache_al

// callback for the cache backend to initialize payload pointers
int32_t
dt_mipmap_cache_allocate_dynamic(void *data, const uint32_t key, int32_t *cost, void **buf)
dt_mipmap_cache_allocate_dynamic(void *data, const uint32_t key, size_t *cost, void **buf)
{
dt_mipmap_cache_one_t *cache = (dt_mipmap_cache_one_t *)data;
// for full image buffers
Expand Down

0 comments on commit 70626dd

Please sign in to comment.