Merge pull request #82 from dpavlin/memory_usage

Memory usage configurable from lua
close #80
This commit is contained in:
{Qingping,Dave} Hou
2012-03-31 06:28:12 -07:00
5 changed files with 51 additions and 8 deletions

20
djvu.c
View File

@@ -71,7 +71,6 @@ static int handle(lua_State *L, ddjvu_context_t *ctx, int wait)
static int openDocument(lua_State *L) {
const char *filename = luaL_checkstring(L, 1);
/*const char *password = luaL_checkstring(L, 2);*/
int cache_size = luaL_optint(L, 2, 10 << 20);
DjvuDocument *doc = (DjvuDocument*) lua_newuserdata(L, sizeof(DjvuDocument));
@@ -82,6 +81,8 @@ static int openDocument(lua_State *L) {
if (! doc->context) {
return luaL_error(L, "cannot create context.");
}
printf("## cache_size = %d\n", cache_size);
ddjvu_cache_set_size(doc->context, (unsigned long)cache_size);
doc->doc_ref = ddjvu_document_create_by_filename_utf8(doc->context, filename, TRUE);
@@ -449,6 +450,21 @@ static int drawPage(lua_State *L) {
return 0;
}
static int getCacheSize(lua_State *L) {
DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument");
unsigned long size = ddjvu_cache_get_size(doc->context);
printf("## ddjvu_cache_get_size = %d\n", size);
lua_pushnumber(L, size);
return 1;
}
static int cleanCache(lua_State *L) {
DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument");
printf("## ddjvu_cache_clear\n");
ddjvu_cache_clear(doc->context);
return 0;
}
static const struct luaL_Reg djvu_func[] = {
{"openDocument", openDocument},
{NULL, NULL}
@@ -460,6 +476,8 @@ static const struct luaL_Reg djvudocument_meth[] = {
{"getTOC", getTableOfContent},
{"getPageText", getPageText},
{"close", closeDocument},
{"getCacheSize", getCacheSize},
{"cleanCache", cleanCache},
{"__gc", closeDocument},
{NULL, NULL}
};

View File

@@ -6,7 +6,7 @@ DJVUReader = UniReader:new{}
-- DJVU does not support password yet
function DJVUReader:open(filename)
local ok
ok, self.doc = pcall(djvu.openDocument, filename, 10*1024*1024)
ok, self.doc = pcall(djvu.openDocument, filename, self.cache_document_size)
if not ok then
return ok, self.doc -- this will be the error message instead
end

6
pdf.c
View File

@@ -164,16 +164,16 @@ fz_alloc_context my_alloc_default =
static int openDocument(lua_State *L) {
char *filename = strdup(luaL_checkstring(L, 1));
int cachesize = luaL_optint(L, 2, 64 << 20); // 64 MB limit default
int cache_size = luaL_optint(L, 2, 64 << 20); // 64 MB limit default
char buf[15];
printf("cachesize: %s\n",readable_fs(cachesize,buf));
printf("## cache_size: %s\n",readable_fs(cache_size,buf));
PdfDocument *doc = (PdfDocument*) lua_newuserdata(L, sizeof(PdfDocument));
luaL_getmetatable(L, "pdfdocument");
lua_setmetatable(L, -2);
doc->context = fz_new_context(&my_alloc_default, NULL, cachesize);
doc->context = fz_new_context(&my_alloc_default, NULL, cache_size);
fz_try(doc->context) {
doc->xref = fz_open_document(doc->context, filename);

View File

@@ -8,7 +8,7 @@ function PDFReader:open(filename)
-- muPDF manages its own cache, set second parameter
-- to the maximum size you want it to grow
local ok
ok, self.doc = pcall(pdf.openDocument, filename, 64*1024*1024)
ok, self.doc = pcall(pdf.openDocument, filename, self.cache_document_size)
if not ok then
return false, self.doc -- will contain error message
end

View File

@@ -72,6 +72,8 @@ UniReader = {
-- tile cache state:
cache_current_memsize = 0,
cache = {},
-- renderer cache size
cache_document_size = 1024*1024*8, -- FIXME random, needs testing
pagehash = nil,
@@ -105,7 +107,7 @@ end
-- open a file and its settings store
-- tips: you can use self:loadSettings in open() method.
function UniReader:open(filename, password)
function UniReader:open(filename, cache_size)
return false
end
@@ -126,12 +128,24 @@ function UniReader:toggleTextHighLight(word_list)
return
end
----------------------------------------------------
-- renderer memory
----------------------------------------------------
function UniReader:getCacheSize()
return -1
end
function UniReader:cleanCache()
return
end
--[ following are default methods ]--
function UniReader:loadSettings(filename)
if self.doc ~= nil then
self.settings = DocSettings:open(filename)
self.settings = DocSettings:open(filename,self.cache_document_size)
local gamma = self.settings:readSetting("gamma")
if gamma then
@@ -804,6 +818,15 @@ function UniReader:showMenu()
ypos = ypos + 15
blitbuffer.progressBar(fb.bb, 10, ypos, width-20, 15,
5, 4, load_percent, 8)
-- display memory on top of page
fb.bb:paintRect(0, 0, width, 15+6*2, 0)
renderUtf8Text(fb.bb, 10, 15+6, face, fhash,
"Memory: "..
math.ceil( self.cache_current_memsize / 1024 ).."/"..( self.cache_max_memsize / 1024 )..
" "..( self.cache_item_max_pixels / 1024 ).." "..( self.cache_document_size / 1024 ).." k",
true)
fb:refresh(1)
while 1 do
local ev = input.waitForEvent()
@@ -811,6 +834,8 @@ function UniReader:showMenu()
if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then
if ev.code == KEY_BACK or ev.code == KEY_MENU then
return
elseif ev.code == KEY_C then
self.doc:cleanCache()
end
end
end