[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: how to make a string outlive the invocation of a C function?
- From: Jesús Ruiz de Infante <jesus.ruizdeinfante@...>
- Date: Tue, 17 Apr 2007 11:04:17 +0200
Gé Weijers wrote:
You are right. Static variables contained in DLLs are not shared by the
processes linked against them. At least not on Linux. Is this point
Every process should get a copy of your global variables, not share them
with other processes. Global variables are shared between (Posix)
threads that are part of the same process, not between processes (the
library loader usually uses copy-on-write pages to implement this). I
would still prefer a reentrant solution though, so storing it in the
module environment is the right thing to do.
Your solution is a little complicated, you could just store the ident
string passed as an argument itself, not a userdata object, why keep two
copies of the same thing around when Lua string are immutable? When you
need it you just use lua_tolstring to get the same string pointer again
(and again). So your code simplified to:
syslog() uses the pointer to ident set by the previous call to
openlog(). There is no explicit ident parameter passed to syslog() .
So there is actually no need for lua_tolstring.
It should be guaranteed however that the ident pointer passed to
openlog() points "for ever" to the ident string. In addition, storing a
pointer to an internal Lua string is not a good idea, even if it is
stored in the module enviroment (?). Hence the userdata.
My intention regarding the call to lua_setfield() above is just to
ensure that the ident string is referenced by Lua and does not get
static int l_openlog(lua_State *L)
/* handle ident option */
const char *ident = luaL_checklstring(L, 1, &len);
/* store it in our environment table */
lua_setfield(L, LUA_ENVIRONINDEX, "ident");
openlog(ident, option, facility);
If you're worried about efficiency you could store the string at integer
index 1, but you'll bog down the syslog daemon long before that :-/
(syslogd tends to do an 'fsync' call after every write)
So there is no "ident" lookup when calling syslog(), just in closelog(),
which should happen only once ;-)
Scanned by *MailScanner* <http://www.mailscanner.info/>.
Mit freundlichen Grüßen
Jesus Ruiz de Infante
HALE electronic GmbH
Eugen-Müller-Straße 18, 5020 Salzburg, Austria
Tel: +43 (662) 439011 0
Fax: +43 (662) 439011 9
Firmenbuchnummer: FN 66801m HG Salzburg
Scanned by MailScanner.