Annotation of embedaddon/ntp/libntp/emalloc.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * emalloc - return new memory obtained from the system.  Belch if none.
                      3:  */
                      4: #include <config.h>
                      5: #include "ntp_types.h"
                      6: #include "ntp_malloc.h"
                      7: #include "ntp_syslog.h"
                      8: #include "ntp_stdlib.h"
                      9: 
                     10: 
                     11: /*
                     12:  * When using the debug MS CRT allocator, each allocation stores the
                     13:  * callsite __FILE__ and __LINE__, which is then displayed at process
                     14:  * termination, to track down leaks.  We don't want all of our
                     15:  * allocations to show up as coming from emalloc.c, so we preserve the
                     16:  * original callsite's source file and line using macros which pass
                     17:  * __FILE__ and __LINE__ as parameters to these routines.
                     18:  * Other debug malloc implementations can be used by defining
                     19:  * EREALLOC_IMPL() as ports/winnt/include/config.h does.
                     20:  */
                     21: 
                     22: void *
                     23: ereallocz(
                     24:        void *  ptr,
                     25:        size_t  newsz,
                     26:        size_t  priorsz,
                     27:        int     zero_init
                     28: #ifdef EREALLOC_CALLSITE               /* ntp_malloc.h */
                     29:                         ,
                     30:        const char *    file,
                     31:        int             line
                     32: #endif
                     33:        )
                     34: {
                     35:        char *  mem;
                     36:        size_t  allocsz;
                     37: 
                     38:        if (0 == newsz)
                     39:                allocsz = 1;
                     40:        else
                     41:                allocsz = newsz;
                     42: 
                     43:        mem = EREALLOC_IMPL(ptr, allocsz, file, line);
                     44:        if (NULL == mem) {
                     45:                msyslog_term = TRUE;
                     46: #ifndef EREALLOC_CALLSITE
                     47:                msyslog(LOG_ERR, "fatal out of memory (%lu bytes)",
                     48:                        (u_long)newsz);
                     49: #else
                     50:                msyslog(LOG_ERR,
                     51:                        "fatal out of memory %s line %d (%lu bytes)",
                     52:                        file, line, (u_long)newsz);
                     53: #endif
                     54:                exit(1);
                     55:        }
                     56: 
                     57:        if (zero_init && newsz > priorsz)
                     58:                zero_mem(mem + priorsz, newsz - priorsz);
                     59: 
                     60:        return mem;
                     61: }
                     62: 
                     63: 
                     64: char *
                     65: estrdup_impl(
                     66:        const char *    str
                     67: #ifdef EREALLOC_CALLSITE
                     68:                           ,
                     69:        const char *    file,
                     70:        int             line
                     71: #endif
                     72:        )
                     73: {
                     74:        char *  copy;
                     75:        size_t  bytes;
                     76: 
                     77:        bytes = strlen(str) + 1;
                     78:        copy = ereallocz(NULL, bytes, 0, FALSE
                     79: #ifdef EREALLOC_CALLSITE
                     80:                         , file, line
                     81: #endif
                     82:                         );
                     83:        memcpy(copy, str, bytes);
                     84: 
                     85:        return copy;
                     86: }
                     87: 
                     88: 
                     89: #ifndef EREALLOC_CALLSITE
                     90: void *
                     91: emalloc(size_t newsz)
                     92: {
                     93:        return ereallocz(NULL, newsz, 0, FALSE);
                     94: }
                     95: #endif
                     96: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>