• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CRPC_HASH_TABLE_H
17 #define CRPC_HASH_TABLE_H
18 
19 #include "context.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /*
26  * HashTable
27  * _______________
28  * |             |
29  * |   ListNode  |----> ListNode ----> ListNode
30  * |_____________|
31  * |             |
32  * |   ListNode  |----> ListNode ----> ListNode
33  * |_____________|
34  * |             |
35  * |   ListNode  |----> ListNode ----> ListNode
36  * |_____________|
37  * |             |
38  * |   ListNode  |----> ListNode ----> ListNode
39  * |_____________|
40  */
41 
42 typedef struct ListNode ListNode;
43 struct ListNode {
44     struct Context *context;
45     struct ListNode *next;
46 };
47 
48 typedef struct HashTable HashTable;
49 struct HashTable {
50     unsigned int slots;
51     int size;
52     ListNode **list;
53 };
54 
55 /**
56  * @Description Create a hash table
57  *
58  * @param slots - Number of buckets
59  * @return HashTable* - pointer to the hash table or NULL if failed
60  */
61 HashTable *InitHashTable(int slots);
62 
63 /**
64  * @Description Destroy HashTable
65  *
66  * @param p - HashTable object's pointer
67  */
68 void DestroyHashTable(HashTable *p);
69 
70 /**
71  * @Description Find Context object by fd
72  *
73  * @param p - HashTable object's pointer
74  * @param fd - Socket fd
75  * @return Context* - return the context pointer
76  */
77 Context *FindContext(HashTable *p, int fd);
78 
79 /**
80  * @Description Insert a Context to the HashTable
81  *
82  * @param p - HashTable object's pointer
83  * @param context - Context to be added
84  * @return int - 0 Success, -1 Failed, -2 Exist
85  */
86 int InsertHashTable(HashTable *p, Context *context);
87 
88 /**
89  * @Description Delete a context from the HashTable
90  *
91  * @param p - HashTable object's pointer
92  * @param context - Context to be deleted
93  */
94 void DeleteHashTable(HashTable *p, const Context *context);
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 #endif
100