X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1f0299c17dc878540bf190c290392db241b425ad..51dc95a4c8ccb00741be48f6353749ada3e9f39a:/src/png/pngmem.c diff --git a/src/png/pngmem.c b/src/png/pngmem.c index b92fcc8ae0..06c4c1b7ea 100644 --- a/src/png/pngmem.c +++ b/src/png/pngmem.c @@ -1,30 +1,21 @@ /* pngmem.c - stub functions for memory allocation * - * libpng 1.0.1 + * libpng 1.0.3 - January 14, 1999 * For conditions of distribution and use, see copyright notice in png.h * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. * Copyright (c) 1996, 1997 Andreas Dilger - * Copyright (c) 1998, Glenn Randers-Pehrson - * March 15, 1998 + * Copyright (c) 1998, 1999 Glenn Randers-Pehrson * - * This file provides a location for all memory allocation. Users which - * need special memory handling are expected to modify the code in this file - * to meet their needs. See the instructions at each function. + * This file provides a location for all memory allocation. Users who + * need special memory handling are expected to supply replacement + * functions for png_malloc() and png_free(), and to use + * png_create_read_struct_2() and png_create_write_struct_2() to + * identify the replacement functions. */ #define PNG_INTERNAL -#include "../png/png.h" - -/* The following "hides" PNG_MALLOC and PNG_FREE thus allowing the pngtest - application to put a wrapper on top of them. */ -#ifdef PNGTEST_MEMORY_DEBUG -#define PNG_MALLOC png_debug_malloc -#define PNG_FREE png_debug_free -#else -#define PNG_MALLOC png_malloc -#define PNG_FREE png_free -#endif +#include "png.h" /* Borland DOS special memory handler */ #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) @@ -35,6 +26,15 @@ png_voidp png_create_struct(int type) { +#ifdef PNG_USER_MEM_SUPPORTED + return (png_create_struct_2(type, NULL)); +} + +/* Alternate version of png_create_struct, for use with user-defined malloc. */ +png_voidp +png_create_struct_2(int type, png_malloc_ptr malloc_fn) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ png_size_t size; png_voidp struct_ptr; @@ -45,11 +45,18 @@ png_create_struct(int type) else return ((png_voidp)NULL); +#ifdef PNG_USER_MEM_SUPPORTED + if(malloc_fn != NULL) + { + if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL) + png_memset(struct_ptr, 0, size); + return (struct_ptr); + } +#endif /* PNG_USER_MEM_SUPPORTED */ if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL) { png_memset(struct_ptr, 0, size); } - return (struct_ptr); } @@ -58,8 +65,27 @@ png_create_struct(int type) void png_destroy_struct(png_voidp struct_ptr) { +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL); +} + +/* Free memory allocated by a png_create_struct() call */ +void +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn) +{ +#endif if (struct_ptr != NULL) { +#ifdef PNG_USER_MEM_SUPPORTED + if(free_fn != NULL) + { + png_struct dummy_struct; + png_structp png_ptr = &dummy_struct; + (*(free_fn))(png_ptr, struct_ptr); + struct_ptr = NULL; + return; + } +#endif /* PNG_USER_MEM_SUPPORTED */ farfree (struct_ptr); struct_ptr = NULL; } @@ -72,7 +98,7 @@ png_destroy_struct(png_voidp struct_ptr) * have the ability to do that. * * Borland seems to have a problem in DOS mode for exactly 64K. - * It gives you a segment with an offset of 8 (perhaps to store it's + * It gives you a segment with an offset of 8 (perhaps to store its * memory stuff). zlib doesn't like this at all, so we have to * detect and deal with it. This code should not be needed in * Windows or OS/2 modes, and only in 16 bit mode. This code has @@ -85,12 +111,27 @@ png_destroy_struct(png_voidp struct_ptr) * (which should cause a fatal error) and introducing major problems. */ png_voidp -PNG_MALLOC(png_structp png_ptr, png_uint_32 size) +png_malloc(png_structp png_ptr, png_uint_32 size) { +#ifndef PNG_USER_MEM_SUPPORTED png_voidp ret; +#endif if (png_ptr == NULL || size == 0) return ((png_voidp)NULL); +#ifdef PNG_USER_MEM_SUPPORTED + if(png_ptr->malloc_fn != NULL) + return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size)); + else + return png_malloc_default(png_ptr, size); +} + +png_voidp +png_malloc_default(png_structp png_ptr, png_uint_32 size) +{ + png_voidp ret; +#endif /* PNG_USER_MEM_SUPPORTED */ + #ifdef PNG_MAX_MALLOC_64K if (size > (png_uint_32)65536L) png_error(png_ptr, "Cannot Allocate > 64K"); @@ -181,15 +222,30 @@ PNG_MALLOC(png_structp png_ptr, png_uint_32 size) return (ret); } -/* free a pointer allocated by PNG_MALLOC(). In the default +/* free a pointer allocated by png_malloc(). In the default configuration, png_ptr is not used, but is passed in case it is needed. If ptr is NULL, return without taking any action. */ void -PNG_FREE(png_structp png_ptr, png_voidp ptr) +png_free(png_structp png_ptr, png_voidp ptr) { if (png_ptr == NULL || ptr == NULL) return; +#ifdef PNG_USER_MEM_SUPPORTED + if (png_ptr->free_fn != NULL) + { + (*(png_ptr->free_fn))(png_ptr, ptr); + ptr = NULL; + return; + } + else png_free_default(png_ptr, ptr); +} + +void +png_free_default(png_structp png_ptr, png_voidp ptr) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ + if (png_ptr->offset_table != NULL) { int i; @@ -227,6 +283,17 @@ PNG_FREE(png_structp png_ptr, png_voidp ptr) png_voidp png_create_struct(int type) { +#ifdef PNG_USER_MEM_SUPPORTED + return (png_create_struct_2(type, NULL)); +} + +/* Allocate memory for a png_struct or a png_info. The malloc and + memset can be replaced by a single call to calloc() if this is thought + to improve performance noticably.*/ +png_voidp +png_create_struct_2(int type, png_malloc_ptr malloc_fn) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ png_size_t size; png_voidp struct_ptr; @@ -237,6 +304,15 @@ png_create_struct(int type) else return ((png_voidp)NULL); +#ifdef PNG_USER_MEM_SUPPORTED + if(malloc_fn != NULL) + { + if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL) + png_memset(struct_ptr, 0, size); + return (struct_ptr); + } +#endif /* PNG_USER_MEM_SUPPORTED */ + #if defined(__TURBOC__) && !defined(__FLAT__) if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL) #else @@ -258,15 +334,37 @@ png_create_struct(int type) void png_destroy_struct(png_voidp struct_ptr) { +#ifdef PNG_USER_MEM_SUPPORTED + png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL); +} + +/* Free memory allocated by a png_create_struct() call */ +void +png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ if (struct_ptr != NULL) { +#ifdef PNG_USER_MEM_SUPPORTED + if(free_fn != NULL) + { + png_struct dummy_struct; + png_structp png_ptr = &dummy_struct; + (*(free_fn))(png_ptr, struct_ptr); + struct_ptr = NULL; + return; + } +#endif /* PNG_USER_MEM_SUPPORTED */ #if defined(__TURBOC__) && !defined(__FLAT__) farfree(struct_ptr); + struct_ptr = NULL; #else # if defined(_MSC_VER) && defined(MAXSEG_64K) hfree(struct_ptr); + struct_ptr = NULL; # else free(struct_ptr); + struct_ptr = NULL; # endif #endif } @@ -280,13 +378,26 @@ png_destroy_struct(png_voidp struct_ptr) have the ability to do that. */ png_voidp -PNG_MALLOC(png_structp png_ptr, png_uint_32 size) +png_malloc(png_structp png_ptr, png_uint_32 size) { +#ifndef PNG_USER_MEM_SUPPORTED png_voidp ret; - +#endif if (png_ptr == NULL || size == 0) return ((png_voidp)NULL); +#ifdef PNG_USER_MEM_SUPPORTED + if(png_ptr->malloc_fn != NULL) + return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size)); + else + return (png_malloc_default(png_ptr, size)); +} +png_voidp +png_malloc_default(png_structp png_ptr, png_uint_32 size) +{ + png_voidp ret; +#endif /* PNG_USER_MEM_SUPPORTED */ + #ifdef PNG_MAX_MALLOC_64K if (size > (png_uint_32)65536L) png_error(png_ptr, "Cannot Allocate > 64K"); @@ -310,22 +421,38 @@ PNG_MALLOC(png_structp png_ptr, png_uint_32 size) return (ret); } -/* Free a pointer allocated by PNG_MALLOC(). In the default - configuration, png_ptr is not used, but is passed in case it - is needed. If ptr is NULL, return without taking any action. */ +/* Free a pointer allocated by png_malloc(). If ptr is NULL, return + without taking any action. */ void -PNG_FREE(png_structp png_ptr, png_voidp ptr) +png_free(png_structp png_ptr, png_voidp ptr) { if (png_ptr == NULL || ptr == NULL) return; +#ifdef PNG_USER_MEM_SUPPORTED + if (png_ptr->free_fn != NULL) + { + (*(png_ptr->free_fn))(png_ptr, ptr); + ptr = NULL; + return; + } + else png_free_default(png_ptr, ptr); +} +void +png_free_default(png_structp png_ptr, png_voidp ptr) +{ +#endif /* PNG_USER_MEM_SUPPORTED */ + #if defined(__TURBOC__) && !defined(__FLAT__) farfree(ptr); + ptr = NULL; #else # if defined(_MSC_VER) && defined(MAXSEG_64K) hfree(ptr); + ptr = NULL; # else free(ptr); + ptr = NULL; # endif #endif } @@ -341,7 +468,7 @@ png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2, size = (png_size_t)length; if ((png_uint_32)size != length) png_error(png_ptr,"Overflow in png_memcpy_check."); - + return(png_memcpy (s1, s2, size)); } @@ -358,3 +485,27 @@ png_memset_check (png_structp png_ptr, png_voidp s1, int value, return (png_memset (s1, value, size)); } + +#ifdef PNG_USER_MEM_SUPPORTED +/* This function is called when the application wants to use another method + * of allocating and freeing memory. + */ +void +png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr + malloc_fn, png_free_ptr free_fn) +{ + png_ptr->mem_ptr = mem_ptr; + png_ptr->malloc_fn = malloc_fn; + png_ptr->free_fn = free_fn; +} + +/* This function returns a pointer to the mem_ptr associated with the user + * functions. The application should free any memory associated with this + * pointer before png_write_destroy and png_read_destroy are called. + */ +png_voidp +png_get_mem_ptr(png_structp png_ptr) +{ + return ((png_voidp)png_ptr->mem_ptr); +} +#endif /* PNG_USER_MEM_SUPPORTED */