diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.c')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.c | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 67f5de512e..08b8a088d0 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.86 2007/02/12 20:00:14 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.87 2007/09/03 14:37:02 millert 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); | 253 | static void *imalloc(size_t size, int zero_fill); |
| 254 | static void ifree(void *ptr); | 254 | static void ifree(void *ptr); |
| 255 | static void *irealloc(void *ptr, size_t size); | 255 | static void *irealloc(void *ptr, size_t size, int zero_fill); |
| 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) | 1191 | imalloc(size_t size, int zero_fill) |
| 1192 | { | 1192 | { |
| 1193 | void *result; | 1193 | void *result; |
| 1194 | int ptralloc = 0; | 1194 | int ptralloc = 0; |
| @@ -1218,7 +1218,7 @@ imalloc(size_t size) | |||
| 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 && result != NULL) | 1221 | if ((malloc_zero || zero_fill) && 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) | |||
| 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) | 1233 | irealloc(void *ptr, size_t size, int zero_fill) |
| 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) | |||
| 1253 | if (size <= PTR_SIZE) | 1253 | if (size <= PTR_SIZE) |
| 1254 | return (ptr); | 1254 | return (ptr); |
| 1255 | 1255 | ||
| 1256 | p = imalloc(size); | 1256 | p = imalloc(size, zero_fill); |
| 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,7 +1315,9 @@ irealloc(void *ptr, size_t size) | |||
| 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 (malloc_junk) | 1318 | if (zero_fill) |
| 1319 | memset((char *)ptr + size, 0, osize - size); | ||
| 1320 | else if (malloc_junk) | ||
| 1319 | memset((char *)ptr + size, SOME_JUNK, osize - size); | 1321 | memset((char *)ptr + size, SOME_JUNK, osize - size); |
| 1320 | return (ptr); /* ..don't do anything else. */ | 1322 | return (ptr); /* ..don't do anything else. */ |
| 1321 | } | 1323 | } |
| @@ -1338,7 +1340,9 @@ irealloc(void *ptr, size_t size) | |||
| 1338 | 1340 | ||
| 1339 | if (!malloc_realloc && size <= osize && | 1341 | if (!malloc_realloc && size <= osize && |
| 1340 | (size > osize / 2 || osize == malloc_minsize)) { | 1342 | (size > osize / 2 || osize == malloc_minsize)) { |
| 1341 | if (malloc_junk) | 1343 | if (zero_fill) |
| 1344 | memset((char *) ptr + size, 0, osize - size); | ||
| 1345 | else if (malloc_junk) | ||
| 1342 | memset((char *) ptr + size, SOME_JUNK, osize - size); | 1346 | memset((char *) ptr + size, SOME_JUNK, osize - size); |
| 1343 | return (ptr); /* ..don't do anything else. */ | 1347 | return (ptr); /* ..don't do anything else. */ |
| 1344 | } | 1348 | } |
| @@ -1347,7 +1351,7 @@ irealloc(void *ptr, size_t size) | |||
| 1347 | return (NULL); | 1351 | return (NULL); |
| 1348 | } | 1352 | } |
| 1349 | 1353 | ||
| 1350 | p = imalloc(size); | 1354 | p = imalloc(size, zero_fill); |
| 1351 | 1355 | ||
| 1352 | if (p != NULL) { | 1356 | if (p != NULL) { |
| 1353 | /* copy the lesser of the two sizes, and free the old one */ | 1357 | /* copy the lesser of the two sizes, and free the old one */ |
| @@ -1876,7 +1880,7 @@ malloc(size_t size) | |||
| 1876 | malloc_recurse(); | 1880 | malloc_recurse(); |
| 1877 | return (NULL); | 1881 | return (NULL); |
| 1878 | } | 1882 | } |
| 1879 | r = imalloc(size); | 1883 | r = imalloc(size, 0); |
| 1880 | UTRACE(0, size, r); | 1884 | UTRACE(0, size, r); |
| 1881 | malloc_active--; | 1885 | malloc_active--; |
| 1882 | _MALLOC_UNLOCK(); | 1886 | _MALLOC_UNLOCK(); |
| @@ -1907,8 +1911,8 @@ free(void *ptr) | |||
| 1907 | return; | 1911 | return; |
| 1908 | } | 1912 | } |
| 1909 | 1913 | ||
| 1910 | void * | 1914 | static void * |
| 1911 | realloc(void *ptr, size_t size) | 1915 | _realloc(void *ptr, size_t size, int zero_fill) |
| 1912 | { | 1916 | { |
| 1913 | void *r; | 1917 | void *r; |
| 1914 | 1918 | ||
| @@ -1920,9 +1924,9 @@ realloc(void *ptr, size_t size) | |||
| 1920 | } | 1924 | } |
| 1921 | 1925 | ||
| 1922 | if (ptr == NULL) | 1926 | if (ptr == NULL) |
| 1923 | r = imalloc(size); | 1927 | r = imalloc(size, zero_fill); |
| 1924 | else | 1928 | else |
| 1925 | r = irealloc(ptr, size); | 1929 | r = irealloc(ptr, size, zero_fill); |
| 1926 | 1930 | ||
| 1927 | UTRACE(ptr, size, r); | 1931 | UTRACE(ptr, size, r); |
| 1928 | malloc_active--; | 1932 | malloc_active--; |
| @@ -1933,3 +1937,19 @@ realloc(void *ptr, size_t size) | |||
| 1933 | } | 1937 | } |
| 1934 | return (r); | 1938 | return (r); |
| 1935 | } | 1939 | } |
| 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 | } | ||
