elua: use Eina_File (mmap)

This commit is contained in:
Daniel Kolesa 2014-04-04 10:56:20 +01:00 committed by Daniel Kolesa
parent 26dc3505bc
commit 063dd4147e
3 changed files with 43 additions and 89 deletions

View File

@ -10,32 +10,31 @@
/* bytecode caching */
static int check_bc(const char *fname, const char *mode, Eina_Bool *bc) {
const char *ext = strstr(fname, ".lua");
static Eina_File *check_bc(Eina_File *of, const char *mode, Eina_Bool *bc) {
const char *fname = eina_file_filename_get(of);
const char *ext = strstr(fname, ".lua");
if (ext && !ext[4] && (!mode || strchr(mode, 't'))) {
/* loading lua source file, try cached */
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "%sc", fname);
int fd = open(buf, O_RDONLY);
if (fd < 0) {
Eina_File *f = eina_file_open(buf, EINA_FALSE);
if (!f) {
/* no cached bytecode */
*bc = EINA_TRUE;
} else {
/* cached bytecode, check timestamp */
struct stat s1, s2;
stat(fname, &s1);
stat(buf, &s2);
if (s2.st_ctime > s1.st_ctime) {
/* cached bytecode, check timestamps */
if (eina_file_mtime_get(f) > eina_file_mtime_get(of)) {
/* bytecode new enough, chunkname stays the same */
return fd;
eina_file_close(of);
return f;
} else {
/* bytecode too old, remove old file */
close(fd);
eina_file_close(f);
*bc = EINA_TRUE;
}
}
}
return -1;
return of;
}
static int writef(lua_State *L EINA_UNUSED, const void *p, size_t size,
@ -56,111 +55,69 @@ static void write_bc(lua_State *L, const char *fname) {
}
}
/* loadfile - regular version */
typedef struct Cached_Stream {
FILE *f;
char buff[LUAL_BUFFERSIZE];
} Cached_Stream;
static const char *getf(lua_State *L EINA_UNUSED, void *ud, size_t *size) {
Cached_Stream *s = ud;
if (feof(s->f)) return NULL;
*size = fread(s->buff, 1, sizeof(s->buff), s->f);
return (*size > 0) ? s->buff : NULL;
char *buff = *((char**)ud);
if (feof(stdin)) return NULL;
*size = fread(buff, 1, LUAL_BUFFERSIZE, stdin);
return (*size > 0) ? buff : NULL;
}
int elua_loadfilex(lua_State *L, const char *fname, const char *mode) {
Cached_Stream s;
int status;
const char *chname;
Eina_Bool bcache = EINA_FALSE;
if (!fname) {
s.f = stdin;
chname = "=stdin";
} else {
if (!(s.f = fopen(fname, "rb"))) {
lua_pushfstring(L, "cannot open %s: %s", fname, strerror(errno));
return LUA_ERRFILE;
}
chname = lua_pushfstring(L, "@%s", fname);
int fd = check_bc(fname, mode, &bcache);
if (fd >= 0) {
fclose(s.f);
s.f = fdopen(fd, "rb");
}
}
status = lua_loadx(L, getf, &s, chname, mode);
if (ferror(s.f)) {
static int elua_loadstdin(lua_State *L, const char *mode) {
char buff[LUAL_BUFFERSIZE];
int status = lua_loadx(L, getf, &buff, "=stdin", mode);
if (ferror(stdin)) {
lua_pop(L, 1);
lua_pushfstring(L, "cannot read %s: %s", chname + 1, strerror(errno));
if (fname) {
lua_remove(L, -2);
fclose(s.f);
}
lua_pushfstring(L, "cannot read stdin: %s", strerror(errno));
return LUA_ERRFILE;
}
/* trigger bytecode writing */
if (!status && bcache) write_bc(L, fname);
if (fname) {
lua_remove(L, -2);
fclose(s.f);
}
return status;
}
int elua_loadfile(lua_State *L, const char *fname) {
return elua_loadfilex(L, fname, NULL);
}
/* loadfile - mmap version */
typedef struct Map_Stream {
char *fmap;
size_t flen;
} Map_Stream;
static const char *getf_map(lua_State *L EINA_UNUSED, void *ud, size_t *size) {
Map_Stream *s = ud;
Map_Stream *s = ud;
const char *fmap = s->fmap;
*size = s->flen;
return s->fmap;
/* gotta null it - tell luajit to terminate reading */
s->fmap = NULL;
return fmap;
}
int elua_loadfilex_mmap(lua_State *L, const char *fname, const char *mode) {
int elua_loadfilex(lua_State *L, const char *fname, const char *mode) {
Map_Stream s;
int status, fd, nfd;
int status;
Eina_File *f;
const char *chname;
Eina_Bool bcache = EINA_FALSE;
if (!fname) {
return elua_loadfilex(L, fname, mode);
return elua_loadstdin(L, mode);
}
if ((fd = open(fname, O_RDONLY, 0)) < 0) {
if (!(f = eina_file_open(fname, EINA_FALSE))) {
lua_pushfstring(L, "cannot open %s: %s", fname, strerror(errno));
return LUA_ERRFILE;
}
chname = lua_pushfstring(L, "@%s", fname);
if ((nfd = check_bc(fname, mode, &bcache)) > 0) {
close(fd);
fd = nfd;
}
s.flen = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
if ((s.fmap = mmap(NULL, s.flen, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0))
== MAP_FAILED) {
f = check_bc(f, mode, &bcache);
s.flen = eina_file_size_get(f);
if (!(s.fmap = eina_file_map_all(f, EINA_FILE_RANDOM))) {
lua_pushfstring(L, "cannot read %s: %s", chname + 1, strerror(errno));
lua_remove(L, -2);
return LUA_ERRFILE;
}
status = lua_loadx(L, getf_map, &s, chname, mode);
munmap(s.fmap, s.flen);
close(fd);
eina_file_map_free(f, s.fmap);
eina_file_close(f);
if (!status && bcache) write_bc(L, fname);
lua_remove(L, -2);
return status;
}
int elua_loadfile_mmap(lua_State *L, const char *fname) {
return elua_loadfilex_mmap(L, fname, NULL);
int elua_loadfile(lua_State *L, const char *fname) {
return elua_loadfilex(L, fname, NULL);
}
/* lua function */
@ -168,12 +125,8 @@ int elua_loadfile_mmap(lua_State *L, const char *fname) {
static int loadfile(lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
const char *mode = luaL_optstring(L, 2, NULL);
int status, hasenv = (lua_gettop(L) >= 3);
if (lua_toboolean(L, 4)) {
status = elua_loadfilex_mmap(L, fname, mode);
} else {
status = elua_loadfilex(L, fname, mode);
}
int status = elua_loadfilex(L, fname, mode),
hasenv = (lua_gettop(L) >= 3);
if (!status) {
if (hasenv) {
lua_pushvalue(L, 3);

View File

@ -229,7 +229,10 @@ static int lua_main(lua_State *L) {
luaL_openlibs(L);
elua_loadfile(L, ELUA_DATA_DIR "/core/module.lua");
if (report(L, elua_loadfile(L, ELUA_DATA_DIR "/core/module.lua"))) {
m->status = 1;
return 0;
}
lua_pushcfunction(L, register_require);
lua_createtable(L, 0, 0);
luaL_register(L, NULL, utillib);

View File

@ -30,8 +30,6 @@ extern int el_log_domain;
int elua_loadfilex(lua_State *L, const char *fname, const char *mode);
int elua_loadfile(lua_State *L, const char *fname);
int elua_loadfilex_mmap(lua_State *L, const char *fname, const char *mode);
int elua_loadfile_mmap(lua_State *L, const char *fname);
void elua_register_cache(lua_State *L);
#endif