]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blobdiff - src/trackfds.h
Prevent inlining for more functions.
[coloredstderr/coloredstderr.git] / src / trackfds.h
index 879445bc81c47cda6e9ed2a9e9a0a6fcdfab3496..eddff4c5b5dc92bbf296c0e52b3979231479900c 100644 (file)
@@ -78,6 +78,8 @@ static void init_from_environment(void) {
 #endif
     char const *env;
 
+    int saved_errno = errno;
+
     initialized = 1;
     tracked_fds_list_count = 0;
 
@@ -90,6 +92,7 @@ static void init_from_environment(void) {
 
     env = getenv(ENV_NAME_FDS);
     if (!env) {
+        errno = saved_errno;
         return;
     }
     /* Environment is read-only. */
@@ -135,7 +138,6 @@ static void init_from_environment(void) {
                  * elements doesn't hurt. */
                 if (!init_tracked_fds_list(count)) {
                     /* Couldn't allocate memory, skip this entry. */
-                    warning("foo\n");
                     goto next;
                 }
             }
@@ -154,6 +156,8 @@ next:
 #ifdef DEBUG
     tracked_fds_debug();
 #endif
+
+    errno = saved_errno;
 }
 
 static char *update_environment_buffer_entry(char *x, int fd) {
@@ -232,6 +236,8 @@ static void tracked_fds_add(int fd) {
     }
 
     if (tracked_fds_list_count >= tracked_fds_list_space) {
+        int saved_errno = errno;
+
         size_t new_space = tracked_fds_list_space + TRACKFDS_REALLOC_STEP;
         int *tmp = realloc(tracked_fds_list,
                            sizeof(*tracked_fds_list) * new_space);
@@ -243,8 +249,11 @@ static void tracked_fds_add(int fd) {
             warning("realloc(tracked_fds_list, %zu) failed! [%d]\n",
                     sizeof(*tracked_fds_list) * new_space, getpid());
 #endif
+            errno = saved_errno;
             return;
         }
+        errno = saved_errno;
+
         tracked_fds_list = tmp;
         tracked_fds_list_space = new_space;
     }
@@ -290,10 +299,25 @@ static int tracked_fds_remove(int fd) {
     /* Not found. */
     return 0;
 }
+
+static int tracked_fds_find_slow(int fd) __noinline;
+/*
+ * tracked_fds_find() is called for each hook call and should be as fast as
+ * possible. As most file descriptors are < TRACKFDS_STATIC_COUNT, force the
+ * compiler to inline that part which is almost exclusively used.
+ *
+ * Inlining tracked_fds_add()/tracked_fds_remove() isn't worth the effort as
+ * they are not called often enough.
+ */
+inline static int tracked_fds_find(int fd) __always_inline;
 static int tracked_fds_find(int fd) {
     if (fd < TRACKFDS_STATIC_COUNT) {
         return tracked_fds[fd];
     }
+
+    return tracked_fds_find_slow(fd);
+}
+static int tracked_fds_find_slow(int fd) {
     if (tracked_fds_list_count == 0) {
         return 0;
     }