1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2019 The FreeBSD Foundation
5 *
6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/types.h>
32 #include <sys/mman.h>
33 #include <rtld_malloc.h>
34 #include "thr_private.h"
35
36 int npagesizes;
37 size_t *pagesizes;
38 static size_t pagesizes_d[2];
39 static struct umutex thr_malloc_umtx;
40 static u_int thr_malloc_umtx_level;
41
42 void
__thr_malloc_init(void)43 __thr_malloc_init(void)
44 {
45
46 if (npagesizes != 0)
47 return;
48 npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d));
49 if (npagesizes == -1) {
50 PANIC("Unable to read page sizes");
51 }
52 pagesizes = pagesizes_d;
53 _thr_umutex_init(&thr_malloc_umtx);
54 }
55
56 static void
thr_malloc_lock(struct pthread * curthread)57 thr_malloc_lock(struct pthread *curthread)
58 {
59 uint32_t curtid;
60
61 if (curthread == NULL)
62 return;
63 curthread->locklevel++;
64 curtid = TID(curthread);
65 if ((uint32_t)thr_malloc_umtx.m_owner == curtid)
66 thr_malloc_umtx_level++;
67 else
68 _thr_umutex_lock(&thr_malloc_umtx, curtid);
69 }
70
71 static void
thr_malloc_unlock(struct pthread * curthread)72 thr_malloc_unlock(struct pthread *curthread)
73 {
74
75 if (curthread == NULL)
76 return;
77 if (thr_malloc_umtx_level > 0)
78 thr_malloc_umtx_level--;
79 else
80 _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
81 curthread->locklevel--;
82 _thr_ast(curthread);
83 }
84
85 void *
__thr_calloc(size_t num,size_t size)86 __thr_calloc(size_t num, size_t size)
87 {
88 struct pthread *curthread;
89 void *res;
90
91 curthread = _get_curthread();
92 thr_malloc_lock(curthread);
93 res = __crt_calloc(num, size);
94 thr_malloc_unlock(curthread);
95 return (res);
96 }
97
98 void
__thr_free(void * cp)99 __thr_free(void *cp)
100 {
101 struct pthread *curthread;
102
103 curthread = _get_curthread();
104 thr_malloc_lock(curthread);
105 __crt_free(cp);
106 thr_malloc_unlock(curthread);
107 }
108
109 void *
__thr_malloc(size_t nbytes)110 __thr_malloc(size_t nbytes)
111 {
112 struct pthread *curthread;
113 void *res;
114
115 curthread = _get_curthread();
116 thr_malloc_lock(curthread);
117 res = __crt_malloc(nbytes);
118 thr_malloc_unlock(curthread);
119 return (res);
120 }
121
122 void *
__thr_aligned_alloc_offset(size_t align,size_t size,size_t offset)123 __thr_aligned_alloc_offset(size_t align, size_t size, size_t offset)
124 {
125 struct pthread *curthread;
126 void *res;
127
128 curthread = _get_curthread();
129 thr_malloc_lock(curthread);
130 res = __crt_aligned_alloc_offset(align, size, offset);
131 thr_malloc_unlock(curthread);
132 return (res);
133 }
134
135 void *
__thr_realloc(void * cp,size_t nbytes)136 __thr_realloc(void *cp, size_t nbytes)
137 {
138 struct pthread *curthread;
139 void *res;
140
141 curthread = _get_curthread();
142 thr_malloc_lock(curthread);
143 res = __crt_realloc(cp, nbytes);
144 thr_malloc_unlock(curthread);
145 return (res);
146 }
147
148 void
__thr_malloc_prefork(struct pthread * curthread)149 __thr_malloc_prefork(struct pthread *curthread)
150 {
151
152 _thr_umutex_lock(&thr_malloc_umtx, TID(curthread));
153 }
154
155 void
__thr_malloc_postfork(struct pthread * curthread)156 __thr_malloc_postfork(struct pthread *curthread)
157 {
158
159 _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
160 }
161