utils.c 3.56 KB
Newer Older
1 2 3 4 5 6 7 8 9
/* Wendelin. Miscellaneous utilities
 * Copyright (C) 2014-2015  Nexedi SA and Contributors.
 *                          Kirill Smelkov <kirr@nexedi.com>
 *
 * This program is free software: you can Use, Study, Modify and Redistribute
 * it under the terms of the GNU General Public License version 3, or (at your
 * option) any later version, as published by the Free Software Foundation.
 *
 * You can also Link and Combine this program with other software covered by
10 11 12 13
 * the terms of any of the Free Software licenses or any of the Open Source
 * Initiative approved licenses and Convey the resulting work. Corresponding
 * source of such a combination shall include the source code for all other
 * software used.
14 15 16 17 18
 *
 * This program is distributed WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See COPYING file for full licensing terms.
19
 * See https://www.nexedi.com/licensing for rationale and options.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
 */

#include <wendelin/utils.h>
#include <wendelin/bug.h>

#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>


/* ilog2 that must be exact */
unsigned ilog2_exact(unsigned x)
{
    BUG_ON(__builtin_popcount(x) != 1);
    return __builtin_ctz(x);
}


/* if allocating memory on DSO init or ram types registration fails, we have
 * bigger problems */
void *xzalloc(size_t size)
{
    void *addr;
    addr = zalloc(size);
    if (!addr)
        BUGe();
    return addr;
}


/* likewise */
char *xstrdup(const char *s)
{
    char *d;
    d = strdup(s);
    if (!d)
        BUGe();
    return d;
}


/* closing correct fd on shmfs-like filesystem should be ok */
void xclose(int fd)
{
    int err;
    err = close(fd);
    if (err)
        BUGe();
}

/* unmapping memory should not fail, if it was previously mmaped ok */
void xmunmap(void *addr, size_t len)
{
    int err;
    BUG_ON(!addr);  /* munmap(2) does not catch this XXX why ? */
    err = munmap(addr, len);
    if (err)
        BUGe();
}

/* changing protection to previously RW-mmaped memory should not fail */
void xmprotect(void *addr, size_t len, int prot)
{
    int err;
    err = mprotect(addr, len, prot);
    if (err)
        BUGe();
}

/* releasing memory with correct offset and len should not fail */
void xfallocate(int fd, int mode, off_t offset, off_t len)
{
    int err;
    err = fallocate(fd, mode, offset, len);
    if (err)
        BUGe();
}

/* truncating file to 0 size should be ok */
void xftruncate(int fd, off_t len)
{
    int err;
    err = ftruncate(fd, len);
    if (err)
        BUGe();
}


/* sig*set should not fail on any correct usage */
void xsigemptyset(sigset_t *set)
{
    int err;
    err = sigemptyset(set);
    if (err)
        BUGe();
}

void xsigaddset(sigset_t *set, int sig)
{
    int err;
    err = sigaddset(set, sig);
    if (err)
        BUGe();
}

128 129 130 131 132 133 134 135 136 137
int xsigismember(const sigset_t *set, int sig)
{
    int ret;
    ret = sigismember(set, sig);
    if (ret == -1)
        BUGe();

    return ret;
}

138 139 140 141 142 143
/* pthread_sigmask() should not fail on any correct usage */
void xpthread_sigmask(int how, const sigset_t *set, sigset_t *oldset)
{
    int err;
    err = pthread_sigmask(how, set, oldset);
    if (err)
144
        BUGerr(err);
145
}
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163


/* mutex lock/unlock should not fail if mutex was correctly initialized/used */
void xpthread_mutex_lock(pthread_mutex_t *lock)
{
    int err;
    err = pthread_mutex_lock(lock);
    if (err)
        BUGerr(err);
}

void xpthread_mutex_unlock(pthread_mutex_t *lock)
{
    int err;
    err = pthread_mutex_unlock(lock);
    if (err)
        BUGerr(err);
}