From e2e5af1c6135f846a0995e45cff0a73bc5a2063d Mon Sep 17 00:00:00 2001 From: traycold Date: Thu, 22 Mar 2012 02:08:31 +0100 Subject: [PATCH 1/4] added some debug code to better understand memory usage by muPdf --- Makefile | 6 +-- pdf.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 137 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index ce64f0778..8d9fb99b0 100644 --- a/Makefile +++ b/Makefile @@ -31,14 +31,14 @@ ARM_CFLAGS:=-march=armv6 # in that case. ifdef EMULATE_READER - CC:=$(HOSTCC) + CC:=$(HOSTCC) -g CXX:=$(HOSTCXX) EMULATE_READER_W?=824 EMULATE_READER_H?=1200 EMU_CFLAGS?=$(shell sdl-config --cflags) EMU_CFLAGS+= -DEMULATE_READER \ -DEMULATE_READER_W=$(EMULATE_READER_W) \ - -DEMULATE_READER_H=$(EMULATE_READER_H) + -DEMULATE_READER_H=$(EMULATE_READER_H) EMU_LDFLAGS?=$(shell sdl-config --libs) else CFLAGS+= $(ARM_CFLAGS) @@ -131,7 +131,7 @@ $(MUPDFLIBS) $(THIRDPARTYLIBS): $(MUPDFDIR)/cmapdump.host $(MUPDFDIR)/fontdump.h CFLAGS="$(CFLAGS)" make -C mupdf CC="$(CC)" CMAPDUMP=cmapdump.host FONTDUMP=fontdump.host MUPDF= XPS_APPS= $(DJVULIBS): - -mkdir $(DJVUDIR)/build + -mkdir $(DJVUDIR)/build ifdef EMULATE_READER cd $(DJVUDIR)/build && ../configure --disable-desktopfiles --disable-shared --enable-static else diff --git a/pdf.c b/pdf.c index b335d65c7..5583e09a0 100644 --- a/pdf.c +++ b/pdf.c @@ -20,6 +20,10 @@ #include "blitbuffer.h" #include "drawcontext.h" #include "pdf.h" +#include +#include +#include + typedef struct PdfDocument { fz_document *xref; @@ -35,16 +39,141 @@ typedef struct PdfPage { PdfDocument *doc; } PdfPage; + +static double LOG_TRESHOLD_PERC = 0.05; // 5% + +enum { + MAGIC = 0x3795d42b, +}; + +typedef struct header { + int magic; + size_t sz; +} header; + +static size_t msize=0; +static size_t msize_prev; +static size_t msize_max; +static size_t msize_min; +static size_t msize_iniz; +static int is_realloc=0; + +char* readable_fs(double size/*in bytes*/, char *buf) { + int i = 0; + const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; + while (size > 1024) { + size /= 1024; + i++; + } + sprintf(buf, "%.*f %s", i, size, units[i]); + return buf; +} + +static void resetMsize(){ + msize_iniz = msize; + msize_prev = 0; + msize_max = 0; + msize_min = (size_t)-1; +} + +static void showMsize(){ + char buf[15],buf2[15],buf3[15],buf4[15]; + printf("§§§ now: %s was: %s - min: %s - max: %s\n",readable_fs(msize,buf),readable_fs(msize_iniz,buf2),readable_fs(msize_min,buf3),readable_fs(msize_max,buf4)); + resetMsize(); +} + +static void log_size(char *funcName){ + if(msize_max < msize) + msize_max = msize; + if(msize_min > msize) + msize_min = msize; + if(1==0 && abs(msize-msize_prev)>msize_prev*LOG_TRESHOLD_PERC){ + char buf[15],buf2[15]; + printf("§§§ %s - total: %s (was %s)\n",funcName, readable_fs(msize,buf),readable_fs(msize_prev,buf2)); + msize_prev = msize; + } +} + +static void * +my_malloc_default(void *opaque, unsigned int size) +{ + struct header * h = malloc(size + sizeof(header)); + if (h == NULL) + return NULL; + + h -> magic = MAGIC; + h -> sz = size; + msize += size + sizeof(struct header); + if(is_realloc!=1) + log_size("alloc"); + return (void *)(h + 1); +} + +static void +my_free_default(void *opaque, void *ptr) +{ + if (ptr != NULL) { + struct header * h = ((struct header *)ptr) - 1; + if (h -> magic != MAGIC) { /* Not allocated by us */ + } else { + msize -= h -> sz + sizeof(struct header); + free(h); + } + } + if(is_realloc!=1) + log_size("free"); +} + +static void * +my_realloc_default(void *opaque, void *old, unsigned int size) +{ + void * newp; + if (old==NULL) { //practically, it's a malloc + newp = my_malloc_default(opaque, size); + } else { + struct header * h = ((struct header *)old) - 1; + if (h -> magic != MAGIC) { // Not allocated by my_malloc_default + printf("§§§ warn: not allocated by my_malloc_default, new size: %i\n",size); + newp = realloc(old,size); + } else { // malloc + free + is_realloc = 1; + size_t oldsize = h -> sz; + //printf("realloc %i -> %i\n",oldsize,size); + newp = my_malloc_default(opaque, size); + if (NULL != newp) { + memcpy(newp, old, oldsizecontext = fz_new_context(NULL, NULL, cachesize); + doc->context = fz_new_context(&my_alloc_default, NULL, cachesize); fz_try(doc->context) { doc->xref = fz_open_document(doc->context, filename); @@ -116,7 +245,7 @@ static int walkTableOfContent(lua_State *L, fz_outline* ol, int *count, int dept lua_settable(L, -3); lua_pushstring(L, "depth"); - lua_pushnumber(L, depth); + lua_pushnumber(L, depth); lua_settable(L, -3); lua_pushstring(L, "title"); @@ -178,6 +307,7 @@ static int openPage(lua_State *L) { fz_catch(doc->context) { return luaL_error(L, "cannot open page #%d", pageno); } + showMsize(); return 1; } @@ -192,8 +322,8 @@ static int getPageSize(lua_State *L) { ctm = fz_scale(dc->zoom, dc->zoom) ; ctm = fz_concat(ctm, fz_rotate(dc->rotate)); bbox = fz_transform_rect(ctm, bounds); - - lua_pushnumber(L, bbox.x1-bbox.x0); + + lua_pushnumber(L, bbox.x1-bbox.x0); lua_pushnumber(L, bbox.y1-bbox.y0); return 2; From 71c36064e2f46545012e583e7d069b1f43bc9e16 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 27 Mar 2012 18:31:41 +0200 Subject: [PATCH 2/4] added K3 left page keys for emulator on F3 and F4 --- keys.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/keys.lua b/keys.lua index 43e683120..489a93e05 100644 --- a/keys.lua +++ b/keys.lua @@ -123,6 +123,8 @@ end function setEmuKeycodes() KEY_PGFWD = 117 KEY_PGBCK = 112 + KEY_LPGBCK = 69 -- F3 + KEY_LPGFWD = 70 -- F4 KEY_HOME = 110 -- home KEY_BACK = 22 -- backspace KEY_DEL = 119 -- Delete From d3318beb84b006f07fd10caf197439f4277a6f95 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 27 Mar 2012 19:54:07 +0200 Subject: [PATCH 3/4] print defined commands using dump --- unireader.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/unireader.lua b/unireader.lua index bf190fabd..d021cf267 100644 --- a/unireader.lua +++ b/unireader.lua @@ -1175,6 +1175,5 @@ function UniReader:addAllCommands() end end) -- end panning - --print defined commands - --for k,v in pairs(self.commands.map) do print(v) end + print("## defined commands "..dump(self.commands.map)) end From 69a8ee83bbc3a2e03dad9c59f3e92e76ae126c91 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 27 Mar 2012 19:58:29 +0200 Subject: [PATCH 4/4] specify multiple keys for single command #55 This code still doesn't support different modifiers on keys, but I would rather refactor it in small steps first Current changes adds support for Kindle 3 left page keys reported by @houqp in issue #55 --- commands.lua | 11 +++++++++-- unireader.lua | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/commands.lua b/commands.lua index 43dfca87d..a6622ac43 100644 --- a/commands.lua +++ b/commands.lua @@ -62,8 +62,15 @@ Commands = { size = 0 } function Commands:add(keycode,modifier,keydescr,help,func) - local keydef = Keydef:new(keycode,modifier,keydescr) - self:_addImpl(keydef,help,func) + if type(keycode) == "table" then + for i=1,#keycode,1 do + local keydef = Keydef:new(keycode[i],modifier,keydescr) + self:_addImpl(keydef,help,func) + end + else + local keydef = Keydef:new(keycode,modifier,keydescr) + self:_addImpl(keydef,help,func) + end end function Commands:addGroup(keygroup,keys,help,func) for _k,keydef in pairs(keys) do diff --git a/unireader.lua b/unireader.lua index d021cf267..b2cb982a1 100644 --- a/unireader.lua +++ b/unireader.lua @@ -875,12 +875,12 @@ end -- command definitions function UniReader:addAllCommands() self.commands = Commands:new() - self.commands:add(KEY_PGFWD,nil,">", + self.commands:add({KEY_PGFWD,KEY_LPGFWD},nil,">", "next page", function(unireader) unireader:goto(unireader:nextView()) end) - self.commands:add(KEY_PGBCK,nil,"<", + self.commands:add({KEY_PGBCK,KEY_LPGBCK},nil,"<", "previous page", function(unireader) unireader:goto(unireader:prevView())