[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- 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:
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.
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
standarized?
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() [1].
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.
static int l_openlog(lua_State *L)
{
...
/* handle ident option */
size_t len;
const char *ident = luaL_checklstring(L, 1, &len);
lua_pushvalue(L, 1);
/* store it in our environment table */
lua_setfield(L, LUA_ENVIRONINDEX, "ident");
openlog(ident, option, facility);
return 0;
}
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)
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
garbage collected.
So there is no "ident" lookup when calling syslog(), just in closelog(),
which should happen only once ;-)
Gé
--
Gé Weijers
ge@weijers.org <mailto:ge@weijers.org>
[1] http://www.hmug.org/man/3/syslog.php
--
Scanned by *MailScanner* <http://www.mailscanner.info/>.
--
Mit freundlichen Grüßen
Jesus Ruiz de Infante
(Entwicklung)
HALE electronic GmbH
Eugen-Müller-Straße 18, 5020 Salzburg, Austria
Tel: +43 (662) 439011 0
Fax: +43 (662) 439011 9
jesus.ruizdeinfante@hale.at
Firmenbuchnummer: FN 66801m HG Salzburg
--
Scanned by MailScanner.