xref: /freebsd/lib/libthr/thread/thr_malloc.c (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/types.h>
36 #include <sys/mman.h>
37 #include <rtld_malloc.h>
38 #include "thr_private.h"
39 
40 int npagesizes;
41 size_t *pagesizes;
42 static size_t pagesizes_d[2];
43 static struct umutex thr_malloc_umtx;
44 static u_int thr_malloc_umtx_level;
45 
46 void
47 __thr_malloc_init(void)
48 {
49 
50 	if (npagesizes != 0)
51 		return;
52 	npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d));
53 	if (npagesizes == -1) {
54 		npagesizes = 1;
55 		pagesizes_d[0] = PAGE_SIZE;
56 	}
57 	pagesizes = pagesizes_d;
58 	_thr_umutex_init(&thr_malloc_umtx);
59 }
60 
61 static void
62 thr_malloc_lock(struct pthread *curthread)
63 {
64 	uint32_t curtid;
65 
66 	if (curthread == NULL)
67 		return;
68 	curthread->locklevel++;
69 	curtid = TID(curthread);
70 	if ((uint32_t)thr_malloc_umtx.m_owner == curtid)
71 		thr_malloc_umtx_level++;
72 	else
73 		_thr_umutex_lock(&thr_malloc_umtx, curtid);
74 }
75 
76 static void
77 thr_malloc_unlock(struct pthread *curthread)
78 {
79 
80 	if (curthread == NULL)
81 		return;
82 	if (thr_malloc_umtx_level > 0)
83 		thr_malloc_umtx_level--;
84 	else
85 		_thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
86 	curthread->locklevel--;
87 	_thr_ast(curthread);
88 }
89 
90 void *
91 __thr_calloc(size_t num, size_t size)
92 {
93 	struct pthread *curthread;
94 	void *res;
95 
96 	curthread = _get_curthread();
97 	thr_malloc_lock(curthread);
98 	res = __crt_calloc(num, size);
99 	thr_malloc_unlock(curthread);
100 	return (res);
101 }
102 
103 void
104 __thr_free(void *cp)
105 {
106 	struct pthread *curthread;
107 
108 	curthread = _get_curthread();
109 	thr_malloc_lock(curthread);
110 	__crt_free(cp);
111 	thr_malloc_unlock(curthread);
112 }
113 
114 void *
115 __thr_malloc(size_t nbytes)
116 {
117 	struct pthread *curthread;
118 	void *res;
119 
120 	curthread = _get_curthread();
121 	thr_malloc_lock(curthread);
122 	res = __crt_malloc(nbytes);
123 	thr_malloc_unlock(curthread);
124 	return (res);
125 }
126 
127 void *
128 __thr_realloc(void *cp, size_t nbytes)
129 {
130 	struct pthread *curthread;
131 	void *res;
132 
133 	curthread = _get_curthread();
134 	thr_malloc_lock(curthread);
135 	res = __crt_realloc(cp, nbytes);
136 	thr_malloc_unlock(curthread);
137 	return (res);
138 }
139 
140 void
141 __thr_malloc_prefork(struct pthread *curthread)
142 {
143 
144 	_thr_umutex_lock(&thr_malloc_umtx, TID(curthread));
145 }
146 
147 void
148 __thr_malloc_postfork(struct pthread *curthread)
149 {
150 
151 	_thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
152 }
153