summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorotto <>2008-05-19 19:36:15 +0000
committerotto <>2008-05-19 19:36:15 +0000
commit64e5a691eaebd488c6d4609a36c21e319b92aef7 (patch)
treed57f66c3fe98f30ee573bedaad3d266449eec1c1
parent420c0a4b82db84eb999f5d8ea7b33cc5c1515150 (diff)
downloadopenbsd-64e5a691eaebd488c6d4609a36c21e319b92aef7.tar.gz
openbsd-64e5a691eaebd488c6d4609a36c21e319b92aef7.tar.bz2
openbsd-64e5a691eaebd488c6d4609a36c21e319b92aef7.zip
remove recalloc(3); it is buggy and impossible to repair without big
costs; ok jmc@ for the man page bits; ok millert@ deraadt@
-rw-r--r--src/lib/libc/stdlib/Makefile.inc4
-rw-r--r--src/lib/libc/stdlib/malloc.328
-rw-r--r--src/lib/libc/stdlib/malloc.c50
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
55MLINKS+=labs.3 llabs.3 55MLINKS+=labs.3 llabs.3
56MLINKS+=lsearch.3 lfind.3 56MLINKS+=lsearch.3 lfind.3
57MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 57MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3
58MLINKS+=malloc.3 recalloc.3 malloc.3 cfree.3 malloc.3 malloc.conf.5 58MLINKS+=malloc.3 cfree.3 malloc.3 malloc.conf.5
59MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 59MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3
60MLINKS+=radixsort.3 sradixsort.3 60MLINKS+=radixsort.3 sradixsort.3
61MLINKS+=rand.3 srand.3 rand.3 rand_r.3 61MLINKS+=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
209The 206Malloc will first look for a symbolic link called
210.Fn recalloc
211function is similar to
212.Fn realloc
213except that it shares semantics with
214.Fn calloc
215rather than
216.Fn malloc .
217Newly allocated space is initialized to zero and the resulting size is
218checked for integer overflow.
219.Pp
220These functions will first look for a symbolic link called
221.Pa /etc/malloc.conf 207.Pa /etc/malloc.conf
222and next check the environment for a variable called 208and 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 .
273Always reallocate when 259Always reallocate when
274.Fn realloc 260.Fn realloc
275or
276.Fn recalloc
277is called, even if the initial allocation was big enough. 261is called, even if the initial allocation was big enough.
278This can substantially aid in compacting memory. 262This can substantially aid in compacting memory.
279.\".Pp 263.\".Pp
@@ -458,12 +442,6 @@ The
458.Fn malloc 442.Fn malloc
459function conforms to 443function conforms to
460.St -ansiC . 444.St -ansiC .
461.Pp
462The
463.Fn recalloc
464function is an
465.Ox
466extension.
467.Sh HISTORY 445.Sh HISTORY
468The present implementation of 446The 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 */
253static void *imalloc(size_t size, int zero_fill); 253static void *imalloc(size_t size);
254static void ifree(void *ptr); 254static void ifree(void *ptr);
255static void *irealloc(void *ptr, size_t size, int zero_fill); 255static void *irealloc(void *ptr, size_t size);
256static void *malloc_bytes(size_t size); 256static void *malloc_bytes(size_t size);
257 257
258static struct pginfo *pginfo_list; 258static 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 */
1190static void * 1190static void *
1191imalloc(size_t size, int zero_fill) 1191imalloc(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 */
1232static void * 1232static void *
1233irealloc(void *ptr, size_t size, int zero_fill) 1233irealloc(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
1914static void * 1910void *
1915_realloc(void *ptr, size_t size, int zero_fill) 1911realloc(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
1941void *
1942realloc(void *ptr, size_t size)
1943{
1944 return (_realloc(ptr, size, 0));
1945}
1946
1947void *
1948recalloc(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}