1 /*******************************************************************************
2 Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 *******************************************************************************/
23 #include <sys/types.h>
24 #include <sys/event.h>
30 #include "kqueue-utils.h"
32 static gboolean ku_debug_enabled
= FALSE
;
33 #define KU_W if (ku_debug_enabled) g_warning
37 #define KEVENTS_EXTEND_COUNT 10
43 * @n_initial: the initial preallocated memory size. If it is less than
44 * %KEVENTS_EXTEND_COUNT, this value will be used instead.
46 * Initializes a #kevents object.
49 kevents_init_sz (kevents
*kv
, gsize n_initial
)
51 g_assert (kv
!= NULL
);
53 memset (kv
, 0, sizeof (kevents
));
55 if (n_initial
< KEVENTS_EXTEND_COUNT
)
56 n_initial
= KEVENTS_EXTEND_COUNT
;
58 kv
->memory
= g_new0 (struct kevent
, n_initial
);
59 kv
->kq_allocated
= n_initial
;
66 * @n_new: the number of new objects to be added
68 * Extends the allocated memory, if needed.
71 kevents_extend_sz (kevents
*kv
, gsize n_new
)
73 g_assert (kv
!= NULL
);
75 if (kv
->kq_size
+ n_new
<= kv
->kq_allocated
)
78 kv
->kq_allocated
+= (n_new
+ KEVENTS_EXTEND_COUNT
);
79 kv
->memory
= g_renew (struct kevent
, kv
->memory
, kv
->kq_allocated
);
87 * Reduces the allocated heap size, if needed.
89 * If the allocated heap size is >= 3*used
90 * and 2*used >= %KEVENTS_EXTEND_COUNT, reduce it to 2*used.
93 kevents_reduce (kevents
*kv
)
95 g_assert (kv
!= NULL
);
98 if (kv
->kq_size
== 0 || kv
->kq_allocated
== 0 || kv
->memory
== NULL
)
101 candidate_sz
= 2 * kv
->kq_size
;
103 if (((double) kv
->kq_allocated
/ kv
->kq_size
) >= 3 &&
104 candidate_sz
>= KEVENTS_EXTEND_COUNT
)
106 kv
->kq_allocated
= candidate_sz
;
107 kv
->memory
= g_renew (struct kevent
, kv
->memory
, kv
->kq_allocated
);
116 * Resets the kevents object and frees all the associated memory.
119 kevents_free (kevents
*kv
)
121 g_assert (kv
!= NULL
);
124 memset (kv
, 0, sizeof (kevents
));
128 #define SAFE_GENERIC_OP(fcn, fd, data, size) \
131 gsize retval = fcn (fd, data, size); \
134 if (errno == EINTR) \
147 * @fd: a file descriptor
148 * @data: the destination buffer
149 * @size: how many bytes to read
151 * A ready-to-EINTR version of read().
153 * This function expects to work with a blocking socket.
155 * Returns: %TRUE on success, %FALSE otherwise
158 _ku_read (int fd
, gpointer data
, gsize size
)
160 SAFE_GENERIC_OP (read
, fd
, data
, size
);
166 * @fd: a file descriptor
167 * @data: the buffer to write
168 * @size: how many bytes to write
170 * A ready-to-EINTR version of write().
172 * This function expects to work with a blocking socket.
174 * Returns: %TRUE on success, %FALSE otherwise
177 _ku_write (int fd
, gconstpointer data
, gsize size
)
179 SAFE_GENERIC_OP (write
, fd
, data
, size
);
184 * Get some file information by its file descriptor.
186 * @param[in] fd A file descriptor.
187 * @param[out] is_dir A flag indicating directory.
188 * @param[out] inode A file's inode number.
191 _ku_file_information (int fd
, int *is_dir
, ino_t
*inode
)
196 memset (&st
, 0, sizeof (struct stat
));
198 if (fstat (fd
, &st
) == -1)
200 KU_W ("fstat failed, assuming it is just a file");
206 *is_dir
= ((st
.st_mode
& S_IFDIR
) == S_IFDIR
) ? 1 : 0;