This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
pxz-hos-client-cpp-module/src/hos_hash.cpp

85 lines
2.4 KiB
C++
Raw Normal View History

2020-09-21 19:19:18 +08:00
/*************************************************************************
> 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, &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);
2020-09-21 19:19:18 +08:00
HASH_ADD_INT(*handle, fd, value);
}
else
{
2020-09-23 19:06:09 +08:00
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);
2020-09-21 19:19:18 +08:00
value->callback = input->callback;
value->userdata = input->userdata;
value->cache = input->cache;
2020-12-01 16:12:41 +08:00
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;
2020-09-21 19:19:18 +08:00
}
}
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;
}
2020-09-23 19:06:09 +08:00
void delete_info_by_fd(hos_info_t **handle, size_t fd)
2020-09-21 19:19:18 +08:00
{
hos_info_t *value = NULL;
2020-09-23 19:06:09 +08:00
HASH_FIND_INT(*handle, &fd, value);
2020-09-21 19:19:18 +08:00
if (value)
{
if (value->bucket)
{
free(value->bucket);
}
if (value->object)
{
free(value->object);
}
2020-09-23 19:06:09 +08:00
HASH_DEL(*handle, value);
2020-09-21 19:19:18 +08:00
free(value);
}
}
2020-09-23 19:06:09 +08:00
void delete_all(hos_info_t **handle)
2020-09-21 19:19:18 +08:00
{
hos_info_t *current, *tmp;
2020-09-23 19:06:09 +08:00
HASH_ITER(hh, *handle, current, tmp)
2020-09-21 19:19:18 +08:00
{
if (current->bucket)
{
free(current->bucket);
}
if (current->object)
{
free(current->object);
}
2020-09-23 19:06:09 +08:00
HASH_DEL(*handle, current);
free(current);
2020-09-21 19:19:18 +08:00
}
}