diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 138 |
1 files changed, 120 insertions, 18 deletions
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 index 3bbf2bf65e..b6acd64854 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | 1 | .\" Copyright (c) 1980, 1991, 1993 |
| 2 | .\" All rights reserved. | 2 | .\" The Regents of the University of California. All rights reserved. |
| 3 | .\" | 3 | .\" |
| 4 | .\" This code is derived from software contributed to Berkeley by | 4 | .\" This code is derived from software contributed to Berkeley by |
| 5 | .\" the American National Standards Committee X3, on Information | 5 | .\" the American National Standards Committee X3, on Information |
| @@ -33,19 +33,28 @@ | |||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | .\" SUCH DAMAGE. | 34 | .\" SUCH DAMAGE. |
| 35 | .\" | 35 | .\" |
| 36 | .\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 | 36 | .\" $OpenBSD: malloc.3,v 1.2 1996/08/10 04:18:49 tholo Exp $ |
| 37 | .\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | 37 | .\" |
| 39 | .Dd June 29, 1991 | 38 | .Dd June 4, 1993 |
| 40 | .Dt MALLOC 3 | 39 | .Dt MALLOC 3 |
| 41 | .Os BSD 4 | 40 | .Os BSD 4 |
| 42 | .Sh NAME | 41 | .Sh NAME |
| 43 | .Nm malloc | 42 | .Nm malloc , |
| 44 | .Nd general memory allocation function | 43 | .Nd general memory allocation function |
| 44 | .Pp | ||
| 45 | .Nm free | ||
| 46 | .Nd free up memory allocated with malloc, calloc or realloc | ||
| 47 | .Pp | ||
| 48 | .Nm realloc | ||
| 49 | .Nd reallocation of memory function | ||
| 45 | .Sh SYNOPSIS | 50 | .Sh SYNOPSIS |
| 46 | .Fd #include <stdlib.h> | 51 | .Fd #include <stdlib.h> |
| 47 | .Ft void * | 52 | .Ft void * |
| 48 | .Fn malloc "size_t size" | 53 | .Fn malloc "size_t size" |
| 54 | .Ft void | ||
| 55 | .Fn free "void *ptr" | ||
| 56 | .Ft void * | ||
| 57 | .Fn realloc "void *ptr" "size_t size" | ||
| 49 | .Sh DESCRIPTION | 58 | .Sh DESCRIPTION |
| 50 | The | 59 | The |
| 51 | .Fn malloc | 60 | .Fn malloc |
| @@ -62,30 +71,123 @@ suitably aligned (after possible pointer | |||
| 62 | coercion) for storage of any type of object. If the space is of | 71 | coercion) for storage of any type of object. If the space is of |
| 63 | .Em pagesize | 72 | .Em pagesize |
| 64 | or larger, the memory returned will be page-aligned. | 73 | or larger, the memory returned will be page-aligned. |
| 74 | .Pp | ||
| 75 | The | ||
| 76 | .Fn free | ||
| 77 | function causes the space pointed to by | ||
| 78 | .Fa ptr | ||
| 79 | to be deallocated, that is, at least made available for further allocation, | ||
| 80 | but if possible, it will passed back to the kernel with | ||
| 81 | .Xr sbrk 2 . | ||
| 82 | If | ||
| 83 | .Fa ptr | ||
| 84 | is a null pointer, no action occurs. | ||
| 85 | .Pp | ||
| 86 | The | ||
| 87 | .Fn realloc | ||
| 88 | function changes the size of the object pointed to by | ||
| 89 | .Fa ptr | ||
| 90 | to the size specified by | ||
| 91 | .Fa size . | ||
| 92 | The contents of the object are unchanged up to the lesser | ||
| 93 | of the new and old sizes. | ||
| 94 | If the new size is larger, the value of the newly allocated portion | ||
| 95 | of the object is indeterminate. | ||
| 96 | If | ||
| 97 | .Fa ptr | ||
| 98 | is a null pointer, the | ||
| 99 | .Fn realloc | ||
| 100 | function behaves like the | ||
| 101 | .Fn malloc | ||
| 102 | function for the specified size. | ||
| 103 | If the space cannot be allocated, the object | ||
| 104 | pointed to by | ||
| 105 | .Fa ptr | ||
| 106 | is unchanged. | ||
| 107 | If | ||
| 108 | .Fa size | ||
| 109 | is zero and | ||
| 110 | .Fa ptr | ||
| 111 | is not a null pointer, the object it points to is freed. | ||
| 112 | .Pp | ||
| 113 | |||
| 114 | .Sh ENVIRONMENT | ||
| 115 | This malloc will check the environment for a variable called | ||
| 116 | .Em MALLOC_OPTIONS | ||
| 117 | and scan it for flags. | ||
| 118 | Flags are single letters, uppercase means on, lowercase means off. | ||
| 119 | .Bl -tag -width indent | ||
| 120 | .It A | ||
| 121 | ``abort'' malloc will coredump the process, rather than tolerate failure. | ||
| 122 | This is a very handy debugging aid, since the core file will represent the | ||
| 123 | time of failure, | ||
| 124 | rather than when the NULL pointer was accessed. | ||
| 125 | |||
| 126 | .It D | ||
| 127 | ``dump'' malloc will dump statistics in a file called ``malloc.out'' at exit. | ||
| 128 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
| 129 | order to have any effect. | ||
| 130 | |||
| 131 | .It J | ||
| 132 | ``junk'' fill some junk into the area allocated. | ||
| 133 | Currently junk is bytes of 0xd0, this is pronounced ``Duh'' :-) | ||
| 134 | |||
| 135 | .It R | ||
| 136 | ``realloc'' always reallocate when | ||
| 137 | .Fn realloc | ||
| 138 | is called, even if the initial allocation was big enough. | ||
| 139 | This can substantially aid in compacting memory. | ||
| 140 | |||
| 141 | .It Z | ||
| 142 | ``zero'' fill some junk into the area allocated (see ``J''), | ||
| 143 | except for the exact length the user asked for, which is zeroed. | ||
| 144 | |||
| 145 | .El | ||
| 146 | .Pp | ||
| 147 | The ``J'' and ``Z'' is mostly for testing and debugging, | ||
| 148 | if a program changes behavior if either of these options are used, | ||
| 149 | it is buggy. | ||
| 65 | .Sh RETURN VALUES | 150 | .Sh RETURN VALUES |
| 66 | The | 151 | The |
| 67 | .Fn malloc | 152 | .Fn malloc |
| 68 | function returns | 153 | function returns |
| 69 | a pointer to the allocated space if successful; otherwise | 154 | a pointer to the allocated space if successful; otherwise |
| 70 | a null pointer is returned. | 155 | a null pointer is returned. |
| 156 | .Pp | ||
| 157 | The | ||
| 158 | .Fn free | ||
| 159 | function returns no value. | ||
| 160 | .Pp | ||
| 161 | The | ||
| 162 | .Fn realloc | ||
| 163 | function returns either a null pointer or a pointer | ||
| 164 | to the possibly moved allocated space. | ||
| 71 | .Sh SEE ALSO | 165 | .Sh SEE ALSO |
| 72 | .Xr brk 2 , | 166 | .Xr brk 2 , |
| 73 | .Xr getpagesize 2 , | ||
| 74 | .Xr free 3 , | ||
| 75 | .Xr calloc 3 , | ||
| 76 | .Xr alloca 3 , | 167 | .Xr alloca 3 , |
| 77 | .Xr realloc 3 , | 168 | .Xr calloc 3 , |
| 169 | .Xr getpagesize 3 , | ||
| 78 | .Xr memory 3 | 170 | .Xr memory 3 |
| 79 | .Sh STANDARDS | 171 | .Sh STANDARDS |
| 80 | The | 172 | The |
| 81 | .Fn malloc | 173 | .Fn malloc |
| 82 | function conforms to | 174 | function conforms to |
| 83 | .St -ansiC . | 175 | .St -ansiC . |
| 84 | .Sh BUGS | 176 | .Sh HISTORY |
| 85 | The current implementation of | 177 | The present implementation of malloc started out as a filesystem on a drum |
| 86 | .Xr malloc | 178 | attached to a 20bit binary challenged computer built with discrete germanium |
| 87 | does not always fail gracefully when system | 179 | transistors, and it has since graduated to handle primary storage rather than |
| 88 | memory limits are approached. | 180 | secondary. |
| 89 | It may fail to allocate memory when larger free blocks could be broken | 181 | .Pp |
| 90 | up, or when limits are exceeded because the size is rounded up. | 182 | The main difference from other malloc implementations are believed to be that |
| 91 | It is optimized for sizes that are powers of two. | 183 | the free pages are not accessed until allocated. |
| 184 | Most malloc implementations will store a data structure containing a, | ||
| 185 | possibly double-, linked list in the free chunks of memory, used to tie | ||
| 186 | all the free memory together. | ||
| 187 | That is a quite suboptimal thing to do. | ||
| 188 | Every time the free-list is traversed, all the otherwise unused, and very | ||
| 189 | likely paged out, pages get faulted into primary memory, just to see what | ||
| 190 | lies after them in the list. | ||
| 191 | .Pp | ||
| 192 | On systems which are paging, this can make a factor five in difference on the | ||
| 193 | page-faults of a process. | ||
