summaryrefslogtreecommitdiff
path: root/glnx-local-alloc.h
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2017-10-06 10:09:18 -0400
committerColin Walters <walters@verbum.org>2017-10-06 10:14:57 -0400
commit97cd6a6c1d58a90b52edcff2e2dcb69c8cf7855a (patch)
tree0b50d219529393b01c7fcdacab75a509443496b7 /glnx-local-alloc.h
parentdea16cd8beff76867bd791b1b9c8b51df8a16057 (diff)
downloadlibglnx-97cd6a6c1d58a90b52edcff2e2dcb69c8cf7855a.tar.gz
Add glnx_fd_close() and glnx_autofd
I'd like to have the checks for `EBADF` as well as the "assign to -1" in more places. The cleanup function we had for `glnx_fd_close` is actually what we want. Let's rename the cleanup macro to `glnx_autofd` to better match other autocleanups like `g_autofree`. Then we can use `glnx_fd_close()` as a replacement for plain Unix `close()`. I left the `glnx_close_fd` macro, but it's obviously confusing now with the former. We'll eventually remove it.
Diffstat (limited to 'glnx-local-alloc.h')
-rw-r--r--glnx-local-alloc.h38
1 files changed, 26 insertions, 12 deletions
diff --git a/glnx-local-alloc.h b/glnx-local-alloc.h
index 46dd9d2..3be1fa4 100644
--- a/glnx-local-alloc.h
+++ b/glnx-local-alloc.h
@@ -42,14 +42,30 @@ glnx_local_obj_unref (void *v)
}
#define glnx_unref_object __attribute__ ((cleanup(glnx_local_obj_unref)))
+static inline int
+glnx_steal_fd (int *fdp)
+{
+ int fd = *fdp;
+ *fdp = -1;
+ return fd;
+}
+
+/**
+ * glnx_close_fd:
+ * @fdp: Pointer to fd
+ *
+ * Effectively `close (glnx_steal_fd (&fd))`. Also
+ * asserts that `close()` did not raise `EBADF` - encountering
+ * that error is usually a critical bug in the program.
+ */
static inline void
-glnx_cleanup_close_fdp (int *fdp)
+glnx_close_fd (int *fdp)
{
- int fd, errsv;
+ int errsv;
g_assert (fdp);
- fd = *fdp;
+ int fd = glnx_steal_fd (fdp);
if (fd >= 0)
{
errsv = errno;
@@ -62,16 +78,14 @@ glnx_cleanup_close_fdp (int *fdp)
/**
* glnx_fd_close:
*
+ * Deprecated in favor of `glnx_autofd`.
+ */
+#define glnx_fd_close __attribute__((cleanup(glnx_close_fd)))
+/**
+ * glnx_autofd:
+ *
* Call close() on a variable location when it goes out of scope.
*/
-#define glnx_fd_close __attribute__((cleanup(glnx_cleanup_close_fdp)))
-
-static inline int
-glnx_steal_fd (int *fdp)
-{
- int fd = *fdp;
- *fdp = -1;
- return fd;
-}
+#define glnx_autofd __attribute__((cleanup(glnx_close_fd)))
G_END_DECLS