ObjFW  Diff

Differences From Artifact [f68dab1660]:

To Artifact [8fcdaec4c9]:


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
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







+




-
-


















-
+











-
+








#ifdef HAVE_POLL_H
# include <poll.h>
#endif

#import "OFPollKernelEventObserver.h"
#import "OFData.h"
#import "OFSocket+Private.h"

#import "OFObserveFailedException.h"
#import "OFOutOfRangeException.h"

#import "socket_helpers.h"

#ifdef OF_WII
# define pollfd pollsd
# define fd socket
#endif

@implementation OFPollKernelEventObserver
- (instancetype)init
{
	self = [super init];

	@try {
		struct pollfd p = { _cancelFD[0], POLLIN, 0 };

		_FDs = [[OFMutableData alloc] initWithItemSize:
		    sizeof(struct pollfd)];
		[_FDs addItem: &p];

		_maxFD = _cancelFD[0];
		_FDToObject = of_alloc((size_t)_maxFD + 1, sizeof(id));
		_FDToObject = OFAllocMemory((size_t)_maxFD + 1, sizeof(id));
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_FDs release];
	free(_FDToObject);
	OFFreeMemory(_FDToObject);

	[super dealloc];
}

static void
addObject(OFPollKernelEventObserver *self, id object, int fd, short events)
{
91
92
93
94
95
96
97
98

99
100
101
102
103
104
105
90
91
92
93
94
95
96

97
98
99
100
101
102
103
104







-
+







	}

	if (!found) {
		struct pollfd p = { fd, events, 0 };

		if (fd > self->_maxFD) {
			self->_maxFD = fd;
			self->_FDToObject = of_realloc(self->_FDToObject,
			self->_FDToObject = OFResizeMemory(self->_FDToObject,
			    (size_t)self->_maxFD + 1, sizeof(id));
		}

		self->_FDToObject[fd] = object;
		[self->_FDs addItem: &p];
	}
}
158
159
160
161
162
163
164
165

166
167
168
169
170
171
172
157
158
159
160
161
162
163

164
165
166
167
168
169
170
171







-
+







- (void)removeObjectForWriting: (id <OFReadyForWritingObserving>)object
{
	removeObject(self, object, object.fileDescriptorForWriting, POLLOUT);

	[super removeObjectForWriting: object];
}

- (void)observeForTimeInterval: (of_time_interval_t)timeInterval
- (void)observeForTimeInterval: (OFTimeInterval)timeInterval
{
	void *pool;
	struct pollfd *FDs;
	int events;
	size_t nFDs;

	if ([self of_processReadBuffers])
195
196
197
198
199
200
201
202

203
204
205


206
207
208
209
210
211
212
194
195
196
197
198
199
200

201
202


203
204
205
206
207
208
209
210
211







-
+

-
-
+
+







		if (FDs[i].revents & POLLIN) {
			void *pool2;

			if (FDs[i].fd == _cancelFD[0]) {
				char buffer;

#ifdef OF_HAVE_PIPE
				OF_ENSURE(read(_cancelFD[0], &buffer, 1) == 1);
				OFEnsure(read(_cancelFD[0], &buffer, 1) == 1);
#else
				OF_ENSURE(recvfrom(_cancelFD[0], &buffer, 1,
				    0, NULL, NULL) == 1);
				OFEnsure(recvfrom(_cancelFD[0], &buffer, 1, 0,
				    NULL, NULL) == 1);
#endif
				FDs[i].revents = 0;

				continue;
			}

			pool2 = objc_autoreleasePoolPush();