#define xcalloc(x,y) xcalloc_f(x,y, __FILE__, __LINE__)
#define xfree(x) xfree_f(x, __FILE__, __LINE__)
#define xstrdup(s) xstrdup_f(s, __FILE__, __LINE__)
+#define xstrndup(s, n) xstrndup_f(s, n, __FILE__, __LINE__)
#define xmalloc_trav(s) xmalloc_trav_f(s, __FILE__, __LINE__)
YAZ_EXPORT void *xrealloc_f (void *o, size_t size, const char *file, int line);
YAZ_EXPORT void *xcalloc_f (size_t nmemb, size_t size,
const char *file, int line);
YAZ_EXPORT char *xstrdup_f (const char *p, const char *file, int line);
+YAZ_EXPORT char *xstrndup_f(const char *p, size_t n,
+ const char *file, int line);
YAZ_EXPORT void xfree_f (void *p, const char *file, int line);
YAZ_EXPORT void xmalloc_trav_f(const char *s, const char *file, int line);
yaz_log (log_level, "%s:%d: xfree %p", file, line, p);
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 = xmalloc_f(n+1, file, line);
+ memcpy(a, s, n);
+ a[n] = '\0';
+ return a;
+ }
+}
+
/*
* Local variables:
* c-basic-offset: 4
## This file is part of the YAZ toolkit.
## Copyright (C) 1995-2009 Index Data
-check_PROGRAMS = tsticonv tstnmem tstmatchstr tstwrbuf tstodr tstccl tstlog \
+check_PROGRAMS = tstxmalloc tsticonv tstnmem tstmatchstr tstwrbuf tstodr \
+ tstccl tstlog \
tstsoap1 tstsoap2 tstodrstack tstlogthread tstxmlquery tstpquery \
tst_comstack tst_filepath tst_record_conv tst_retrieval tst_tpath \
tst_timing tst_query_charset tst_oid tst_icu_I18N tst_match_glob tst_rpn2cql
CONFIG_CLEAN_FILES=*.log
+tstxmalloc_SOURCES = tstxmalloc.c
tsticonv_SOURCES = tsticonv.c
tstnmem_SOURCES = tstnmem.c
tstmatchstr_SOURCES = tstmatchstr.c
--- /dev/null
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2009 Index Data
+ * See the file LICENSE for details.
+ */
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <string.h>
+#include <yaz/xmalloc.h>
+#include <yaz/test.h>
+
+void tst(void)
+{
+ char *p = 0;
+
+ p = xmalloc(10);
+ YAZ_CHECK(p);
+ p = xrealloc(p, 20);
+ YAZ_CHECK(p);
+ xfree(p);
+
+ p = xstrdup("hello");
+ YAZ_CHECK(p);
+ if (!p)
+ return;
+ YAZ_CHECK(!strcmp(p, "hello"));
+ xfree(p);
+
+ p = xstrndup("hello", 2);
+ YAZ_CHECK(p);
+ if (!p)
+ return;
+ YAZ_CHECK(!strcmp(p, "he"));
+ xfree(p);
+
+ p = xstrndup("hello", 6);
+ YAZ_CHECK(p);
+ if (!p)
+ return;
+ YAZ_CHECK(!strcmp(p, "hello"));
+ xfree(p);
+}
+
+int main (int argc, char **argv)
+{
+ YAZ_CHECK_INIT(argc, argv);
+ tst();
+ YAZ_CHECK_TERM;
+}
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+