+2008-05-09 Eric Blake <ebb9@byu.net>
+
+ Add xmemdup0.
+ * lib/xalloc.h (xmemdup0): New prototype and C++ typesafe
+ implementation.
+ * lib/xmalloc.c (xmemdup0): New C implementation.
+
2008-05-08 Bruno Haible <bruno@clisp.org>
* m4/wctype.m4 (gl_WCTYPE_H): Correct indentation.
void *xrealloc (void *p, size_t s);
void *x2realloc (void *p, size_t *pn);
void *xmemdup (void const *p, size_t s);
+void *xmemdup0 (void const *p, size_t s);
char *xstrdup (char const *str);
/* Return 1 if an array of N objects, each of size S, cannot exist due
return (T *) xmemdup ((void const *) p, s);
}
+template <typename T> inline T *
+xmemdup0 (T const *p, size_t s)
+{
+ return (T *) xmemdup0 ((void const *) p, s);
+}
+
# endif
/* xmalloc.c -- malloc with out of memory checking
Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
- 1999, 2000, 2002, 2003, 2004, 2005, 2006 Free Software Foundation,
+ 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2008 Free Software Foundation,
Inc.
This program is free software: you can redistribute it and/or modify
return memcpy (xmalloc (s), p, s);
}
+/* Clone an object P of size S, with error checking, and include a
+ terminating NUL byte.
+
+ The terminating NUL makes it safe to use strlen or rawmemchr to
+ check for embedded NUL; it also speeds up algorithms such as escape
+ sequence processing on arbitrary memory, by making it always safe
+ to read the byte after the escape character rather than having to
+ check if each escape character is the last byte in the object. */
+
+void *
+xmemdup0 (void const *p, size_t s)
+{
+ char *result = xcharalloc (s + 1);
+ memcpy (result, p, s);
+ result[s] = 0;
+ return result;
+}
+
/* Clone STRING. */
char *