summaryrefslogtreecommitdiff
path: root/lib/lvgl/src/misc/cache/lv_cache_entry.c
blob: 242e1a0f261170d73914e73402d6603288ce1546 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* @file lv_cache_entry.c
*
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_cache_entry.h"
#include "../../stdlib/lv_sprintf.h"
#include "../lv_assert.h"
#include "lv_cache.h"
#include "lv_cache_entry_private.h"
#include "lv_cache_private.h"
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/
struct _lv_cache_entry_t {
    const lv_cache_t * cache;
    int32_t ref_cnt;
    uint32_t node_size;

    bool is_invalid;
};
/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  GLOBAL VARIABLES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_cache_entry_reset_ref(lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);
    entry->ref_cnt = 0;
}
void lv_cache_entry_inc_ref(lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);
    entry->ref_cnt++;
}
void lv_cache_entry_dec_ref(lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);
    entry->ref_cnt--;
    if(entry->ref_cnt < 0) {
        LV_LOG_WARN("ref_cnt(%" LV_PRIu32 ") < 0", entry->ref_cnt);
        entry->ref_cnt = 0;
    }
}
int32_t lv_cache_entry_get_ref(lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);
    return entry->ref_cnt;
}
uint32_t lv_cache_entry_get_node_size(lv_cache_entry_t * entry)
{
    return entry->node_size;
}
void lv_cache_entry_set_node_size(lv_cache_entry_t * entry, uint32_t node_size)
{
    LV_ASSERT_NULL(entry);
    entry->node_size = node_size;
}
void lv_cache_entry_set_invalid(lv_cache_entry_t * entry, bool is_invalid)
{
    LV_ASSERT_NULL(entry);
    entry->is_invalid = is_invalid;
}
bool lv_cache_entry_is_invalid(lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);
    return entry->is_invalid;
}
void * lv_cache_entry_get_data(lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);
    return (uint8_t *)entry - entry->node_size;
}
void * lv_cache_entry_acquire_data(lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);

    lv_cache_entry_inc_ref(entry);
    return lv_cache_entry_get_data(entry);
}
void lv_cache_entry_release_data(lv_cache_entry_t * entry, void * user_data)
{
    LV_UNUSED(user_data);

    LV_ASSERT_NULL(entry);
    if(lv_cache_entry_get_ref(entry) == 0) {
        LV_LOG_ERROR("ref_cnt(%" LV_PRIu32 ") == 0", entry->ref_cnt);
        return;
    }

    lv_cache_entry_dec_ref(entry);
}
lv_cache_entry_t * lv_cache_entry_get_entry(void * data, const uint32_t node_size)
{
    LV_ASSERT_NULL(data);
    return (lv_cache_entry_t *)((uint8_t *)data + node_size);
}
void lv_cache_entry_set_cache(lv_cache_entry_t * entry, const lv_cache_t * cache)
{
    LV_ASSERT_NULL(entry);
    entry->cache = cache;
}
const lv_cache_t * lv_cache_entry_get_cache(const lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);
    return entry->cache;
}

uint32_t lv_cache_entry_get_size(const uint32_t node_size)
{
    return node_size + sizeof(lv_cache_entry_t);
}
lv_cache_entry_t * lv_cache_entry_alloc(const uint32_t node_size, const lv_cache_t * cache)
{
    void * res = lv_malloc_zeroed(lv_cache_entry_get_size(node_size));
    LV_ASSERT_MALLOC(res)
    if(res == NULL) {
        LV_LOG_ERROR("malloc failed");
        return NULL;
    }
    lv_cache_entry_t * entry = (lv_cache_entry_t *)res;
    lv_cache_entry_init(entry, cache, node_size);
    return (lv_cache_entry_t *)((uint8_t *)entry + node_size);
}
void lv_cache_entry_init(lv_cache_entry_t * entry, const lv_cache_t * cache, const uint32_t node_size)
{
    LV_ASSERT_NULL(entry);
    LV_ASSERT_NULL(cache);

    entry->cache = cache;
    entry->node_size = node_size;
    entry->ref_cnt = 0;
    entry->is_invalid = false;
}
void lv_cache_entry_delete(lv_cache_entry_t * entry)
{
    LV_ASSERT_NULL(entry);

    void * data = lv_cache_entry_get_data(entry);
    lv_free(data);
}
/**********************
 *   STATIC FUNCTIONS
 **********************/