goo/gmem.cc | 92 +++++++++++++++++++++++++++++------------------------------- goo/gmem.h | 4 +- 2 files changed, 48 insertions(+), 48 deletions(-)
New commits: commit 50c4ee413929e5a70133839e3cde039da738fab2 Author: Albert Astals Cid <[email protected]> Date: Wed Mar 18 22:00:05 2009 +0100 Add more _checkoverflow variants, rework internals diff --git a/goo/gmem.cc b/goo/gmem.cc index ca41928..298d5dd 100644 --- a/goo/gmem.cc +++ b/goo/gmem.cc @@ -14,7 +14,7 @@ // under GPL version 2 or later // // Copyright (C) 2005 Takashi Iwai <[email protected]> -// Copyright (C) 2007, 2008 Albert Astals Cid <[email protected]> +// Copyright (C) 2007-2009 Albert Astals Cid <[email protected]> // Copyright (C) 2008 Jonathan Kew <[email protected]> // // To see a description of the changes please see the Changelog file that @@ -63,7 +63,7 @@ static int gMemInUse = 0; #endif /* DEBUG_MEM */ -void *gmalloc(size_t size) GMEM_EXCEP { +inline static void *gmalloc(size_t size, bool checkoverflow) GMEM_EXCEP { #ifdef DEBUG_MEM int size1; char *mem; @@ -80,7 +80,8 @@ void *gmalloc(size_t size) GMEM_EXCEP { throw GMemException(); #else fprintf(stderr, "Out of memory\n"); - exit(1); + if (checkoverflow) return NULL; + else exit(1); #endif } hdr = (GMemHdr *)mem; @@ -115,14 +116,23 @@ void *gmalloc(size_t size) GMEM_EXCEP { throw GMemException(); #else fprintf(stderr, "Out of memory\n"); - exit(1); + if (checkoverflow) return NULL; + else exit(1); #endif } return p; #endif } -void *grealloc(void *p, size_t size) GMEM_EXCEP { +void *gmalloc(size_t size) GMEM_EXCEP { + return gmalloc(size, false); +} + +void *gmalloc_checkoverflow(size_t size) GMEM_EXCEP { + return gmalloc(size, true); +} + +inline static void *grealloc(void *p, size_t size, bool checkoverflow) GMEM_EXCEP { #ifdef DEBUG_MEM GMemHdr *hdr; void *q; @@ -137,11 +147,11 @@ void *grealloc(void *p, size_t size) GMEM_EXCEP { if (p) { hdr = (GMemHdr *)((char *)p - gMemHdrSize); oldSize = hdr->size; - q = gmalloc(size); + q = gmalloc(size, checkoverflow); memcpy(q, p, size < oldSize ? size : oldSize); gfree(p); } else { - q = gmalloc(size); + q = gmalloc(size, checkoverflow); } return q; #else @@ -163,32 +173,23 @@ void *grealloc(void *p, size_t size) GMEM_EXCEP { throw GMemException(); #else fprintf(stderr, "Out of memory\n"); - exit(1); + if (checkoverflow) return NULL; + else exit(1); #endif } return q; #endif } -void *gmallocn(int nObjs, int objSize) GMEM_EXCEP { - int n; +void *grealloc(void *p, size_t size) GMEM_EXCEP { + return grealloc(p, size, false); +} - if (nObjs == 0) { - return NULL; - } - n = nObjs * objSize; - if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Bogus memory allocation size\n"); - exit(1); -#endif - } - return gmalloc(n); +void *grealloc_checkoverflow(void *p, size_t size) GMEM_EXCEP { + return grealloc(p, size, true); } -void *gmallocn_checkoverflow(int nObjs, int objSize) GMEM_EXCEP { +inline static void *gmallocn(int nObjs, int objSize, bool checkoverflow) GMEM_EXCEP { int n; if (nObjs == 0) { @@ -200,34 +201,22 @@ void *gmallocn_checkoverflow(int nObjs, int objSize) GMEM_EXCEP { throw GMemException(); #else fprintf(stderr, "Bogus memory allocation size\n"); - return NULL; + if (checkoverflow) return NULL; + else exit(1); #endif } - return gmalloc(n); + return gmalloc(n, checkoverflow); } -void *greallocn(void *p, int nObjs, int objSize) GMEM_EXCEP { - int n; +void *gmallocn(int nObjs, int objSize) GMEM_EXCEP { + return gmallocn(nObjs, objSize, false); +} - if (nObjs == 0) { - if (p) { - gfree(p); - } - return NULL; - } - n = nObjs * objSize; - if (objSize <= 0 || nObjs < 0 || nObjs >= INT_MAX / objSize) { -#if USE_EXCEPTIONS - throw GMemException(); -#else - fprintf(stderr, "Bogus memory allocation size\n"); - exit(1); -#endif - } - return grealloc(p, n); +void *gmallocn_checkoverflow(int nObjs, int objSize) GMEM_EXCEP { + return gmallocn(nObjs, objSize, true); } -void *greallocn_checkoverflow(void *p, int nObjs, int objSize) GMEM_EXCEP { +inline static void *greallocn(void *p, int nObjs, int objSize, bool checkoverflow) GMEM_EXCEP { int n; if (nObjs == 0) { @@ -242,10 +231,19 @@ void *greallocn_checkoverflow(void *p, int nObjs, int objSize) GMEM_EXCEP { throw GMemException(); #else fprintf(stderr, "Bogus memory allocation size\n"); - return NULL; + if (checkoverflow) return NULL; + else exit(1); #endif } - return grealloc(p, n); + return grealloc(p, n, checkoverflow); +} + +void *greallocn(void *p, int nObjs, int objSize) GMEM_EXCEP { + return greallocn(p, nObjs, objSize, false); +} + +void *greallocn_checkoverflow(void *p, int nObjs, int objSize) GMEM_EXCEP { + return greallocn(p, nObjs, objSize, true); } void gfree(void *p) { diff --git a/goo/gmem.h b/goo/gmem.h index 5258fa7..96d834d 100644 --- a/goo/gmem.h +++ b/goo/gmem.h @@ -14,7 +14,7 @@ // under GPL version 2 or later // // Copyright (C) 2005 Takashi Iwai <[email protected]> -// Copyright (C) 2007, 2008 Albert Astals Cid <[email protected]> +// Copyright (C) 2007-2009 Albert Astals Cid <[email protected]> // Copyright (C) 2008 Jonathan Kew <[email protected]> // // To see a description of the changes please see the Changelog file that @@ -53,12 +53,14 @@ extern "C" { * returns NULL. */ extern void *gmalloc(size_t size) GMEM_EXCEP; +extern void *gmalloc_checkoverflow(size_t size) GMEM_EXCEP; /* * Same as realloc, but prints error message and exits if realloc() * returns NULL. If <p> is NULL, calls malloc instead of realloc(). */ extern void *grealloc(void *p, size_t size) GMEM_EXCEP; +extern void *grealloc_checkoverflow(size_t size) GMEM_EXCEP; /* * These are similar to gmalloc and grealloc, but take an object count _______________________________________________ poppler mailing list [email protected] http://lists.freedesktop.org/mailman/listinfo/poppler
