aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/mmgr/mcxt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/mmgr/mcxt.c')
-rw-r--r--src/backend/utils/mmgr/mcxt.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
index 9574fd3c7a3..b7beb130ea3 100644
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -852,3 +852,52 @@ pnstrdup(const char *in, Size len)
out[len] = '\0';
return out;
}
+
+/*
+ * asprintf()-like functions around palloc, adapted from
+ * http://ftp.netbsd.org/pub/pkgsrc/current/pkgsrc/pkgtools/libnbcompat/files/asprintf.c
+ */
+
+char *
+psprintf(const char *format, ...)
+{
+ va_list ap;
+ char *retval;
+
+ va_start(ap, format);
+ retval = pvsprintf(format, ap);
+ va_end(ap);
+
+ return retval;
+}
+
+char *
+pvsprintf(const char *format, va_list ap)
+{
+ char *buf, *new_buf;
+ size_t len;
+ int retval;
+ va_list ap2;
+
+ len = 128;
+ buf = palloc(len);
+
+ va_copy(ap2, ap);
+ retval = vsnprintf(buf, len, format, ap);
+ Assert(retval >= 0);
+
+ if (retval < len)
+ {
+ new_buf = repalloc(buf, retval + 1);
+ va_end(ap2);
+ return new_buf;
+ }
+
+ len = (size_t)retval + 1;
+ pfree(buf);
+ buf = palloc(len);
+ retval = vsnprintf(buf, len, format, ap2);
+ va_end(ap2);
+ Assert(retval == len - 1);
+ return buf;
+}