-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheventslots.c
365 lines (317 loc) · 8.61 KB
/
heventslots.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/***************************************************************
* Name: heventslots.c
* Purpose: 实现heventslots接口
* Author: HYH (hyhsystem.cn)
* Created: 2023-07-14
* Copyright: HYH (hyhsystem.cn)
* License: MIT
**************************************************************/
#include "heventslots.h"
#include "hmemoryheap.h"
#include "hdefaults.h"
typedef struct heventslots_slot
{
//用户参数
void *usr;
//槽函数,第一个参数为signal指针(由发射信号时指定),第二个参数为用户参数
void (*slot)(void *,void *);
//槽释放,第一个参数为用户参数
void (*onfree)(void *);
//下一个槽指针
struct heventslots_slot *next;
//槽id
uint32_t id;
} heventslots_slot_t;
struct heventslots
{
//用户参数
void *usr;
hdefaults_api_table_t api;
//槽单向链表指针
heventslots_slot_t *slot_start;
//下一个槽id
uint32_t slot_next_id;
//标志位
struct
{
uint32_t has_internal_heap:1;
};
};
size_t heventslots_with_internal_heap_min_size(void)
{
return sizeof(heventslots_t)+2*sizeof(heventslots_slot_t)+64;//空间足够2个槽。
}
static void *internal_heap_mem_alloc(size_t nbytes,void *usr)
{
return hmemoryheap_pool_malloc((hmemoryheap_pool_t *)usr,nbytes);
}
static void internal_heap_mem_free(void * ptr,void *usr)
{
hmemoryheap_pool_free((hmemoryheap_pool_t *)usr,ptr);
}
heventslots_t * heventslots_with_internal_heap_init(void *usr,void (*mutex_lock)(void *),void (*mutex_unlock)(void *),void *mem,size_t length)
{
if(mem==NULL || length < heventslots_with_internal_heap_min_size())
{
return NULL;
}
hmemoryheap_pool_t *pool=hmemoryheap_pool_format(usr,mutex_lock,mutex_unlock,(uint8_t *)mem,length);
heventslots_t *slots=heventslots_new_with_memmang_and_lock(pool,internal_heap_mem_alloc,internal_heap_mem_free,mutex_lock,mutex_unlock);
if(slots!=NULL)
{
slots->has_internal_heap=1;
}
return slots;
}
heventslots_t * heventslots_new_with_memmang_and_lock(void *usr,void *(*mem_alloc)(size_t,void *),void (*mem_free)(void *,void *),void (*mutex_lock)(void *),void (*mutex_unlock)(void *))
{
struct heventslots *slots=NULL;
if(mem_alloc!=NULL)
{
slots=(struct heventslots *)mem_alloc(sizeof(struct heventslots),usr);
}
else
{
slots=(struct heventslots *)hdefaults_get_api_table()->mem_alloc(sizeof(struct heventslots),usr);
}
if(slots==NULL)
{
return NULL;
}
memset(slots,0,sizeof(struct heventslots));
slots->usr=usr;
slots->api=(*hdefaults_get_api_table());
slots->api.mem_alloc=mem_alloc;
slots->api.mem_free=mem_free;
slots->api.mutex_lock=mutex_lock;
slots->api.mutex_unlock=mutex_unlock;
slots->slot_start=NULL;
slots->slot_next_id=1;
return slots;
}
heventslots_t * heventslots_new_with_memmang(void *usr,void *(*mem_alloc)(size_t,void *),void (*mem_free)(void *,void *))
{
return heventslots_new_with_memmang_and_lock(usr,mem_alloc,mem_free,hdefaults_mutex_lock,hdefaults_mutex_unlock);
}
heventslots_t * heventslots_new_with_lock(void *usr,void (*mutex_lock)(void *),void (*mutex_unlock)(void *))
{
return heventslots_new_with_memmang_and_lock(usr,hdefaults_malloc,hdefaults_free,mutex_lock,mutex_unlock);
}
heventslots_t * heventslots_new(void *usr)
{
return heventslots_new_with_memmang_and_lock(usr,hdefaults_malloc,hdefaults_free,hdefaults_mutex_lock,hdefaults_mutex_unlock);
}
void * heventslots_get_usr_ptr(heventslots_t *slots)
{
if(slots==NULL)
{
return NULL;
}
if(slots->has_internal_heap)
{
if(slots->usr!=NULL)
{
return hmemoryheap_pool_get_usr_ptr((hmemoryheap_pool_t *)slots->usr);
}
}
else
{
return slots->usr;
}
return NULL;
}
void heventslots_free(heventslots_t *slots)
{
if(slots==NULL)
{
return;
}
heventslots_unregister_all_slot(slots);
if(slots->api.mem_free!=NULL)
{
slots->api.mem_free(slots,slots->usr);
}
else
{
hdefaults_get_api_table()->mem_free(slots,slots->usr);
}
}
void heventslots_emit_signal(heventslots_t *slots,void *signal)
{
if(slots==NULL)
{
return;
}
//加锁
if(slots->api.mutex_lock!=NULL)
{
slots->api.mutex_lock(slots->usr);
}
heventslots_slot_t *slot=slots->slot_start;
while(slot!=NULL)
{
if(slot->slot!=NULL)
{
slot->slot(signal,slot->usr);
}
slot=slot->next;
}
//解锁
if(slots->api.mutex_unlock!=NULL)
{
slots->api.mutex_unlock(slots->usr);
}
}
uint32_t heventslots_register_slot(heventslots_t *slots,void *slot_usr,void (*onslot)(void *,void *),void (*onfree)(void *))
{
if(slots==NULL)
{
return 0;
}
heventslots_slot_t *slot=NULL;
if(slots->api.mem_alloc!=NULL)
{
slot=(heventslots_slot_t *)slots->api.mem_alloc(sizeof(heventslots_slot_t),slots->usr);
}
else
{
slot=(heventslots_slot_t *)hdefaults_get_api_table()->mem_alloc(sizeof(heventslots_slot_t),slots->usr);
}
if(slot==NULL)
{
return 0;
}
slot->usr=slot_usr;
slot->slot=onslot;
slot->onfree=onfree;
slot->id=slots->slot_next_id++;
slot->next=NULL;
if(slot->id==0)
{
slot->id=slots->slot_next_id++;
}
//加锁
if(slots->api.mutex_lock!=NULL)
{
slots->api.mutex_lock(slots->usr);
}
heventslots_slot_t *slots_slot=slots->slot_start;
while(slots_slot!=NULL)
{
if(slots_slot->next==NULL)
{
slots_slot->next=slot;
break;
}
slots_slot=slots_slot->next;
}
if(slots_slot==NULL)
{
slots->slot_start=slot;
}
//解锁
if(slots->api.mutex_unlock!=NULL)
{
slots->api.mutex_unlock(slots->usr);
}
return slot->id;
}
void heventslots_unregister_slot(heventslots_t *slots,uint32_t id)
{
if(slots==NULL)
{
return;
}
//加锁
if(slots->api.mutex_lock!=NULL)
{
slots->api.mutex_lock(slots->usr);
}
heventslots_slot_t *slots_slot=slots->slot_start;
if(slots_slot!=NULL && slots_slot->id==id)
{
heventslots_slot_t *slot=slots_slot->next;
if(slots_slot->onfree!=NULL)
{
slots_slot->onfree(slots_slot->usr);
}
if(slots->api.mem_free!=NULL)
{
slots->api.mem_free(slots_slot,slots->usr);
}
else
{
hdefaults_get_api_table()->mem_free(slots_slot,slots->usr);
}
slots->slot_start=slot;
slots_slot=NULL;
}
while(slots_slot!=NULL)
{
if(slots_slot->next==NULL)
{
break;
}
if(slots_slot->next->id==id)
{
heventslots_slot_t *slot=slots_slot->next;
slots_slot->next=slots_slot->next->next;
if(slot->onfree!=NULL)
{
slot->onfree(slot->usr);
}
if(slots->api.mem_free!=NULL)
{
slots->api.mem_free(slot,slots->usr);
}
else
{
hdefaults_get_api_table()->mem_free(slot,slots->usr);
}
break;
}
slots_slot=slots_slot->next;
}
//解锁
if(slots->api.mutex_unlock!=NULL)
{
slots->api.mutex_unlock(slots->usr);
}
}
void heventslots_unregister_all_slot(heventslots_t *slots)
{
if(slots==NULL)
{
return;
}
//加锁
if(slots->api.mutex_lock!=NULL)
{
slots->api.mutex_lock(slots->usr);
}
heventslots_slot_t *slot=slots->slot_start;
slots->slot_start=NULL;
while(slot!=NULL)
{
heventslots_slot_t *next_slot=slot->next;
if(slot->onfree!=NULL)
{
slot->onfree(slot->usr);
}
if(slots->api.mem_free!=NULL)
{
slots->api.mem_free(slot,slots->usr);
}
else
{
hdefaults_get_api_table()->mem_free(slot,slots->usr);
}
slot=next_slot;
}
slots->slot_next_id=1;
//解锁
if(slots->api.mutex_unlock!=NULL)
{
slots->api.mutex_unlock(slots->usr);
}
}