From f397fc443c7e798bb5491ca9ab2acef54c5fc0f0 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Sun, 29 Jul 2007 17:35:38 +0200 Subject: [PATCH 01/25] make bcopy use memmove --- src/core/string.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/core/string.c b/src/core/string.c index 040a7785..12d05d53 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -424,12 +424,7 @@ void * memset(void * s,int c,size_t count) */ char * bcopy(const char * src, char * dest, int count) { - char *tmp = dest; - - while (count--) - *tmp++ = *src++; - - return dest; + return memmove(dest,src,count); } #endif From c7ad6778aca7ea8c23ae29318aa379b402e0bf6f Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 00:27:19 +0200 Subject: [PATCH 02/25] put unused functions from string.c into separate file --- src/core/stringextra.c | 288 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 288 insertions(+) create mode 100644 src/core/stringextra.c diff --git a/src/core/stringextra.c b/src/core/stringextra.c new file mode 100644 index 00000000..e8a6d938 --- /dev/null +++ b/src/core/stringextra.c @@ -0,0 +1,288 @@ +/* + * Copyright (C) 1991, 1992 Linus Torvalds + * Copyright (C) 2004 Tobias Lorenz + * + * string handling functions + * based on linux/lib/string.c + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +/* + * stupid library routines.. The optimized versions should generally be found + * as inline code in + * + * These are buggy as well.. + * + * * Fri Jun 25 1999, Ingo Oeser + * - Added strsep() which will replace strtok() soon (because strsep() is + * reentrant and should be faster). Use only strsep() in new code, please. + */ + +/* + * these are the standard string functions that are currently not used by + * any code in etherboot. put into a separate file to avoid linking them in + * with the rest of string.o + * if anything ever does want to use a function of these, consider moving + * the function in question back into string.c + */ + +#include +#include +#include +#include + +/* *** FROM string.c *** */ + +#ifndef __HAVE_ARCH_STRNICMP +/** + * strnicmp - Case insensitive, length-limited string comparison + * @s1: One string + * @s2: The other string + * @len: the maximum number of characters to compare + */ +int strnicmp(const char *s1, const char *s2, size_t len) +{ + /* Yes, Virginia, it had better be unsigned */ + unsigned char c1, c2; + + c1 = 0; c2 = 0; + if (len) { + do { + c1 = *s1; c2 = *s2; + s1++; s2++; + if (!c1) + break; + if (!c2) + break; + if (c1 == c2) + continue; + c1 = tolower(c1); + c2 = tolower(c2); + if (c1 != c2) + break; + } while (--len); + } + return (int)c1 - (int)c2; +} +#endif + +char * ___strtok; + +#ifndef __HAVE_ARCH_STRNCAT +/** + * strncat - Append a length-limited, %NUL-terminated string to another + * @dest: The string to be appended to + * @src: The string to append to it + * @count: The maximum numbers of bytes to copy + * + * Note that in contrast to strncpy, strncat ensures the result is + * terminated. + */ +char * strncat(char *dest, const char *src, size_t count) +{ + char *tmp = dest; + + if (count) { + while (*dest) + dest++; + while ((*dest++ = *src++)) { + if (--count == 0) { + *dest = '\0'; + break; + } + } + } + + return tmp; +} +#endif + +#ifndef __HAVE_ARCH_STRSPN +/** + * strspn - Calculate the length of the initial substring of @s which only + * contain letters in @accept + * @s: The string to be searched + * @accept: The string to search for + */ +size_t strspn(const char *s, const char *accept) +{ + const char *p; + const char *a; + size_t count = 0; + + for (p = s; *p != '\0'; ++p) { + for (a = accept; *a != '\0'; ++a) { + if (*p == *a) + break; + } + if (*a == '\0') + return count; + ++count; + } + + return count; +} +#endif + +#ifndef __HAVE_ARCH_STRCSPN +/** + * strcspn - Calculate the length of the initial substring of @s which only + * contain letters not in @reject + * @s: The string to be searched + * @accept: The string to search for + */ +size_t strcspn(const char *s, const char *reject) +{ + const char *p; + const char *r; + size_t count = 0; + + for (p = s; *p != '\0'; ++p) { + for (r = reject; *r != '\0'; ++r) { + if (*p == *r) + return count; + } + ++count; + } + + return count; +} +#endif + +#ifndef __HAVE_ARCH_STRPBRK +/** + * strpbrk - Find the first occurrence of a set of characters + * @cs: The string to be searched + * @ct: The characters to search for + */ +char * strpbrk(const char * cs,const char * ct) +{ + const char *sc1,*sc2; + + for( sc1 = cs; *sc1 != '\0'; ++sc1) { + for( sc2 = ct; *sc2 != '\0'; ++sc2) { + if (*sc1 == *sc2) + return (char *) sc1; + } + } + return NULL; +} +#endif + +#ifndef __HAVE_ARCH_STRTOK +/** + * strtok - Split a string into tokens + * @s: The string to be searched + * @ct: The characters to search for + * + * WARNING: strtok is deprecated, use strsep instead. + */ +char * strtok(char * s,const char * ct) +{ + char *sbegin, *send; + + sbegin = s ? s : ___strtok; + if (!sbegin) { + return NULL; + } + sbegin += strspn(sbegin,ct); + if (*sbegin == '\0') { + ___strtok = NULL; + return( NULL ); + } + send = strpbrk( sbegin, ct); + if (send && *send != '\0') + *send++ = '\0'; + ___strtok = send; + return (sbegin); +} +#endif + +#ifndef __HAVE_ARCH_STRSEP +/** + * strsep - Split a string into tokens + * @s: The string to be searched + * @ct: The characters to search for + * + * strsep() updates @s to point after the token, ready for the next call. + * + * It returns empty tokens, too, behaving exactly like the libc function + * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. + * Same semantics, slimmer shape. ;) + */ +char * strsep(char **s, const char *ct) +{ + char *sbegin = *s, *end; + + if (sbegin == NULL) + return NULL; + + end = strpbrk(sbegin, ct); + if (end) + *end++ = '\0'; + *s = end; + + return sbegin; +} +#endif + +#ifndef __HAVE_ARCH_BCOPY +/** + * bcopy - Copy one area of memory to another + * @src: Where to copy from + * @dest: Where to copy to + * @count: The size of the area. + * + * Note that this is the same as memcpy(), with the arguments reversed. + * memcpy() is the standard, bcopy() is a legacy BSD function. + * + * You should not use this function to access IO space, use memcpy_toio() + * or memcpy_fromio() instead. + */ +char * bcopy(const char * src, char * dest, int count) +{ + return memmove(dest,src,count); +} +#endif + +#ifndef __HAVE_ARCH_MEMSCAN +/** + * memscan - Find a character in an area of memory. + * @addr: The memory area + * @c: The byte to search for + * @size: The size of the area. + * + * returns the address of the first occurrence of @c, or 1 byte past + * the area if @c is not found + */ +void * memscan(void * addr, int c, size_t size) +{ + unsigned char * p = (unsigned char *) addr; + + while (size) { + if (*p == c) + return (void *) p; + p++; + size--; + } + return (void *) p; +} +#endif + +char * strndup(const char *s, size_t n) +{ + size_t len = strlen(s); + char *new; + + if (len>n) + len = n; + new = malloc(len+1); + if (new) { + new[len] = '\0'; + memcpy(new,s,len); + } + return new; +} From c9c97b344461e260f076d69af719c32eae151b39 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 00:27:37 +0200 Subject: [PATCH 03/25] stripped down version of string.c containing only the used functions --- src/core/string.c | 251 ---------------------------------------------- 1 file changed, 251 deletions(-) diff --git a/src/core/string.c b/src/core/string.c index 12d05d53..8577215b 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -28,41 +28,6 @@ /* *** FROM string.c *** */ -#ifndef __HAVE_ARCH_STRNICMP -/** - * strnicmp - Case insensitive, length-limited string comparison - * @s1: One string - * @s2: The other string - * @len: the maximum number of characters to compare - */ -int strnicmp(const char *s1, const char *s2, size_t len) -{ - /* Yes, Virginia, it had better be unsigned */ - unsigned char c1, c2; - - c1 = 0; c2 = 0; - if (len) { - do { - c1 = *s1; c2 = *s2; - s1++; s2++; - if (!c1) - break; - if (!c2) - break; - if (c1 == c2) - continue; - c1 = tolower(c1); - c2 = tolower(c2); - if (c1 != c2) - break; - } while (--len); - } - return (int)c1 - (int)c2; -} -#endif - -char * ___strtok; - #ifndef __HAVE_ARCH_STRCPY /** * strcpy - Copy a %NUL terminated string @@ -120,35 +85,6 @@ char * strcat(char * dest, const char * src) } #endif -#ifndef __HAVE_ARCH_STRNCAT -/** - * strncat - Append a length-limited, %NUL-terminated string to another - * @dest: The string to be appended to - * @src: The string to append to it - * @count: The maximum numbers of bytes to copy - * - * Note that in contrast to strncpy, strncat ensures the result is - * terminated. - */ -char * strncat(char *dest, const char *src, size_t count) -{ - char *tmp = dest; - - if (count) { - while (*dest) - dest++; - while ((*dest++ = *src++)) { - if (--count == 0) { - *dest = '\0'; - break; - } - } - } - - return tmp; -} -#endif - #ifndef __HAVE_ARCH_STRCMP /** * strcmp - Compare two strings @@ -260,135 +196,6 @@ size_t strnlen(const char * s, size_t count) } #endif -#ifndef __HAVE_ARCH_STRSPN -/** - * strspn - Calculate the length of the initial substring of @s which only - * contain letters in @accept - * @s: The string to be searched - * @accept: The string to search for - */ -size_t strspn(const char *s, const char *accept) -{ - const char *p; - const char *a; - size_t count = 0; - - for (p = s; *p != '\0'; ++p) { - for (a = accept; *a != '\0'; ++a) { - if (*p == *a) - break; - } - if (*a == '\0') - return count; - ++count; - } - - return count; -} -#endif - -#ifndef __HAVE_ARCH_STRCSPN -/** - * strcspn - Calculate the length of the initial substring of @s which only - * contain letters not in @reject - * @s: The string to be searched - * @accept: The string to search for - */ -size_t strcspn(const char *s, const char *reject) -{ - const char *p; - const char *r; - size_t count = 0; - - for (p = s; *p != '\0'; ++p) { - for (r = reject; *r != '\0'; ++r) { - if (*p == *r) - return count; - } - ++count; - } - - return count; -} -#endif - -#ifndef __HAVE_ARCH_STRPBRK -/** - * strpbrk - Find the first occurrence of a set of characters - * @cs: The string to be searched - * @ct: The characters to search for - */ -char * strpbrk(const char * cs,const char * ct) -{ - const char *sc1,*sc2; - - for( sc1 = cs; *sc1 != '\0'; ++sc1) { - for( sc2 = ct; *sc2 != '\0'; ++sc2) { - if (*sc1 == *sc2) - return (char *) sc1; - } - } - return NULL; -} -#endif - -#ifndef __HAVE_ARCH_STRTOK -/** - * strtok - Split a string into tokens - * @s: The string to be searched - * @ct: The characters to search for - * - * WARNING: strtok is deprecated, use strsep instead. - */ -char * strtok(char * s,const char * ct) -{ - char *sbegin, *send; - - sbegin = s ? s : ___strtok; - if (!sbegin) { - return NULL; - } - sbegin += strspn(sbegin,ct); - if (*sbegin == '\0') { - ___strtok = NULL; - return( NULL ); - } - send = strpbrk( sbegin, ct); - if (send && *send != '\0') - *send++ = '\0'; - ___strtok = send; - return (sbegin); -} -#endif - -#ifndef __HAVE_ARCH_STRSEP -/** - * strsep - Split a string into tokens - * @s: The string to be searched - * @ct: The characters to search for - * - * strsep() updates @s to point after the token, ready for the next call. - * - * It returns empty tokens, too, behaving exactly like the libc function - * of that name. In fact, it was stolen from glibc2 and de-fancy-fied. - * Same semantics, slimmer shape. ;) - */ -char * strsep(char **s, const char *ct) -{ - char *sbegin = *s, *end; - - if (sbegin == NULL) - return NULL; - - end = strpbrk(sbegin, ct); - if (end) - *end++ = '\0'; - *s = end; - - return sbegin; -} -#endif - #ifndef __HAVE_ARCH_MEMSET /** * memset - Fill a region of memory with the given value @@ -409,25 +216,6 @@ void * memset(void * s,int c,size_t count) } #endif -#ifndef __HAVE_ARCH_BCOPY -/** - * bcopy - Copy one area of memory to another - * @src: Where to copy from - * @dest: Where to copy to - * @count: The size of the area. - * - * Note that this is the same as memcpy(), with the arguments reversed. - * memcpy() is the standard, bcopy() is a legacy BSD function. - * - * You should not use this function to access IO space, use memcpy_toio() - * or memcpy_fromio() instead. - */ -char * bcopy(const char * src, char * dest, int count) -{ - return memmove(dest,src,count); -} -#endif - #ifndef __HAVE_ARCH_MEMCPY /** * memcpy - Copy one area of memory to another @@ -498,30 +286,6 @@ int memcmp(const void * cs,const void * ct,size_t count) } #endif -#ifndef __HAVE_ARCH_MEMSCAN -/** - * memscan - Find a character in an area of memory. - * @addr: The memory area - * @c: The byte to search for - * @size: The size of the area. - * - * returns the address of the first occurrence of @c, or 1 byte past - * the area if @c is not found - */ -void * memscan(void * addr, int c, size_t size) -{ - unsigned char * p = (unsigned char *) addr; - - while (size) { - if (*p == c) - return (void *) p; - p++; - size--; - } - return (void *) p; -} -#endif - #ifndef __HAVE_ARCH_STRSTR /** * strstr - Find the first substring in a %NUL terminated string @@ -569,21 +333,6 @@ void * memchr(const void *s, int c, size_t n) #endif -char * strndup(const char *s, size_t n) -{ - size_t len = strlen(s); - char *new; - - if (len>n) - len = n; - new = malloc(len+1); - if (new) { - new[len] = '\0'; - memcpy(new,s,len); - } - return new; -} - char * strdup(const char *s) { return strndup(s, ~((size_t)0)); } From cd619a1eec451f440c109122cd41790e0a3f70ee Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 00:27:54 +0200 Subject: [PATCH 04/25] define __pure and __const --- src/include/compiler.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/include/compiler.h b/src/include/compiler.h index b130f28f..e79f9fb2 100644 --- a/src/include/compiler.h +++ b/src/include/compiler.h @@ -279,6 +279,18 @@ extern void dbg_hex_dump_da ( unsigned long dispaddr, /** Apply standard C calling conventions */ #define __cdecl __attribute__ (( cdecl , regparm(0) )) +/** + * Declare a function as pure - i.e. without side effects + */ +#define __pure __attribute__ (( pure )) + +/** + * Declare a function as const - i.e. it does not access global memory + * (including dereferencing pointers passed to it) at all. + * Must also not call any non-const functions. + */ +#define __const __attribute__ (( const )) + /** * Declare a function as used. * From 8e3e97fcef46cd3864da144e8295250b8f77a0db Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 00:51:38 +0200 Subject: [PATCH 05/25] set __pure attribute where possible --- src/include/string.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/include/string.h b/src/include/string.h index 1d104c52..3274bcba 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -17,32 +17,32 @@ #include #include -int strnicmp(const char *s1, const char *s2, size_t len); +int __pure strnicmp(const char *s1, const char *s2, size_t len); char * strcpy(char * dest,const char *src); char * strncpy(char * dest,const char *src,size_t count); char * strcat(char * dest, const char * src); char * strncat(char *dest, const char *src, size_t count); -int __attribute__ (( pure )) strcmp(const char * cs,const char * ct); -int __attribute__ (( pure )) strncmp(const char * cs,const char * ct, +int __pure strcmp(const char * cs,const char * ct); +int __pure strncmp(const char * cs,const char * ct, size_t count); -char * strchr(const char * s, int c); -char * strrchr(const char * s, int c); -size_t strlen(const char * s); -size_t strnlen(const char * s, size_t count); -size_t strspn(const char *s, const char *accept); -size_t strcspn(const char *s, const char *reject); -char * strpbrk(const char * cs,const char * ct); +char * __pure strchr(const char * s, int c); +char * __pure strrchr(const char * s, int c); +size_t __pure strlen(const char * s); +size_t __pure strnlen(const char * s, size_t count); +size_t __pure strspn(const char *s, const char *accept); +size_t __pure strcspn(const char *s, const char *reject); +char * __pure strpbrk(const char * cs,const char * ct); char * strtok(char * s,const char * ct); char * strsep(char **s, const char *ct); void * memset(void * s,int c,size_t count); void * memmove(void * dest,const void *src,size_t count); -int __attribute__ (( pure )) memcmp(const void * cs,const void * ct, +int __pure memcmp(const void * cs,const void * ct, size_t count); -void * memscan(void * addr, int c, size_t size); -char * strstr(const char * s1,const char * s2); -void * memchr(const void *s, int c, size_t n); -char * strdup(const char *s); -char * strndup(const char *s, size_t n); +void * __pure memscan(const void * addr, int c, size_t size); +char * __pure strstr(const char * s1,const char * s2); +void * __pure memchr(const void *s, int c, size_t n); +char * __pure strdup(const char *s); +char * __pure strndup(const char *s, size_t n); extern const char * strerror ( int errno ); From d0d044443d91e2670ac10a6c0f8d70dadec751b7 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 00:52:04 +0200 Subject: [PATCH 06/25] memscan does not alter the mem pointer - make it const --- src/core/stringextra.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/stringextra.c b/src/core/stringextra.c index e8a6d938..bd45ae89 100644 --- a/src/core/stringextra.c +++ b/src/core/stringextra.c @@ -258,7 +258,7 @@ char * bcopy(const char * src, char * dest, int count) * returns the address of the first occurrence of @c, or 1 byte past * the area if @c is not found */ -void * memscan(void * addr, int c, size_t size) +void * memscan(const void * addr, int c, size_t size) { unsigned char * p = (unsigned char *) addr; From e3d10ac6734833e94d329c517c868223d530a955 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 01:01:01 +0200 Subject: [PATCH 07/25] define __nonnull --- src/include/compiler.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/include/compiler.h b/src/include/compiler.h index e79f9fb2..32416a56 100644 --- a/src/include/compiler.h +++ b/src/include/compiler.h @@ -291,6 +291,13 @@ extern void dbg_hex_dump_da ( unsigned long dispaddr, */ #define __const __attribute__ (( const )) +/** + * Declare a function's pointer parameters as non-null - i.e. force + * compiler to check pointers at compile time and enable possible + * optimizations based on that fact + */ +#define __nonnull __attribute__ (( nonnull )) + /** * Declare a function as used. * From 7e8e4f8abdc05a1fdba60edfd50a11a2f3065efd Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 01:02:06 +0200 Subject: [PATCH 08/25] declare pointers passed to string functions as nonnull --- src/include/string.h | 48 ++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/include/string.h b/src/include/string.h index 3274bcba..201ae75c 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -17,32 +17,32 @@ #include #include -int __pure strnicmp(const char *s1, const char *s2, size_t len); -char * strcpy(char * dest,const char *src); -char * strncpy(char * dest,const char *src,size_t count); -char * strcat(char * dest, const char * src); -char * strncat(char *dest, const char *src, size_t count); -int __pure strcmp(const char * cs,const char * ct); +int __pure strnicmp(const char *s1, const char *s2, size_t len) __nonnull; +char * strcpy(char * dest,const char *src) __nonnull; +char * strncpy(char * dest,const char *src,size_t count) __nonnull; +char * strcat(char * dest, const char * src) __nonnull; +char * strncat(char *dest, const char *src, size_t count) __nonnull; +int __pure strcmp(const char * cs,const char * ct) __nonnull; int __pure strncmp(const char * cs,const char * ct, - size_t count); -char * __pure strchr(const char * s, int c); -char * __pure strrchr(const char * s, int c); -size_t __pure strlen(const char * s); -size_t __pure strnlen(const char * s, size_t count); -size_t __pure strspn(const char *s, const char *accept); -size_t __pure strcspn(const char *s, const char *reject); -char * __pure strpbrk(const char * cs,const char * ct); -char * strtok(char * s,const char * ct); -char * strsep(char **s, const char *ct); -void * memset(void * s,int c,size_t count); -void * memmove(void * dest,const void *src,size_t count); + size_t count) __nonnull; +char * __pure strchr(const char * s, int c) __nonnull; +char * __pure strrchr(const char * s, int c) __nonnull; +size_t __pure strlen(const char * s) __nonnull; +size_t __pure strnlen(const char * s, size_t count) __nonnull; +size_t __pure strspn(const char *s, const char *accept) __nonnull; +size_t __pure strcspn(const char *s, const char *reject) __nonnull; +char * __pure strpbrk(const char * cs,const char * ct) __nonnull; +char * strtok(char * s,const char * ct) __nonnull; +char * strsep(char **s, const char *ct) __nonnull; +void * memset(void * s,int c,size_t count) __nonnull; +void * memmove(void * dest,const void *src,size_t count) __nonnull; int __pure memcmp(const void * cs,const void * ct, - size_t count); -void * __pure memscan(const void * addr, int c, size_t size); -char * __pure strstr(const char * s1,const char * s2); -void * __pure memchr(const void *s, int c, size_t n); -char * __pure strdup(const char *s); -char * __pure strndup(const char *s, size_t n); + size_t count) __nonnull; +void * __pure memscan(const void * addr, int c, size_t size) __nonnull; +char * __pure strstr(const char * s1,const char * s2) __nonnull; +void * __pure memchr(const void *s, int c, size_t n) __nonnull; +char * __pure strdup(const char *s) __nonnull; +char * __pure strndup(const char *s, size_t n) __nonnull; extern const char * strerror ( int errno ); From 6b6dbe5600e60897d56be9551a5c3f7dc567f008 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 01:47:31 +0200 Subject: [PATCH 09/25] move strndup back to string.c - used by strdup --- src/core/stringextra.c | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/core/stringextra.c b/src/core/stringextra.c index bd45ae89..c2be4fc4 100644 --- a/src/core/stringextra.c +++ b/src/core/stringextra.c @@ -271,18 +271,3 @@ void * memscan(const void * addr, int c, size_t size) return (void *) p; } #endif - -char * strndup(const char *s, size_t n) -{ - size_t len = strlen(s); - char *new; - - if (len>n) - len = n; - new = malloc(len+1); - if (new) { - new[len] = '\0'; - memcpy(new,s,len); - } - return new; -} From bb94c143d9d4368337dc25d857237b16a2ee4603 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 01:48:20 +0200 Subject: [PATCH 10/25] move strndup back to string.c - used by strdup --- src/core/string.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/core/string.c b/src/core/string.c index 8577215b..2e17bdcb 100644 --- a/src/core/string.c +++ b/src/core/string.c @@ -333,6 +333,21 @@ void * memchr(const void *s, int c, size_t n) #endif +char * strndup(const char *s, size_t n) +{ + size_t len = strlen(s); + char *new; + + if (len>n) + len = n; + new = malloc(len+1); + if (new) { + new[len] = '\0'; + memcpy(new,s,len); + } + return new; +} + char * strdup(const char *s) { return strndup(s, ~((size_t)0)); } From 7e1db201b92a955120f565018b1b144aae50d8b4 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Thu, 2 Aug 2007 03:04:44 +0200 Subject: [PATCH 11/25] strdup and strndup call malloc and are not pure --- src/include/string.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/include/string.h b/src/include/string.h index 201ae75c..210c3753 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -41,8 +41,8 @@ int __pure memcmp(const void * cs,const void * ct, void * __pure memscan(const void * addr, int c, size_t size) __nonnull; char * __pure strstr(const char * s1,const char * s2) __nonnull; void * __pure memchr(const void *s, int c, size_t n) __nonnull; -char * __pure strdup(const char *s) __nonnull; -char * __pure strndup(const char *s, size_t n) __nonnull; +char * strdup(const char *s) __nonnull; +char * strndup(const char *s, size_t n) __nonnull; extern const char * strerror ( int errno ); From 2220e1a676fd46286f48aa193d70fe5197629f49 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:28:11 +0200 Subject: [PATCH 12/25] define malloc attribute --- src/include/compiler.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/include/compiler.h b/src/include/compiler.h index 32416a56..15efb709 100644 --- a/src/include/compiler.h +++ b/src/include/compiler.h @@ -298,6 +298,12 @@ extern void dbg_hex_dump_da ( unsigned long dispaddr, */ #define __nonnull __attribute__ (( nonnull )) +/** + * Declare a pointer returned by a function as a unique memory address + * as returned by malloc-type functions. + */ +#define __malloc __attribute__ (( malloc )) + /** * Declare a function as used. * From dea89e8d62941a3b369c87b885243efaa71627ea Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:28:23 +0200 Subject: [PATCH 13/25] use malloc attribute --- src/crypto/axtls/bigint.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/axtls/bigint.c b/src/crypto/axtls/bigint.c index ee51c14d..49cad971 100644 --- a/src/crypto/axtls/bigint.c +++ b/src/crypto/axtls/bigint.c @@ -58,7 +58,7 @@ static bigint *bi_int_multiply(BI_CTX *ctx, bigint *bi, comp i); static bigint *bi_int_divide(BI_CTX *ctx, bigint *biR, comp denom); -static bigint *alloc(BI_CTX *ctx, int size); +static bigint __malloc *alloc(BI_CTX *ctx, int size); static bigint *trim(bigint *bi); static void more_comps(bigint *bi, int n); #if defined(CONFIG_BIGINT_KARATSUBA) || defined(CONFIG_BIGINT_BARRETT) || \ From 68455adf37ed94363a1cb95c2d166529d6d9fba4 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:28:47 +0200 Subject: [PATCH 14/25] use malloc attribute --- src/crypto/cryptoLayer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crypto/cryptoLayer.h b/src/crypto/cryptoLayer.h index 538b84f2..28ce97bc 100644 --- a/src/crypto/cryptoLayer.h +++ b/src/crypto/cryptoLayer.h @@ -31,7 +31,7 @@ typedef void psPool_t; #define sslAssert( ... ) assert ( __VA_ARGS__ ) -static inline __attribute__ (( always_inline )) void * +static inline __attribute__ (( always_inline )) void * __malloc psMalloc ( psPool_t *pool __unused, size_t len ) { return malloc ( len ); } From 938626343378dfa74cdf50a0c4a23bdb88605a45 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:29:09 +0200 Subject: [PATCH 15/25] use malloc attribute --- src/include/gpxe/dhcp.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/gpxe/dhcp.h b/src/include/gpxe/dhcp.h index 86322786..8f5651b1 100644 --- a/src/include/gpxe/dhcp.h +++ b/src/include/gpxe/dhcp.h @@ -502,7 +502,7 @@ extern void register_dhcp_options ( struct dhcp_option_block *options ); extern void unregister_dhcp_options ( struct dhcp_option_block *options ); extern void init_dhcp_options ( struct dhcp_option_block *options, void *data, size_t max_len ); -extern struct dhcp_option_block * alloc_dhcp_options ( size_t max_len ); +extern struct dhcp_option_block * __malloc alloc_dhcp_options ( size_t max_len ); extern struct dhcp_option * set_dhcp_option ( struct dhcp_option_block *options, unsigned int tag, const void *data, size_t len ); From 05708503e54af40a304210466ad3f733f5a13c48 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:29:23 +0200 Subject: [PATCH 16/25] use malloc attribute --- src/include/gpxe/iobuf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/gpxe/iobuf.h b/src/include/gpxe/iobuf.h index c0dfd434..e3db01ac 100644 --- a/src/include/gpxe/iobuf.h +++ b/src/include/gpxe/iobuf.h @@ -161,7 +161,7 @@ static inline size_t iob_tailroom ( struct io_buffer *iobuf ) { return ( iobuf->end - iobuf->tail ); } -extern struct io_buffer * alloc_iob ( size_t len ); +extern struct io_buffer * __malloc alloc_iob ( size_t len ); extern void free_iob ( struct io_buffer *iobuf ); extern void iob_pad ( struct io_buffer *iobuf, size_t min_len ); extern int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ); From c4285e148d163121e7ecdbe7a4386a4307fb0624 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:29:30 +0200 Subject: [PATCH 17/25] use malloc attribute --- src/include/gpxe/malloc.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/include/gpxe/malloc.h b/src/include/gpxe/malloc.h index 0e18f6a3..cce5d1d3 100644 --- a/src/include/gpxe/malloc.h +++ b/src/include/gpxe/malloc.h @@ -19,7 +19,7 @@ extern size_t freemem; -extern void * alloc_memblock ( size_t size, size_t align ); +extern void * __malloc alloc_memblock ( size_t size, size_t align ); extern void free_memblock ( void *ptr, size_t size ); extern void mpopulate ( void *start, size_t len ); extern void mdumpfree ( void ); @@ -35,7 +35,7 @@ extern void mdumpfree ( void ); * * @c align must be a power of two. @c size may not be zero. */ -static inline void * malloc_dma ( size_t size, size_t phys_align ) { +static inline void * __malloc malloc_dma ( size_t size, size_t phys_align ) { return alloc_memblock ( size, phys_align ); } From 347ab9b8c2b13c76ac964bd88e60d632a196a509 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:29:41 +0200 Subject: [PATCH 18/25] use malloc attribute --- src/include/readline/readline.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/readline/readline.h b/src/include/readline/readline.h index 4214a44f..1a03b483 100644 --- a/src/include/readline/readline.h +++ b/src/include/readline/readline.h @@ -7,6 +7,6 @@ * */ -extern char * readline ( const char *prompt ); +extern char * __malloc readline ( const char *prompt ); #endif /* _READLINE_H */ From 6f61e254dc801da7fab388ed4791ac9de73aaeba Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:29:50 +0200 Subject: [PATCH 19/25] use malloc attribute --- src/include/stdlib.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/include/stdlib.h b/src/include/stdlib.h index 5c8fc3df..ae996962 100644 --- a/src/include/stdlib.h +++ b/src/include/stdlib.h @@ -20,10 +20,10 @@ extern unsigned long strtoul ( const char *p, char **endp, int base ); **************************************************************************** */ -extern void * malloc ( size_t size ); +extern void * __malloc malloc ( size_t size ); extern void * realloc ( void *old_ptr, size_t new_size ); extern void free ( void *ptr ); -extern void * zalloc ( size_t len ); +extern void * __malloc zalloc ( size_t len ); /** * Allocate cleared memory @@ -38,7 +38,7 @@ extern void * zalloc ( size_t len ); * function in zalloc(), since in most cases @c nmemb will be 1 and * doing the multiply is just wasteful. */ -static inline void * calloc ( size_t nmemb, size_t size ) { +static inline void * __malloc calloc ( size_t nmemb, size_t size ) { return zalloc ( nmemb * size ); } From 8bb295e3fd54a59b554ff5e2ff2d510ebf32f5f5 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:29:58 +0200 Subject: [PATCH 20/25] use malloc attribute --- src/include/string.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/include/string.h b/src/include/string.h index 210c3753..a2894a3a 100644 --- a/src/include/string.h +++ b/src/include/string.h @@ -41,9 +41,9 @@ int __pure memcmp(const void * cs,const void * ct, void * __pure memscan(const void * addr, int c, size_t size) __nonnull; char * __pure strstr(const char * s1,const char * s2) __nonnull; void * __pure memchr(const void *s, int c, size_t n) __nonnull; -char * strdup(const char *s) __nonnull; -char * strndup(const char *s, size_t n) __nonnull; +char * __malloc strdup(const char *s) __nonnull; +char * __malloc strndup(const char *s, size_t n) __nonnull; -extern const char * strerror ( int errno ); +extern const char * __pure strerror ( int errno ); #endif /* ETHERBOOT_STRING */ From 6f0eca14007d8d2c06dea9482adc3a0bd034fdb0 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:30:11 +0200 Subject: [PATCH 21/25] use malloc attribute --- src/net/ipv4.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/net/ipv4.c b/src/net/ipv4.c index 35341b3d..2f50f0e4 100644 --- a/src/net/ipv4.c +++ b/src/net/ipv4.c @@ -39,10 +39,9 @@ static LIST_HEAD ( frag_buffers ); * @v gateway Gateway address (or @c INADDR_NONE for no gateway) * @ret miniroute Routing table entry, or NULL */ -static struct ipv4_miniroute * add_ipv4_miniroute ( struct net_device *netdev, - struct in_addr address, - struct in_addr netmask, - struct in_addr gateway ) { +static struct ipv4_miniroute * __malloc +add_ipv4_miniroute ( struct net_device *netdev, struct in_addr address, + struct in_addr netmask, struct in_addr gateway ) { struct ipv4_miniroute *miniroute; DBG ( "IPv4 add %s", inet_ntoa ( address ) ); From bc9f960b66f40479153dfe632aff286f1d61d7ff Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:30:17 +0200 Subject: [PATCH 22/25] use malloc attribute --- src/net/ipv6.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/net/ipv6.c b/src/net/ipv6.c index 3b7429b8..c228f09c 100644 --- a/src/net/ipv6.c +++ b/src/net/ipv6.c @@ -55,11 +55,10 @@ static LIST_HEAD ( miniroutes ); * @v gateway Gateway address (or ::0 for no gateway) * @ret miniroute Routing table entry, or NULL */ -static struct ipv6_miniroute * add_ipv6_miniroute ( struct net_device *netdev, - struct in6_addr prefix, - int prefix_len, - struct in6_addr address, - struct in6_addr gateway ) { +static struct ipv6_miniroute * __malloc +add_ipv6_miniroute ( struct net_device *netdev, struct in6_addr prefix, + int prefix_len, struct in6_addr address, + struct in6_addr gateway ) { struct ipv6_miniroute *miniroute; miniroute = malloc ( sizeof ( *miniroute ) ); From f92096d1800f3d9ec7e90cc9bb0e0874569fab50 Mon Sep 17 00:00:00 2001 From: Holger Lubitz Date: Mon, 20 Aug 2007 20:30:24 +0200 Subject: [PATCH 23/25] use malloc attribute --- src/net/tls.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/tls.c b/src/net/tls.c index 64e44b55..5c201b32 100644 --- a/src/net/tls.c +++ b/src/net/tls.c @@ -1044,7 +1044,7 @@ static void tls_hmac ( struct tls_session *tls __unused, * @ret plaintext_len Length of plaintext record * @ret plaintext Allocated plaintext record */ -static void * tls_assemble_stream ( struct tls_session *tls, +static void * __malloc tls_assemble_stream ( struct tls_session *tls, const void *data, size_t len, void *digest, size_t *plaintext_len ) { size_t mac_len = tls->tx_cipherspec.digest->digestsize; From d078fb3cb72fcd3803c4e4e74f45c31d08040736 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Wed, 22 Aug 2007 23:10:00 +0100 Subject: [PATCH 24/25] Allow legacy "make bin/etherboot.%". --- src/Makefile.housekeeping | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Makefile.housekeeping b/src/Makefile.housekeeping index ee652b0a..6126247b 100644 --- a/src/Makefile.housekeeping +++ b/src/Makefile.housekeeping @@ -403,6 +403,11 @@ include $(MEDIA_DEPS) allroms allzroms : all%s : $(foreach ROM,$(ROMS),$(BIN)/$(ROM).%) all%s : $(foreach DRIVER,$(DRIVERS),$(BIN)/$(DRIVER).%) +# Alias for gpxe.% +# +$(BIN)/etherboot.% : $(BIN)/gpxe.% + ln -sf $(notdir $<) $@ + # The compression utilities # $(NRV2B) : util/nrv2b.c $(MAKEDEPS) From 01b755704a7ebc235d4e4413e631e97ee1b997e2 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Thu, 23 Aug 2007 20:50:24 +0100 Subject: [PATCH 25/25] Auto-assign drive number only if set to 0xff; almost all applications require that we use drive 0x80, so it should probably be the default. --- src/arch/i386/interface/pcbios/int13.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/arch/i386/interface/pcbios/int13.c b/src/arch/i386/interface/pcbios/int13.c index 53817c7a..a26dcff4 100644 --- a/src/arch/i386/interface/pcbios/int13.c +++ b/src/arch/i386/interface/pcbios/int13.c @@ -543,8 +543,9 @@ void register_int13_drive ( struct int13_drive *drive ) { /* Assign drive number if none specified, update BIOS drive count */ get_real ( num_drives, BDA_SEG, BDA_NUM_DRIVES ); - if ( ! drive->drive ) - drive->drive = ( num_drives | 0x80 ); + if ( ( drive->drive & 0xff ) == 0xff ) + drive->drive = num_drives; + drive->drive |= 0x80; num_drives++; if ( num_drives <= ( drive->drive & 0x7f ) ) num_drives = ( ( drive->drive & 0x7f ) + 1 );