2012-03-02 17:59:36 +05:30
|
|
|
/*
|
|
|
|
* alloc.c - memory allocation functions
|
|
|
|
* Copyright (C) 1992-1998 by Michael K. Johnson, johnsonm@redhat.com
|
|
|
|
* Copyright 2002 Albert Cahalan
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2002-12-15 06:00:17 +05:30
|
|
|
|
2011-11-16 22:19:02 +05:30
|
|
|
#include <stdarg.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <stdio.h>
|
2011-11-16 22:19:02 +05:30
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2002-12-09 12:30:07 +05:30
|
|
|
#include "alloc.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2011-11-16 22:19:02 +05:30
|
|
|
static void xdefault_error(const char *restrict fmts, ...) __attribute__((format(printf,1,2)));
|
|
|
|
static void xdefault_error(const char *restrict fmts, ...) {
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, fmts);
|
1970-01-01 05:30:00 +05:30
|
|
|
vfprintf(stderr, fmts, va);
|
2011-11-16 22:19:02 +05:30
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
message_fn xalloc_err_handler = xdefault_error;
|
|
|
|
|
|
|
|
|
|
|
|
void *xcalloc(unsigned int size) {
|
|
|
|
void * p;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
++size;
|
|
|
|
p = calloc(1, size);
|
|
|
|
if (!p) {
|
|
|
|
xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
|
|
|
|
exit(EXIT_FAILURE);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
2011-11-16 22:19:02 +05:30
|
|
|
return p;
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
|
2012-11-02 23:20:50 +05:30
|
|
|
void *xmalloc(size_t size) {
|
2002-02-02 04:17:29 +05:30
|
|
|
void *p;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
++size;
|
|
|
|
p = malloc(size);
|
|
|
|
if (!p) {
|
2012-11-02 23:20:50 +05:30
|
|
|
xalloc_err_handler("%s failed to allocate %zu bytes of memory", __func__, size);
|
2011-11-16 22:19:02 +05:30
|
|
|
exit(EXIT_FAILURE);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *xrealloc(void *oldp, unsigned int size) {
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
++size;
|
|
|
|
p = realloc(oldp, size);
|
|
|
|
if (!p) {
|
2011-11-16 22:19:02 +05:30
|
|
|
xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xstrdup(const char *str) {
|
|
|
|
char *p = NULL;
|
|
|
|
|
|
|
|
if (str) {
|
|
|
|
unsigned int size = strlen(str) + 1;
|
|
|
|
p = malloc(size);
|
|
|
|
if (!p) {
|
|
|
|
xalloc_err_handler("%s failed to allocate %u bytes of memory", __func__, size);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
strcpy(p, str);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|
|
|
|
return(p);
|
|
|
|
}
|