]> mj.ucw.cz Git - libucw.git/commitdiff
Mempool: Implemented various functions for appending to a growing buffer
authorMartin Mares <mj@ucw.cz>
Thu, 15 May 2014 07:20:57 +0000 (09:20 +0200)
committerMartin Mares <mj@ucw.cz>
Thu, 15 May 2014 07:24:35 +0000 (09:24 +0200)
maint/libucw.abi
ucw/mempool-fmt.c
ucw/mempool.h

index 00377b47212c44ffe2df58dcc14e6a86f39f81b3..c2ca648bc116ce2c3489cd5155c26c7faaa18564 100644 (file)
@@ -387,8 +387,8 @@ mp_strjoin
 mp_str_from_mem
 mp_printf
 mp_vprintf
-mp_printf_append
-mp_vprintf_append
+mp_append_printf
+mp_append_vprintf
 # ucw/opt-internal.h
 opt_precompute
 # ucw/opt.h
index 4f81dd8b21a8857750755281ae032687ab72f0a0..dbda703d60350c1427b4fec7d6d1eb48099bd4ab 100644 (file)
@@ -65,7 +65,7 @@ mp_printf(struct mempool *p, const char *fmt, ...)
 }
 
 char *
-mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args)
+mp_append_vprintf(struct mempool *mp, char *ptr, const char *fmt, va_list args)
 {
   uns ofs = mp_open(mp, ptr);
   ASSERT(ofs && !ptr[ofs - 1]);
@@ -73,7 +73,7 @@ mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args)
 }
 
 char *
-mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...)
+mp_append_printf(struct mempool *mp, char *ptr, const char *fmt, ...)
 {
   va_list args;
   va_start(args, fmt);
@@ -89,9 +89,9 @@ int main(void)
   struct mempool *mp = mp_new(64);
   char *x = mp_printf(mp, "<Hello, %s!>", "World");
   fputs(x, stdout);
-  x = mp_printf_append(mp, x, "<Appended>");
+  x = mp_append_printf(mp, x, "<Appended>");
   fputs(x, stdout);
-  x = mp_printf(mp, "<Hello, %50s!>\n", "World");
+  x = mp_append_printf(mp, "<Hello, %50s!>\n", "World");
   fputs(x, stdout);
   return 0;
 }
index ad24a3983fc1580ee0f8f8a0b5ef3d6a6e660f7f..562d8f99936feab4af41c80784ad32f2a26fdf81 100644 (file)
 #define _UCW_POOLS_H
 
 #include <ucw/alloc.h>
+#include <string.h>
 
 #ifdef CONFIG_UCW_CLEAN_ABI
 #define mp_alloc ucw_mp_alloc
 #define mp_alloc_internal ucw_mp_alloc_internal
 #define mp_alloc_noalign ucw_mp_alloc_noalign
 #define mp_alloc_zero ucw_mp_alloc_zero
+#define mp_append_printf ucw_mp_append_printf
+#define mp_append_vprintf ucw_mp_append_vprintf
 #define mp_delete ucw_mp_delete
 #define mp_flush ucw_mp_flush
 #define mp_grow_internal ucw_mp_grow_internal
@@ -28,7 +31,6 @@
 #define mp_open ucw_mp_open
 #define mp_pop ucw_mp_pop
 #define mp_printf ucw_mp_printf
-#define mp_printf_append ucw_mp_printf_append
 #define mp_push ucw_mp_push
 #define mp_realloc ucw_mp_realloc
 #define mp_realloc_zero ucw_mp_realloc_zero
@@ -43,7 +45,6 @@
 #define mp_strjoin ucw_mp_strjoin
 #define mp_total_size ucw_mp_total_size
 #define mp_vprintf ucw_mp_vprintf
-#define mp_vprintf_append ucw_mp_vprintf_append
 #endif
 
 /***
@@ -318,6 +319,40 @@ static inline void *mp_spread(struct mempool *pool, void *p, uns size)
   return (((uns)((byte *)pool->state.last[pool->idx] - (byte *)p) >= size) ? p : mp_spread_internal(pool, p, size));
 }
 
+/**
+ * Append a character to the growing buffer. Called with @p pointing after
+ * the last byte in the buffer, returns a pointer after the last byte
+ * of the new (possibly reallocated) buffer.
+ **/
+static inline char *mp_append_char(struct mempool *pool, char *p, uns c)
+{
+  p = mp_spread(pool, p, 1);
+  *p++ = c;
+  return p;
+}
+
+/**
+ * Append a memory block to the growing buffer. Called with @p pointing after
+ * the last byte in the buffer, returns a pointer after the last byte
+ * of the new (possibly reallocated) buffer.
+ **/
+static inline void *mp_append_block(struct mempool *pool, void *p, const void *block, uns size)
+{
+  char *q = mp_spread(pool, p, size);
+  memcpy(q, block, size);
+  return q + size;
+}
+
+/**
+ * Append a string to the growing buffer. Called with @p pointing after
+ * the last byte in the buffer, returns a pointer after the last byte
+ * of the new (possibly reallocated) buffer.
+ **/
+static inline void *mp_append_string(struct mempool *pool, void *p, const char *str)
+{
+  return mp_append_block(pool, p, str, strlen(str));
+}
+
 /**
  * Close the growing buffer. The @end must point just behind the data, you want to keep
  * allocated (so it can be in the interval `[@mp_ptr(@pool), @mp_ptr(@pool) + @mp_avail(@pool)]`).
@@ -330,6 +365,15 @@ static inline void *mp_end(struct mempool *pool, void *end)
   return p;
 }
 
+/**
+ * Close the growing buffer as a string. That is, append a zero byte and call mp_end().
+ **/
+static inline char *mp_end_string(struct mempool *pool, void *end)
+{
+  end = mp_append_char(pool, end, 0);
+  return mp_end(pool, end);
+}
+
 /**
  * Return size in bytes of the last allocated memory block (with @mp_alloc() or @mp_end()).
  **/
@@ -489,11 +533,19 @@ char *mp_vprintf(struct mempool *mp, const char *fmt, va_list args) LIKE_MALLOC;
  *
  * Returns pointer to the beginning of the string (the pointer may have
  * changed due to reallocation).
+ *
+ * Alternatively, this function may be called mp_printf_append() for compatibility with
+ * previous releases of LibUCW.
  **/
-char *mp_printf_append(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);
+char *mp_append_printf(struct mempool *mp, char *ptr, const char *fmt, ...) FORMAT_CHECK(printf,3,4);
+#define mp_printf_append mp_append_printf
 /**
- * Like @mp_printf_append(), but uses `va_list` for parameters.
+ * Like @mp_append_printf(), but uses `va_list` for parameters.
+ *
+ * Alternatively, this function may be called mp_vprintf_append() for compatibility with
+ * previous releases of LibUCW.
  **/
-char *mp_vprintf_append(struct mempool *mp, char *ptr, const char *fmt, va_list args);
+char *mp_append_vprintf(struct mempool *mp, char *ptr, const char *fmt, va_list args);
+#define mp_vprintf_append mp_append_vprintf
 
 #endif