io-event 1.2.3 → 1.3.1

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: de2b826a00b27006568dad0139dd9d695ce8708882607fd1e1e4c4eda7d727cd
4
+ data.tar.gz: 3c34946c623e2c03d31c05f7e62283b005d8e8c8d1a6dd78d80a9cf847e7231c
5
5
  SHA512:
6
- metadata.gz: 2d32bce07ae295fecd57c67920ec2c0f3796313d80154179290116eb8ad8fd3b58e440c5531a23186bb9cfe2dfdd87fccd709742742559d8d151ae9b0cdb1419
7
- data.tar.gz: e6a6476decc497fab325b047ec7c90c235e505356cf07e145c5ed9e00443b9273743a8180c6e12e5a2d496b8e8d4b42a781438e175d3768f3d6f86ce3b84c9fc
6
+ metadata.gz: e2f752ea2f36c3cb848b3db0a8f2db62d27cd7a97f1dc1c245a8a094e8bd44368a4bc9bfcef3091d6473d02f2027423ddbdb9adcf9073d1103404408860ae411
7
+ data.tar.gz: bab17c94312eedabf98144e2363a37e0e9356e264f51108dcde0e0a188249eaa543ba164fe330a7fa3332b623418fe8cd5d3190774e723794b9d0fc121688f26
checksums.yaml.gz.sig CHANGED
Binary file
data/ext/extconf.rb CHANGED
@@ -3,26 +3,7 @@
3
3
 
4
4
  # Released under the MIT License.
5
5
  # Copyright, 2021-2023, by Samuel Williams.
6
-
7
- # Copyright, 2021, by Samuel G. D. Williams. <http://www.codeotaku.com>
8
- #
9
- # Permission is hereby granted, free of charge, to any person obtaining a copy
10
- # of this software and associated documentation files (the "Software"), to deal
11
- # in the Software without restriction, including without limitation the rights
12
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
- # copies of the Software, and to permit persons to whom the Software is
14
- # furnished to do so, subject to the following conditions:
15
- #
16
- # The above copyright notice and this permission notice shall be included in
17
- # all copies or substantial portions of the Software.
18
- #
19
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
- # THE SOFTWARE.
6
+ # Copyright, 2023, by Math Ieu.
26
7
 
27
8
  return if RUBY_DESCRIPTION =~ /jruby/
28
9
 
@@ -35,6 +16,10 @@ extension_name = 'IO_Event'
35
16
 
36
17
  $CFLAGS << " -Wall -Wno-unknown-pragmas -std=c99"
37
18
 
19
+ if ENV.key?('RUBY_DEBUG')
20
+ $CFLAGS << " -DRUBY_DEBUG -O0"
21
+ end
22
+
38
23
  $srcs = ["io/event/event.c", "io/event/selector/selector.c"]
39
24
  $VPATH << "$(srcdir)/io/event"
40
25
  $VPATH << "$(srcdir)/io/event/selector"
@@ -50,9 +35,7 @@ if have_header('sys/epoll.h')
50
35
  $srcs << "io/event/selector/epoll.c"
51
36
  end
52
37
 
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')
38
+ if have_header('sys/event.h')
56
39
  $srcs << "io/event/selector/kqueue.c"
57
40
  end
58
41
 
@@ -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
+ }