2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 The FreeBSD Foundation
7 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/types.h>
37 #include <rtld_malloc.h>
38 #include "thr_private.h"
42 static size_t pagesizes_d[2];
43 static struct umutex thr_malloc_umtx;
44 static u_int thr_malloc_umtx_level;
47 __thr_malloc_init(void)
52 npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d));
53 if (npagesizes == -1) {
54 PANIC("Unable to read page sizes");
56 pagesizes = pagesizes_d;
57 _thr_umutex_init(&thr_malloc_umtx);
61 thr_malloc_lock(struct pthread *curthread)
65 if (curthread == NULL)
67 curthread->locklevel++;
68 curtid = TID(curthread);
69 if ((uint32_t)thr_malloc_umtx.m_owner == curtid)
70 thr_malloc_umtx_level++;
72 _thr_umutex_lock(&thr_malloc_umtx, curtid);
76 thr_malloc_unlock(struct pthread *curthread)
79 if (curthread == NULL)
81 if (thr_malloc_umtx_level > 0)
82 thr_malloc_umtx_level--;
84 _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
85 curthread->locklevel--;
90 __thr_calloc(size_t num, size_t size)
92 struct pthread *curthread;
95 curthread = _get_curthread();
96 thr_malloc_lock(curthread);
97 res = __crt_calloc(num, size);
98 thr_malloc_unlock(curthread);
105 struct pthread *curthread;
107 curthread = _get_curthread();
108 thr_malloc_lock(curthread);
110 thr_malloc_unlock(curthread);
114 __thr_malloc(size_t nbytes)
116 struct pthread *curthread;
119 curthread = _get_curthread();
120 thr_malloc_lock(curthread);
121 res = __crt_malloc(nbytes);
122 thr_malloc_unlock(curthread);
127 __thr_realloc(void *cp, size_t nbytes)
129 struct pthread *curthread;
132 curthread = _get_curthread();
133 thr_malloc_lock(curthread);
134 res = __crt_realloc(cp, nbytes);
135 thr_malloc_unlock(curthread);
140 __thr_malloc_prefork(struct pthread *curthread)
143 _thr_umutex_lock(&thr_malloc_umtx, TID(curthread));
147 __thr_malloc_postfork(struct pthread *curthread)
150 _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));