Graphviz  2.29.20120523.0446
lib/common/memory.c
Go to the documentation of this file.
00001 /* $Id$ $Revision$ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /*************************************************************************
00005  * Copyright (c) 2011 AT&T Intellectual Property 
00006  * All rights reserved. This program and the accompanying materials
00007  * are made available under the terms of the Eclipse Public License v1.0
00008  * which accompanies this distribution, and is available at
00009  * http://www.eclipse.org/legal/epl-v10.html
00010  *
00011  * Contributors: See CVS logs. Details at http://www.graphviz.org/
00012  *************************************************************************/
00013 
00014 #ifdef HAVE_CONFIG_H
00015 #include "config.h"
00016 #endif
00017 
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include "memory.h"
00021 
00022 void *zmalloc(size_t nbytes)
00023 {
00024     char *rv;
00025     if (nbytes == 0)
00026         return 0;
00027     rv = gmalloc(nbytes);
00028     memset(rv, 0, nbytes);
00029     return rv;
00030 }
00031 
00032 void *zrealloc(void *ptr, size_t size, size_t elt, size_t osize)
00033 {
00034     void *p = realloc(ptr, size * elt);
00035     if (p == NULL && size) {
00036         fprintf(stderr, "out of memory\n");
00037         return p;
00038     }
00039     if (osize < size)
00040         memset((char *) p + (osize * elt), '\0', (size - osize) * elt);
00041     return p;
00042 }
00043 
00044 void *gmalloc(size_t nbytes)
00045 {
00046     char *rv;
00047     if (nbytes == 0)
00048         return NULL;
00049     rv = malloc(nbytes);
00050     if (rv == NULL) {
00051         fprintf(stderr, "out of memory\n");
00052     }
00053     return rv;
00054 }
00055 
00056 void *grealloc(void *ptr, size_t size)
00057 {
00058     void *p = realloc(ptr, size);
00059     if (p == NULL && size) {
00060         fprintf(stderr, "out of memory\n");
00061     }
00062     return p;
00063 }