1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2019 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/mman.h> 34 #include <rtld_malloc.h> 35 #include "thr_private.h" 36 37 int npagesizes; 38 size_t *pagesizes; 39 static size_t pagesizes_d[2]; 40 static struct umutex thr_malloc_umtx; 41 static u_int thr_malloc_umtx_level; 42 43 void 44 __thr_malloc_init(void) 45 { 46 47 if (npagesizes != 0) 48 return; 49 npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d)); 50 if (npagesizes == -1) { 51 PANIC("Unable to read page sizes"); 52 } 53 pagesizes = pagesizes_d; 54 _thr_umutex_init(&thr_malloc_umtx); 55 } 56 57 static void 58 thr_malloc_lock(struct pthread *curthread) 59 { 60 uint32_t curtid; 61 62 if (curthread == NULL) 63 return; 64 curthread->locklevel++; 65 curtid = TID(curthread); 66 if ((uint32_t)thr_malloc_umtx.m_owner == curtid) 67 thr_malloc_umtx_level++; 68 else 69 _thr_umutex_lock(&thr_malloc_umtx, curtid); 70 } 71 72 static void 73 thr_malloc_unlock(struct pthread *curthread) 74 { 75 76 if (curthread == NULL) 77 return; 78 if (thr_malloc_umtx_level > 0) 79 thr_malloc_umtx_level--; 80 else 81 _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); 82 curthread->locklevel--; 83 _thr_ast(curthread); 84 } 85 86 void * 87 __thr_calloc(size_t num, size_t size) 88 { 89 struct pthread *curthread; 90 void *res; 91 92 curthread = _get_curthread(); 93 thr_malloc_lock(curthread); 94 res = __crt_calloc(num, size); 95 thr_malloc_unlock(curthread); 96 return (res); 97 } 98 99 void 100 __thr_free(void *cp) 101 { 102 struct pthread *curthread; 103 104 curthread = _get_curthread(); 105 thr_malloc_lock(curthread); 106 __crt_free(cp); 107 thr_malloc_unlock(curthread); 108 } 109 110 void * 111 __thr_malloc(size_t nbytes) 112 { 113 struct pthread *curthread; 114 void *res; 115 116 curthread = _get_curthread(); 117 thr_malloc_lock(curthread); 118 res = __crt_malloc(nbytes); 119 thr_malloc_unlock(curthread); 120 return (res); 121 } 122 123 void * 124 __thr_realloc(void *cp, size_t nbytes) 125 { 126 struct pthread *curthread; 127 void *res; 128 129 curthread = _get_curthread(); 130 thr_malloc_lock(curthread); 131 res = __crt_realloc(cp, nbytes); 132 thr_malloc_unlock(curthread); 133 return (res); 134 } 135 136 void 137 __thr_malloc_prefork(struct pthread *curthread) 138 { 139 140 _thr_umutex_lock(&thr_malloc_umtx, TID(curthread)); 141 } 142 143 void 144 __thr_malloc_postfork(struct pthread *curthread) 145 { 146 147 _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); 148 } 149