author | Jon Tibble <meths@btinternet.com> |
Thu, 09 Dec 2010 22:32:39 +0100 | |
changeset 13255 | 4afa820d78b9 |
parent 6812 | febeba71273d |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
2186
7b18ba5d9cfe
6418491 solaris 10 runtime prevents sigbus signal to correctly get passed to the handler
raf
parents:
0
diff
changeset
|
5 |
* Common Development and Distribution License (the "License"). |
7b18ba5d9cfe
6418491 solaris 10 runtime prevents sigbus signal to correctly get passed to the handler
raf
parents:
0
diff
changeset
|
6 |
* You may not use this file except in compliance with the License. |
0 | 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 |
*/ |
|
2186
7b18ba5d9cfe
6418491 solaris 10 runtime prevents sigbus signal to correctly get passed to the handler
raf
parents:
0
diff
changeset
|
21 |
|
0 | 22 |
/* |
6515
10dab2b883e0
6678310 using LD_AUDIT, ld.so.1 calls shared library's .init before library is fully relocated
raf
parents:
2658
diff
changeset
|
23 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
0 | 24 |
* Use is subject to license terms. |
25 |
*/ |
|
26 |
||
27 |
/* Copyright (c) 1988 AT&T */ |
|
28 |
/* All Rights Reserved */ |
|
29 |
||
6812 | 30 |
#pragma ident "%Z%%M% %I% %E% SMI" |
31 |
||
32 |
#include "lint.h" |
|
0 | 33 |
#include "mallint.h" |
34 |
#include "mtlib.h" |
|
35 |
||
36 |
#define _misaligned(p) ((unsigned)(p) & 3) |
|
37 |
/* 4-byte "word" alignment is considered ok in LP64 */ |
|
38 |
#define _nextblk(p, size) ((TREE *)((uintptr_t)(p) + (size))) |
|
39 |
||
40 |
/* |
|
41 |
* memalign(align, nbytes) |
|
42 |
* |
|
43 |
* Description: |
|
44 |
* Returns a block of specified size on a specified alignment boundary. |
|
45 |
* |
|
46 |
* Algorithm: |
|
47 |
* Malloc enough to ensure that a block can be aligned correctly. |
|
48 |
* Find the alignment point and return the fragments |
|
49 |
* before and after the block. |
|
50 |
* |
|
51 |
* Errors: |
|
52 |
* Returns NULL and sets errno as follows: |
|
53 |
* [EINVAL] |
|
54 |
* if nbytes = 0, |
|
55 |
* or if alignment is misaligned, |
|
56 |
* or if the heap has been detectably corrupted. |
|
57 |
* [ENOMEM] |
|
58 |
* if the requested memory could not be allocated. |
|
59 |
*/ |
|
60 |
||
61 |
void * |
|
62 |
memalign(size_t align, size_t nbytes) |
|
63 |
{ |
|
64 |
size_t reqsize; /* Num of bytes to get from malloc() */ |
|
65 |
TREE *p; /* Ptr returned from malloc() */ |
|
66 |
TREE *blk; /* For addressing fragment blocks */ |
|
67 |
size_t blksize; /* Current (shrinking) block size */ |
|
68 |
TREE *alignedp; /* Ptr to properly aligned boundary */ |
|
69 |
TREE *aligned_blk; /* The block to be returned */ |
|
70 |
size_t frag_size; /* size of fragments fore and aft */ |
|
71 |
size_t x; |
|
72 |
||
6515
10dab2b883e0
6678310 using LD_AUDIT, ld.so.1 calls shared library's .init before library is fully relocated
raf
parents:
2658
diff
changeset
|
73 |
if (!primary_link_map) { |
10dab2b883e0
6678310 using LD_AUDIT, ld.so.1 calls shared library's .init before library is fully relocated
raf
parents:
2658
diff
changeset
|
74 |
errno = ENOTSUP; |
10dab2b883e0
6678310 using LD_AUDIT, ld.so.1 calls shared library's .init before library is fully relocated
raf
parents:
2658
diff
changeset
|
75 |
return (NULL); |
10dab2b883e0
6678310 using LD_AUDIT, ld.so.1 calls shared library's .init before library is fully relocated
raf
parents:
2658
diff
changeset
|
76 |
} |
10dab2b883e0
6678310 using LD_AUDIT, ld.so.1 calls shared library's .init before library is fully relocated
raf
parents:
2658
diff
changeset
|
77 |
|
0 | 78 |
/* |
79 |
* check for valid size and alignment parameters |
|
80 |
* MAX_ALIGN check prevents overflow in later calculation. |
|
81 |
*/ |
|
82 |
if (nbytes == 0 || _misaligned(align) || align == 0 || |
|
83 |
align > MAX_ALIGN) { |
|
84 |
errno = EINVAL; |
|
85 |
return (NULL); |
|
86 |
} |
|
87 |
||
88 |
/* |
|
89 |
* Malloc enough memory to guarantee that the result can be |
|
90 |
* aligned correctly. The worst case is when malloc returns |
|
91 |
* a block so close to the next alignment boundary that a |
|
92 |
* fragment of minimum size cannot be created. In order to |
|
93 |
* make sure we can handle this, we need to force the |
|
94 |
* alignment to be at least as large as the minimum frag size |
|
95 |
* (MINSIZE + WORDSIZE). |
|
96 |
*/ |
|
97 |
||
98 |
/* check for size that could overflow calculations */ |
|
99 |
if (nbytes > MAX_MALLOC) { |
|
100 |
errno = ENOMEM; |
|
101 |
return (NULL); |
|
102 |
} |
|
103 |
ROUND(nbytes); |
|
104 |
if (nbytes < MINSIZE) |
|
105 |
nbytes = MINSIZE; |
|
106 |
ROUND(align); |
|
107 |
while (align < MINSIZE + WORDSIZE) |
|
108 |
align <<= 1; |
|
109 |
reqsize = nbytes + align + (MINSIZE + WORDSIZE); |
|
110 |
||
111 |
/* check for overflow */ |
|
112 |
if (reqsize < nbytes) { |
|
113 |
errno = ENOMEM; |
|
114 |
return (NULL); |
|
115 |
} |
|
116 |
||
117 |
p = (TREE *)malloc(reqsize); |
|
118 |
if (p == (TREE *)NULL) { |
|
119 |
/* malloc sets errno */ |
|
120 |
return (NULL); |
|
121 |
} |
|
6515
10dab2b883e0
6678310 using LD_AUDIT, ld.so.1 calls shared library's .init before library is fully relocated
raf
parents:
2658
diff
changeset
|
122 |
(void) mutex_lock(&libc_malloc_lock); |
0 | 123 |
|
124 |
/* |
|
125 |
* get size of the entire block (overhead and all) |
|
126 |
*/ |
|
127 |
blk = BLOCK(p); /* back up to get length word */ |
|
128 |
blksize = SIZE(blk); |
|
129 |
CLRBITS01(blksize); |
|
130 |
||
131 |
/* |
|
132 |
* locate the proper alignment boundary within the block. |
|
133 |
*/ |
|
134 |
x = (size_t)p; |
|
135 |
if (x % align != 0) |
|
136 |
x += align - (x % align); |
|
137 |
alignedp = (TREE *)x; |
|
138 |
aligned_blk = BLOCK(alignedp); |
|
139 |
||
140 |
/* |
|
141 |
* Check out the space to the left of the alignment |
|
142 |
* boundary, and split off a fragment if necessary. |
|
143 |
*/ |
|
144 |
frag_size = (size_t)aligned_blk - (size_t)blk; |
|
145 |
if (frag_size != 0) { |
|
146 |
/* |
|
147 |
* Create a fragment to the left of the aligned block. |
|
148 |
*/ |
|
149 |
if (frag_size < MINSIZE + WORDSIZE) { |
|
150 |
/* |
|
151 |
* Not enough space. So make the split |
|
152 |
* at the other end of the alignment unit. |
|
153 |
* We know this yields enough space, because |
|
154 |
* we forced align >= MINSIZE + WORDSIZE above. |
|
155 |
*/ |
|
156 |
frag_size += align; |
|
157 |
aligned_blk = _nextblk(aligned_blk, align); |
|
158 |
} |
|
159 |
blksize -= frag_size; |
|
160 |
SIZE(aligned_blk) = blksize | BIT0; |
|
161 |
frag_size -= WORDSIZE; |
|
162 |
SIZE(blk) = frag_size | BIT0 | ISBIT1(SIZE(blk)); |
|
163 |
_free_unlocked(DATA(blk)); |
|
164 |
} |
|
165 |
||
166 |
/* |
|
167 |
* Is there a (sufficiently large) fragment to the |
|
168 |
* right of the aligned block? |
|
169 |
*/ |
|
170 |
frag_size = blksize - nbytes; |
|
171 |
if (frag_size >= MINSIZE + WORDSIZE) { |
|
172 |
/* |
|
173 |
* split and free a fragment on the right |
|
174 |
*/ |
|
175 |
blksize = SIZE(aligned_blk); |
|
176 |
SIZE(aligned_blk) = nbytes; |
|
177 |
blk = NEXT(aligned_blk); |
|
178 |
SETOLD01(SIZE(aligned_blk), blksize); |
|
179 |
frag_size -= WORDSIZE; |
|
180 |
SIZE(blk) = frag_size | BIT0; |
|
181 |
_free_unlocked(DATA(blk)); |
|
182 |
} |
|
6515
10dab2b883e0
6678310 using LD_AUDIT, ld.so.1 calls shared library's .init before library is fully relocated
raf
parents:
2658
diff
changeset
|
183 |
(void) mutex_unlock(&libc_malloc_lock); |
0 | 184 |
return (DATA(aligned_blk)); |
185 |
} |