blob: 4c147643e5a515ba3a6d5cdb546b2d69e03057a8 [file] [log] [blame]
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001
Guy Schalnat0f716451995-11-28 11:22:13 -06002/* pngmem.c - stub functions for memory allocation
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson13831bc2011-12-21 08:28:28 -06004 * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050013 * This file provides a location for all memory allocation. Users who
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050014 * need special memory handling are expected to supply replacement
15 * functions for png_malloc() and png_free(), and to use
16 * png_create_read_struct_2() and png_create_write_struct_2() to
17 * identify the replacement functions.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060018 */
Guy Schalnat0d580581995-07-20 02:43:20 -050019
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050020#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060021
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060022#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
Guy Schalnate5a37791996-06-05 15:50:50 -050023/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060024 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -050025 to improve performance noticably. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050026PNG_FUNCTION(png_voidp /* PRIVATE */,
27png_create_struct,(int type),PNG_ALLOCATED)
Guy Schalnate5a37791996-06-05 15:50:50 -050028{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060029# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050030 return (png_create_struct_2(type, NULL, NULL));
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050031}
32
33/* Allocate memory for a png_struct or a png_info. The malloc and
34 memset can be replaced by a single call to calloc() if this is thought
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -050035 to improve performance noticably. */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -050036PNG_FUNCTION(png_voidp /* PRIVATE */,
37png_create_struct_2,(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr),
38 PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050039{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060040# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -050041 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050042 png_voidp struct_ptr;
43
44 if (type == PNG_STRUCT_INFO)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050045 size = png_sizeof(png_info);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050046
Guy Schalnate5a37791996-06-05 15:50:50 -050047 else if (type == PNG_STRUCT_PNG)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050048 size = png_sizeof(png_struct);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050049
Guy Schalnate5a37791996-06-05 15:50:50 -050050 else
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -050051 return (NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -050052
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060053# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050054 if (malloc_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050055 {
Glenn Randers-Pehrson5cded0b2001-11-07 07:10:08 -060056 png_struct dummy_struct;
57 png_structp png_ptr = &dummy_struct;
58 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonae498dc2001-11-24 14:53:31 -060059 struct_ptr = (*(malloc_fn))(png_ptr, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050060
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050061 if (struct_ptr != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050062 png_memset(struct_ptr, 0, size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050063
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050064 return (struct_ptr);
65 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060066# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050067
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060068# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050069 struct_ptr = (png_voidp)farmalloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060070# else
71# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050072 struct_ptr = (png_voidp)halloc(size, 1);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060073# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050074 struct_ptr = (png_voidp)malloc(size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060075# endif
76# endif
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -050077
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -050078 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050079 png_memset(struct_ptr, 0, size);
Guy Schalnate5a37791996-06-05 15:50:50 -050080
81 return (struct_ptr);
82}
83
84
85/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050086void /* PRIVATE */
Guy Schalnate5a37791996-06-05 15:50:50 -050087png_destroy_struct(png_voidp struct_ptr)
88{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060089# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -050090 png_destroy_struct_2(struct_ptr, NULL, NULL);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050091}
92
93/* Free memory allocated by a png_create_struct() call */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050094void /* PRIVATE */
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -050095png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
96 png_voidp mem_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050097{
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -060098# endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -050099 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600100 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600101# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500102 if (free_fn != NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500103 {
104 png_struct dummy_struct;
105 png_structp png_ptr = &dummy_struct;
Glenn Randers-Pehrson8b6a8892001-05-18 04:54:50 -0500106 png_ptr->mem_ptr=mem_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500107 (*(free_fn))(png_ptr, struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500108 return;
109 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600110# endif /* PNG_USER_MEM_SUPPORTED */
111# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500112 farfree(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500113
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600114# else
115# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500116 hfree(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500117
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600118# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500119 free(struct_ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500120
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600121# endif
122# endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600123 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500124}
125
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600126/* Allocate memory. For reasonable files, size should never exceed
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500127 * 64K. However, zlib may allocate more then 64K if you don't tell
128 * it not to. See zconf.h and png.h for more information. zlib does
129 * need to allocate exactly 64K, so whatever you call here must
130 * have the ability to do that.
131 */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600132
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500133PNG_FUNCTION(png_voidp,PNGAPI
134png_calloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600135{
136 png_voidp ret;
137
138 ret = (png_malloc(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500139
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600140 if (ret != NULL)
141 png_memset(ret,0,(png_size_t)size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500142
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600143 return (ret);
144}
145
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500146PNG_FUNCTION(png_voidp,PNGAPI
147png_malloc,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600148{
149 png_voidp ret;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500150
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600151# ifdef PNG_USER_MEM_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -0500152 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrson3f549252001-10-27 07:35:13 -0500153 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600154
Glenn Randers-Pehrsonbeb572e2006-08-19 13:59:24 -0500155 if (png_ptr->malloc_fn != NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500156 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500157
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500158 else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500159 ret = (png_malloc_default(png_ptr, size));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500160
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500161 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500162 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500163
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500164 return (ret);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500165}
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500166
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500167PNG_FUNCTION(png_voidp,PNGAPI
168png_malloc_default,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500169{
170 png_voidp ret;
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600171# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500172
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -0500173 if (png_ptr == NULL || size == 0)
174 return (NULL);
175
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600176# ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500177 if (size > (png_uint_32)65536L)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600178 {
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600179# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500180 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600181 png_error(png_ptr, "Cannot Allocate > 64K");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500182
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600183 else
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600184# endif
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600185 return NULL;
186 }
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600187# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600188
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500189 /* Check for overflow */
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600190# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500191
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500192 if (size != (unsigned long)size)
193 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500194
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500195 else
196 ret = farmalloc(size);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500197
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600198# else
199# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500200 if (size != (unsigned long)size)
201 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500202
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500203 else
204 ret = halloc(size, 1);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500205
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600206# else
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500207 if (size != (size_t)size)
208 ret = NULL;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500209
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500210 else
211 ret = malloc((size_t)size);
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600212# endif
213# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600214
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600215# ifndef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrson9c0f0942002-02-21 23:14:23 -0600216 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600217 png_error(png_ptr, "Out of Memory");
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600218# endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600220 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600221}
222
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500223/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500224 * without taking any action.
225 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500226void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500227png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600228{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600230 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500231
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600232# ifdef PNG_USER_MEM_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500233 if (png_ptr->free_fn != NULL)
234 {
235 (*(png_ptr->free_fn))(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500236 return;
237 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500238
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500239 else
240 png_free_default(png_ptr, ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500241}
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500242
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -0600243void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500244png_free_default(png_structp png_ptr, png_voidp ptr)
245{
Glenn Randers-Pehrson6d8f3b01999-10-23 08:39:18 -0500246 if (png_ptr == NULL || ptr == NULL)
247 return;
248
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600249# endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500250
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600251# if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500252 farfree(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500253
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600254# else
255# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500256 hfree(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500257
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600258# else
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500259 free(ptr);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500260
Glenn Randers-Pehrson6f6a91a2010-03-06 13:54:59 -0600261# endif
262# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500263}
Guy Schalnat6d764711995-12-19 03:22:19 -0600264
Glenn Randers-Pehrson2ae022d2002-07-01 22:23:46 -0500265/* This function was added at libpng version 1.2.3. The png_malloc_warn()
Glenn Randers-Pehrson5fea36f2004-07-28 08:20:44 -0500266 * function will set up png_malloc() to issue a png_warning and return NULL
267 * instead of issuing a png_error, if it fails to allocate the requested
268 * memory.
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500269 */
Glenn Randers-Pehrson77396b62010-08-02 08:00:10 -0500270PNG_FUNCTION(png_voidp,PNGAPI
271png_malloc_warn,(png_structp png_ptr, png_alloc_size_t size),PNG_ALLOCATED)
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500272{
273 png_voidp ptr;
Glenn Randers-Pehrsonae4bd5c2006-11-16 20:35:49 -0600274 png_uint_32 save_flags;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500275 if (png_ptr == NULL)
276 return (NULL);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500277
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500278 save_flags = png_ptr->flags;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500279 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
280 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
281 png_ptr->flags=save_flags;
282 return(ptr);
283}
284
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500285
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500286#ifdef PNG_USER_MEM_SUPPORTED
287/* This function is called when the application wants to use another method
288 * of allocating and freeing memory.
289 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500290void PNGAPI
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500291png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
292 malloc_fn, png_free_ptr free_fn)
293{
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500294 if (png_ptr != NULL)
295 {
296 png_ptr->mem_ptr = mem_ptr;
297 png_ptr->malloc_fn = malloc_fn;
298 png_ptr->free_fn = free_fn;
Glenn Randers-Pehrson6b12c082006-11-14 10:53:30 -0600299 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500300}
301
302/* This function returns a pointer to the mem_ptr associated with the user
303 * functions. The application should free any memory associated with this
304 * pointer before png_write_destroy and png_read_destroy are called.
305 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500306png_voidp PNGAPI
John Bowler0a5c9c02011-01-22 17:36:34 -0600307png_get_mem_ptr(png_const_structp png_ptr)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500308{
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500309 if (png_ptr == NULL)
310 return (NULL);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500311
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500312 return ((png_voidp)png_ptr->mem_ptr);
313}
314#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -0600315#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */