X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=gcc%2FREADME.Portability;h=4101a2f7b4240e998f83f5bd61583b6c92b33cac;hb=cf95b05b99e654234040272fc4095ec0a455d7e0;hp=2724f2a53fd65ccf8db1c04e96b31017b6d36e81;hpb=34c059db3cfd6b16de62f2312f776b74eb459ca5;p=pf3gnuchains%2Fgcc-fork.git diff --git a/gcc/README.Portability b/gcc/README.Portability index 2724f2a53fd..4101a2f7b42 100644 --- a/gcc/README.Portability +++ b/gcc/README.Portability @@ -10,7 +10,7 @@ This knowledge until know has been sparsely spread around, so I thought I'd collect it in one useful place. Please add and correct any problems as you come across them. -I'm going to start from a base of the ISO C89 standard, since that is +I'm going to start from a base of the ISO C90 standard, since that is probably what most people code to naturally. Obviously using constructs introduced after that is not a good idea. @@ -51,14 +51,28 @@ foo (bar, ) needs to be coded in some other way. -free and realloc ----------------- +Avoid unnecessary test before free +---------------------------------- -Some implementations crash upon attempts to free or realloc the null -pointer. Thus if mem might be null, you need to write +Since SunOS 4 stopped being a reasonable portability target, +(which happened around 2007) there has been no need to guard +against "free (NULL)". Thus, any guard like the following +constitutes a redundant test: + + if (P) + free (P); + +It is better to avoid the test.[*] +Instead, simply free P, regardless of whether it is NULL. + +[*] However, if your profiling exposes a test like this in a +performance-critical loop, say where P is nearly always NULL, and +the cost of calling free on a NULL pointer would be prohibitively +high, consider using __builtin_expect, e.g., like this: + + if (__builtin_expect (ptr != NULL, 0)) + free (ptr); - if (mem) - free (mem); Trigraphs @@ -194,4 +208,3 @@ o Passing incorrect types to fprintf and friends. o Adding a function declaration for a module declared in another file to a .c file instead of to a .h file. -