s3:smb2_server: optimize req->in.vector allocation
[Samba.git] / lib / tevent / tevent_fd.c
blob455961b67c840db4dc90606b039030eb1ccf308d
1 /*
2 Unix SMB/CIFS implementation.
4 common events code for fd events
6 Copyright (C) Stefan Metzmacher 2009
8 ** NOTE! The following LGPL license applies to the tevent
9 ** library. This does NOT imply that all of Samba is released
10 ** under the LGPL
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include "replace.h"
27 #include "tevent.h"
28 #include "tevent_internal.h"
29 #include "tevent_util.h"
31 int tevent_common_fd_destructor(struct tevent_fd *fde)
33 if (fde->event_ctx) {
34 DLIST_REMOVE(fde->event_ctx->fd_events, fde);
37 if (fde->close_fn) {
38 fde->close_fn(fde->event_ctx, fde, fde->fd, fde->private_data);
39 fde->fd = -1;
42 return 0;
45 struct tevent_fd *tevent_common_add_fd(struct tevent_context *ev, TALLOC_CTX *mem_ctx,
46 int fd, uint16_t flags,
47 tevent_fd_handler_t handler,
48 void *private_data,
49 const char *handler_name,
50 const char *location)
52 struct tevent_fd *fde;
54 /* tevent will crash later on select() if we save
55 * a negative file descriptor. Better to fail here
56 * so that consumers will be able to debug it
58 if (fd < 0) return NULL;
60 fde = talloc(mem_ctx?mem_ctx:ev, struct tevent_fd);
61 if (!fde) return NULL;
63 fde->event_ctx = ev;
64 fde->fd = fd;
65 fde->flags = flags;
66 fde->handler = handler;
67 fde->close_fn = NULL;
68 fde->private_data = private_data;
69 fde->handler_name = handler_name;
70 fde->location = location;
71 fde->additional_flags = 0;
72 fde->additional_data = NULL;
74 DLIST_ADD(ev->fd_events, fde);
76 talloc_set_destructor(fde, tevent_common_fd_destructor);
78 return fde;
80 uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde)
82 return fde->flags;
85 void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
87 if (fde->flags == flags) return;
88 fde->flags = flags;
91 void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
92 tevent_fd_close_fn_t close_fn)
94 fde->close_fn = close_fn;