lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In lua_loadfile(), there are 3 strings added to the string table that
can be immediately garbage collected (and thus, fragment the memory heap
some).  The strings are "@", filename, and "@"+filename.  As far as I
can tell, pushing them to the Lua stack (and as a result, insertion into
the string table) appears to only be used to perform a concatenation on
the strings.  Could the same be accomplished with a static character
buffer on the C stack and strcat()?

The lines in question:

  lua_pushliteral(L, l_s("@"));
  lua_pushstring(L, (filename == NULL) ? l_s("(stdin)") : filename);
  lua_concat(L, 2);
  nlevel = lua_gettop(L);
  filename = lua_tostring(L, -1);  /* filename = `@'..filename */
  luaZ_Fopen(&z, f, filename);
  status = protectedparser(L, &z, bin);
  lua_remove(L, nlevel);  /* remove filename */

Could be replaced with:

  char finalFilename[512];  /* Some size to fit the largest filename */
  strcpy(finalFilename, "@");
  strcat(finalFilename, (filename == NULL) ? l_s("(stdin)") : filename);
  luaZ_Fopen(&z, f, finalFilename);
  status = protectedparser(L, &z, bin);

I've tested this here and it appears to work, but I'm not sure if there
are any repercussions from making the change.

Thanks,
Josh