2018-12-20 22:13:14 +08:00
|
|
|
#include <stdio.h>
|
2019-01-21 05:15:20 +08:00
|
|
|
#include <string.h>
|
2018-12-20 22:13:14 +08:00
|
|
|
#include <sys/uio.h>
|
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "string_utils.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/// Report allocation failure without allocating memory
|
|
|
|
void report_allocation_failure(const char *func, const char *file, unsigned int line) {
|
|
|
|
// Since memory allocation failed, we try to print this error message without any
|
|
|
|
// memory allocation. Since logging framework allocates memory (and might even
|
|
|
|
// have not been initialized yet), so we can't use it.
|
|
|
|
char buf[11];
|
|
|
|
int llen = uitostr(line, buf);
|
|
|
|
const char msg1[] = " has failed to allocate memory, ";
|
|
|
|
const char msg2[] = ". Aborting...\n";
|
|
|
|
const struct iovec v[] = {
|
|
|
|
{.iov_base = (void *)func, .iov_len = strlen(func)},
|
|
|
|
{.iov_base = "()", .iov_len = 2},
|
|
|
|
{.iov_base = (void *)msg1, .iov_len = sizeof(msg1) - 1},
|
|
|
|
{.iov_base = "at ", .iov_len = 3},
|
|
|
|
{.iov_base = (void *)file, .iov_len = strlen(file)},
|
|
|
|
{.iov_base = ":", .iov_len = 1},
|
2019-03-30 17:07:21 +08:00
|
|
|
{.iov_base = buf, .iov_len = (size_t)llen},
|
2018-12-20 22:13:14 +08:00
|
|
|
{.iov_base = (void *)msg2, .iov_len = sizeof(msg2) - 1},
|
|
|
|
};
|
|
|
|
|
|
|
|
writev(STDERR_FILENO, v, ARR_SIZE(v));
|
|
|
|
abort();
|
|
|
|
|
|
|
|
unreachable;
|
|
|
|
}
|
|
|
|
|
2019-11-06 07:34:57 +08:00
|
|
|
///
|
|
|
|
/// Calculates next closest power of two of 32bit integer n
|
|
|
|
/// ref: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
|
|
|
///
|
|
|
|
int next_power_of_two(int n)
|
|
|
|
{
|
|
|
|
n--;
|
|
|
|
n |= n >> 1;
|
|
|
|
n |= n >> 2;
|
|
|
|
n |= n >> 4;
|
|
|
|
n |= n >> 8;
|
|
|
|
n |= n >> 16;
|
|
|
|
n++;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-12-20 22:13:14 +08:00
|
|
|
// vim: set noet sw=8 ts=8 :
|