From: Oliver Kurth Date: Fri, 15 Sep 2017 18:23:29 +0000 (-0700) Subject: Add Util_Memdup(); replace DynBuf_AllocGet() calls with Util_Memdup(). X-Git-Tag: stable-10.2.0~308 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c04c7f646558a39445d2348dd99beec90ad587a6;p=thirdparty%2Fopen-vm-tools.git Add Util_Memdup(); replace DynBuf_AllocGet() calls with Util_Memdup(). 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 --- diff --git a/open-vm-tools/lib/dynxdr/dynxdr.c b/open-vm-tools/lib/dynxdr/dynxdr.c index e39cf9b95..9b2681966 100644 --- a/open-vm-tools/lib/dynxdr/dynxdr.c +++ b/open-vm-tools/lib/dynxdr/dynxdr.c @@ -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)); } diff --git a/open-vm-tools/lib/include/dynbuf.h b/open-vm-tools/lib/include/dynbuf.h index 55fc2d062..6d378e355 100644 --- a/open-vm-tools/lib/include/dynbuf.h +++ b/open-vm-tools/lib/include/dynbuf.h @@ -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 diff --git a/open-vm-tools/lib/include/util.h b/open-vm-tools/lib/include/util.h index 39c028e6f..6d3d75d1e 100644 --- a/open-vm-tools/lib/include/util.h +++ b/open-vm-tools/lib/include/util.h @@ -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); diff --git a/open-vm-tools/lib/misc/dynbuf.c b/open-vm-tools/lib/misc/dynbuf.c index 129384154..08cc32526 100644 --- a/open-vm-tools/lib/misc/dynbuf.c +++ b/open-vm-tools/lib/misc/dynbuf.c @@ -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; -} - - /* *----------------------------------------------------------------------------- * diff --git a/open-vm-tools/lib/misc/utilMem.c b/open-vm-tools/lib/misc/utilMem.c index ca80ace54..0f84e3311 100644 --- a/open-vm-tools/lib/misc/utilMem.c +++ b/open-vm-tools/lib/misc/utilMem.c @@ -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; } - - diff --git a/open-vm-tools/lib/user/util.c b/open-vm-tools/lib/user/util.c index 4b728e3b5..dd1f1359f 100644 --- a/open-vm-tools/lib/user/util.c +++ b/open-vm-tools/lib/user/util.c @@ -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; -}