memory.c

Go to the documentation of this file.
00001 /* Memory management routines.
00002  *
00003  * (C) 2003-2007 Anope Team
00004  * Contact us at info@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church. 
00010  * 
00011  * $Id: memory.c 1265 2007-08-26 15:33:06Z geniusdex $ 
00012  *
00013  */
00014 
00015 #include "services.h"
00016 
00017 /* smalloc, scalloc, srealloc, sstrdup:
00018  *  Versions of the memory allocation functions which will cause the
00019  *  program to terminate with an "Out of memory" error if the memory
00020  *  cannot be allocated.  (Hence, the return value from these functions
00021  *  is never NULL.)
00022  */
00023 
00024 /*************************************************************************/
00025 
00031 void *smalloc(long size)
00032 {
00033     void *buf;
00034 
00035     if (!size) {
00036         size = 1;
00037     }
00038     buf = malloc(size);
00039     if (!buf)
00040 #ifndef _WIN32
00041         raise(SIGUSR1);
00042 #else
00043         abort();
00044 #endif
00045     return buf;
00046 }
00047 
00048 /*************************************************************************/
00049 
00056 void *scalloc(long elsize, long els)
00057 {
00058     void *buf;
00059 
00060     if (!elsize || !els) {
00061         elsize = els = 1;
00062     }
00063     buf = calloc(elsize, els);
00064     if (!buf)
00065 #ifndef _WIN32
00066         raise(SIGUSR1);
00067 #else
00068         abort();
00069 #endif
00070     return buf;
00071 }
00072 
00073 /*************************************************************************/
00074 
00081 void *srealloc(void *oldptr, long newsize)
00082 {
00083     void *buf;
00084 
00085     if (!newsize) {
00086         newsize = 1;
00087     }
00088     buf = realloc(oldptr, newsize);
00089     if (!buf)
00090 #ifndef _WIN32
00091         raise(SIGUSR1);
00092 #else
00093         abort();
00094 #endif
00095     return buf;
00096 }
00097 
00098 /*************************************************************************/
00099 
00106 char *sstrdup(const char *src)
00107 {
00108     char *ret = NULL;
00109     if (src) {
00110 #ifdef __STRICT_ANSI__
00111         if ((ret = (char *) malloc(strlen(src) + 1))) {;
00112             strcpy(ret, src);
00113         }
00114 #else
00115         ret = strdup(src);
00116 #endif
00117         if (!ret)
00118 #ifndef _WIN32
00119             raise(SIGUSR1);
00120 #else
00121             abort();
00122 #endif
00123     } else {
00124         alog("sstrdup() called with NULL-arg");
00125         if (debug)
00126             do_backtrace(0);
00127     }
00128 
00129     return ret;
00130 }
00131 
00132 /*************************************************************************/
00133 /*************************************************************************/
00134 
00135 /* In the future: malloc() replacements that tell us if we're leaking and
00136  * maybe do sanity checks too... */
00137 
00138 /*************************************************************************/

Generated on Sun Dec 30 09:26:49 2007 for Anope by  doxygen 1.5.1-20070107