Home | History | Annotate | Line # | Download | only in dist
      1  1.2  christos /*	$OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $	*/
      2  1.1  christos 
      3  1.2  christos /*
      4  1.2  christos  * Copyright (c) 2008, 2017 Otto Moerbeek <otto (at) drijf.net>
      5  1.1  christos  *
      6  1.2  christos  * Permission to use, copy, modify, and distribute this software for any
      7  1.2  christos  * purpose with or without fee is hereby granted, provided that the above
      8  1.2  christos  * copyright notice and this permission notice appear in all copies.
      9  1.1  christos  *
     10  1.2  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  1.2  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  1.2  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  1.2  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  1.2  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  1.2  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  1.2  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  1.1  christos  */
     18  1.1  christos 
     19  1.2  christos /* OPENBSD ORIGINAL: lib/libc/stdlib/recallocarray.c */
     20  1.2  christos 
     21  1.1  christos #include "includes.h"
     22  1.2  christos #ifndef HAVE_RECALLOCARRAY
     23  1.3  christos __RCSID("$NetBSD: recallocarray.c,v 1.3 2025/04/09 15:49:32 christos Exp $");
     24  1.1  christos 
     25  1.1  christos #include <errno.h>
     26  1.2  christos #include <stdlib.h>
     27  1.2  christos #ifdef HAVE_STDINT_H
     28  1.2  christos #include <stdint.h>
     29  1.2  christos #endif
     30  1.1  christos #include <string.h>
     31  1.2  christos #include <unistd.h>
     32  1.2  christos 
     33  1.2  christos /*
     34  1.2  christos  * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
     35  1.2  christos  * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
     36  1.2  christos  */
     37  1.2  christos #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
     38  1.1  christos 
     39  1.1  christos void *
     40  1.2  christos recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size)
     41  1.1  christos {
     42  1.2  christos 	size_t oldsize, newsize;
     43  1.2  christos 	void *newptr;
     44  1.2  christos 
     45  1.2  christos 	if (ptr == NULL)
     46  1.2  christos 		return calloc(newnmemb, size);
     47  1.1  christos 
     48  1.2  christos 	if ((newnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
     49  1.2  christos 	    newnmemb > 0 && SIZE_MAX / newnmemb < size) {
     50  1.2  christos 		errno = ENOMEM;
     51  1.2  christos 		return NULL;
     52  1.2  christos 	}
     53  1.2  christos 	newsize = newnmemb * size;
     54  1.2  christos 
     55  1.2  christos 	if ((oldnmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
     56  1.2  christos 	    oldnmemb > 0 && SIZE_MAX / oldnmemb < size) {
     57  1.2  christos 		errno = EINVAL;
     58  1.2  christos 		return NULL;
     59  1.2  christos 	}
     60  1.2  christos 	oldsize = oldnmemb * size;
     61  1.2  christos 
     62  1.2  christos 	/*
     63  1.2  christos 	 * Don't bother too much if we're shrinking just a bit,
     64  1.2  christos 	 * we do not shrink for series of small steps, oh well.
     65  1.2  christos 	 */
     66  1.2  christos 	if (newsize <= oldsize) {
     67  1.2  christos 		size_t d = oldsize - newsize;
     68  1.2  christos 
     69  1.2  christos 		if (d < oldsize / 2 && d < (size_t)getpagesize()) {
     70  1.2  christos 			memset((char *)ptr + newsize, 0, d);
     71  1.2  christos 			return ptr;
     72  1.2  christos 		}
     73  1.2  christos 	}
     74  1.2  christos 
     75  1.2  christos 	newptr = malloc(newsize);
     76  1.2  christos 	if (newptr == NULL)
     77  1.2  christos 		return NULL;
     78  1.2  christos 
     79  1.2  christos 	if (newsize > oldsize) {
     80  1.2  christos 		memcpy(newptr, ptr, oldsize);
     81  1.2  christos 		memset((char *)newptr + oldsize, 0, newsize - oldsize);
     82  1.2  christos 	} else
     83  1.2  christos 		memcpy(newptr, ptr, newsize);
     84  1.1  christos 
     85  1.2  christos 	explicit_bzero(ptr, oldsize);
     86  1.2  christos 	free(ptr);
     87  1.2  christos 
     88  1.2  christos 	return newptr;
     89  1.1  christos }
     90  1.2  christos /* DEF_WEAK(recallocarray); */
     91  1.2  christos 
     92  1.2  christos #endif /* HAVE_RECALLOCARRAY */
     93