]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Slim down malloc/realloc/calloc/strdup wrappers in non-debug builds.
authorVMware, Inc <>
Tue, 13 Mar 2012 20:02:15 +0000 (13:02 -0700)
committerDmitry Torokhov <dtor@vmware.com>
Wed, 14 Mar 2012 16:04:57 +0000 (09:04 -0700)
The way we wrap malloc, realloc, calloc, strdup etc.
causes us to pass a set of extra parameters into the
functions that do the actual work, including a bug number
(which is almost always -1), __FILE__ and __LINE__.

In the release vmx alone, this costs us approximately 55 KB
of extra text.

This change slims down the wrappers in non-debug builds so
that we avoid passing the extra arguments.

One can argue about exactly which build type(s) should use
slim wrappers; my proposal is anything without VMX86_DEBUG.

Kevin tells me that there is some sort of scenario due to how
gobuild works (for official? sandbox? only? builds) where we
may end up calling the non-debug version of a library from a
debug call site (you say huh? I say huh?!). To handle this case,
all build types contain both versions of the work functions
with differently suffixed names ("0" and "1").

I've also cleaned up the mksMemMgr code slightly. It no longer
directly accesses Util_SafeInternal functions, although it still
reaches into the internal malloc data structures in a not so nice
way.

Updated release vmx size data, accounting for the need to handle
the build weirdness:

text    data     bss     dec     hex filename
8926420  414752 1498600 10839772  a566dc  current wrapping
8871692  414752 1498600 10785044  a49114  leaner wrapping
------------------
saved: 54728 bytes
------------------

Tests: build WS obj/opt/release, boot/halt xp64 with obj and opt,
test-esx, sandbox wsall beta build.

Signed-off-by: Dmitry Torokhov <dtor@vmware.com>
open-vm-tools/lib/include/util.h
open-vm-tools/lib/misc/utilMem.c

index f0e960295868a2dae180909b8aafa854231d0e1a..7212cb2363f5257ff1394cc1a497294bf1f62a9d 100644 (file)
@@ -302,52 +302,98 @@ EXTERN Bool Util_MakeSureDirExistsAndAccessible(char const *path,
  *--------------------------------------------------------------------------
  */
 
-EXTERN void *Util_SafeInternalMalloc(int bugNumber, size_t size,
-                                     const char *file, int lineno);
+EXTERN void *UtilSafeMalloc0(size_t size);
+EXTERN void *UtilSafeMalloc1(size_t size,
+                             int bugNumber, const char *file, int lineno);
 
-EXTERN void *Util_SafeInternalRealloc(int bugNumber, void *ptr, size_t size,
-                                      const char *file, int lineno);
+EXTERN void *UtilSafeRealloc0(void *ptr, size_t size);
+EXTERN void *UtilSafeRealloc1(void *ptr, size_t size,
+                             int bugNumber, const char *file, int lineno);
 
-EXTERN void *Util_SafeInternalCalloc(int bugNumber, size_t nmemb,
-                                     size_t size, const char *file, int lineno);
+EXTERN void *UtilSafeCalloc0(size_t nmemb, size_t size);
+EXTERN void *UtilSafeCalloc1(size_t nmemb, size_t size,
+                             int bugNumber, const char *file, int lineno);
 
-EXTERN char *Util_SafeInternalStrdup(int bugNumber, const char *s,
-                                     const char *file, int lineno);
+EXTERN char *UtilSafeStrdup0(const char *s);
+EXTERN char *UtilSafeStrdup1(const char *s,
+                             int bugNumber, const char *file, int lineno);
 
-EXTERN char *Util_SafeInternalStrndup(int bugNumber, const char *s, size_t n,
-                                      const char *file, int lineno);
+EXTERN char *UtilSafeStrndup0(const char *s, size_t n);
+EXTERN char *UtilSafeStrndup1(const char *s, size_t n,
+                             int bugNumber, const char *file, int lineno);
 
-EXTERN void *Util_Memcpy(void *dest, const void *src, size_t count);
+/* 
+ * Debug builds carry extra arguments into the allocation functions for
+ * better error reporting. Non-debug builds don't pay this extra overhead.
+ */
+#ifdef VMX86_DEBUG
 
 #define Util_SafeMalloc(_size) \
-   Util_SafeInternalMalloc(-1, (_size), __FILE__, __LINE__)
+   UtilSafeMalloc1((_size), -1, __FILE__, __LINE__)
 
 #define Util_SafeMallocBug(_bugNr, _size) \
-   Util_SafeInternalMalloc((_bugNr), (_size), __FILE__, __LINE__)
+   UtilSafeMalloc1((_size),(_bugNr), __FILE__, __LINE__)
 
 #define Util_SafeRealloc(_ptr, _size) \
-   Util_SafeInternalRealloc(-1, (_ptr), (_size), __FILE__, __LINE__)
+   UtilSafeRealloc1((_ptr), (_size), -1, __FILE__, __LINE__)
 
 #define Util_SafeReallocBug(_bugNr, _ptr, _size) \
-   Util_SafeInternalRealloc((_bugNr), (_ptr), (_size), __FILE__, __LINE__)
+   UtilSafeRealloc1((_ptr), (_size), (_bugNr), __FILE__, __LINE__)
+
+#define Util_SafeCalloc(_nmemb, _size) \
+   UtilSafeCalloc1((_nmemb), (_size), -1, __FILE__, __LINE__)
+
+#define Util_SafeCallocBug(_bugNr, _nmemb, _size) \
+   UtilSafeCalloc1((_nmemb), (_size), (_bugNr), __FILE__, __LINE__)
+
+#define Util_SafeStrndup(_str, _size) \
+   UtilSafeStrndup1((_str), (_size), -1, __FILE__, __LINE__)
+
+#define Util_SafeStrndupBug(_bugNr, _str, _size) \
+   UtilSafeStrndup1((_str), (_size), (_bugNr), __FILE__, __LINE__)
+
+#define Util_SafeStrdup(_str) \
+   UtilSafeStrdup1((_str), -1, __FILE__, __LINE__)
+
+#define Util_SafeStrdupBug(_bugNr, _str) \
+   UtilSafeStrdup1((_str), (_bugNr), __FILE__, __LINE__)
+
+#else  /* VMX86_DEBUG */
+
+#define Util_SafeMalloc(_size) \
+   UtilSafeMalloc0((_size))
+
+#define Util_SafeMallocBug(_bugNr, _size) \
+   UtilSafeMalloc0((_size))
+
+#define Util_SafeRealloc(_ptr, _size) \
+   UtilSafeRealloc0((_ptr), (_size))
+
+#define Util_SafeReallocBug(_ptr, _size) \
+   UtilSafeRealloc0((_ptr), (_size))
 
 #define Util_SafeCalloc(_nmemb, _size) \
-   Util_SafeInternalCalloc(-1, (_nmemb), (_size), __FILE__, __LINE__)
+   UtilSafeCalloc0((_nmemb), (_size))
 
 #define Util_SafeCallocBug(_bugNr, _nmemb, _size) \
-   Util_SafeInternalCalloc((_bugNr), (_nmemb), (_size), __FILE__, __LINE__)
+   UtilSafeCalloc0((_nmemb), (_size))
 
 #define Util_SafeStrndup(_str, _size) \
-   Util_SafeInternalStrndup(-1, (_str), (_size), __FILE__, __LINE__)
+   UtilSafeStrndup0((_str), (_size))
 
 #define Util_SafeStrndupBug(_bugNr, _str, _size) \
-   Util_SafeInternalStrndup((_bugNr), (_str), (_size), __FILE__, __LINE__)
+   UtilSafeStrndup0((_str), (_size))
 
 #define Util_SafeStrdup(_str) \
-   Util_SafeInternalStrdup(-1, (_str), __FILE__, __LINE__)
+   UtilSafeStrdup0((_str))
 
 #define Util_SafeStrdupBug(_bugNr, _str) \
-   Util_SafeInternalStrdup((_bugNr), (_str), __FILE__, __LINE__)
+   UtilSafeStrdup0((_str))
+
+#endif  /* VMX86_DEBUG */
+
+
+EXTERN void *Util_Memcpy(void *dest, const void *src, size_t count);
 
 
 /*
index d5714ea87150aca22c8f788376ae79f1218302af..70047d0eed5a8b04b168d79e0a70111db330ebee 100644 (file)
 #endif
 #endif
 
+static NORETURN void UtilAllocationFailure0(void);
+static NORETURN void UtilAllocationFailure1(int bugNumber, 
+                                            const char *file, int lineno);
+
+
+static void
+UtilAllocationFailure0(void)
+{
+   Panic("Unrecoverable memory allocation failure\n");
+}
+
+
+static void
+UtilAllocationFailure1(int bugNumber, const char *file, int lineno)
+{
+   if (bugNumber == -1) {
+      Panic("Unrecoverable memory allocation failure at %s:%d\n",
+            file, lineno);
+   } else {
+      Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
+            "number: %d\n", file, lineno, bugNumber);
+   }
+}
+
 
 /*
  *-----------------------------------------------------------------------------
  *
- * Util_SafeInternalMalloc --
+ * UtilSafeMalloc0 --
+ * UtilSafeMalloc1 --
  *      Helper function for malloc
  *
  * Results:
  */
 
 void *
-Util_SafeInternalMalloc(int bugNumber,        // IN:
-                        size_t size,          // IN:
-                       const char *file,     // IN:
-                        int lineno)           // IN:
+UtilSafeMalloc0(size_t size)            // IN:
 {
    void *result = malloc(size);
+   if (result == NULL && size != 0) {
+      UtilAllocationFailure0();
+   }
+   return result;
+}
+
 
+void *
+UtilSafeMalloc1(size_t size,            // IN:
+                int bugNumber,          // IN:
+                const char *file,       // IN:
+                int lineno)             // IN:
+{
+   void *result = malloc(size);
    if (result == NULL && size != 0) {
-      if (bugNumber == -1) {
-         Panic("Unrecoverable memory allocation failure at %s:%d\n",
-               file, lineno);
-      } else {
-         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
-               "number: %d\n", file, lineno, bugNumber);
-      }
+      UtilAllocationFailure1(bugNumber, file, lineno);
    }
    return result;
 }
@@ -78,7 +107,8 @@ Util_SafeInternalMalloc(int bugNumber,        // IN:
 /*
  *-----------------------------------------------------------------------------
  *
- * Util_SafeInternalRealloc --
+ * UtilSafeRealloc0 --
+ * UtilSafeRealloc1 --
  *      Helper function for realloc
  *
  * Results:
@@ -91,22 +121,27 @@ Util_SafeInternalMalloc(int bugNumber,        // IN:
  */
 
 void *
-Util_SafeInternalRealloc(int bugNumber,        // IN:
-                         void *ptr,            // IN:
-                         size_t size,          // IN:
-                         const char *file,     // IN:
-                         int lineno)           // IN:
+UtilSafeRealloc0(void *ptr,            // IN:
+                 size_t size)          // IN:
 {
    void *result = realloc(ptr, size);
+   if (result == NULL && size != 0) {
+      UtilAllocationFailure0();
+   }
+   return result;
+}
 
+
+void *
+UtilSafeRealloc1(void *ptr,            // IN:
+                 size_t size,          // IN:
+                 int bugNumber,        // IN:
+                 const char *file,     // IN:
+                 int lineno)           // IN:
+{
+   void *result = realloc(ptr, size);
    if (result == NULL && size != 0) {
-      if (bugNumber == -1) {
-         Panic("Unrecoverable memory allocation failure at %s:%d\n",
-               file, lineno);
-      } else {
-         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
-               "number: %d\n", file, lineno, bugNumber);
-      }
+      UtilAllocationFailure1(bugNumber, file, lineno);
    }
    return result;
 }
@@ -115,7 +150,8 @@ Util_SafeInternalRealloc(int bugNumber,        // IN:
 /*
  *-----------------------------------------------------------------------------
  *
- * Util_SafeInternalCalloc --
+ * UtilSafeCalloc0 --
+ * UtilSafeCalloc1 --
  *      Helper function for calloc
  *
  * Results:
@@ -128,22 +164,27 @@ Util_SafeInternalRealloc(int bugNumber,        // IN:
  */
 
 void *
-Util_SafeInternalCalloc(int bugNumber,        // IN:
-                        size_t nmemb,         // IN:
-                        size_t size,          // IN:
-                       const char *file,     // IN:
-                        int lineno)           // IN:
+UtilSafeCalloc0(size_t nmemb,         // IN:
+                size_t size)          // IN:
 {
    void *result = calloc(nmemb, size);
+   if (result == NULL && nmemb != 0 && size != 0) {
+      UtilAllocationFailure0();
+   }
+   return result;
+}
+
 
+void *
+UtilSafeCalloc1(size_t nmemb,         // IN:
+                size_t size,          // IN:
+                int bugNumber,        // IN:
+                const char *file,     // IN:
+                int lineno)           // IN:
+{
+   void *result = calloc(nmemb, size);
    if (result == NULL && nmemb != 0 && size != 0) {
-      if (bugNumber == -1) {
-         Panic("Unrecoverable memory allocation failure at %s:%d\n",
-               file, lineno);
-      } else {
-         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
-               "number: %d\n", file, lineno, bugNumber);
-      }
+      UtilAllocationFailure1(bugNumber, file, lineno);
    }
    return result;
 }
@@ -152,7 +193,7 @@ Util_SafeInternalCalloc(int bugNumber,        // IN:
 /*
  *-----------------------------------------------------------------------------
  *
- * Util_SafeInternalStrdup --
+ * Util_SafeStrdup --
  *      Helper function for strdup
  *
  * Results:
@@ -165,31 +206,40 @@ Util_SafeInternalCalloc(int bugNumber,        // IN:
  */
 
 char *
-Util_SafeInternalStrdup(int bugNumber,        // IN:
-                        const char *s,        // IN:
-                        const char *file,     // IN:
-                        int lineno)           // IN:
+UtilSafeStrdup0(const char *s)        // IN:
 {
    char *result;
-
    if (s == NULL) {
       return NULL;
    }
-
 #if defined(_WIN32)
    if ((result = _strdup(s)) == NULL) {
 #else
    if ((result = strdup(s)) == NULL) {
 #endif
-      if (bugNumber == -1) {
-         Panic("Unrecoverable memory allocation failure at %s:%d\n",
-               file, lineno);
-      } else {
-         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
-               "number: %d\n", file, lineno, bugNumber);
-      }
+      UtilAllocationFailure0();
    }
+   return result;
+}
+
 
+char *
+UtilSafeStrdup1(const char *s,        // IN:
+                int bugNumber,        // IN:
+                const char *file,     // IN:
+                int lineno)           // IN:
+{
+   char *result;
+   if (s == NULL) {
+      return NULL;
+   }
+#if defined(_WIN32)
+   if ((result = _strdup(s)) == NULL) {
+#else
+   if ((result = strdup(s)) == NULL) {
+#endif
+      UtilAllocationFailure1(bugNumber, file, lineno);
+   }
    return result;
 }
 
@@ -197,7 +247,7 @@ Util_SafeInternalStrdup(int bugNumber,        // IN:
 /*
  *-----------------------------------------------------------------------------
  *
- * UtilSafeStrndupInternal --
+ * Util_SafeStrndup --
  *
  *      Returns a string consisting of first n characters of 's' if 's' has
  *      length >= 'n', otherwise returns a string duplicate of 's'.
@@ -212,11 +262,8 @@ Util_SafeInternalStrdup(int bugNumber,        // IN:
  */
 
 char *
-Util_SafeInternalStrndup(int bugNumber,        // IN:
-                         const char *s,        // IN:
-                         size_t n,             // IN:
-                         const char *file,     // IN:
-                         int lineno)           // IN:
+UtilSafeStrndup0(const char *s,        // IN:
+                 size_t n)             // IN:
 {
    size_t size;
    char *copy;
@@ -227,17 +274,11 @@ Util_SafeInternalStrndup(int bugNumber,        // IN:
    }
 
    null = (char *) memchr(s, '\0', n);
-   size = null ? null - s: n;
+   size = null ? null - s : n;
    copy = (char *) malloc(size + 1);
 
    if (copy == NULL) {
-      if (bugNumber == -1) {
-         Panic("Unrecoverable memory allocation failure at %s:%d\n",
-               file, lineno);
-      } else {
-         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
-               "number: %d\n", file, lineno, bugNumber);
-      }
+      UtilAllocationFailure0();
    }
 
    copy[size] = '\0';
@@ -245,6 +286,36 @@ Util_SafeInternalStrndup(int bugNumber,        // IN:
    return (char *) memcpy(copy, s, size);
 }
 
+
+char *
+UtilSafeStrndup1(const char *s,        // IN:
+                 size_t n,             // IN:
+                 int bugNumber,        // IN:
+                 const char *file,     // IN:
+                 int lineno)           // IN:
+{
+   size_t size;
+   char *copy;
+   const char *null;
+
+   if (s == NULL) {
+      return NULL;
+   }
+
+   null = (char *) memchr(s, '\0', n);
+   size = null ? null - s : n;
+   copy = (char *) malloc(size + 1);
+
+   if (copy == NULL) {
+      UtilAllocationFailure1(bugNumber, file, lineno);
+   }
+
+   copy[size] = '\0';
+
+   return (char *) memcpy(copy, s, size);
+}
+
+
 void *
 Util_Memcpy(void *dest,
             const void *src,