1
1
#include "events.h"
2
2
3
3
#include <stdlib.h>
4
- #include <stddef.h>
5
4
#include <string.h>
6
5
7
6
@@ -19,7 +18,7 @@ int equeue_create(struct equeue *q, unsigned size) {
19
18
int equeue_create_inplace (struct equeue * q , unsigned size , void * buffer ) {
20
19
q -> slab .size = size ;
21
20
q -> slab .data = buffer ;
22
- memset ( q -> chunks , 0 , EVENT_CHUNK_LISTS * sizeof ( struct event * )) ;
21
+ q -> chunks = 0 ;
23
22
q -> buffer = 0 ;
24
23
25
24
q -> queue = 0 ;
@@ -56,53 +55,61 @@ void equeue_destroy(struct equeue *q) {
56
55
}
57
56
58
57
// equeue allocation functions
59
- static inline unsigned equeue_size (unsigned size ) {
60
- size += sizeof (struct event );
61
- unsigned alignment = offsetof(struct { char c ; struct event e ; }, e );
62
- return (size + alignment - 1 ) & ~(alignment - 1 );
63
- }
64
-
65
- static struct event * equeue_alloc (struct equeue * q , unsigned size ) {
66
- size = equeue_size (size );
58
+ static void * equeue_alloc (struct equeue * q , unsigned size ) {
59
+ size = size + sizeof (unsigned );
60
+ size = (size + sizeof (unsigned )-1 ) & ~(sizeof (unsigned )-1 );
61
+ if (size < sizeof (struct equeue_chunk )) {
62
+ size = sizeof (struct equeue_chunk );
63
+ }
67
64
68
65
events_mutex_lock (& q -> freelock );
69
66
70
- for (int i = 0 ; i < EVENT_CHUNK_LISTS ; i ++ ) {
71
- if (q -> chunks [i ] && q -> chunks [i ]-> size >= size ) {
72
- struct event * e = q -> chunks [i ];
73
- q -> chunks [i ] = e -> next ;
67
+ for (struct equeue_chunk * * p = & q -> chunks ; * p ; p = & (* p )-> nchunk ) {
68
+ if ((* p )-> size >= size ) {
69
+ struct equeue_chunk * c = * p ;
70
+ if (c -> next ) {
71
+ * p = c -> next ;
72
+ (* p )-> nchunk = c -> nchunk ;
73
+ } else {
74
+ * p = c -> nchunk ;
75
+ }
74
76
events_mutex_unlock (& q -> freelock );
75
- return e ;
77
+ return ( unsigned * ) c + 1 ;
76
78
}
77
79
}
78
80
79
81
if (q -> slab .size >= size ) {
80
- struct event * e = (struct event * )q -> slab .data ;
82
+ struct equeue_chunk * c = (struct equeue_chunk * )q -> slab .data ;
81
83
q -> slab .data += size ;
82
84
q -> slab .size -= size ;
83
- e -> size = size ;
85
+ c -> size = size ;
84
86
events_mutex_unlock (& q -> freelock );
85
- return e ;
87
+ return ( unsigned * ) c + 1 ;
86
88
}
87
89
88
90
events_mutex_unlock (& q -> freelock );
89
91
return 0 ;
90
92
}
91
93
92
- static void equeue_dealloc (struct equeue * q , struct event * e ) {
93
- int i = 0 ;
94
+ static void equeue_dealloc (struct equeue * q , void * e ) {
95
+ struct equeue_chunk * c = ( struct equeue_chunk * )(( unsigned * ) e - 1 ) ;
94
96
95
97
events_mutex_lock (& q -> freelock );
96
98
97
- for (; i < EVENT_CHUNK_LISTS - 1 ; i ++ ) {
98
- if (q -> chunks [i + 1 ] && q -> chunks [i + 1 ]-> size >= e -> size ) {
99
- break ;
100
- }
99
+ struct equeue_chunk * * p = & q -> chunks ;
100
+ while (* p && (* p )-> size < c -> size ) {
101
+ p = & (* p )-> nchunk ;
101
102
}
102
103
103
- e -> next = q -> chunks [i ];
104
- q -> chunks [i ] = e ;
105
-
104
+ if (* p && (* p )-> size == c -> size ) {
105
+ c -> next = * p ;
106
+ c -> nchunk = (* p )-> nchunk ;
107
+ } else {
108
+ c -> next = 0 ;
109
+ c -> nchunk = * p ;
110
+ }
111
+ * p = c ;
112
+
106
113
events_mutex_unlock (& q -> freelock );
107
114
}
108
115
@@ -116,7 +123,7 @@ static inline int event_next_id(struct equeue *q) {
116
123
}
117
124
118
125
void * event_alloc (struct equeue * q , unsigned size ) {
119
- struct event * e = equeue_alloc (q , size );
126
+ struct event * e = equeue_alloc (q , sizeof ( struct event ) + size );
120
127
if (!e ) {
121
128
return 0 ;
122
129
}
0 commit comments