GFileMonitor: Add kqueue(3) support to GIO
[platform/upstream/glib.git] / gio / kqueue / kqueue-utils.c
1 /*******************************************************************************
2   Copyright (c) 2011, 2012 Dmitry Matveev <me@dmitrymatveev.co.uk>
3
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:
10
11   The above copyright notice and this permission notice shall be included in
12   all copies or substantial portions of the Software.
13
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
20   THE SOFTWARE.
21 *******************************************************************************/
22
23 #include <sys/types.h>
24 #include <sys/event.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <unistd.h>
28 #include <sys/stat.h> 
29 #include <errno.h>
30 #include "kqueue-utils.h"
31
32 static gboolean ku_debug_enabled = FALSE;
33 #define KU_W if (ku_debug_enabled) g_warning
34
35
36
37 #define KEVENTS_EXTEND_COUNT 10
38
39
40 /**
41  * kevents_init_sz:
42  * @kv: a #kevents
43  * @n_initial: the initial preallocated memory size. If it is less than
44  *      %KEVENTS_EXTEND_COUNT, this value will be used instead.
45  *
46  * Initializes a #kevents object.
47  **/
48 void
49 kevents_init_sz (kevents *kv, gsize n_initial)
50 {
51   g_assert (kv != NULL);
52
53   memset (kv, 0, sizeof (kevents));
54
55   if (n_initial < KEVENTS_EXTEND_COUNT)
56     n_initial = KEVENTS_EXTEND_COUNT;
57
58   kv->memory = g_new0 (struct kevent, n_initial);
59   kv->kq_allocated = n_initial;
60 }
61
62
63 /**
64  * kevents_extend_sz:
65  * @kv: a #kevents
66  * @n_new: the number of new objects to be added
67  *
68  * Extends the allocated memory, if needed.
69  **/
70 void
71 kevents_extend_sz (kevents *kv, gsize n_new)
72 {
73   g_assert (kv != NULL);
74
75   if (kv->kq_size + n_new <= kv->kq_allocated)
76     return;
77
78   kv->kq_allocated += (n_new + KEVENTS_EXTEND_COUNT);
79   kv->memory = g_renew (struct kevent, kv->memory, kv->kq_allocated);
80 }
81
82
83 /**
84  * kevents_reduce:
85  * @kv: a #kevents
86  *
87  * Reduces the allocated heap size, if needed.
88  *
89  * If the allocated heap size is >= 3*used
90  * and 2*used >= %KEVENTS_EXTEND_COUNT, reduce it to 2*used.
91  **/
92 void
93 kevents_reduce (kevents *kv)
94 {
95   g_assert (kv != NULL);
96   gsize candidate_sz;
97
98   if (kv->kq_size == 0 || kv->kq_allocated == 0 || kv->memory == NULL)
99     return;
100
101   candidate_sz = 2 * kv->kq_size;
102
103   if (((double) kv->kq_allocated / kv->kq_size) >= 3 &&
104       candidate_sz >= KEVENTS_EXTEND_COUNT)
105     {
106       kv->kq_allocated = candidate_sz;
107       kv->memory = g_renew (struct kevent, kv->memory, kv->kq_allocated);
108     }
109 }
110
111
112 /**
113  * kevents_free:
114  * @kv: a #kevents
115  *
116  * Resets the kevents object and frees all the associated memory.
117  **/
118 void
119 kevents_free (kevents *kv)
120 {
121   g_assert (kv != NULL);
122
123   g_free (kv->memory);
124   memset (kv, 0, sizeof (kevents));
125 }
126
127
128 #define SAFE_GENERIC_OP(fcn, fd, data, size) \
129   while (size > 0) \
130     { \
131       gsize retval = fcn (fd, data, size); \
132       if (retval == -1) \
133         { \
134           if (errno == EINTR) \
135             continue; \
136           else \
137             return FALSE; \
138         } \
139       size -= retval; \
140       data += retval; \
141     } \
142   return TRUE;
143
144
145 /**
146  * _ku_read:
147  * @fd: a file descriptor
148  * @data: the destination buffer
149  * @size: how many bytes to read
150  *
151  * A ready-to-EINTR version of read().
152  *
153  * This function expects to work with a blocking socket.
154  *
155  * Returns: %TRUE on success, %FALSE otherwise
156  **/
157 gboolean
158 _ku_read (int fd, gpointer data, gsize size)
159 {
160   SAFE_GENERIC_OP (read, fd, data, size);
161 }
162
163
164 /**
165  * _ku_write:
166  * @fd: a file descriptor
167  * @data: the buffer to write
168  * @size: how many bytes to write
169  *
170  * A ready-to-EINTR version of write().
171  *
172  * This function expects to work with a blocking socket.
173  *
174  * Returns: %TRUE on success, %FALSE otherwise
175  **/
176 gboolean
177 _ku_write (int fd, gconstpointer data, gsize size)
178 {
179   SAFE_GENERIC_OP (write, fd, data, size);
180 }
181
182
183 /**
184  * Get some file information by its file descriptor.
185  *
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.
189  **/
190 void
191 _ku_file_information (int fd, int *is_dir, ino_t *inode)
192 {
193   g_assert (fd != -1);
194
195   struct stat st;
196   memset (&st, 0, sizeof (struct stat));
197
198   if (fstat (fd, &st) == -1)
199     {
200       KU_W ("fstat failed, assuming it is just a file");
201       is_dir = NULL;
202       return;
203     }
204
205   if (is_dir != NULL)
206       *is_dir = ((st.st_mode & S_IFDIR) == S_IFDIR) ? 1 : 0;
207
208   if (inode != NULL)
209       *inode = st.st_ino;
210 }
211
212 /**
213  * Create a file path using its name and a path to its directory.
214  *
215  * @param[in] dir  A path to a file directory. May end with a '/'.
216  * @param[in] file File name.
217  * @return A concatenated path. Should be freed with free().
218  **/
219 gchar*
220 _ku_path_concat (const gchar *dir, const gchar *file)
221 {
222   int dir_len = strlen (dir);
223   int file_len = strlen (file);
224
225   char *path = g_malloc (dir_len + file_len + 2);
226   if (path == NULL)
227     {
228       KU_W ("Failed to allocate memory path for concatenation");
229       return NULL;
230     }
231
232   strcpy (path, dir);
233
234   if (dir[dir_len - 1] != '/') {
235       ++dir_len;
236       path[dir_len - 1] = '/';
237   }
238
239   strcpy (path + dir_len, file);
240   return path;
241 }
242