diff options
author | millert <> | 2007-09-03 14:37:02 +0000 |
---|---|---|
committer | millert <> | 2007-09-03 14:37:02 +0000 |
commit | 2056049ed6b7a028e611838a742280fced6d2c23 (patch) | |
tree | e20e18fc5434b1b49708f65ad179f4ae2602951b /src/lib | |
parent | 6fdd2cf23672d7bc626fee11d4ca588e6882a436 (diff) | |
download | openbsd-2056049ed6b7a028e611838a742280fced6d2c23.tar.gz openbsd-2056049ed6b7a028e611838a742280fced6d2c23.tar.bz2 openbsd-2056049ed6b7a028e611838a742280fced6d2c23.zip |
add recaloc(3)
Diffstat (limited to 'src/lib')
-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, 62 insertions, 20 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index c7ee0a80ec..04e84fe941 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.35 2006/01/13 17:58:09 millert Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.36 2007/09/03 14:37:02 millert 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 cfree.3 malloc.3 malloc.conf.5 | 58 | MLINKS+=malloc.3 recalloc.3 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 35f83a0c24..4c5ea84c08 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -30,15 +30,16 @@ | |||
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.47 2007/08/08 21:24:29 millert Exp $ | 33 | .\" $OpenBSD: malloc.3,v 1.48 2007/09/03 14:37:02 millert Exp $ |
34 | .\" | 34 | .\" |
35 | .Dd $Mdocdate: August 8 2007 $ | 35 | .Dd $Mdocdate: September 3 2007 $ |
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 , | ||
42 | .Nm free , | 43 | .Nm free , |
43 | .Nm cfree | 44 | .Nm cfree |
44 | .Nd memory allocation and deallocation | 45 | .Nd memory allocation and deallocation |
@@ -50,6 +51,8 @@ | |||
50 | .Fn calloc "size_t nmemb" "size_t size" | 51 | .Fn calloc "size_t nmemb" "size_t size" |
51 | .Ft void * | 52 | .Ft void * |
52 | .Fn realloc "void *ptr" "size_t size" | 53 | .Fn realloc "void *ptr" "size_t size" |
54 | .Ft void * | ||
55 | .Fn recalloc "void *ptr" "size_t nmemb" "size_t size" | ||
53 | .Ft void | 56 | .Ft void |
54 | .Fn free "void *ptr" | 57 | .Fn free "void *ptr" |
55 | .Ft void | 58 | .Ft void |
@@ -203,7 +206,18 @@ if ((newp = realloc(p, num * size)) == NULL) { | |||
203 | ... | 206 | ... |
204 | .Ed | 207 | .Ed |
205 | .Pp | 208 | .Pp |
206 | Malloc will first look for a symbolic link called | 209 | The |
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 | ||
207 | .Pa /etc/malloc.conf | 221 | .Pa /etc/malloc.conf |
208 | and next check the environment for a variable called | 222 | and next check the environment for a variable called |
209 | .Ev MALLOC_OPTIONS | 223 | .Ev MALLOC_OPTIONS |
@@ -258,6 +272,8 @@ sizeof(ptr) errors where sizeof(*ptr) is meant. | |||
258 | .Dq realloc . | 272 | .Dq realloc . |
259 | Always reallocate when | 273 | Always reallocate when |
260 | .Fn realloc | 274 | .Fn realloc |
275 | or | ||
276 | .Fn recalloc | ||
261 | is called, even if the initial allocation was big enough. | 277 | is called, even if the initial allocation was big enough. |
262 | This can substantially aid in compacting memory. | 278 | This can substantially aid in compacting memory. |
263 | .\".Pp | 279 | .\".Pp |
@@ -440,6 +456,12 @@ The | |||
440 | .Fn malloc | 456 | .Fn malloc |
441 | function conforms to | 457 | function conforms to |
442 | .St -ansiC . | 458 | .St -ansiC . |
459 | .Pp | ||
460 | The | ||
461 | .Fn recalloc | ||
462 | function is an | ||
463 | .Ox | ||
464 | extension. | ||
443 | .Sh HISTORY | 465 | .Sh HISTORY |
444 | The present implementation of | 466 | The present implementation of |
445 | .Fn malloc | 467 | .Fn malloc |
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 | } | ||