]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Add Util_Memdup(); replace DynBuf_AllocGet() calls with Util_Memdup().
authorOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:29 +0000 (11:23 -0700)
committerOliver Kurth <okurth@vmware.com>
Fri, 15 Sep 2017 18:23:29 +0000 (11:23 -0700)
Remove Util_CombineStrings() and Util_SeparateStrings().
  - lib/dynxdr/dynxdr.c
  - lib/include/dynbuf.h
  - lib/include/util.h
  - lib/misc/dynbuf.c
  - lib/misc/utilMem.c
  - lib/user/util.c

open-vm-tools/lib/dynxdr/dynxdr.c
open-vm-tools/lib/include/dynbuf.h
open-vm-tools/lib/include/util.h
open-vm-tools/lib/misc/dynbuf.c
open-vm-tools/lib/misc/utilMem.c
open-vm-tools/lib/user/util.c

index e39cf9b9585c8de269dbc6c80eabcee753abcc3a..9b2681966a482e9fba51b74eba3266fd25dc1ace 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "dynxdr.h"
 #include "dynbuf.h"
+#include "util.h"
 
 /*
  * dynxdr.c --
@@ -420,7 +421,7 @@ void *
 DynXdr_AllocGet(XDR *xdrs) // IN
 {
    DynBuf *buf = &((DynXdrData *) xdrs->x_private)->data;
-   return DynBuf_AllocGet(buf);
+   return Util_Memdup(DynBuf_Get(buf), DynBuf_GetSize(buf));
 }
 
 
index 55fc2d0627c2a31311f54863bb018c40e4675717..6d378e355194f2aad9cfa608735f501c817be9f2 100644 (file)
@@ -51,9 +51,6 @@ DynBuf_InitWithMemory(DynBuf *b,
 void
 DynBuf_Destroy(DynBuf *b); // IN
 
-void *
-DynBuf_AllocGet(DynBuf const *b); // IN
-
 void
 DynBuf_Attach(DynBuf *b,    // IN
               size_t size,  // IN
index 39c028e6f6decc21c14663bea277a3ce23c611f7..6d3d75d1eb78653a997a076dfe48a8fc71999dde 100644 (file)
@@ -103,9 +103,6 @@ char *Util_DeriveFileName(const char *source,
                           const char *name,
                           const char *ext);
 
-char *Util_CombineStrings(char **sources, int count);
-char **Util_SeparateStrings(char *source, int *count);
-
 typedef struct UtilSingleUseResource UtilSingleUseResource;
 UtilSingleUseResource *Util_SingleUseAcquire(const char *name);
 void Util_SingleUseRelease(UtilSingleUseResource *res);
@@ -424,6 +421,7 @@ char *UtilSafeStrndup1(const char *s, size_t n,
 #endif  /* VMX86_DEBUG */
 
 
+void *Util_Memdup(const void *src, size_t size);
 void *Util_Memcpy(void *dest, const void *src, size_t count);
 
 
index 129384154a78a8e152e26ba9dda435fa873a2eed..08cc32526de11d8018d3a33c221c2bf27bf331d7 100644 (file)
@@ -117,43 +117,6 @@ DynBuf_Destroy(DynBuf *b)  // IN/OUT:
 }
 
 
-/*
- *-----------------------------------------------------------------------------
- *
- * DynBuf_AllocGet --
- *
- *      Retrieve a pointer to the data contained in a dynamic buffer.  Return
- *      a copy of that data.
- *
- * Results:
- *      The pointer to the data.  NULL on out of memory failure or if the
- *      input DynBuf is empty.
- *
- * Side effects:
- *      Allocates memory.
- *
- *-----------------------------------------------------------------------------
- */
-
-void *
-DynBuf_AllocGet(DynBuf const *b)  // IN:
-{
-   void *new_data;
-   ASSERT(b);
-
-   if (b->size == 0) {
-      return NULL;
-   }
-
-   new_data = malloc(b->size);
-   if (new_data) {
-      memcpy(new_data, b->data, b->size);
-   }
-
-   return new_data;
-}
-
-
 /*
  *-----------------------------------------------------------------------------
  *
index ca80ace54fb368d47a570c040830be5a16cf2423..0f84e331126991f7813e81e49d8b44d8c0c7ceff 100644 (file)
@@ -218,6 +218,7 @@ UtilSafeStrdup0(const char *s)        // IN:
    if (s == NULL) {
       return NULL;
    }
+
 #if defined(_WIN32)
    if ((result = _strdup(s)) == NULL) {
 #else
@@ -298,7 +299,7 @@ UtilSafeStrndup0(const char *s,        // IN:
 
    copy[size] = '\0';
 
-   return (char *) memcpy(copy, s, size);
+   return memcpy(copy, s, size);
 }
 
 
@@ -333,14 +334,76 @@ UtilSafeStrndup1(const char *s,        // IN:
 
    copy[size] = '\0';
 
-   return (char *) memcpy(copy, s, size);
+   return memcpy(copy, s, size);
 }
 
 
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * Util_Memdup --
+ *
+ *      Allocates a copy of data.
+ *
+ * Results:
+ *      Returns a pointer to the allocated copy.  The caller is responsible for
+ *      freeing it with free().  Returns NULL on failure or if the input size
+ *      is 0.
+ *
+ * Side effects:
+ *      None.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
 void *
-Util_Memcpy(void *dest,
-            const void *src,
-            size_t count)
+Util_Memdup(const void *src, // IN:
+            size_t size)     // IN:
+{
+   void *dest;
+
+   if (size == 0) {
+      return NULL;
+   }
+
+   ASSERT(src != NULL);
+
+   dest = malloc(size);
+   if (dest != NULL) {
+      Util_Memcpy(dest, src, size);
+   }
+   return dest;
+}
+
+
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * Util_Memcpy --
+ *
+ *      Version of memcpy intended to accelerate aligned copies.
+ *
+ *      Expected benefits:
+ *      2-4x performance improvemenet for small buffers (count <= 256 bytes)
+ *      Equivalent performance on mid-sized buffers (256 bytes < count < 4K)
+ *      ~25% performance improvement on large buffers (4K < count)
+ *
+ *      Has a drawback that falling through to standard memcpy has overhead
+ *      of 5 instructions and 2 branches.
+ *
+ * Results:
+ *      Returns a pointer to the destination buffer.
+ *
+ * Side effects:
+ *      None.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+void *
+Util_Memcpy(void *dest,      // OUT:
+            const void *src, // IN:
+            size_t count)    // IN:
 {
 #if defined(__x86_64__) || defined(__i386__)
    uintptr_t align = ((uintptr_t)dest | (uintptr_t)src | count);
@@ -391,7 +454,7 @@ Util_Memcpy(void *dest,
       }
 
    #endif
-  
+
 #elif defined _MSC_VER
 
    #if defined(__x86_64__)
@@ -420,5 +483,3 @@ Util_Memcpy(void *dest,
    memcpy(dest, src, count);
    return dest;
 }
-            
-
index 4b728e3b509b8569676a21f34cee7e0e177e7f22..dd1f1359f42cc4a8be3f030d6d13d607475cf739 100644 (file)
@@ -847,133 +847,3 @@ Util_DeriveFileName(const char *source, // IN: path to dict file (incl filename)
    free(base);
    return returnResult;
 }
-
-
-/*
- *-----------------------------------------------------------------------------
- *
- * Util_CombineStrings --
- *
- *      Takes a vector of strings, and combines them into one string,
- *      where each string is separated by a 0 (zero) byte.
- *
- *      The 0 bytes are then escaped out, and the result is returned.
- *
- * Results:
- *
- *      A NULL terminated string
- *
- * Side effects:
- *
- *      The result string is allocated
- *
- *-----------------------------------------------------------------------------
- */
-
-char *
-Util_CombineStrings(char **sources,             // IN
-                    int count)                  // IN
-{
-   size_t size = 0;
-   int index = 0;
-
-   char *combinedString = NULL;
-   char *cursor = NULL;
-   char *escapedString = NULL;
-
-   int bytesToEsc[256];
-
-   ASSERT(sources != NULL);
-
-   memset(bytesToEsc, 0, sizeof bytesToEsc);
-   bytesToEsc[0] = 1;
-   bytesToEsc['#'] = 1;
-
-   for (index = 0; index < count; index++) {
-      /*
-       * Count the size of each string + the delimeter
-       */
-
-      size += strlen(sources[index]) + 1;
-   }
-
-   combinedString = Util_SafeMalloc(size);
-
-   cursor = combinedString;
-   for (index = 0; index < count; index++) {
-      memcpy(cursor, sources[index], strlen(sources[index]));
-      cursor += strlen(sources[index]);
-      cursor[0] = '\0';
-      cursor++;
-   }
-
-   escapedString = Escape_Do('#', bytesToEsc, combinedString, size, NULL);
-
-   free(combinedString);
-
-   return escapedString;
-}
-
-
-/*
- *-----------------------------------------------------------------------------
- *
- * Util_SeparateStrings --
- *
- *      Takes as input the result of a call to Util_CombineStrings, and
- *      separates the strings back onto a vector of strings.
- *
- * Results:
- *
- *      A vector of strings, the count will also reflect the number of
- *      entries in the vector
- *
- * Side effects:
- *
- *      The vector is allocated, and each string in the vector must be
- *      freed by the caller
- *
- *-----------------------------------------------------------------------------
- */
-
-char **
-Util_SeparateStrings(char *source,              // IN
-                     int *count)                // OUT
-{
-   char *data = NULL;
-   size_t dataSize = 0;
-
-   int index = 0;
-   char *cursor = NULL;
-   char *endCursor = NULL;
-
-   char **stringVector = NULL;
-
-   ASSERT(count != NULL);
-
-   *count = 0;
-
-   data = Escape_Undo('#', source, strlen(source), &dataSize);
-   ASSERT(data != NULL);
-
-   endCursor = data + dataSize;
-   ASSERT(endCursor[0] == '\0');
-
-   cursor = data;
-   while (cursor < endCursor) {
-      (*count)++;
-      cursor += strlen(cursor) + 1;
-   }
-
-   stringVector = Util_SafeMalloc(sizeof(char *) * (*count));
-
-   cursor = data;
-   for (index = 0; index < (*count); index++) {
-      stringVector[index] = Util_SafeStrdup(cursor);
-      cursor += strlen(cursor) + 1;
-   }
-
-   free(data);
-
-   return stringVector;
-}