diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.3')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 105 |
1 files changed, 28 insertions, 77 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 index 4185648c84..d6c8b375ae 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
| @@ -30,7 +30,7 @@ | |||
| 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | .\" SUCH DAMAGE. | 31 | .\" SUCH DAMAGE. |
| 32 | .\" | 32 | .\" |
| 33 | .\" $OpenBSD: malloc.3,v 1.79 2014/10/19 17:58:14 doug Exp $ | 33 | .\" $OpenBSD: malloc.3,v 1.80 2014/10/19 21:05:53 schwarze Exp $ |
| 34 | .\" | 34 | .\" |
| 35 | .Dd $Mdocdate: October 19 2014 $ | 35 | .Dd $Mdocdate: October 19 2014 $ |
| 36 | .Dt MALLOC 3 | 36 | .Dt MALLOC 3 |
| @@ -97,6 +97,14 @@ If the space cannot be allocated, the object | |||
| 97 | pointed to by | 97 | pointed to by |
| 98 | .Fa ptr | 98 | .Fa ptr |
| 99 | is unchanged. | 99 | is unchanged. |
| 100 | If | ||
| 101 | .Fa ptr | ||
| 102 | is | ||
| 103 | .Dv NULL , | ||
| 104 | .Fn realloc | ||
| 105 | behaves like | ||
| 106 | .Fn malloc | ||
| 107 | and allocates a new object. | ||
| 100 | .Pp | 108 | .Pp |
| 101 | The | 109 | The |
| 102 | .Fn reallocarray | 110 | .Fn reallocarray |
| @@ -139,95 +147,38 @@ function is also provided for compatibility with old systems and other | |||
| 139 | libraries; it is simply an alias for | 147 | libraries; it is simply an alias for |
| 140 | .Fn free . | 148 | .Fn free . |
| 141 | .Sh RETURN VALUES | 149 | .Sh RETURN VALUES |
| 142 | If | 150 | Upon successful completion, the functions |
| 143 | .Fn malloc , | 151 | .Fn malloc , |
| 144 | .Fn calloc , | 152 | .Fn calloc , |
| 145 | .Fn realloc , | 153 | .Fn realloc , |
| 146 | or | ||
| 147 | .Fn reallocarray | ||
| 148 | is called with | ||
| 149 | .Fa size | ||
| 150 | or | ||
| 151 | .Fa nmemb | ||
| 152 | is equal to 0, | ||
| 153 | a pointer to an access protected, zero sized object is returned. | ||
| 154 | .Pp | ||
| 155 | If | ||
| 156 | .Fn malloc | ||
| 157 | is called with | ||
| 158 | .Fa size | ||
| 159 | greater than 0, it returns a pointer to the allocated space if successful; | ||
| 160 | otherwise, a | ||
| 161 | .Dv NULL | ||
| 162 | pointer is returned and | ||
| 163 | .Va errno | ||
| 164 | is set to | ||
| 165 | .Er ENOMEM . | ||
| 166 | .Pp | ||
| 167 | The | ||
| 168 | .Fn calloc | ||
| 169 | function checks for integer overflow and returns | ||
| 170 | .Dv NULL | ||
| 171 | if | ||
| 172 | .Fa nmemb | ||
| 173 | * | ||
| 174 | .Fa size | ||
| 175 | will result in integer overflow. | ||
| 176 | If | ||
| 177 | .Fa nmemb | ||
| 178 | and | 154 | and |
| 179 | .Fa size | 155 | .Fn reallocarray |
| 180 | are greater than 0, | 156 | return a pointer to the allocated space; otherwise, a |
| 181 | .Fn calloc | ||
| 182 | returns a pointer to the allocated space if successful; otherwise, a | ||
| 183 | .Dv NULL | 157 | .Dv NULL |
| 184 | pointer is returned and | 158 | pointer is returned and |
| 185 | .Va errno | 159 | .Va errno |
| 186 | is set to | 160 | is set to |
| 187 | .Er ENOMEM . | 161 | .Er ENOMEM . |
| 188 | .Pp | ||
| 189 | The | ||
| 190 | .Fn realloc | ||
| 191 | function behaves like | ||
| 192 | .Fn malloc | ||
| 193 | for the specified | ||
| 194 | .Fa size | ||
| 195 | when | ||
| 196 | .Fa ptr | ||
| 197 | is | ||
| 198 | .Dv NULL . | ||
| 199 | If | 162 | If |
| 200 | .Fa size | 163 | .Fa size |
| 201 | is greater than 0, | 164 | or |
| 202 | .Fn realloc | 165 | .Fa nmemb |
| 203 | returns a pointer to the allocated space if successful; otherwise, a | 166 | is equal to 0, |
| 204 | .Dv NULL | 167 | a pointer to an access protected, zero sized object is returned. |
| 205 | pointer is returned and | ||
| 206 | .Va errno | ||
| 207 | is set to | ||
| 208 | .Er ENOMEM . | ||
| 209 | .Pp | 168 | .Pp |
| 210 | The | 169 | If multiplying |
| 211 | .Fn reallocarray | ||
| 212 | function checks for integer overflow and returns | ||
| 213 | .Dv NULL | ||
| 214 | if | ||
| 215 | .Fa nmemb | 170 | .Fa nmemb |
| 216 | * | 171 | and |
| 217 | .Fa size | ||
| 218 | will result in integer overflow. | ||
| 219 | If | ||
| 220 | .Fn reallocarray | ||
| 221 | is called with | ||
| 222 | .Fa size | 172 | .Fa size |
| 173 | results in integer overflow, | ||
| 174 | .Fn calloc | ||
| 223 | and | 175 | and |
| 224 | .Fa nmemb | 176 | .Fn reallocarray |
| 225 | greater than 0, it returns a pointer to the allocated space if successful; | 177 | return |
| 226 | otherwise, a | ||
| 227 | .Dv NULL | 178 | .Dv NULL |
| 228 | pointer is returned and | 179 | and set |
| 229 | .Va errno | 180 | .Va errno |
| 230 | is set to | 181 | to |
| 231 | .Er ENOMEM . | 182 | .Er ENOMEM . |
| 232 | .Pp | 183 | .Pp |
| 233 | The | 184 | The |
| @@ -315,8 +266,8 @@ Code designed for some ancient platforms avoided calling | |||
| 315 | with a | 266 | with a |
| 316 | .Dv NULL | 267 | .Dv NULL |
| 317 | .Fa ptr . | 268 | .Fa ptr . |
| 318 | Such hacks are no longer necessary in modern code. Instead of | 269 | Such hacks are no longer necessary in modern code. |
| 319 | this idiom: | 270 | Instead of this idiom: |
| 320 | .Bd -literal -offset indent | 271 | .Bd -literal -offset indent |
| 321 | if (p == NULL) | 272 | if (p == NULL) |
| 322 | newp = malloc(newsize); | 273 | newp = malloc(newsize); |
| @@ -361,8 +312,8 @@ if ((p = malloc(size * num)) == NULL) | |||
| 361 | err(1, "malloc"); | 312 | err(1, "malloc"); |
| 362 | .Ed | 313 | .Ed |
| 363 | .Pp | 314 | .Pp |
| 364 | The above test is not sufficient in all cases. For example, multiplying | 315 | The above test is not sufficient in all cases. |
| 365 | ints requires a different set of checks: | 316 | For example, multiplying ints requires a different set of checks: |
| 366 | .Bd -literal -offset indent | 317 | .Bd -literal -offset indent |
| 367 | int size; | 318 | int size; |
| 368 | int num; | 319 | int num; |
