lib/sod-hosted.c (sod_makev): Use two statements rather than tricky expression.
[sod] / lib / sod-hosted.c
index 961aa22..32aabec 100644 (file)
 
 /*----- Header files ------------------------------------------------------*/
 
+#include <stdio.h>
 #include <stdlib.h>
 
 #include "sod.h"
 
+/*----- Preliminary macros ------------------------------------------------*/
+
+#if __STDC_VERSION__ >= 199901
+#  define PRIuSZ "zu"
+#  define PRINT_SZ(x) (x)
+#else
+#  define PRIuSZ "lu"
+#  define PRINT_SZ(x) ((unsigned long)(x))
+#endif
+
 /*----- Main code ---------------------------------------------------------*/
 
+/* --- @sod__chksz_fail@ --- *
+ *
+ * Arguments:  @const SodClass *cls@ = class we were trying to instantiate
+ *             @size_t sz@ = size allocated
+ *
+ * Returns:    Doesn't.
+ *
+ * Use:                Reports instantiation failure caused by a mismatch between
+ *             the size allocated (@sz@) and the size required for an
+ *             instance of class @cls@.
+ */
+
+SOD__NORETURN void sod__chksz_fail(const SodClass *cls, size_t sz)
+{
+  fprintf(stderr, "INTERNAL ERROR: size mismatch for class `%s': "
+         "%"PRIuSZ" allocated but %"PRIuSZ" required",
+         cls->cls.name, PRINT_SZ(sz), PRINT_SZ(cls->cls.initsz));
+  abort();
+}
+
 /* --- @sod_make@, @sod_makev@ --- *
  *
  * Arguments:  @const SodClass *cls@ = class object for new instance
@@ -69,7 +100,7 @@ void *sod_makev(const SodClass *cls, va_list ap)
 {
   void *p;
 
-  if ((p = malloc(cls->cls.initsz)) == 0) return (0);
+  p = malloc(cls->cls.initsz); if (!p) return (0);
   return (sod_initv(cls, p, ap));
 }