io-event 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41757dbe8c584fbdf66a0d7adb6b4e7f3ab377aed137c65b49eed48e7ff1d223
4
- data.tar.gz: 84672d42c12a9db38d901a7f4ab4d8f3d74bc98d398fb615d95c6780533bb2bb
3
+ metadata.gz: 0743271de82b1b4324953fadd2f5da09cb79609c1153ae8637e4f23f31f0615c
4
+ data.tar.gz: e90309751d3bc2716362d596b07a477a0f9b1c6ef27443f06469547bce10cb40
5
5
  SHA512:
6
- metadata.gz: 2d32bce07ae295fecd57c67920ec2c0f3796313d80154179290116eb8ad8fd3b58e440c5531a23186bb9cfe2dfdd87fccd709742742559d8d151ae9b0cdb1419
7
- data.tar.gz: e6a6476decc497fab325b047ec7c90c235e505356cf07e145c5ed9e00443b9273743a8180c6e12e5a2d496b8e8d4b42a781438e175d3768f3d6f86ce3b84c9fc
6
+ metadata.gz: f1fe9c27d9ed25969eaca758565f99346dea63f397e32e8bec027312b0936b61b6677a520cbacb4f0c3e976979ff3ee4cfac93ea766d92cd8b88fa19cab1ad7c
7
+ data.tar.gz: 4c476754268f03564134fc461bdeee26c0989fc6673cd27235ccc6d1a6df4d03b680f677469db531e8b1613622bd356ffa762d35b2b39ab455d522c6d624d45e
checksums.yaml.gz.sig CHANGED
Binary file
data/ext/extconf.rb CHANGED
@@ -35,6 +35,10 @@ extension_name = 'IO_Event'
35
35
 
36
36
  $CFLAGS << " -Wall -Wno-unknown-pragmas -std=c99"
37
37
 
38
+ if ENV.key?('RUBY_DEBUG')
39
+ $CFLAGS << " -DRUBY_DEBUG -O0"
40
+ end
41
+
38
42
  $srcs = ["io/event/event.c", "io/event/selector/selector.c"]
39
43
  $VPATH << "$(srcdir)/io/event"
40
44
  $VPATH << "$(srcdir)/io/event/selector"
@@ -50,9 +54,7 @@ if have_header('sys/epoll.h')
50
54
  $srcs << "io/event/selector/epoll.c"
51
55
  end
52
56
 
53
- # The order matters, because we MUST have EV_UDATA_SPECIFIC.
54
- # The `have_header` call is just to add the -D to the compiler flags.
55
- if have_const('EV_UDATA_SPECIFIC', 'sys/event.h') and have_header('sys/event.h')
57
+ if have_header('sys/event.h')
56
58
  $srcs << "io/event/selector/kqueue.c"
57
59
  end
58
60
 
@@ -0,0 +1,135 @@
1
+ // Released under the MIT License.
2
+ // Copyright, 2023, by Samuel Williams.
3
+
4
+ // Provides a simple implementation of unique pointers to elements of the given size.
5
+
6
+ #include <stdlib.h>
7
+ #include <errno.h>
8
+ #include <assert.h>
9
+
10
+ struct IO_Event_Array {
11
+ // The array of pointers to elements:
12
+ void **base;
13
+
14
+ // The allocated size of the array:
15
+ size_t count;
16
+
17
+ // The biggest item we've seen so far:
18
+ size_t limit;
19
+
20
+ // The size of each element that is allocated:
21
+ size_t element_size;
22
+
23
+ void (*element_initialize)(void*);
24
+ void (*element_free)(void*);
25
+ };
26
+
27
+ inline static void IO_Event_Array_allocate(struct IO_Event_Array *array, size_t count, size_t element_size)
28
+ {
29
+ if (count) {
30
+ array->base = (void**)calloc(count, sizeof(void*));
31
+ array->count = count;
32
+ } else {
33
+ array->base = NULL;
34
+ array->count = 0;
35
+ }
36
+
37
+ array->limit = 0;
38
+ array->element_size = element_size;
39
+ }
40
+
41
+ inline static size_t IO_Event_Array_memory_size(const struct IO_Event_Array *array)
42
+ {
43
+ // Upper bound.
44
+ return array->count * (sizeof(void*) + array->element_size);
45
+ }
46
+
47
+ inline static void IO_Event_Array_free(struct IO_Event_Array *array)
48
+ {
49
+ for (size_t i = 0; i < array->limit; i += 1) {
50
+ void *element = array->base[i];
51
+ if (element) {
52
+ array->element_free(element);
53
+ free(element);
54
+ }
55
+ }
56
+
57
+ if (array->base)
58
+ free(array->base);
59
+
60
+ array->base = NULL;
61
+ array->count = 0;
62
+ array->limit = 0;
63
+ }
64
+
65
+ inline static int IO_Event_Array_resize(struct IO_Event_Array *array, size_t count)
66
+ {
67
+ if (count <= array->count) {
68
+ return 0;
69
+ }
70
+
71
+ // Compute the next multiple (ideally a power of 2):
72
+ size_t new_count = array->count;
73
+ while (new_count < count) {
74
+ new_count *= 2;
75
+ }
76
+
77
+ void **new_base = (void**)realloc(array->base, new_count * sizeof(void*));
78
+
79
+ if (new_base == NULL) {
80
+ return -1;
81
+ }
82
+
83
+ // Zero out the new memory:
84
+ memset(new_base + array->count, 0, (new_count - array->count) * sizeof(void*));
85
+
86
+ array->base = (void**)new_base;
87
+ array->count = new_count;
88
+
89
+ return 1;
90
+ }
91
+
92
+ inline static void* IO_Event_Array_lookup(struct IO_Event_Array *array, size_t index)
93
+ {
94
+ size_t count = index + 1;
95
+
96
+ // Resize the array if necessary:
97
+ if (count > array->count) {
98
+ if (IO_Event_Array_resize(array, count) == -1) {
99
+ return NULL;
100
+ }
101
+ }
102
+
103
+ // Get the element:
104
+ void **element = array->base + index;
105
+
106
+ // Allocate the element if it doesn't exist:
107
+ if (*element == NULL) {
108
+ *element = malloc(array->element_size);
109
+
110
+ if (array->element_initialize) {
111
+ array->element_initialize(*element);
112
+ }
113
+
114
+ // Update the limit:
115
+ if (count > array->limit) array->limit = count;
116
+ }
117
+
118
+ return *element;
119
+ }
120
+
121
+ // Push a new element onto the end of the array.
122
+ inline static void* IO_Event_Array_push(struct IO_Event_Array *array)
123
+ {
124
+ return IO_Event_Array_lookup(array, array->limit);
125
+ }
126
+
127
+ inline static void IO_Event_Array_each(struct IO_Event_Array *array, void (*callback)(void*))
128
+ {
129
+ for (size_t i = 0; i < array->limit; i += 1) {
130
+ void *element = array->base[i];
131
+ if (element) {
132
+ callback(element);
133
+ }
134
+ }
135
+ }