xref: /titanic_52/usr/src/lib/libc/port/sys/sbrk.c (revision 09b1eac246a4e627fcbd1ce5bf8005746cbe45ea)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #pragma weak _sbrk = sbrk
30 #pragma weak _brk = brk
31 
32 #include "lint.h"
33 #include <synch.h>
34 #include <errno.h>
35 #include <sys/isa_defs.h>
36 #include <sys/types.h>
37 #include <sys/sysmacros.h>
38 #include <inttypes.h>
39 #include <unistd.h>
40 #include "mtlib.h"
41 #include "libc.h"
42 
43 extern int _end;
44 void *_nd = &_end;
45 mutex_t __sbrk_lock = DEFAULTMUTEX;
46 
47 extern int _brk_unlocked(void *);
48 extern void *_sbrk_unlocked(intptr_t);
49 
50 /*
51  * The break must always be at least 8-byte aligned
52  */
53 #if (_MAX_ALIGNMENT < 8)
54 #define	ALIGNSZ		8
55 #else
56 #define	ALIGNSZ		_MAX_ALIGNMENT
57 #endif
58 
59 #define	BRKALIGN(x)	(caddr_t)P2ROUNDUP((uintptr_t)(x), ALIGNSZ)
60 
61 void *
62 sbrk(intptr_t addend)
63 {
64 	void *result;
65 
66 	if (!primary_link_map) {
67 		errno = ENOTSUP;
68 		return ((void *)-1);
69 	}
70 	lmutex_lock(&__sbrk_lock);
71 	result = _sbrk_unlocked(addend);
72 	lmutex_unlock(&__sbrk_lock);
73 
74 	return (result);
75 }
76 
77 /*
78  * _sbrk_unlocked() aligns the old break, adds the addend, aligns
79  * the new break, and calls _brk_unlocked() to set the new break.
80  * We must align the old break because _nd may begin life misaligned.
81  * The addend can be either positive or negative, so there are two
82  * overflow/underflow edge conditions to reject:
83  *
84  *   - the addend is negative and brk + addend < 0.
85  *   - the addend is positive and brk + addend > ULONG_MAX
86  */
87 void *
88 _sbrk_unlocked(intptr_t addend)
89 {
90 	char *old_brk = BRKALIGN(_nd);
91 	char *new_brk = BRKALIGN(old_brk + addend);
92 
93 	if ((addend > 0 && new_brk < old_brk) ||
94 	    (addend < 0 && new_brk > old_brk)) {
95 		errno = ENOMEM;
96 		return ((void *)-1);
97 	}
98 	if (_brk_unlocked(new_brk) != 0)
99 		return ((void *)-1);
100 	_nd = new_brk;
101 	return (old_brk);
102 }
103 
104 /*
105  * _sbrk_grow_aligned() aligns the old break to a low_align boundry,
106  * adds min_size, aligns to a high_align boundry, and calls _brk_unlocked()
107  * to set the new break.  The low_aligned-aligned value is returned, and
108  * the actual space allocated is returned through actual_size.
109  *
110  * Unlike sbrk(2), _sbrk_grow_aligned takes an unsigned size, and does
111  * not allow shrinking the heap.
112  */
113 void *
114 _sbrk_grow_aligned(size_t min_size, size_t low_align, size_t high_align,
115     size_t *actual_size)
116 {
117 	uintptr_t old_brk;
118 	uintptr_t ret_brk;
119 	uintptr_t high_brk;
120 	uintptr_t new_brk;
121 	int brk_result;
122 
123 	if (!primary_link_map) {
124 		errno = ENOTSUP;
125 		return ((void *)-1);
126 	}
127 	if ((low_align & (low_align - 1)) != 0 ||
128 	    (high_align & (high_align - 1)) != 0) {
129 		errno = EINVAL;
130 		return ((void *)-1);
131 	}
132 	low_align = MAX(low_align, ALIGNSZ);
133 	high_align = MAX(high_align, ALIGNSZ);
134 
135 	lmutex_lock(&__sbrk_lock);
136 
137 	old_brk = (uintptr_t)BRKALIGN(_nd);
138 	ret_brk = P2ROUNDUP(old_brk, low_align);
139 	high_brk = ret_brk + min_size;
140 	new_brk = P2ROUNDUP(high_brk, high_align);
141 
142 	/*
143 	 * Check for overflow
144 	 */
145 	if (ret_brk < old_brk || high_brk < ret_brk || new_brk < high_brk) {
146 		lmutex_unlock(&__sbrk_lock);
147 		errno = ENOMEM;
148 		return ((void *)-1);
149 	}
150 
151 	if ((brk_result = _brk_unlocked((void *)new_brk)) == 0)
152 		_nd = (void *)new_brk;
153 	lmutex_unlock(&__sbrk_lock);
154 
155 	if (brk_result != 0)
156 		return ((void *)-1);
157 
158 	if (actual_size != NULL)
159 		*actual_size = (new_brk - ret_brk);
160 	return ((void *)ret_brk);
161 }
162 
163 int
164 brk(void *new_brk)
165 {
166 	int result;
167 
168 	if (!primary_link_map) {
169 		errno = ENOTSUP;
170 		return (-1);
171 	}
172 	/*
173 	 * Need to align this here;  _brk_unlocked won't do it for us.
174 	 */
175 	new_brk = BRKALIGN(new_brk);
176 
177 	lmutex_lock(&__sbrk_lock);
178 	if ((result = _brk_unlocked(new_brk)) == 0)
179 		_nd = new_brk;
180 	lmutex_unlock(&__sbrk_lock);
181 
182 	return (result);
183 }
184