diff options
Diffstat (limited to 'src/lib/libc')
| -rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 4 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/malloc.3 | 28 | ||||
| -rw-r--r-- | src/lib/libc/stdlib/malloc.c | 50 |
3 files changed, 20 insertions, 62 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index 04e84fe941..b4b716dd71 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile.inc,v 1.36 2007/09/03 14:37:02 millert Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.37 2008/05/19 19:36:15 otto Exp $ |
| 2 | 2 | ||
| 3 | # stdlib sources | 3 | # stdlib sources |
| 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib |
| @@ -55,7 +55,7 @@ MLINKS+=insque.3 remque.3 | |||
| 55 | MLINKS+=labs.3 llabs.3 | 55 | MLINKS+=labs.3 llabs.3 |
| 56 | MLINKS+=lsearch.3 lfind.3 | 56 | MLINKS+=lsearch.3 lfind.3 |
| 57 | MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 | 57 | MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 |
| 58 | MLINKS+=malloc.3 recalloc.3 malloc.3 cfree.3 malloc.3 malloc.conf.5 | 58 | MLINKS+=malloc.3 cfree.3 malloc.3 malloc.conf.5 |
| 59 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 | 59 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 |
| 60 | MLINKS+=radixsort.3 sradixsort.3 | 60 | MLINKS+=radixsort.3 sradixsort.3 |
| 61 | MLINKS+=rand.3 srand.3 rand.3 rand_r.3 | 61 | MLINKS+=rand.3 srand.3 rand.3 rand_r.3 |
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 index e82dc45842..1b481933ca 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
| @@ -30,16 +30,15 @@ | |||
| 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.50 2007/09/05 06:03:08 deraadt Exp $ | 33 | .\" $OpenBSD: malloc.3,v 1.51 2008/05/19 19:36:15 otto Exp $ |
| 34 | .\" | 34 | .\" |
| 35 | .Dd $Mdocdate: September 5 2007 $ | 35 | .Dd $Mdocdate: May 19 2008 $ |
| 36 | .Dt MALLOC 3 | 36 | .Dt MALLOC 3 |
| 37 | .Os | 37 | .Os |
| 38 | .Sh NAME | 38 | .Sh NAME |
| 39 | .Nm malloc , | 39 | .Nm malloc , |
| 40 | .Nm calloc , | 40 | .Nm calloc , |
| 41 | .Nm realloc , | 41 | .Nm realloc , |
| 42 | .Nm recalloc , | ||
| 43 | .Nm free , | 42 | .Nm free , |
| 44 | .Nm cfree | 43 | .Nm cfree |
| 45 | .Nd memory allocation and deallocation | 44 | .Nd memory allocation and deallocation |
| @@ -51,8 +50,6 @@ | |||
| 51 | .Fn calloc "size_t nmemb" "size_t size" | 50 | .Fn calloc "size_t nmemb" "size_t size" |
| 52 | .Ft void * | 51 | .Ft void * |
| 53 | .Fn realloc "void *ptr" "size_t size" | 52 | .Fn realloc "void *ptr" "size_t size" |
| 54 | .Ft void * | ||
| 55 | .Fn recalloc "void *ptr" "size_t nmemb" "size_t size" | ||
| 56 | .Ft void | 53 | .Ft void |
| 57 | .Fn free "void *ptr" | 54 | .Fn free "void *ptr" |
| 58 | .Ft void | 55 | .Ft void |
| @@ -206,18 +203,7 @@ if ((newp = realloc(p, num * size)) == NULL) { | |||
| 206 | ... | 203 | ... |
| 207 | .Ed | 204 | .Ed |
| 208 | .Pp | 205 | .Pp |
| 209 | The | 206 | Malloc will first look for a symbolic link called |
| 210 | .Fn recalloc | ||
| 211 | function is similar to | ||
| 212 | .Fn realloc | ||
| 213 | except that it shares semantics with | ||
| 214 | .Fn calloc | ||
| 215 | rather than | ||
| 216 | .Fn malloc . | ||
| 217 | Newly allocated space is initialized to zero and the resulting size is | ||
| 218 | checked for integer overflow. | ||
| 219 | .Pp | ||
| 220 | These functions will first look for a symbolic link called | ||
| 221 | .Pa /etc/malloc.conf | 207 | .Pa /etc/malloc.conf |
| 222 | and next check the environment for a variable called | 208 | and next check the environment for a variable called |
| 223 | .Ev MALLOC_OPTIONS | 209 | .Ev MALLOC_OPTIONS |
| @@ -272,8 +258,6 @@ sizeof(ptr) errors where sizeof(*ptr) is meant. | |||
| 272 | .Dq realloc . | 258 | .Dq realloc . |
| 273 | Always reallocate when | 259 | Always reallocate when |
| 274 | .Fn realloc | 260 | .Fn realloc |
| 275 | or | ||
| 276 | .Fn recalloc | ||
| 277 | is called, even if the initial allocation was big enough. | 261 | is called, even if the initial allocation was big enough. |
| 278 | This can substantially aid in compacting memory. | 262 | This can substantially aid in compacting memory. |
| 279 | .\".Pp | 263 | .\".Pp |
| @@ -458,12 +442,6 @@ The | |||
| 458 | .Fn malloc | 442 | .Fn malloc |
| 459 | function conforms to | 443 | function conforms to |
| 460 | .St -ansiC . | 444 | .St -ansiC . |
| 461 | .Pp | ||
| 462 | The | ||
| 463 | .Fn recalloc | ||
| 464 | function is an | ||
| 465 | .Ox | ||
| 466 | extension. | ||
| 467 | .Sh HISTORY | 445 | .Sh HISTORY |
| 468 | The present implementation of | 446 | The present implementation of |
| 469 | .Fn malloc | 447 | .Fn malloc |
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index c4869527ad..a4a4e52bda 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: malloc.c,v 1.89 2008/04/13 00:22:16 djm Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.90 2008/05/19 19:36:15 otto Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * ---------------------------------------------------------------------------- | 4 | * ---------------------------------------------------------------------------- |
| @@ -250,9 +250,9 @@ static char *malloc_func; | |||
| 250 | /* | 250 | /* |
| 251 | * Necessary function declarations. | 251 | * Necessary function declarations. |
| 252 | */ | 252 | */ |
| 253 | static void *imalloc(size_t size, int zero_fill); | 253 | static void *imalloc(size_t size); |
| 254 | static void ifree(void *ptr); | 254 | static void ifree(void *ptr); |
| 255 | static void *irealloc(void *ptr, size_t size, int zero_fill); | 255 | static void *irealloc(void *ptr, size_t size); |
| 256 | static void *malloc_bytes(size_t size); | 256 | static void *malloc_bytes(size_t size); |
| 257 | 257 | ||
| 258 | static struct pginfo *pginfo_list; | 258 | static struct pginfo *pginfo_list; |
| @@ -1188,7 +1188,7 @@ malloc_bytes(size_t size) | |||
| 1188 | * Allocate a piece of memory | 1188 | * Allocate a piece of memory |
| 1189 | */ | 1189 | */ |
| 1190 | static void * | 1190 | static void * |
| 1191 | imalloc(size_t size, int zero_fill) | 1191 | imalloc(size_t size) |
| 1192 | { | 1192 | { |
| 1193 | void *result; | 1193 | void *result; |
| 1194 | int ptralloc = 0; | 1194 | int ptralloc = 0; |
| @@ -1218,7 +1218,7 @@ imalloc(size_t size, int zero_fill) | |||
| 1218 | if (malloc_abort == 1 && result == NULL) | 1218 | if (malloc_abort == 1 && result == NULL) |
| 1219 | wrterror("allocation failed"); | 1219 | wrterror("allocation failed"); |
| 1220 | 1220 | ||
| 1221 | if ((malloc_zero || zero_fill) && result != NULL) | 1221 | if (malloc_zero && result != NULL) |
| 1222 | memset(result, 0, size); | 1222 | memset(result, 0, size); |
| 1223 | 1223 | ||
| 1224 | if (result && ptralloc) | 1224 | if (result && ptralloc) |
| @@ -1230,7 +1230,7 @@ imalloc(size_t size, int zero_fill) | |||
| 1230 | * Change the size of an allocation. | 1230 | * Change the size of an allocation. |
| 1231 | */ | 1231 | */ |
| 1232 | static void * | 1232 | static void * |
| 1233 | irealloc(void *ptr, size_t size, int zero_fill) | 1233 | irealloc(void *ptr, size_t size) |
| 1234 | { | 1234 | { |
| 1235 | void *p; | 1235 | void *p; |
| 1236 | size_t osize; | 1236 | size_t osize; |
| @@ -1253,7 +1253,7 @@ irealloc(void *ptr, size_t size, int zero_fill) | |||
| 1253 | if (size <= PTR_SIZE) | 1253 | if (size <= PTR_SIZE) |
| 1254 | return (ptr); | 1254 | return (ptr); |
| 1255 | 1255 | ||
| 1256 | p = imalloc(size, zero_fill); | 1256 | p = imalloc(size); |
| 1257 | if (p) | 1257 | if (p) |
| 1258 | memcpy(p, ptr, PTR_SIZE); | 1258 | memcpy(p, ptr, PTR_SIZE); |
| 1259 | ifree(ptr); | 1259 | ifree(ptr); |
| @@ -1315,9 +1315,7 @@ irealloc(void *ptr, size_t size, int zero_fill) | |||
| 1315 | 1315 | ||
| 1316 | if (!malloc_realloc && size <= osize && | 1316 | if (!malloc_realloc && size <= osize && |
| 1317 | size > osize - malloc_pagesize) { | 1317 | size > osize - malloc_pagesize) { |
| 1318 | if (zero_fill) | 1318 | if (malloc_junk) |
| 1319 | memset((char *)ptr + size, 0, osize - size); | ||
| 1320 | else if (malloc_junk) | ||
| 1321 | memset((char *)ptr + size, SOME_JUNK, osize - size); | 1319 | memset((char *)ptr + size, SOME_JUNK, osize - size); |
| 1322 | return (ptr); /* ..don't do anything else. */ | 1320 | return (ptr); /* ..don't do anything else. */ |
| 1323 | } | 1321 | } |
| @@ -1340,9 +1338,7 @@ irealloc(void *ptr, size_t size, int zero_fill) | |||
| 1340 | 1338 | ||
| 1341 | if (!malloc_realloc && size <= osize && | 1339 | if (!malloc_realloc && size <= osize && |
| 1342 | (size > osize / 2 || osize == malloc_minsize)) { | 1340 | (size > osize / 2 || osize == malloc_minsize)) { |
| 1343 | if (zero_fill) | 1341 | if (malloc_junk) |
| 1344 | memset((char *) ptr + size, 0, osize - size); | ||
| 1345 | else if (malloc_junk) | ||
| 1346 | memset((char *) ptr + size, SOME_JUNK, osize - size); | 1342 | memset((char *) ptr + size, SOME_JUNK, osize - size); |
| 1347 | return (ptr); /* ..don't do anything else. */ | 1343 | return (ptr); /* ..don't do anything else. */ |
| 1348 | } | 1344 | } |
| @@ -1351,7 +1347,7 @@ irealloc(void *ptr, size_t size, int zero_fill) | |||
| 1351 | return (NULL); | 1347 | return (NULL); |
| 1352 | } | 1348 | } |
| 1353 | 1349 | ||
| 1354 | p = imalloc(size, zero_fill); | 1350 | p = imalloc(size); |
| 1355 | 1351 | ||
| 1356 | if (p != NULL) { | 1352 | if (p != NULL) { |
| 1357 | /* copy the lesser of the two sizes, and free the old one */ | 1353 | /* copy the lesser of the two sizes, and free the old one */ |
| @@ -1880,7 +1876,7 @@ malloc(size_t size) | |||
| 1880 | malloc_recurse(); | 1876 | malloc_recurse(); |
| 1881 | return (NULL); | 1877 | return (NULL); |
| 1882 | } | 1878 | } |
| 1883 | r = imalloc(size, 0); | 1879 | r = imalloc(size); |
| 1884 | UTRACE(0, size, r); | 1880 | UTRACE(0, size, r); |
| 1885 | malloc_active--; | 1881 | malloc_active--; |
| 1886 | _MALLOC_UNLOCK(); | 1882 | _MALLOC_UNLOCK(); |
| @@ -1911,8 +1907,8 @@ free(void *ptr) | |||
| 1911 | return; | 1907 | return; |
| 1912 | } | 1908 | } |
| 1913 | 1909 | ||
| 1914 | static void * | 1910 | void * |
| 1915 | _realloc(void *ptr, size_t size, int zero_fill) | 1911 | realloc(void *ptr, size_t size) |
| 1916 | { | 1912 | { |
| 1917 | void *r; | 1913 | void *r; |
| 1918 | 1914 | ||
| @@ -1924,9 +1920,9 @@ _realloc(void *ptr, size_t size, int zero_fill) | |||
| 1924 | } | 1920 | } |
| 1925 | 1921 | ||
| 1926 | if (ptr == NULL) | 1922 | if (ptr == NULL) |
| 1927 | r = imalloc(size, zero_fill); | 1923 | r = imalloc(size); |
| 1928 | else | 1924 | else |
| 1929 | r = irealloc(ptr, size, zero_fill); | 1925 | r = irealloc(ptr, size); |
| 1930 | 1926 | ||
| 1931 | UTRACE(ptr, size, r); | 1927 | UTRACE(ptr, size, r); |
| 1932 | malloc_active--; | 1928 | malloc_active--; |
| @@ -1937,19 +1933,3 @@ _realloc(void *ptr, size_t size, int zero_fill) | |||
| 1937 | } | 1933 | } |
| 1938 | return (r); | 1934 | return (r); |
| 1939 | } | 1935 | } |
| 1940 | |||
| 1941 | void * | ||
| 1942 | realloc(void *ptr, size_t size) | ||
| 1943 | { | ||
| 1944 | return (_realloc(ptr, size, 0)); | ||
| 1945 | } | ||
| 1946 | |||
| 1947 | void * | ||
| 1948 | recalloc(void *ptr, size_t nmemb, size_t size) | ||
| 1949 | { | ||
| 1950 | if (nmemb && SIZE_MAX / nmemb < size) { | ||
| 1951 | errno = ENOMEM; | ||
| 1952 | return (NULL); | ||
| 1953 | } | ||
| 1954 | return (_realloc(ptr, nmemb * size, 1)); | ||
| 1955 | } | ||
