-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_list.c
169 lines (143 loc) · 3.24 KB
/
key_list.c
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
168
169
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include "key_list.h"
KEY_LIST_PTR create_new_key_list(int capacity, pthread_mutex_t *lock) {
KEY_LIST_PTR p = (KEY_LIST_PTR)malloc(sizeof(KEY_LIST));
if (p == NULL) {
printf("Memory allocation for key_list failed!\n");
return NULL;
}
p->key_gen = 1;
p->capacity = capacity;
p->size = 0;
p->head = NULL;
p->lock = lock;
return p;
}
void destroy_key_list(KEY_LIST_PTR q) {
KEY_LIST_NODE_PTR p = q->head;
while (p != NULL) {
KEY_LIST_NODE_PTR r = p;
p = p->next;
if (r->data != NULL) {
free(r->data);
}
free(r);
}
free(q);
}
void process_key_list(KEY_LIST_PTR q, bool (*proc)(void *data)) {
KEY_LIST_NODE_PTR p = q->head;
while (p != NULL) {
if (proc(p->data)) {
break;
}
p = p->next;
}
}
KEY_LIST_NODE_PTR find_key_list_node(KEY_LIST_PTR q, int key) {
if (q == NULL) {
printf("Key_list isn\'t allocated!\n");
return NULL;
}
KEY_LIST_NODE_PTR p = q->head;
while (p != NULL) {
if (p->key == key) {
return p;
}
p = p->next;
}
return NULL;
}
int add_key_list_node(KEY_LIST_PTR q, void *data) {
if (q == NULL) {
printf("Key_list isn\'t allocated!\n");
return -1;
}
KEY_LIST_NODE_PTR n = (KEY_LIST_NODE_PTR)malloc(sizeof(KEY_LIST_NODE));
if (n == NULL) {
printf("Memory allocation for key_list_node failed!\n");
return -2;
}
n->key = get_next_key(q);
n->data = data;
n->next = NULL;
if (q->head == NULL) {
q->head = n;
} else {
n->next = q->head;
q->head = n;
}
q->size++;
return n->key;
}
void *remove_key_list_node(KEY_LIST_PTR q, int key) {
void *data = NULL;
if (q->head == NULL) {
return NULL;
}
pthread_mutex_lock(q->lock);
KEY_LIST_NODE_PTR r = NULL, p = q->head;
while (p != NULL) {
if (p->key == key) {
if (r != NULL) {
r->next = p->next;
}
break;
}
r = p;
p = p->next;
}
if (p != NULL) {
q->size--;
data = p->data;
free(p);
}
if (q->size == 0) {
q->head = NULL;
}
pthread_mutex_unlock(q->lock);
return data;
}
int get_key_list_size(KEY_LIST_PTR q) {
int n = 0;
if (q == NULL) {
return 0;
}
pthread_mutex_lock(q->lock);
n = q->size;
pthread_mutex_unlock(q->lock);
return n;
}
bool set_key_list_size(KEY_LIST_PTR q, int new_size) {
if (q == NULL) {
return false;
}
pthread_mutex_lock(q->lock);
q->size = new_size;
pthread_mutex_unlock(q->lock);
return true;
}
bool is_key_list_full(KEY_LIST_PTR q) {
bool r;
if (q == NULL) {
return true;
}
pthread_mutex_lock(q->lock);
r = q->capacity <= q->size;
pthread_mutex_unlock(q->lock);
return r;
}
int get_next_key(KEY_LIST_PTR q) {
int n;
pthread_mutex_lock(q->lock);
if (q->key_gen > INT_MAX - 10) {
q->key_gen = 1;
}
n = q->key_gen;
q->key_gen++;
pthread_mutex_unlock(q->lock);
return n;
}