diff options
Diffstat (limited to 'fitz/res_pixmap.c')
-rw-r--r-- | fitz/res_pixmap.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c index c6410a80..09d6e1a4 100644 --- a/fitz/res_pixmap.c +++ b/fitz/res_pixmap.c @@ -1,5 +1,8 @@ #include "fitz.h" +static int fz_memory_limit = 256 << 20; +static int fz_memory_used = 0; + fz_pixmap * fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char *samples) { @@ -31,6 +34,7 @@ fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char * } else { + fz_memory_used += pix->w * pix->h * pix->n; pix->samples = fz_calloc(pix->h, pix->w * pix->n); pix->free_samples = 1; } @@ -39,6 +43,20 @@ fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char * } fz_pixmap * +fz_new_pixmap_with_limit(fz_colorspace *colorspace, int w, int h) +{ + int n = colorspace ? colorspace->n + 1 : 1; + int size = w * h * n; + if (fz_memory_used + size > fz_memory_limit) + { + fz_warn("pixmap memory exceeds soft limit %dM + %dM > %dM", + fz_memory_used/(1<<20), size/(1<<20), fz_memory_limit/(1<<20)); + return NULL; + } + return fz_new_pixmap_with_data(colorspace, w, h, NULL); +} + +fz_pixmap * fz_new_pixmap(fz_colorspace *colorspace, int w, int h) { return fz_new_pixmap_with_data(colorspace, w, h, NULL); @@ -66,6 +84,7 @@ fz_drop_pixmap(fz_pixmap *pix) { if (pix && --pix->refs == 0) { + fz_memory_used -= pix->w * pix->h * pix->n; if (pix->mask) fz_drop_pixmap(pix->mask); if (pix->colorspace) |