projects
/
yaz-moved-to-github.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
yaz-iconv: fix typos
[yaz-moved-to-github.git]
/
src
/
xmalloc.c
diff --git
a/src/xmalloc.c
b/src/xmalloc.c
index
d48daa4
..
1dec219
100644
(file)
--- a/
src/xmalloc.c
+++ b/
src/xmalloc.c
@@
-1,8
+1,6
@@
-/*
- * Copyright (C) 1995-2005, Index Data ApS
- * All rights reserved.
- *
- * $Id: xmalloc.c,v 1.7 2006-04-21 10:28:07 adam Exp $
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) Index Data
+ * See the file LICENSE for details.
*/
/**
* \file xmalloc.c
*/
/**
* \file xmalloc.c
@@
-16,6
+14,7
@@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
#include <yaz/log.h>
#include <yaz/xmalloc.h>
#include <yaz/log.h>
#include <yaz/xmalloc.h>
@@
-24,8
+23,11
@@
#define TRACE_XMALLOC 1
#endif
#define TRACE_XMALLOC 1
#endif
-static int log_level=0;
-static int log_level_initialized=0;
+/* treat any size >1/4 of max value of size_t to be an error */
+#define MALLOC_SIZE_MAX ((size_t)(-1) / 4)
+
+static int log_level = 0;
+static int log_level_initialized = 0;
#if TRACE_XMALLOC > 1
#if TRACE_XMALLOC > 1
@@
-48,27
+50,27
@@
void *xmalloc_d(size_t nbytes, const char *file, int line)
{
char *res;
struct dmalloc_info *dinfo;
{
char *res;
struct dmalloc_info *dinfo;
-
+
if (!log_level_initialized)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char))))
return 0;
dinfo = (struct dmalloc_info *) res;
}
if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char))))
return 0;
dinfo = (struct dmalloc_info *) res;
- strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
+ strncpy(dinfo->file, file, sizeof(dinfo->file)-1);
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
-
+
dinfo->prev = 0;
dinfo->next = dmalloc_list;
if (dinfo->next)
dinfo->next->prev = dinfo;
dmalloc_list = dinfo;
dinfo->prev = 0;
dinfo->next = dmalloc_list;
if (dinfo->next)
dinfo->next->prev = dinfo;
dmalloc_list = dinfo;
-
+
memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
res += sizeof(*dinfo) + 8*sizeof(char);
memcpy(res + nbytes, tail, 8*sizeof(char));
memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
res += sizeof(*dinfo) + 8*sizeof(char);
memcpy(res + nbytes, tail, 8*sizeof(char));
@@
-99,7
+101,7
@@
void xfree_d(void *ptr, const char *file, int line)
dmalloc_list = dinfo->next;
if (dinfo->next)
dinfo->next->prev = dinfo->prev;
dmalloc_list = dinfo->next;
if (dinfo->next)
dinfo->next->prev = dinfo->prev;
- memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
+ memcpy((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
free(dinfo);
return;
}
free(dinfo);
return;
}
@@
-109,11
+111,11
@@
void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
struct dmalloc_info *dinfo;
char *ptr = (char*) p;
char *res;
struct dmalloc_info *dinfo;
char *ptr = (char*) p;
char *res;
-
+
if (!log_level_initialized)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (!ptr)
}
if (!ptr)
@@
-143,10
+145,10
@@
void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
dmalloc_list = dinfo->next;
if (dinfo->next)
dinfo->next->prev = dinfo->prev;
dmalloc_list = dinfo->next;
if (dinfo->next)
dinfo->next->prev = dinfo->prev;
-
+
if (!nbytes)
{
if (!nbytes)
{
- free (dinfo);
+ free(dinfo);
return 0;
}
res = (char *)
return 0;
}
res = (char *)
@@
-155,7
+157,7
@@
void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
if (!res)
return 0;
dinfo = (struct dmalloc_info *) res;
if (!res)
return 0;
dinfo = (struct dmalloc_info *) res;
- strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
+ strncpy(dinfo->file, file, sizeof(dinfo->file)-1);
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
@@
-165,7
+167,7
@@
void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
if (dmalloc_list)
dmalloc_list->prev = dinfo;
dmalloc_list = dinfo;
if (dmalloc_list)
dmalloc_list->prev = dinfo;
dmalloc_list = dinfo;
-
+
memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
res += sizeof(*dinfo) + 8*sizeof(char);
memcpy(res + nbytes, tail, 8*sizeof(char));
memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
res += sizeof(*dinfo) + 8*sizeof(char);
memcpy(res + nbytes, tail, 8*sizeof(char));
@@
-177,27
+179,27
@@
void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
char *res;
struct dmalloc_info *dinfo;
size_t nbytes = nmemb * size;
char *res;
struct dmalloc_info *dinfo;
size_t nbytes = nmemb * size;
-
+
if (!log_level_initialized)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char))))
return 0;
dinfo = (struct dmalloc_info *) res;
}
if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char))))
return 0;
dinfo = (struct dmalloc_info *) res;
- strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
+ strncpy(dinfo->file, file, sizeof(dinfo->file)-1);
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
-
+
dinfo->prev = 0;
dinfo->next = dmalloc_list;
if (dinfo->next)
dinfo->next->prev = dinfo;
dmalloc_list = dinfo;
dinfo->prev = 0;
dinfo->next = dmalloc_list;
if (dinfo->next)
dinfo->next->prev = dinfo;
dmalloc_list = dinfo;
-
+
memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
res += sizeof(*dinfo) + 8*sizeof(char);
memcpy(res + nbytes, tail, 8*sizeof(char));
memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
res += sizeof(*dinfo) + 8*sizeof(char);
memcpy(res + nbytes, tail, 8*sizeof(char));
@@
-208,22
+210,22
@@
void xmalloc_trav_d(const char *file, int line)
{
size_t size = 0;
struct dmalloc_info *dinfo = dmalloc_list;
{
size_t size = 0;
struct dmalloc_info *dinfo = dmalloc_list;
-
+
if (!log_level_initialized)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
}
- yaz_log (log_level, "malloc_trav %s:%d", file, line);
+ yaz_log(log_level, "malloc_trav %s:%d", file, line);
while (dinfo)
{
while (dinfo)
{
- yaz_log (log_level, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
- ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len);
+ yaz_log(log_level, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
+ ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len);
size += dinfo->len;
dinfo = dinfo->next;
}
size += dinfo->len;
dinfo = dinfo->next;
}
- yaz_log (log_level, "total bytes %ld", (long) size);
+ yaz_log(log_level, "total bytes %ld", (long) size);
}
#else
}
#else
@@
-232,101
+234,107
@@
void xmalloc_trav_d(const char *file, int line)
#define xmalloc_d(x, f, l) malloc(x)
#define xcalloc_d(x,y, f, l) calloc(x,y)
#define xfree_d(x, f, l) free(x)
#define xmalloc_d(x, f, l) malloc(x)
#define xcalloc_d(x,y, f, l) calloc(x,y)
#define xfree_d(x, f, l) free(x)
-#define xmalloc_trav_d(f, l)
+#define xmalloc_trav_d(f, l)
#endif
void xmalloc_trav_f(const char *s, const char *file, int line)
{
if (!log_level_initialized)
{
#endif
void xmalloc_trav_f(const char *s, const char *file, int line)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
xmalloc_trav_d(file, line);
}
}
xmalloc_trav_d(file, line);
}
-void *xrealloc_f (void *o, size_t size, const char *file, int line)
+void xmalloc_fatal(size_t size)
{
{
- void *p = xrealloc_d (o, size, file, line);
+ assert(size < MALLOC_SIZE_MAX);
+ exit(1);
+}
+
+void *xrealloc_f(void *o, size_t size, const char *file, int line)
+{
+ void *p = xrealloc_d(o, size, file, line);
if (!log_level_initialized)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
}
- if(log_level)
- yaz_log (log_level,
- "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (long) size, o, p);
+ if (log_level)
+ yaz_log(log_level,
+ "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (long) size, o, p);
if (!p)
{
if (!p)
{
- yaz_log (YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%ld bytes)",
- (long) size);
- exit(1);
+ yaz_log(YLOG_FATAL, "%s:%d: Out of memory, realloc(%ld bytes)",
+ file, line, (long) size);
+ xmalloc_fatal(size);
}
return p;
}
}
return p;
}
-void *xmalloc_f (size_t size, const char *file, int line)
+void *xmalloc_f(size_t size, const char *file, int line)
{
{
- void *p = xmalloc_d (size, file, line);
-
+ void *p = xmalloc_d(size, file, line);
+
if (!log_level_initialized)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (log_level)
}
if (log_level)
- yaz_log (log_level, "%s:%d: xmalloc(s=%ld) %p", file, line,
- (long) size, p);
+ yaz_log(log_level, "%s:%d: xmalloc(s=%ld) %p", file, line,
+ (long) size, p);
if (!p)
{
if (!p)
{
- yaz_log (YLOG_FATAL, "Out of memory - malloc (%ld bytes)",
- (long) size);
- exit (1);
+ yaz_log(YLOG_FATAL, "%s:%d: Out of memory - malloc(%ld bytes)",
+ file, line, (long) size);
+ xmalloc_fatal(size);
}
return p;
}
}
return p;
}
-void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
+void *xcalloc_f(size_t nmemb, size_t size, const char *file, int line)
{
{
- void *p = xcalloc_d (nmemb, size, file, line);
+ void *p = xcalloc_d(nmemb, size, file, line);
if (!log_level_initialized)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (log_level)
}
if (log_level)
- yaz_log (log_level, "%s:%d: xcalloc(s=%ld) %p", file, line,
- (long) size, p);
+ yaz_log(log_level, "%s:%d: xcalloc(s=%ld) %p", file, line,
+ (long) size, p);
if (!p)
{
if (!p)
{
- yaz_log (YLOG_FATAL, "Out of memory - calloc (%ld, %ld)",
- (long) nmemb, (long) size);
- exit (1);
+ yaz_log(YLOG_FATAL, "%s:%d: Out of memory - calloc(%ld, %ld)",
+ file, line, (long) nmemb, (long) size);
+ xmalloc_fatal(size);
}
return p;
}
}
return p;
}
-char *xstrdup_f (const char *s, const char *file, int line)
+char *xstrdup_f(const char *s, const char *file, int line)
{
{
- char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
+ char *p = (char *)xmalloc_d(strlen(s)+1, file, line);
if (!log_level_initialized)
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (log_level)
}
if (log_level)
- yaz_log (log_level, "%s:%d: xstrdup(s=%ld) %p", file, line,
- (long) strlen(s)+1, p);
+ yaz_log(log_level, "%s:%d: xstrdup(s=%ld) %p", file, line,
+ (long) strlen(s)+1, p);
- strcpy (p, s);
+ strcpy(p, s);
return p;
}
return p;
}
@@
-335,12
+343,27
@@
void xfree_f(void *p, const char *file, int line)
if (!p)
return ;
if (log_level)
if (!p)
return ;
if (log_level)
- yaz_log (log_level, "%s:%d: xfree %p", file, line, p);
+ yaz_log(log_level, "%s:%d: xfree %p", file, line, p);
xfree_d(p, file, line);
}
xfree_d(p, file, line);
}
+
+char *xstrndup_f(const char *s, size_t n, const char *file, int line)
+{
+ size_t l = strlen(s);
+ if (l < n)
+ return xstrdup_f(s, file, line);
+ {
+ char *a = (char*) xmalloc_f(n+1, file, line);
+ memcpy(a, s, n);
+ a[n] = '\0';
+ return a;
+ }
+}
+
/*
* Local variables:
* c-basic-offset: 4
/*
* Local variables:
* c-basic-offset: 4
+ * c-file-style: "Stroustrup"
* indent-tabs-mode: nil
* End:
* vim: shiftwidth=4 tabstop=8 expandtab
* indent-tabs-mode: nil
* End:
* vim: shiftwidth=4 tabstop=8 expandtab