xref: /freebsd/lib/libthr/thread/thr_malloc.c (revision 56335f9994b389cadba49a9c8a70ba4e05d0699e)
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 
45 void
46 __thr_malloc_init(void)
47 {
48 
49 	npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d));
50 	if (npagesizes == -1) {
51 		npagesizes = 1;
52 		pagesizes_d[0] = PAGE_SIZE;
53 	}
54 	pagesizes = pagesizes_d;
55 	_thr_umutex_init(&thr_malloc_umtx);
56 }
57 
58 static void
59 thr_malloc_lock(struct pthread *curthread)
60 {
61 
62 	curthread->locklevel++;
63 	_thr_umutex_lock(&thr_malloc_umtx, TID(curthread));
64 }
65 
66 static void
67 thr_malloc_unlock(struct pthread *curthread)
68 {
69 
70 	_thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
71 	curthread->locklevel--;
72 	_thr_ast(curthread);
73 }
74 
75 void *
76 __thr_calloc(size_t num, size_t size)
77 {
78 	struct pthread *curthread;
79 	void *res;
80 
81 	curthread = _get_curthread();
82 	thr_malloc_lock(curthread);
83 	res = __crt_calloc(num, size);
84 	thr_malloc_unlock(curthread);
85 	return (res);
86 }
87 
88 void
89 __thr_free(void *cp)
90 {
91 	struct pthread *curthread;
92 
93 	curthread = _get_curthread();
94 	thr_malloc_lock(curthread);
95 	__crt_free(cp);
96 	thr_malloc_unlock(curthread);
97 }
98 
99 void *
100 __thr_malloc(size_t nbytes)
101 {
102 	struct pthread *curthread;
103 	void *res;
104 
105 	curthread = _get_curthread();
106 	thr_malloc_lock(curthread);
107 	res = __crt_malloc(nbytes);
108 	thr_malloc_unlock(curthread);
109 	return (res);
110 }
111 
112 void *
113 __thr_realloc(void *cp, size_t nbytes)
114 {
115 	struct pthread *curthread;
116 	void *res;
117 
118 	curthread = _get_curthread();
119 	thr_malloc_lock(curthread);
120 	res = __crt_realloc(cp, nbytes);
121 	thr_malloc_unlock(curthread);
122 	return (res);
123 }
124 
125 void
126 __thr_malloc_prefork(struct pthread *curthread)
127 {
128 
129 	_thr_umutex_lock(&thr_malloc_umtx, TID(curthread));
130 }
131 
132 void
133 __thr_malloc_postfork(struct pthread *curthread)
134 {
135 
136 	_thr_umutex_unlock(&thr_malloc_umtx, TID(curthread));
137 }
138