]> granicus.if.org Git - graphviz/commitdiff
branch-tune allocation wrappers
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 26 Jun 2021 19:12:13 +0000 (12:12 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 5 Jul 2021 22:19:51 +0000 (15:19 -0700)
These functions are used extensively within Graphviz. This change tells the
compiler to optimize for the case when allocation succeeds. This may slow the
case when allocation fails, though we do not care about this case as Graphviz is
about to exit with failure in this scenario.

lib/common/memory.c

index 6162c0c9c3d6e83607f0a040a78aae7b17dee06b..f32dc21e18a1a0f30be7edfc85596a77437755ca 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "config.h"
 
+#include <cgraph/likely.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -25,7 +26,7 @@ void *zmalloc(size_t nbytes)
 void *zrealloc(void *ptr, size_t size, size_t elt, size_t osize)
 {
     void *p = realloc(ptr, size * elt);
-    if (p == NULL && size) {
+    if (UNLIKELY(p == NULL && size)) {
        fprintf(stderr, "out of memory\n");
        exit(EXIT_FAILURE);
     }
@@ -37,7 +38,7 @@ void *zrealloc(void *ptr, size_t size, size_t elt, size_t osize)
 void *gcalloc(size_t nmemb, size_t size)
 {
     char *rv = calloc(nmemb, size);
-    if (rv == NULL) {
+    if (UNLIKELY(rv == NULL)) {
        fprintf(stderr, "out of memory\n");
        exit(EXIT_FAILURE);
     }
@@ -50,7 +51,7 @@ void *gmalloc(size_t nbytes)
     if (nbytes == 0)
        return NULL;
     rv = malloc(nbytes);
-    if (rv == NULL) {
+    if (UNLIKELY(rv == NULL)) {
        fprintf(stderr, "out of memory\n");
        exit(EXIT_FAILURE);
     }
@@ -60,7 +61,7 @@ void *gmalloc(size_t nbytes)
 void *grealloc(void *ptr, size_t size)
 {
     void *p = realloc(ptr, size);
-    if (p == NULL && size) {
+    if (UNLIKELY(p == NULL && size)) {
        fprintf(stderr, "out of memory\n");
        exit(EXIT_FAILURE);
     }