Blame libglnx/glnx-local-alloc.h

Packit Service 2a3f3d
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * Copyright (C) 2012,2015 Colin Walters <walters@verbum.org>.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * This library is free software; you can redistribute it and/or
Packit Service 2a3f3d
 * modify it under the terms of the GNU Lesser General Public
Packit Service 2a3f3d
 * License as published by the Free Software Foundation; either
Packit Service 2a3f3d
 * version 2 of the License, or (at your option) any later version.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * This library is distributed in the hope that it will be useful,
Packit Service 2a3f3d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2a3f3d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 2a3f3d
 * Lesser General Public License for more details.
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * You should have received a copy of the GNU Lesser General Public
Packit Service 2a3f3d
 * License along with this library; if not, write to the
Packit Service 2a3f3d
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Service 2a3f3d
 * Boston, MA 02111-1307, USA.
Packit Service 2a3f3d
 */
Packit Service 2a3f3d
Packit Service 2a3f3d
#pragma once
Packit Service 2a3f3d
Packit Service 2a3f3d
#include <gio/gio.h>
Packit Service 2a3f3d
#include <errno.h>
Packit Service 2a3f3d
Packit Service 2a3f3d
G_BEGIN_DECLS
Packit Service 2a3f3d
Packit Service 2a3f3d
/**
Packit Service 2a3f3d
 * glnx_unref_object:
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * Call g_object_unref() on a variable location when it goes out of
Packit Service 2a3f3d
 * scope.  Note that unlike g_object_unref(), the variable may be
Packit Service 2a3f3d
 * %NULL.
Packit Service 2a3f3d
 */
Packit Service 2a3f3d
#define glnx_unref_object __attribute__ ((cleanup(glnx_local_obj_unref)))
Packit Service 2a3f3d
static inline void
Packit Service 2a3f3d
glnx_local_obj_unref (void *v)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  GObject *o = *(GObject **)v;
Packit Service 2a3f3d
  if (o)
Packit Service 2a3f3d
    g_object_unref (o);
Packit Service 2a3f3d
}
Packit Service 2a3f3d
#define glnx_unref_object __attribute__ ((cleanup(glnx_local_obj_unref)))
Packit Service 2a3f3d
Packit Service 2a3f3d
static inline int
Packit Service 2a3f3d
glnx_steal_fd (int *fdp)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  int fd = *fdp;
Packit Service 2a3f3d
  *fdp = -1;
Packit Service 2a3f3d
  return fd;
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
/**
Packit Service 2a3f3d
 * glnx_close_fd:
Packit Service 2a3f3d
 * @fdp: Pointer to fd
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * Effectively `close (glnx_steal_fd (&fd))`.  Also
Packit Service 2a3f3d
 * asserts that `close()` did not raise `EBADF` - encountering
Packit Service 2a3f3d
 * that error is usually a critical bug in the program.
Packit Service 2a3f3d
 */
Packit Service 2a3f3d
static inline void
Packit Service 2a3f3d
glnx_close_fd (int *fdp)
Packit Service 2a3f3d
{
Packit Service 2a3f3d
  int errsv;
Packit Service 2a3f3d
Packit Service 2a3f3d
  g_assert (fdp);
Packit Service 2a3f3d
Packit Service 2a3f3d
  int fd = glnx_steal_fd (fdp);
Packit Service 2a3f3d
  if (fd >= 0)
Packit Service 2a3f3d
    {
Packit Service 2a3f3d
      errsv = errno;
Packit Service 2a3f3d
      if (close (fd) < 0)
Packit Service 2a3f3d
        g_assert (errno != EBADF);
Packit Service 2a3f3d
      errno = errsv;
Packit Service 2a3f3d
    }
Packit Service 2a3f3d
}
Packit Service 2a3f3d
Packit Service 2a3f3d
/**
Packit Service 2a3f3d
 * glnx_fd_close:
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * Deprecated in favor of `glnx_autofd`.
Packit Service 2a3f3d
 */
Packit Service 2a3f3d
#define glnx_fd_close __attribute__((cleanup(glnx_close_fd)))
Packit Service 2a3f3d
/**
Packit Service 2a3f3d
 * glnx_autofd:
Packit Service 2a3f3d
 *
Packit Service 2a3f3d
 * Call close() on a variable location when it goes out of scope.
Packit Service 2a3f3d
 */
Packit Service 2a3f3d
#define glnx_autofd __attribute__((cleanup(glnx_close_fd)))
Packit Service 2a3f3d
Packit Service 2a3f3d
G_END_DECLS