*--------------------------------------------------------------------------
*/
-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);
/*
#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;
}
/*
*-----------------------------------------------------------------------------
*
- * Util_SafeInternalRealloc --
+ * UtilSafeRealloc0 --
+ * UtilSafeRealloc1 --
* Helper function for realloc
*
* Results:
*/
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;
}
/*
*-----------------------------------------------------------------------------
*
- * Util_SafeInternalCalloc --
+ * UtilSafeCalloc0 --
+ * UtilSafeCalloc1 --
* Helper function for calloc
*
* Results:
*/
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;
}
/*
*-----------------------------------------------------------------------------
*
- * Util_SafeInternalStrdup --
+ * Util_SafeStrdup --
* Helper function for strdup
*
* Results:
*/
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;
}
/*
*-----------------------------------------------------------------------------
*
- * 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'.
*/
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;
}
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';
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,