1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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
|
/* Copyright © 2014 Brandon L Black <blblack@gmail.com>
*
* This file is part of gdnsd.
*
* gdnsd is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* gdnsd is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gdnsd. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include <gdnsd/alloc.h>
#include <gdnsd/log.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include <inttypes.h>
// Fast ok if both numbers < half of size_t, otherwise slow division check
// This is basically what jemalloc does, seems pretty reasonable.
F_CONST
static bool mul_ok(size_t n, size_t s, size_t m)
{
static const size_t st_high_bits = SIZE_MAX << (sizeof(size_t) * 8 / 2);
return likely((st_high_bits & (n | s)) == 0) || likely(m / s == n);
}
void* gdnsd_xmalloc(size_t size)
{
void* rv = malloc(size);
if (unlikely(!size) || unlikely(!rv))
log_fatal("Cannot allocate %zu bytes (%s)! backtrace:%s",
size, logf_errno(), logf_bt());
return rv;
}
void* gdnsd_xmalloc_n(size_t nmemb, size_t size)
{
const size_t full_size = nmemb * size;
void* rv = malloc(full_size);
if (unlikely(!full_size) || unlikely(!rv) || unlikely(!mul_ok(nmemb, size, full_size)))
log_fatal("Cannot allocate %zu * %zu bytes (%s)! backtrace:%s",
nmemb, size, logf_errno(), logf_bt());
return rv;
}
void* gdnsd_xcalloc(size_t size)
{
void* rv = calloc(1U, size);
if (unlikely(!size) || unlikely(!rv))
log_fatal("Cannot allocate %zu bytes (%s)! backtrace:%s",
size, logf_errno(), logf_bt());
return rv;
}
void* gdnsd_xcalloc_n(size_t nmemb, size_t size)
{
const size_t full_size = nmemb * size;
void* rv = calloc(nmemb, size);
if (unlikely(!full_size) || unlikely(!rv) || unlikely(!mul_ok(nmemb, size, full_size)))
log_fatal("Cannot allocate %zu * %zu bytes (%s)! backtrace:%s",
nmemb, size, logf_errno(), logf_bt());
return rv;
}
void* gdnsd_xrealloc(void* ptr, size_t size)
{
void* rv = realloc(ptr, size);
if (unlikely(!size) || unlikely(!rv))
log_fatal("Cannot allocate %zu bytes (%s)! backtrace:%s",
size, logf_errno(), logf_bt());
return rv;
}
void* gdnsd_xrealloc_n(void* ptr, size_t nmemb, size_t size)
{
const size_t full_size = nmemb * size;
void* rv = realloc(ptr, full_size);
if (unlikely(!full_size) || unlikely(!rv) || unlikely(!mul_ok(nmemb, size, full_size)))
log_fatal("Cannot allocate %zu * %zu bytes (%s)! backtrace:%s",
nmemb, size, logf_errno(), logf_bt());
return rv;
}
void* gdnsd_xpmalign(size_t alignment, size_t size)
{
void* rv = NULL;
const int pmrv = posix_memalign(&rv, alignment, size);
if (unlikely(!size) || unlikely(pmrv) || unlikely(!rv))
log_fatal("Cannot allocate %zu bytes aligned to %zu (%s)! backtrace:%s",
size, alignment, logf_strerror(pmrv), logf_bt());
return rv;
}
void* gdnsd_xpmalign_n(size_t alignment, size_t nmemb, size_t size)
{
const size_t full_size = nmemb * size;
void* rv = NULL;
const int pmrv = posix_memalign(&rv, alignment, full_size);
if (unlikely(!full_size) || unlikely(pmrv) || unlikely(!rv) || unlikely(!mul_ok(nmemb, size, full_size)))
log_fatal("Cannot allocate %zu * %zu bytes aligned to %zu (%s)! backtrace:%s",
nmemb, size, alignment, logf_strerror(pmrv), logf_bt());
return rv;
}
char* gdnsd_xstrdup(const char* s)
{
char* rv = strdup(s);
if (unlikely(!rv))
log_fatal("strdup() failed: %s! backtrace:%s", logf_errno(), logf_bt());
return rv;
}
|