blob: 593fe608859adc409d9c8f943430ecc724769129 [file]
/* SPDX-License-Identifier: GPL-2.0-only */
// Copyright (c) 2026 Jarkko Sakkinen
#ifndef UCELLS_BASE_H
#define UCELLS_BASE_H
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
#define container_of(ptr, type, member) \
__extension__({ \
static_assert(__builtin_types_compatible_p( \
typeof(ptr), typeof(&((type *)0)->member))); \
(type *)((char *)(ptr) - offsetof(type, member)); \
})
extern thread_local const char *ucells_error;
static inline int err_ctx(int err, const char *context)
{
ucells_error = context;
return err;
}
static inline void close_fd(int fd)
{
int saved_errno = errno;
if (fd >= 0)
close(fd);
errno = saved_errno;
}
static inline int take_fd(int *fd)
{
int old = *fd;
*fd = -1;
return old;
}
#endif