summaryrefslogtreecommitdiff
path: root/src/libs/network/netmanager/ndindex.ci
blob: f922d8b62c0fef393f560a1296ffa6202118eaef (plain)
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
/*
 *  Copyright 2006  Serge van den Boom <svdb@stack.nl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// This file is part of netmanager_bsd.c, from where it is #included.
// Only used for BSD sockets.

// This file provides a mapping of Sockets to NetDescriptors.


static NetDescriptor *netDescriptors[FD_SETSIZE];
		// INV: flags.closed is not set for entries in netDescriptors.
static size_t maxND;
		// One past the largest used ND in netDescriptors, as used in
		// the first argument of select();


static inline void
NDIndex_init(void) {
	size_t i;
	size_t numND = sizeof (netDescriptors) / sizeof (netDescriptors[0]);

	for (i = 0; i < numND; i++)
		netDescriptors[i] = NULL;
}

static inline void
NDIndex_uninit(void) {
	// Nothing to do.
}

static inline int
NDIndex_registerNDWithSocket(Socket *sock, NetDescriptor *nd) {
	if ((unsigned int) sock->fd >= FD_SETSIZE) {
		errno = EMFILE;
		return -1;
	}

	netDescriptors[sock->fd] = nd;

	if ((size_t) sock->fd >= maxND)
		maxND = (size_t) sock->fd + 1;

	return 0;
}

static inline void
NDIndex_unregisterNDForSocket(Socket *sock) {
	NetDescriptor **last;

	netDescriptors[sock->fd] = NULL;

	last = &netDescriptors[sock->fd];

	if ((size_t) sock->fd + 1 == maxND) {
		do {
			maxND--;
			if (last == &netDescriptors[0])
				break;
			last--;
		} while (*last == NULL);
	}
}

static inline NetDescriptor *
NDIndex_getNDForSocket(Socket *sock) {
	assert((size_t) sock->fd < maxND);
	return netDescriptors[sock->fd];
}

static inline NetDescriptor *
NDIndex_getNDForSocketFd(int fd) {
	assert((size_t) fd < maxND);
	return netDescriptors[fd];
}

static inline bool
NDIndex_socketRegistered(Socket *sock) {
	return ((size_t) sock->fd < maxND)
			&& (NDIndex_getNDForSocket(sock) != NULL);
}

// Get the first argument to be used in select().
static inline size_t
NDIndex_getSelectNumND(void) {
	return maxND;
}