Changes malloc to zmalloc
[ust.git] / libustcomm / multipoll.c
1 /*
2 * multipoll.c
3 *
4 * Copyright (C) 2010 - Pierre-Marc Fournier (pierre-marc dot fournier at polymtl dot ca)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /* Multipoll is a framework to poll on several file descriptors and to call
22 * a specific callback depending on the fd that had activity.
23 */
24
25 #include <poll.h>
26 #include <stdlib.h>
27 #include "multipoll.h"
28 #include "usterr.h"
29
30 #define INITIAL_N_AVAIL 16
31
32 /* multipoll_init
33 *
34 * Initialize an mpentries struct, which is initially empty of any fd.
35 */
36
37 int multipoll_init(struct mpentries *ent)
38 {
39 ent->n_used = 0;
40 ent->n_avail = INITIAL_N_AVAIL;
41
42 ent->pollfds = (struct pollfd *) zmalloc(sizeof(struct pollfd) * INITIAL_N_AVAIL);
43 ent->extras = (struct pollfd_extra *) zmalloc(sizeof(struct pollfd_extra) * INITIAL_N_AVAIL);
44
45 return 0;
46 }
47
48 /* multipoll_destroy: free a struct mpentries
49 */
50
51 int multipoll_destroy(struct mpentries *ent)
52 {
53 int i;
54
55 for(i=0; i<ent->n_used; i++) {
56 if(ent->extras[i].destroy_priv) {
57 ent->extras[i].destroy_priv(ent->extras[i].priv);
58 }
59 }
60
61 free(ent->pollfds);
62 free(ent->extras);
63
64 return 0;
65 }
66
67 /* multipoll_add
68 *
69 * Add a file descriptor to be waited on in a struct mpentries.
70 *
71 * @ent: the struct mpentries to add an fd to
72 * @fd: the fd to wait on
73 * @events: a mask of the types of events to wait on, see the poll(2) man page
74 * @func: the callback function to be called if there is activity on the fd
75 * @priv: the private pointer to pass to func
76 * @destroy_priv: a callback to destroy the priv pointer when the mpentries
77 is destroyed; may be NULL
78 */
79
80 int multipoll_add(struct mpentries *ent, int fd, short events, int (*func)(void *priv, int fd, short events), void *priv, int (*destroy_priv)(void *))
81 {
82 int cur;
83
84 if(ent->n_used == ent->n_avail) {
85 ent->n_avail *= 2;
86 ent->pollfds = (struct pollfd *) realloc(ent->pollfds, sizeof(struct pollfd) * ent->n_avail);
87 ent->extras = (struct pollfd_extra *) realloc(ent->extras, sizeof(struct pollfd_extra) * ent->n_avail);
88 }
89
90 cur = ent->n_used;
91 ent->n_used++;
92
93 ent->pollfds[cur].fd = fd;
94 ent->pollfds[cur].events = events;
95 ent->extras[cur].func = func;
96 ent->extras[cur].priv = priv;
97 ent->extras[cur].destroy_priv = destroy_priv;
98
99 return 0;
100 }
101
102 /* multipoll_poll: do the actual poll on a struct mpentries
103 *
104 * File descriptors should have been already added with multipoll_add().
105 *
106 * A struct mpentries may be reused for multiple multipoll_poll calls.
107 *
108 * @ent: the struct mpentries to poll on.
109 * @timeout: the timeout after which to return if there was no activity.
110 */
111
112 int multipoll_poll(struct mpentries *ent, int timeout)
113 {
114 int result;
115 int i;
116
117 result = poll(ent->pollfds, ent->n_used, timeout);
118 if(result == -1) {
119 PERROR("poll");
120 return -1;
121 }
122
123 for(i=0; i<ent->n_used; i++) {
124 if(ent->pollfds[i].revents) {
125 ent->extras[i].func(ent->extras[i].priv, ent->pollfds[i].fd, ent->pollfds[i].revents);
126 }
127 }
128
129 return 0;
130 }
This page took 0.031133 seconds and 4 git commands to generate.