Julius 4.2
libsent/src/util/mymalloc.c
説明を見る。
00001 
00023 /*
00024  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00025  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00026  * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology
00027  * All rights reserved
00028  */
00029 
00030 #include <sent/stddefs.h>
00031 
00032 
00040 void *
00041 mymalloc(size_t size)
00042 {
00043   void *p;
00044   if ( (p = malloc(size)) == NULL) {
00045 #if defined(_WIN32) && !defined(__CYGWIN32__)
00046     jlog("Error: mymalloc: failed to allocate %Iu bytes\n", size);
00047 #else
00048     jlog("Error: mymalloc: failed to allocate %zu bytes\n", size);
00049 #endif
00050     exit(1);
00051   }
00052   return p;
00053 }
00054 
00062 void *
00063 mymalloc_big(size_t elsize, size_t nelem)
00064 {
00065   void *p;
00066   double limit;
00067 
00068   if (sizeof(size_t) == 4) {    /* 32bit environment */
00069     limit = (double)4294967296.0 / elsize; /* 2^32 */
00070     if (nelem >= limit) {
00071 #if defined(_WIN32) && !defined(__CYGWIN32__)
00072       jlog("Error: mymalloc_big: %Iu bytes x %Iu unit exceeds 4GB limit\n", elsize, nelem);
00073 #else
00074       jlog("Error: mymalloc_big: %zu bytes x %zu unit exceeds 4GB limit\n", elsize, nelem);
00075 #endif
00076       exit(1);
00077     }
00078   }
00079   if ( (p = malloc(nelem * elsize)) == NULL) {
00080 #if defined(_WIN32) && !defined(__CYGWIN32__)
00081     jlog("Error: mymalloc_big: failed to allocate %Iu x %Iu bytes\n", elsize, nelem);
00082 #else
00083     jlog("Error: mymalloc_big: failed to allocate %zu x %zu bytes\n", elsize, nelem);
00084 #endif
00085     exit(1);
00086   }
00087   return p;
00088 }
00089 
00098 void *
00099 myrealloc(void *ptr, size_t size)
00100 {
00101   void *p;
00102   if ( (p = realloc(ptr,size)) == NULL) {
00103 #if defined(_WIN32) && !defined(__CYGWIN32__)
00104     jlog("Error: mymalloc: failed to reallocate %Iu bytes\n", size);
00105 #else
00106     jlog("Error: mymalloc: failed to reallocate %zu bytes\n", size);
00107 #endif
00108     exit(1);
00109   }
00110   return p;
00111 }
00112 
00121 void *
00122 mycalloc(size_t nelem, size_t elsize)
00123 {
00124   void *p;
00125   if ( (p = calloc(nelem,elsize)) == NULL) {
00126 #if defined(_WIN32) && !defined(__CYGWIN32__)
00127     jlog("Error: mymalloc: failed to clear-allocate %Iu bytes\n", nelem*elsize);
00128 #else
00129     jlog("Error: mymalloc: failed to clear-allocate %zu bytes\n", nelem*elsize);
00130 #endif
00131     exit(1);
00132   }
00133   return p;
00134 }
00135