🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #50056

Some suggestions

#50056
From: eddy
Date: Fri, 19 Nov 1993 12:36
95 lines
4349 bytes
First, an irrelevant silly that mucked up emacs bracket-matching in dog.c, 
line 474-8; return(obj-otyp > is repeated at the beginning of each version; it 
would be perfectly satisfactory (and slightly reduce the file size) to say
	default: return (obj->otyp > 
#ifdef TUTTI_FRUTTI
			SLIME_MOLD
#else
			CLOVE_OF_GARLIC
#endif
			) ? (carni ? ACCFOOD : MANFOOD) : 
			(herbi ? ACCFOOD : MANFOOD);
In fact this could be re-written so it only says `? ACCFOOD : MANFOOD' once.
But this is irrelevant.

I note that a lot of functions are one-liners, which is very nice programming 
style but may slow things slightly.  Here is a way to turn them into inliners
wherever gcc is in use:

Start with a function (illustrative of many), say `strnlen', defined in file
sundry.c and declared, for everyone else to import, in sundry.h;
So sundry.h says:
extern int strnlen(char *whose, int max);

and sundry.c says:
int strnlen(char *whose, int max)
{ int result = 0;
  while (result < max && whose[result] != '\0') result++;
  return result;
} /* end of strnlen */

Create a file sundry.g and insert the following:

sundry_inline_m int strnlen(char *whose, int max)
#ifdef sundry_bodies_m
{ int result = 0;
  while (result < max && whose[result] != '\0') result++;
  return result;
} /* end of strnlen */
#else
;
#endif

(plus similar for each other function to be treated thus).  Then remove the
declaration(s) from sundry.[ch]; in sundry.h say

#ifndef sundry_inline_m /* ie we're not being included by sundry.c */
#ifdef CanInline
#define sundry_inline_m extern inline
#define sundry_bodies_m /* defined */
#else
#define sundry_inline_m extern
#undef sundry_bodies_m
#endif
#endif
/* you can insert inclusions of other .h files here, of course */
#include "sundry.g"

and in sundry.c say

#define sundry_inline_m /* empty or extern */
#define sundry_bodies_m /* defined */
#include "sundry.h"

and never define sundry_*_m in any other file.  Then, if your compiler supports
gcc-style inlining you can pass it -DCanInline; this will prompt the .h file
to contain extern inline definitions except where it is included by the .c file,
in which instance it generates the (linkable) definition of the extern function
for the places which couldn't use the inlining.  The inline and body macros
need to be file-specific so that sundry.[ch] can include other .h files which 
may be part of the same game without firing off their linkable definitions.

In some situations, it is necessary that declarations of the functions appear
above the definitions.  To do this, just after defining sundry_inline_m to 
extern inline but before defining sundry_bodies_m, insert an extra inclusion
of sundry.g; this generates extern inline declarations without their bodies.  To
supply declarations to the .c file before it gets the definitions, give the
outer #ifdef sundry_bodies_m ... #endif an else clause which says
#undef sundry_bodies_m
#include "sundry.g"
#define sundry_bodies_m
and be sure that the definition of sundry_inline_m given in sundry.c is extern
not empty.

It's only moderately ugly and can be very helpful for getting inlining without
sacrificing ANSI-ness (in the absence of -DCanInline, all of the above is 
perfectly good ANSI code).  It's also possible to change over to such a scheme
incrementally (I modified sundry.[ch], checked I'd got it right by remaking all
my applications both with and without -DCanInline, then went on to modify (in
succession, with similar interludes) memory.[ch] and then vector.[ch]; having 
checked that it all worked in my sundry library I then started rolling over
all my other code to the same scheme).

I hope this can be of use to you, 

				Eddy.
Technical Details
Message-ID: <9311191229.AA18111@eddy.gen.cam.ac.uk>
Path: arc1-ca.pugleaf.net!gmd.de!xlink.net!sol.ctr.columbia.edu!math.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!gen.CAm.ac.UK!eddy

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,4cb031cd01ee57b1,start
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1993-11-19 05:18:20 PST
Path: arc1-ca.pugleaf.net!gmd.de!xlink.net!sol.ctr.columbia.edu!math.ohio-state.edu!magnus.acs.ohio-state.edu!cis.ohio-state.edu!gen.CAm.ac.UK!eddy
From: eddy@gen.CAm.ac.UK (eddy)
Newsgroups: gnu.gcc.help
Subject: Some suggestions
Date: 19 Nov 1993 07:36:21 -0500
Organization: GNUs Not Usenet
Lines: 95
Sender: daemon@cis.ohio-state.edu
Distribution: gnu
Message-ID: <9311191229.AA18111@eddy.gen.cam.ac.uk>