Back to index

tetex-bin  3.0
doalloc.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey <dickey@clark.net> 1998                        *
00031  ****************************************************************************/
00032 
00033 /*
00034  * Wrapper for malloc/realloc.  Standard implementations allow realloc with
00035  * a null pointer, but older libraries may not (e.g., SunOS).
00036  *
00037  * Also if realloc fails, we discard the old memory to avoid leaks.
00038  */
00039 
00040 #include <curses.priv.h>
00041 
00042 MODULE_ID("$Id: doalloc.c,v 1.8 2002/08/31 21:48:11 Philippe.Blain Exp $")
00043 
00044 NCURSES_EXPORT(void *)
00045 _nc_doalloc(void *oldp, size_t amount)
00046 {
00047     void *newp;
00048 
00049     if (oldp != 0) {
00050        if ((newp = realloc(oldp, amount)) == 0) {
00051            free(oldp);
00052            errno = ENOMEM;  /* just in case 'free' reset */
00053        }
00054     } else {
00055        newp = malloc(amount);
00056     }
00057     return newp;
00058 }
00059 
00060 #if !HAVE_STRDUP
00061 NCURSES_EXPORT(char *)
00062 _nc_strdup(const char *src)
00063 {
00064     char *dst;
00065     if (src != 0) {
00066        dst = typeMalloc(char, strlen(src) + 1);
00067        if (dst != 0) {
00068            (void) strcpy(dst, src);
00069        }
00070     } else {
00071        dst = 0;
00072     }
00073     return dst;
00074 }
00075 #endif