X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Falloc.c;h=8d1e39a633e78a967a1315b6c799e72afd33bd22;hb=dc4eb33f68d5eebae670115752d3994f4f3d5c41;hp=0da8f74490fc6082b6e38755e97c30f4bd4bb128;hpb=205ac3afa4c2b19c85819d8695abf3975bb11807;p=pspp diff --git a/src/alloc.c b/src/alloc.c index 0da8f74490..8d1e39a633 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -14,17 +14,15 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA - 02111-1307, USA. */ + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. */ #include #include "alloc.h" -#include "error.h" #include #include +#include "error.h" #include "str.h" - -static void out_of_memory (void); /* Public functions. */ @@ -45,18 +43,31 @@ xmalloc (size_t size) return vp; } -/* Allocates a block of SIZE bytes, fill it with all-bits-0, and - returns it. - If SIZE is 0, returns a null pointer. - Aborts if unsuccessful. */ + +/* Allocates a continous block of N_MEMB by SIZE elements, with all + bits set to 0. + Aborts if unsuccessful. +*/ void * -xcalloc (size_t size) +xcalloc (size_t n_memb, size_t size) { - void *vp = xmalloc (size); - memset (vp, 0, size); + const size_t prod = size * n_memb; + void *vp = 0; + + if (prod == 0) + return NULL; + + /* Trap overflow errors */ + assert ( prod >= size ); + assert ( prod >= n_memb ) ; + + vp = xmalloc ( prod ); + memset (vp, 0, prod); return vp; } + + /* If SIZE is 0, then block PTR is freed and a null pointer is returned. Otherwise, if PTR is a null pointer, then a new block is allocated @@ -106,11 +117,9 @@ xstrdup (const char *s) memcpy (t, s, size); return t; } - -/* Private functions. */ /* Report an out-of-memory condition and abort execution. */ -static void +void out_of_memory (void) { fprintf (stderr, "virtual memory exhausted\n");