image_container.c 11.1 KB
Newer Older
1 2 3
#include <rtgui/image_container.h>

#ifdef RTGUI_IMAGE_CONTAINER
4
typedef unsigned int (*rtgui_hash_func_t)(const void *key);
5
typedef struct _rtgui_hash_table  rtgui_hash_table_t;
6 7
typedef rt_bool_t (*rtgui_equal_func_t)(const void *a, const void *b);
typedef void (*rtgui_user_func_t)(const void *value, const void *data);
8 9 10 11

/*
 *Hash tables
 */
12 13
rtgui_hash_table_t *hash_table_create(rtgui_hash_func_t hash_func, rtgui_equal_func_t key_equal_func);
void hash_table_destroy(rtgui_hash_table_t *hash_table);
14

15 16 17
void *hash_table_find(rtgui_hash_table_t *hash_table, const void *key);
void hash_table_insert(rtgui_hash_table_t *hash_table, const void *key, void *value);
rt_bool_t hash_table_remove(rtgui_hash_table_t *hash_table, const void *key);
18

19 20
void hash_table_foreach(rtgui_hash_table_t *hash_table, rtgui_user_func_t user_func, void *data);
unsigned int hash_table_get_size(rtgui_hash_table_t *hash_table);
21 22 23

/* Hash Functions
 */
24
unsigned int direct_hash(const void *v);
25 26 27 28 29 30 31

#define HASH_TABLE_MIN_SIZE 11
#define HASH_TABLE_MAX_SIZE 6247

typedef struct _gui_hash_node rtgui_hash_node_t;
struct _gui_hash_node
{
32 33 34
    void *key;
    void *value;
    rtgui_hash_node_t *next;
35 36 37 38
};

struct _rtgui_hash_table
{
39 40
    rt_uint16_t size;
    rt_uint16_t nnodes;
41

42 43 44
    rtgui_hash_node_t **nodes;
    rtgui_hash_func_t hash_func;
    rtgui_equal_func_t key_equal_func;
45 46 47 48
};

static const unsigned int primes[] =
{
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
    11,
    19,
    37,
    73,
    109,
    163,
    251,
    367,
    557,
    823,
    1237,
    1861,
    2777,
    4177,
    6247,
    /*
        9371,
        14057,
        21089,
        31627,
        47431,
        71143,
        106721,
        160073,
        240101,
        360163,
        540217,
        810343,
        1215497,
        1823231,
        2734867,
        4102283,
        6153409,
        9230113,
        13845163,
    */
85 86
};

87
static const unsigned int nprimes = sizeof(primes) / sizeof(primes[0]);
88

89 90 91 92 93 94
static void hash_table_resize(rtgui_hash_table_t *hash_table);
static rtgui_hash_node_t **hash_table_find_node(rtgui_hash_table_t *hash_table, const void *key);
static rtgui_hash_node_t *hash_node_create(const void *key, void *value);
static void hash_node_destroy(rtgui_hash_node_t *hash_node);
static void hash_nodes_destroy(rtgui_hash_node_t *hash_node);
static unsigned int primes_closest(unsigned int num);
95 96
static void hash_table_needresize(rtgui_hash_table_t *hash_table);

97
rt_inline unsigned int primes_closest(unsigned int num)
98
{
99
    int i;
100

101 102 103
    for (i = 0; i < nprimes; i++)
        if (primes[i] > num)
            return primes[i];
104

105
    return primes[nprimes - 1];
106 107 108
}

/* directly hash */
109
unsigned int direct_hash(const void *v)
110
{
111
    return (unsigned int)v;
112 113
}

114
rtgui_hash_table_t *hash_table_create(rtgui_hash_func_t hash_func, rtgui_equal_func_t key_equal_func)
115
{
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    rtgui_hash_table_t *hash_table;

    hash_table = (rtgui_hash_table_t *) rtgui_malloc(sizeof(rtgui_hash_table_t));
    if (hash_table != RT_NULL)
    {
        hash_table->size               = HASH_TABLE_MIN_SIZE;
        hash_table->nnodes             = 0;
        hash_table->hash_func          = hash_func ? hash_func : direct_hash;
        hash_table->key_equal_func     = key_equal_func;
        hash_table->nodes              = (rtgui_hash_node_t **)rtgui_malloc(sizeof(rtgui_hash_node_t *) * hash_table->size);
        if (hash_table->nodes == RT_NULL)
        {
            /* no memory yet */
            rtgui_free(hash_table);
            return RT_NULL;
        }

        rt_memset(hash_table->nodes, 0, sizeof(rtgui_hash_node_t *) * hash_table->size);
    }

    return hash_table;
137 138
}

139
void hash_table_destroy(rtgui_hash_table_t *hash_table)
140
{
141
    unsigned int i;
142

143
    RT_ASSERT(hash_table != RT_NULL);
144

145 146
    for (i = 0; i < hash_table->size; i++)
        hash_nodes_destroy(hash_table->nodes[i]);
147

148 149
    rtgui_free(hash_table->nodes);
    rtgui_free(hash_table);
150 151
}

152
static rtgui_hash_node_t **hash_table_find_node(rtgui_hash_table_t *hash_table, const void *key)
153
{
154
    rtgui_hash_node_t **node;
155

156
    node = &hash_table->nodes [(* hash_table->hash_func)(key) % hash_table->size];
157

158 159 160 161 162 163
    if (hash_table->key_equal_func)
        while (*node && !(*hash_table->key_equal_func)((*node)->key, key))
            node = &(*node)->next;
    else
        while (*node && (*node)->key != key)
            node = &(*node)->next;
164

165
    return node;
166 167
}

168
void *hash_table_find(rtgui_hash_table_t *hash_table, const void *key)
169
{
170
    rtgui_hash_node_t *node;
171

172 173
    RT_ASSERT(hash_table != RT_NULL);
    RT_ASSERT(key != RT_NULL);
174

175
    node = *hash_table_find_node(hash_table, key);
176

177 178
    if (node) return node->value;
    else return RT_NULL;
179 180
}

181
void hash_table_insert(rtgui_hash_table_t *hash_table, const void *key, void *value)
182
{
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    rtgui_hash_node_t **node;

    if (hash_table == RT_NULL)return;

    node = hash_table_find_node(hash_table, key);
    if (*node)
    {
        (*node)->value = value;
    }
    else
    {
        *node = hash_node_create(key, value);
        hash_table->nnodes++;
        hash_table_needresize(hash_table);
    }
198 199
}

200
rt_bool_t hash_table_remove(rtgui_hash_table_t *hash_table, const void  *key)
201
{
202
    rtgui_hash_node_t **node, *dest;
203

204
    if (hash_table == RT_NULL) return RT_FALSE;
205

206 207 208 209 210 211 212
    node = hash_table_find_node(hash_table, key);
    if (*node)
    {
        dest = *node;
        (*node) = dest->next;
        hash_node_destroy(dest);
        hash_table->nnodes--;
213

214
        hash_table_needresize(hash_table);
215

216 217
        return RT_TRUE;
    }
218

219
    return RT_FALSE;
220 221
}

222
void hash_table_foreach(rtgui_hash_table_t *hash_table, rtgui_user_func_t user_func, void *data)
223
{
224 225
    rtgui_hash_node_t *node;
    int i;
226

227 228
    RT_ASSERT(hash_table != RT_NULL);
    RT_ASSERT(user_func != RT_NULL);
229

230 231 232
    for (i = 0; i < hash_table->size; i++)
        for (node = hash_table->nodes[i]; node; node = node->next)
            (* user_func)(node->value, data);
233 234
}

235
unsigned int hash_table_get_size(rtgui_hash_table_t *hash_table)
236
{
237
    if (hash_table == NULL) return 0;
238

239
    return hash_table->nnodes;
240 241 242 243
}

static void hash_table_needresize(rtgui_hash_table_t *hash_table)
{
244 245 246
    if ((hash_table->size >= 3 * hash_table->nnodes && hash_table->size > HASH_TABLE_MIN_SIZE) ||
            (3 * hash_table->size <= hash_table->nnodes && hash_table->size < HASH_TABLE_MAX_SIZE))
        hash_table_resize(hash_table);
247 248
}

249
static void hash_table_resize(rtgui_hash_table_t *hash_table)
250
{
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    rtgui_hash_node_t **new_nodes;
    rtgui_hash_node_t *node;
    rtgui_hash_node_t *next;
    unsigned int hash_val;
    int new_size;
    int i;

    i = primes_closest(hash_table->nnodes);
    new_size = i > HASH_TABLE_MAX_SIZE ? HASH_TABLE_MAX_SIZE : i < HASH_TABLE_MIN_SIZE ? HASH_TABLE_MIN_SIZE : i ;

    new_nodes = (rtgui_hash_node_t **)rtgui_malloc(sizeof(rtgui_hash_node_t *) * new_size);
    if (new_nodes == RT_NULL) return; /* no memory yet */
    rt_memset(new_nodes, 0, sizeof(rtgui_hash_node_t *) * new_size);

    for (i = 0; i < hash_table->size; i++)
    {
        for (node = hash_table->nodes[i]; node; node = next)
        {
            next = node->next;

            hash_val = (* hash_table->hash_func)(node->key) % new_size;

            node->next = new_nodes[hash_val];
            new_nodes[hash_val] = node;
        }
    }

    rtgui_free(hash_table->nodes);
    hash_table->nodes = new_nodes;
    hash_table->size = new_size;
281 282
}

283
static rtgui_hash_node_t *hash_node_create(void *key, void *value)
284
{
285
    rtgui_hash_node_t *hash_node;
286

287 288 289 290 291 292
    hash_node = (rtgui_hash_node_t *) rtgui_malloc(sizeof(rtgui_hash_node_t));
    if (hash_node != RT_NULL)
    {
        /* set value and key */
        hash_node->key = key;
        hash_node->value = value;;
293

294 295
        hash_node->next = RT_NULL;
    }
296

297
    return hash_node;
298 299
}

300
static void hash_node_destroy(rtgui_hash_node_t *hash_node)
301
{
302
    rtgui_free(hash_node);
303 304
}

305
static void hash_nodes_destroy(rtgui_hash_node_t *hash_node)
306
{
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    if (hash_node)
    {
        rtgui_hash_node_t *node = hash_node;
        rtgui_hash_node_t *temp;

        while (node->next)
        {
            node->key = NULL;
            node->value = NULL;

            temp = node;
            node = node->next;
            rtgui_free(temp);
        }

        node->key = NULL;
        node->value = NULL;
        rtgui_free(node);
    }
326 327
}

328
unsigned int string_hash_func(const void *self)
329
{
330 331 332 333 334 335 336 337 338 339 340 341 342 343
    const char *p;
    int h = 0, g;

    for (p = self; *p != '\0'; p += 1)
    {
        h = (h << 4) + *p;
        if ((g = h & 0xf0000000))
        {
            h = h ^ (g >> 24);
            h = h ^ g;
        }
    }

    return h ;
344
}
345
rt_bool_t string_equal_func(const void *a, const void *b)
346
{
347
    const char *str1, *str2;
348

349 350
    str1 = (const char *)a;
    str2 = (const char *)b;
351

352 353
    if (strcmp(str1, str2) == 0) return RT_TRUE;
    return RT_FALSE;
354 355
}

356
static rtgui_hash_table_t *image_hash_table;
357 358 359
static rt_bool_t load_image = RT_FALSE;
void rtgui_system_image_container_init(rt_bool_t load)
{
360 361 362
    /* create image hash table */
    image_hash_table = hash_table_create(string_hash_func, string_equal_func);
    RT_ASSERT(image_hash_table != RT_NULL);
363

364 365
    /* set load type */
    load_image = load;
366 367
}

368
#ifdef RTGUI_USING_DFS_FILERW
369
rtgui_image_item_t *rtgui_image_container_get(const char *filename)
370
{
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    struct rtgui_image_item *item;

    item = hash_table_find(image_hash_table, filename);
    if (item == RT_NULL)
    {
        item = (struct rtgui_image_item *) rtgui_malloc(sizeof(struct rtgui_image_item));
        if (item == RT_NULL) return RT_NULL;

        /* create a image object */
        item->image = rtgui_image_create(filename, load_image);
        if (item->image == RT_NULL)
        {
            rtgui_free(item);
            return RT_NULL; /* create image failed */
        }

        item->refcount = 1;
        item->filename = rt_strdup(filename);
        hash_table_insert(image_hash_table, item->filename, item);
    }
    else
    {
        item->refcount ++; /* increase refcount */
    }

    return item;
397
}
398
#endif
399

400
rtgui_image_item_t *rtgui_image_container_get_memref(const char *type, const rt_uint8_t *memory, rt_uint32_t length)
401
{
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    char filename[32];
    struct rtgui_image_item *item;

    /* create filename for image identification */
    rt_snprintf(filename, sizeof(filename), "0x%08x_%s", memory, type);

    /* search in container */
    item = hash_table_find(image_hash_table, filename);
    if (item == RT_NULL)
    {
        item = (struct rtgui_image_item *) rtgui_malloc(sizeof(struct rtgui_image_item));
        if (item == RT_NULL) return RT_NULL;

        /* create image object */
        item->image = rtgui_image_create_from_mem(type, memory, length, load_image);
        if (item->image == RT_NULL)
        {
            rtgui_free(item);
            return RT_NULL; /* create image failed */
        }

        item->refcount = 1;
        item->filename = rt_strdup(filename);
        hash_table_insert(image_hash_table, item->filename, item);
    }
    else item->refcount ++;

    return item;
430 431
}

432
void rtgui_image_container_put(rtgui_image_item_t *item)
433
{
434 435 436 437 438 439 440 441 442 443 444
    item->refcount --;
    if (item->refcount == 0)
    {
        /* remove item from container */
        hash_table_remove(image_hash_table, item->filename);

        /* destroy image and image item */
        rt_free(item->filename);
        rtgui_image_destroy(item->image);
        rtgui_free(item);
    }
445 446 447
}

#endif