#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#ifdef IN_RTLD
#include "rtld.h"
#include "rtld_printf.h"
#include "rtld_paths.h"
#endif
#include "rtld_malloc.h"
#define NPOOLPAGES (128*1024/pagesz)
static caddr_t pagepool_start, pagepool_end;
union overhead {
union overhead *ov_next;
struct {
uint16_t ovu_index;
uint8_t ovu_magic;
} ovu;
#define ov_magic ovu.ovu_magic
#define ov_index ovu.ovu_index
};
static void morecore(int bucket);
static int morepages(int n);
#define MAGIC 0xef
#define AMAGIC 0xdf
#define LOW_BITS 3
#define FIRST_BUCKET_SIZE (1U << LOW_BITS)
#define NBUCKETS 30
static union overhead *nextf[NBUCKETS];
static int pagesz;
static void *
cp2op(void *cp)
{
return (((caddr_t)cp - sizeof(union overhead)));
}
void *
__crt_malloc(size_t nbytes)
{
union overhead *op;
int bucket;
size_t amt;
if (pagesz == 0)
pagesz = pagesizes[0];
amt = FIRST_BUCKET_SIZE;
bucket = 0;
while (nbytes > amt - sizeof(*op)) {
amt <<= 1;
bucket++;
if (amt == 0 || bucket >= NBUCKETS)
return (NULL);
}
if ((op = nextf[bucket]) == NULL) {
morecore(bucket);
if ((op = nextf[bucket]) == NULL)
return (NULL);
}
nextf[bucket] = op->ov_next;
op->ov_magic = MAGIC;
op->ov_index = bucket;
return ((char *)(op + 1));
}
void *
__crt_calloc(size_t num, size_t size)
{
void *ret;
if (size != 0 && (num * size) / size != num) {
return (NULL);
}
if ((ret = __crt_malloc(num * size)) != NULL)
memset(ret, 0, num * size);
return (ret);
}
void *
__crt_aligned_alloc_offset(size_t align, size_t size, size_t offset)
{
void *mem, *ov;
union overhead ov1;
uintptr_t x;
if (align < FIRST_BUCKET_SIZE)
align = FIRST_BUCKET_SIZE;
offset &= align - 1;
mem = __crt_malloc(size + align + offset + sizeof(union overhead));
if (mem == NULL)
return (NULL);
x = roundup2((uintptr_t)mem + sizeof(union overhead), align);
x += offset;
ov = cp2op((void *)x);
ov1.ov_magic = AMAGIC;
ov1.ov_index = x - (uintptr_t)mem + sizeof(union overhead);
memcpy(ov, &ov1, sizeof(ov1));
return ((void *)x);
}
static void
morecore(int bucket)
{
union overhead *op;
int sz;
int amt;
int nblks;
sz = FIRST_BUCKET_SIZE << bucket;
if (sz < pagesz) {
amt = pagesz;
nblks = amt / sz;
} else {
amt = sz;
nblks = 1;
}
if (amt > pagepool_end - pagepool_start)
if (morepages(amt / pagesz + NPOOLPAGES) == 0 &&
morepages(amt / pagesz) == 0)
return;
op = (union overhead *)pagepool_start;
pagepool_start += amt;
nextf[bucket] = op;
while (--nblks > 0) {
op->ov_next = (union overhead *)((caddr_t)op + sz);
op = (union overhead *)((caddr_t)op + sz);
}
}
void
__crt_free(void *cp)
{
union overhead *op, op1;
void *opx;
int size;
if (cp == NULL)
return;
opx = cp2op(cp);
memcpy(&op1, opx, sizeof(op1));
op = op1.ov_magic == AMAGIC ? (void *)((caddr_t)cp - op1.ov_index) :
opx;
if (op->ov_magic != MAGIC)
return;
size = op->ov_index;
op->ov_next = nextf[size];
nextf[size] = op;
}
void *
__crt_realloc(void *cp, size_t nbytes)
{
u_int onb;
int i;
union overhead *op;
char *res;
if (cp == NULL)
return (__crt_malloc(nbytes));
op = cp2op(cp);
if (op->ov_magic != MAGIC)
return (NULL);
i = op->ov_index;
onb = 1 << (i + 3);
if (onb < (u_int)pagesz)
onb -= sizeof(*op);
else
onb += pagesz - sizeof(*op);
if (i != 0) {
i = 1 << (i + 2);
if (i < pagesz)
i -= sizeof(*op);
else
i += pagesz - sizeof(*op);
}
if (nbytes <= onb && nbytes > (size_t)i)
return (cp);
if ((res = __crt_malloc(nbytes)) == NULL)
return (NULL);
bcopy(cp, res, (nbytes < onb) ? nbytes : onb);
__crt_free(cp);
return (res);
}
static int
morepages(int n)
{
caddr_t addr;
int offset;
if (pagepool_end - pagepool_start > pagesz) {
addr = roundup2(pagepool_start, pagesz);
if (munmap(addr, pagepool_end - addr) != 0) {
#ifdef IN_RTLD
rtld_fdprintf(STDERR_FILENO, _BASENAME_RTLD ": "
"morepages: cannot munmap %p: %s\n",
addr, rtld_strerror(errno));
#endif
}
}
offset = (uintptr_t)pagepool_start - rounddown2(
(uintptr_t)pagepool_start, pagesz);
addr = mmap(0, n * pagesz, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0);
if (addr == MAP_FAILED) {
#ifdef IN_RTLD
rtld_fdprintf(STDERR_FILENO, _BASENAME_RTLD ": morepages: "
"cannot mmap anonymous memory: %s\n",
rtld_strerror(errno));
#endif
pagepool_start = pagepool_end = NULL;
return (0);
}
pagepool_start = addr;
pagepool_end = pagepool_start + n * pagesz;
pagepool_start += offset;
return (n);
}