In libthr we use PAGE_SIZE when allocating memory with mmap and to check various structs will fit into a single page so we can use this allocator for them. Ask the kernel for the page size on init for use by the page allcator and add a new machine dependent macro to hold the smallest page size the architecture supports to check the structure is small enough. This allows us to use the same libthr on arm64 with either 4k or 16k pages. Reviewed by: kib, markj, imp Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D34984
152 lines
3.6 KiB
C
152 lines
3.6 KiB
C
/*-
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
*
|
|
* Copyright (c) 2019 The FreeBSD Foundation
|
|
* All rights reserved.
|
|
*
|
|
* This software was developed by Konstantin Belousov <kib@FreeBSD.org>
|
|
* under sponsorship from the FreeBSD Foundation.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*/
|
|
|
|
#include <sys/cdefs.h>
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
#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_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));
|
|
}
|