summaryrefslogtreecommitdiff
path: root/src/hos_hash.cpp
blob: bf54005db692ffdf37f68cdb45bce57f7888d3af (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
/*************************************************************************
    > File Name: uthash.cpp
    > Author: pxz
    > Created Time: Fri 18 Sep 2020 04:26:09 PM CST
 ************************************************************************/
#include "hos_hash.h"

void add_hos_info(hos_info_t **handle, hos_info_t *input)
{
    hos_info_t *value = NULL;
    HASH_FIND_INT(*handle, (int *)&input->fd, value);
    if (value == NULL)
    {
        value = (hos_info_t *)malloc(sizeof(hos_info_t));
        memcpy(value, input, sizeof(hos_info_t));
        value->object = (char *)malloc(strlen(input->object) + 1);
        value->bucket = (char *)malloc(strlen(input->bucket) + 1);
        memcpy(value->bucket, input->bucket, strlen(input->bucket) + 1);
        memcpy(value->object, input->object, strlen(input->object) + 1);
        HASH_ADD_INT(*handle, fd, value);
    }
    else
    {
        value->mode = input->mode;
        value->handle = input->handle;
        memcpy(value->bucket, input->bucket, strlen(input->bucket) + 1);
        memcpy(value->object, input->object, strlen(input->object) + 1);
        value->callback = input->callback;
        value->userdata = input->userdata;
        value->cache = input->cache;
        value->cache_count = input->cache_count;
        value->cache_rest = input->cache_rest;
        value->position = input->position;
        value->recive_cnt = input->recive_cnt;
        value->fd_status = value->fd_status;
        value->overtime = value->overtime;
        value->timeout = value->timeout;
    }
}

hos_info_t *find_info_by_fd(hos_info_t *handle, size_t fd)
{
    hos_info_t *value = NULL;
    HASH_FIND_INT(handle, &fd, value);
    return value;
}

void delete_info_by_fd(hos_info_t **handle, size_t fd)
{
    hos_info_t *value = NULL;

    HASH_FIND_INT(*handle, &fd, value);
    if (value)
    {
        if (value->bucket)
        {
            free(value->bucket);
        }
        if (value->object)
        {
            free(value->object);
        }
        HASH_DEL(*handle, value);
        free(value);
    }
}

void delete_all(hos_info_t **handle)
{
    hos_info_t *current, *tmp;
    HASH_ITER(hh, *handle, current, tmp)
    {
        if (current->bucket)
        {
            free(current->bucket);
        }
        if (current->object)
        {
            free(current->object);
        }
        HASH_DEL(*handle, current);
        free(current);
    }
}