root/libutil/checkalloc.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. check_malloc
  2. check_calloc
  3. check_realloc
  4. check_strdup

   1 /*
   2  * Copyright (c) 2006 Tama Communications Corporation
   3  *
   4  * This file is part of GNU GLOBAL.
   5  *
   6  * This program is free software: you can redistribute it and/or modify
   7  * it under the terms of the GNU General Public License as published by
   8  * the Free Software Foundation, either version 3 of the License, or
   9  * (at your option) any later version.
  10  * 
  11  * This program is distributed in the hope that it will be useful,
  12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14  * GNU General Public License for more details.
  15  * 
  16  * You should have received a copy of the GNU General Public License
  17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18  */
  19 
  20 #ifdef HAVE_CONFIG_H
  21 #include <config.h>
  22 #endif
  23 #include <stdlib.h>
  24 #include <string.h>
  25 
  26 #include "checkalloc.h"
  27 #include "die.h"
  28 /**
  29  * @file
  30  * Functions which allocate memory with check.
  31 */
  32 
  33 /**
  34  * @details
  35  * check_malloc: memory allocator
  36  *
  37  * Uses @NAME{malloc()}.
  38  *
  39  * @note Does not return if memory is not available, calls die() instead.
  40  */
  41 void *
  42 check_malloc(size_t size)
  43 {
  44         void *p = (void *)malloc(size);
  45         if (p == NULL)
  46                 die("short of memory.");
  47         return p;
  48 }
  49 
  50 /**
  51  * @details
  52  * check_calloc: memory allocator
  53  *
  54  * Uses @NAME{calloc()}.
  55  *
  56  * @note Does not return if memory is not available, calls die() instead.
  57  */
  58 void *
  59 check_calloc(size_t number, size_t size)
  60 {
  61         void *p = (void *)calloc(number, size);
  62         if (p == NULL)
  63                 die("short of memory.");
  64         return p;
  65 }
  66 /**
  67  * @details
  68  * check_realloc: memory allocator
  69  *
  70  * Uses @NAME{realloc()}.
  71  *
  72  * @note Does not return if memory is not available, calls die() instead.
  73  */
  74 void *
  75 check_realloc(void *area, size_t size)
  76 {
  77         void *p = (void *)realloc(area, size);
  78         if (p == NULL)
  79                 die("short of memory.");
  80         return p;
  81 }
  82 /**
  83  * @details
  84  * check_strdup: allocate memory and copy string to it.
  85  *
  86  *      @param[in]      string  original string
  87  *      @return         allocated memory
  88  *
  89  * Uses check_malloc().
  90  */
  91 char *
  92 check_strdup(const char *string)
  93 {
  94         char *p = check_malloc(strlen(string) + 1);
  95         strcpy(p, string);
  96         return p;
  97 }

/* [previous][next][first][last][top][bottom][index][help] */