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
#include "dynxdr.h"
#include "dynbuf.h"
+#include "util.h"
/*
* dynxdr.c --
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));
}
void
DynBuf_Destroy(DynBuf *b); // IN
-void *
-DynBuf_AllocGet(DynBuf const *b); // IN
-
void
DynBuf_Attach(DynBuf *b, // IN
size_t size, // IN
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);
#endif /* VMX86_DEBUG */
+void *Util_Memdup(const void *src, size_t size);
void *Util_Memcpy(void *dest, const void *src, size_t count);
}
-/*
- *-----------------------------------------------------------------------------
- *
- * 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;
-}
-
-
/*
*-----------------------------------------------------------------------------
*
if (s == NULL) {
return NULL;
}
+
#if defined(_WIN32)
if ((result = _strdup(s)) == NULL) {
#else
copy[size] = '\0';
- return (char *) memcpy(copy, s, size);
+ return memcpy(copy, s, size);
}
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);
}
#endif
-
+
#elif defined _MSC_VER
#if defined(__x86_64__)
memcpy(dest, src, count);
return dest;
}
-
-
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;
-}