#include <sys/types.h>
#include <sys/mman.h>
#include <rtld_malloc.h>
#include "thr_private.h"
int npagesizes;
size_t *pagesizes;
static size_t pagesizes_d[2];
static struct umutex thr_malloc_umtx;
static u_int thr_malloc_umtx_level;
void
__thr_malloc_init(void)
{
if (npagesizes != 0)
return;
npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d));
if (npagesizes == -1) {
PANIC("Unable to read page sizes");
}
pagesizes = pagesizes_d;
_thr_umutex_init(&thr_malloc_umtx);
}
static void
thr_malloc_lock(struct pthread *curthread)
{
uint32_t curtid;
if (curthread == NULL)
return;
curthread->locklevel++;
curtid = TID(curthread);
if ((uint32_t)thr_malloc_umtx.m_owner == curtid)
thr_malloc_umtx_level++;
else
_thr_umutex_lock(&thr_malloc_umtx, curtid);
}
static void
thr_malloc_unlock(struct pthread *curthread)
{
if (curthread == NULL)
return;
if (thr_malloc_umtx_level > 0)
thr_malloc_umtx_level--;
else
_thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
curthread->locklevel--;
_thr_ast(curthread);
}
void *
__thr_calloc(size_t num, size_t size)
{
struct pthread *curthread;
void *res;
curthread = _get_curthread();
thr_malloc_lock(curthread);
res = __crt_calloc(num, size);
thr_malloc_unlock(curthread);
return (res);
}
void
__thr_free(void *cp)
{
struct pthread *curthread;
curthread = _get_curthread();
thr_malloc_lock(curthread);
__crt_free(cp);
thr_malloc_unlock(curthread);
}
void *
__thr_malloc(size_t nbytes)
{
struct pthread *curthread;
void *res;
curthread = _get_curthread();
thr_malloc_lock(curthread);
res = __crt_malloc(nbytes);
thr_malloc_unlock(curthread);
return (res);
}
void *
__thr_aligned_alloc_offset(size_t align, size_t size, size_t offset)
{
struct pthread *curthread;
void *res;
curthread = _get_curthread();
thr_malloc_lock(curthread);
res = __crt_aligned_alloc_offset(align, size, offset);
thr_malloc_unlock(curthread);
return (res);
}
void *
__thr_realloc(void *cp, size_t nbytes)
{
struct pthread *curthread;
void *res;
curthread = _get_curthread();
thr_malloc_lock(curthread);
res = __crt_realloc(cp, nbytes);
thr_malloc_unlock(curthread);
return (res);
}
void
__thr_malloc_prefork(struct pthread *curthread)
{
_thr_umutex_lock(&thr_malloc_umtx, TID(curthread));
}
void
__thr_malloc_postfork(struct pthread *curthread)
{
_thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
}