Skip to contentSkip to conversation

save around 100 bytes on core.img by inlining small misc.c functions

PATCH In review Vladimir 'phcoder' Serbinenko 2 participants 5 messages Aug 15, 2009 – Aug 24, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 save around 100 bytes on core.img by inlining small misc.c functions

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git
include/grub/misc.h +99 -9
index 769ec5c..a63a0b4 100644
@@ -37,8 +37,42 @@ void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
 char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
 char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c);
 char *EXPORT_FUNC(grub_stpcpy) (char *dest, const char *src);
-char *EXPORT_FUNC(grub_strcat) (char *dest, const char *src);
-char *EXPORT_FUNC(grub_strncat) (char *dest, const char *src, int c);
+
+static inline char *
+grub_strcat (char *dest, const char *src)
+{
+  char *p = dest;
+
+  while (*p)
+    p++;
+
+  while ((*p = *src) != '\0')
+    {
+      p++;
+      src++;
+    }
+
+  return dest;
+}
+
+static inline char *
+grub_strncat (char *dest, const char *src, int c)
+{
+  char *p = dest;
+
+  while (*p)
+    p++;
+
+  while ((*p = *src) != '\0' && c--)
+    {
+      p++;
+      src++;
+    }
+
+  *p = '\0';
+
+  return dest;
+}
 
 /* Prototypes for aliases.  */
 #if !defined (GRUB_UTIL) || !defined (APPLE_CC)
@@ -49,19 +83,41 @@ void *EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
 int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
 int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
 int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
-int EXPORT_FUNC(grub_strcasecmp) (const char *s1, const char *s2);
-int EXPORT_FUNC(grub_strncasecmp) (const char *s1, const char *s2, grub_size_t n);
+
 char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
 char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
 char *EXPORT_FUNC(grub_strstr) (const char *haystack, const char *needle);
-int EXPORT_FUNC(grub_iswordseparator) (int c);
 int EXPORT_FUNC(grub_isspace) (int c);
 int EXPORT_FUNC(grub_isprint) (int c);
-int EXPORT_FUNC(grub_isalpha) (int c);
-int EXPORT_FUNC(grub_isgraph) (int c);
-int EXPORT_FUNC(grub_isdigit) (int c);
-int EXPORT_FUNC(grub_tolower) (int c);
+
+static inline int
+grub_isalpha (int c)
+{
+  return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+}
+
+static inline int
+grub_isgraph (int c)
+{
+  return (c >= '!' && c <= '~');
+}
+
+static inline int
+grub_isdigit (int c)
+{
+  return (c >= '0' && c <= '9');
+}
+
+static inline int
+grub_tolower (int c)
+{
+  if (c >= 'A' && c <= 'Z')
+    return c - 'A' + 'a';
+
+  return c;
+}
+
 static inline int
 grub_toupper (int c)
 {
@@ -71,6 +127,40 @@ grub_toupper (int c)
   return c;
 }
 
+static inline int
+grub_strcasecmp (const char *s1, const char *s2)
+{
+  while (*s1 && *s2)
+    {
+      if (grub_tolower (*s1) != grub_tolower (*s2))
+	break;
+
+      s1++;
+      s2++;
+    }
+
+  return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
+}
+
+static inline int
+grub_strncasecmp (const char *s1, const char *s2, grub_size_t n)
+{
+  if (n == 0)
+    return 0;
+
+  while (*s1 && *s2 && --n)
+    {
+      if (grub_tolower (*s1) != grub_tolower (*s2))
+	break;
+
+      s1++;
+      s2++;
+    }
+
+  return (int) grub_tolower (*s1) - (int) grub_tolower (*s2);
+}
+
+
 unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base);
 unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base);
 char *EXPORT_FUNC(grub_strdup) (const char *s);
kern/misc.c +6 -102

138 lines not on this page · raw message

#1 lore raw
Quoted 8 lines · Vladimir 'phcoder' Serbinenko
include/grub/misc.h +8 -0
+static inline char *
+grub_strncat (char *dest, const char *src, int c)
+{
+  char *p = dest;
+
+  while (*p)
+    p++;
+

include/grub/misc.h void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n); · line 75

Quoted 10 lines
include/grub/misc.h +10 -0
+  while ((*p = *src) != '\0' && c--)
+    {
+      p++;
+      src++;
+    }
+
+  *p = '\0';
+
+  return dest;
+}

Some of them (like this one) are surprisingly big. Did you test them separately?

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#2 lore raw
Quoted 14 lines · Robert Millan
Quoted 13 lines · Vladimir 'phcoder' Serbinenko
include/grub/misc.h +13 -0
+static inline char *
+grub_strncat (char *dest, const char *src, int c)
+{
+  char *p = dest;
+
+  while (*p)
+    p++;
+
+  while ((*p = *src) != '\0' && c--)
+    {
+      p++;
+      src++;
+    }
Quoted 8 lines

include/grub/misc.h void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n); · line 75

Quoted 5 lines
include/grub/misc.h +5 -0
+
+  *p = '\0';
+
+  return dest;
+}

Some of them (like this one) are surprisingly big.  Did you test them separately?

Yes I did. My test environment was a core.img with pc+fat+biosdisk. I have a log which functions save how much. In this case inlining strncat saved 21 bytes. The reason for some bigger functions to be inlined is that they are used only in non-size critical parts. In this case it's used in LUA and iso9660.mod (one call). So inlining strncat always saves space (even if iso9660.mod is in core.img but there is no size limit when booting from cd).

Quoted 14 lines

--
Robert Millan

 The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git
#3 lore raw
Quoted 23 lines · Vladimir 'phcoder' Serbinenko
Quoted 14 lines · Robert Millan
Quoted 13 lines · Vladimir 'phcoder' Serbinenko
include/grub/misc.h +13 -0
+static inline char *
+grub_strncat (char *dest, const char *src, int c)
+{
+  char *p = dest;
+
+  while (*p)
+    p++;
+
+  while ((*p = *src) != '\0' && c--)
+    {
+      p++;
+      src++;
+    }
Quoted 8 lines

include/grub/misc.h void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n); · line 75

Quoted 5 lines
include/grub/misc.h +5 -0
+
+  *p = '\0';
+
+  return dest;
+}

Some of them (like this one) are surprisingly big.  Did you test them separately?

Quoted 7 lines

Yes I did. My test environment was a core.img with pc+fat+biosdisk. I have a log which functions save how much. In this case inlining strncat saved 21 bytes. The reason for some bigger functions to be inlined is that they are used only in non-size critical parts. In this case it's used in LUA and iso9660.mod (one call). So inlining strncat always saves space (even if iso9660.mod is in core.img but there is no size limit when booting from cd).

Ok, fine with me then.

Signature · 5 lines
Robert Millan

  The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and
  how) you may access your data; but nobody's threatening your freedom: we
  still allow you to remove your data and not access it at all."
#4 lore raw
Quoted 32 lines · Robert Millan
Quoted 23 lines · Vladimir 'phcoder' Serbinenko
Quoted 14 lines · Robert Millan
Quoted 13 lines · Vladimir 'phcoder' Serbinenko
include/grub/misc.h +13 -0
+static inline char *
+grub_strncat (char *dest, const char *src, int c)
+{
+  char *p = dest;
+
+  while (*p)
+    p++;
+
+  while ((*p = *src) != '\0' && c--)
+    {
+      p++;
+      src++;
+    }
Quoted 8 lines

include/grub/misc.h void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n); · line 75

Quoted 5 lines
include/grub/misc.h +5 -0
+
+  *p = '\0';
+
+  return dest;
+}

Some of them (like this one) are surprisingly big.  Did you test them separately?

Quoted 7 lines

Yes I did. My test environment was a core.img with pc+fat+biosdisk. I have a log which functions save how much. In this case inlining strncat saved 21 bytes. The reason for some bigger functions to be inlined is that they are used only in non-size critical parts. In this case it's used in LUA and iso9660.mod (one call). So inlining strncat always saves space (even if iso9660.mod is in core.img but there is no size limit when booting from cd).

Quoted 1 line

Ok, fine with me then.

Comitted

Quoted 14 lines

--
Robert Millan

 The DRM opt-in fallacy: "Your data belongs to us. We will decide when (and how) you may access your data; but nobody's threatening your freedom: we still allow you to remove your data and not access it at all."

_______________________________________________
Grub-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/grub-devel

Signature · 4 lines
Regards
Vladimir 'phcoder' Serbinenko

Personal git repository: http://repo.or.cz/w/grub2/phcoder.git