blob: b472e54d8d3fffc81b8a37882d82c9880ab72873 [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-Pehrson5c6aeb21998-12-29 11:47:59 -06004 * libpng 1.0.2a - December 29, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06008 * Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -050010 * This file provides a location for all memory allocation. Users who
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -050011 * need special memory handling are expected to supply replacement
12 * functions for png_malloc() and png_free(), and to use
13 * png_create_read_struct_2() and png_create_write_struct_2() to
14 * identify the replacement functions.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060015 */
Guy Schalnat0d580581995-07-20 02:43:20 -050016
17#define PNG_INTERNAL
18#include "png.h"
19
Guy Schalnat4ee97b01996-01-16 01:51:56 -060020/* Borland DOS special memory handler */
21#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
22/* if you change this, be sure to change the one in png.h also */
23
Guy Schalnate5a37791996-06-05 15:50:50 -050024/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060025 by a single call to calloc() if this is thought to improve performance. */
Guy Schalnate5a37791996-06-05 15:50:50 -050026png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060027png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050028{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050029#ifdef PNG_USER_MEM_SUPPORTED
30 return (png_create_struct_2(type, NULL));
31}
32
33/* Alternate version of png_create_struct, for use with user-defined malloc. */
34png_voidp
35png_create_struct_2(int type, png_malloc_ptr malloc_fn)
36{
37#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060038 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050039 png_voidp struct_ptr;
40
41 if (type == PNG_STRUCT_INFO)
42 size = sizeof(png_info);
43 else if (type == PNG_STRUCT_PNG)
44 size = sizeof(png_struct);
45 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -060046 return ((png_voidp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -050047
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050048#ifdef PNG_USER_MEM_SUPPORTED
49 if(malloc_fn != NULL)
50 {
51 if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
52 png_memset(struct_ptr, 0, size);
53 return (struct_ptr);
54 }
55#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050056 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
57 {
58 png_memset(struct_ptr, 0, size);
59 }
Guy Schalnate5a37791996-06-05 15:50:50 -050060 return (struct_ptr);
61}
62
63
64/* Free memory allocated by a png_create_struct() call */
65void
66png_destroy_struct(png_voidp struct_ptr)
67{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050068#ifdef PNG_USER_MEM_SUPPORTED
69 png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
70}
71
72/* Free memory allocated by a png_create_struct() call */
73void
74png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
75{
76#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050077 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060078 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050079#ifdef PNG_USER_MEM_SUPPORTED
80 if(free_fn != NULL)
81 {
82 png_struct dummy_struct;
83 png_structp png_ptr = &dummy_struct;
84 (*(free_fn))(png_ptr, struct_ptr);
85 struct_ptr = NULL;
86 return;
87 }
88#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -050089 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060090 struct_ptr = NULL;
91 }
Guy Schalnate5a37791996-06-05 15:50:50 -050092}
93
Guy Schalnat0d580581995-07-20 02:43:20 -050094/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050095 * 64K. However, zlib may allocate more then 64K if you don't tell
96 * it not to. See zconf.h and png.h for more information. zlib does
97 * need to allocate exactly 64K, so whatever you call here must
98 * have the ability to do that.
99 *
100 * Borland seems to have a problem in DOS mode for exactly 64K.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500101 * It gives you a segment with an offset of 8 (perhaps to store its
Andreas Dilger47a0c421997-05-16 02:46:07 -0500102 * memory stuff). zlib doesn't like this at all, so we have to
103 * detect and deal with it. This code should not be needed in
104 * Windows or OS/2 modes, and only in 16 bit mode. This code has
105 * been updated by Alexander Lehmann for version 0.89 to waste less
106 * memory.
107 *
108 * Note that we can't use png_size_t for the "size" declaration,
109 * since on some systems a png_size_t is a 16-bit quantity, and as a
110 * result, we would be truncating potentially larger memory requests
111 * (which should cause a fatal error) and introducing major problems.
112 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600113png_voidp
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500114png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600115{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500116#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600117 png_voidp ret;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500118#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500119 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600120 return ((png_voidp)NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600121
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500122#ifdef PNG_USER_MEM_SUPPORTED
123 if(png_ptr->malloc_fn != NULL)
124 return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
125 else
126 return png_malloc_default(png_ptr, size);
127}
128
129png_voidp
130png_malloc_default(png_structp png_ptr, png_uint_32 size)
131{
132 png_voidp ret;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600133#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500134
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600135#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600136 if (size > (png_uint_32)65536L)
137 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600138#endif
139
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600140 if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500142 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600143 {
144 /* try to see if we need to do any of this fancy stuff */
145 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500146 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600147 {
148 int num_blocks;
149 png_uint_32 total_size;
150 png_bytep table;
151 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600152 png_byte huge * hptr;
153
Andreas Dilger47a0c421997-05-16 02:46:07 -0500154 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600155 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600156 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600157 ret = NULL;
158 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600159
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600160 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
161 if (num_blocks < 1)
162 num_blocks = 1;
163 if (png_ptr->zlib_mem_level >= 7)
164 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
165 else
166 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600167
Guy Schalnate5a37791996-06-05 15:50:50 -0500168 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600169
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600170 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600171
Andreas Dilger47a0c421997-05-16 02:46:07 -0500172 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600173 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600174 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600175 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600176
Andreas Dilger47a0c421997-05-16 02:46:07 -0500177 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600178 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500179 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600180 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600181
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600182 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500183 png_ptr->offset_table_ptr = farmalloc(num_blocks *
184 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500185
Andreas Dilger47a0c421997-05-16 02:46:07 -0500186 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600187 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600188 png_error(png_ptr, "Out Of memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500189 }
190
191 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500193 {
194 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
195 hptr += 16L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600196 }
197 for (i = 0; i < num_blocks; i++)
198 {
199 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600200 hptr += (png_uint_32)65536L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600201 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600202
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600203 png_ptr->offset_table_number = num_blocks;
204 png_ptr->offset_table_count = 0;
205 png_ptr->offset_table_count_free = 0;
206 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600207 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500208
209 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600210 png_error(png_ptr, "Out of Memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500211
212 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600213 }
214 else
215 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500216
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500217 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500218 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600219 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500220 }
221
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600222 return (ret);
Guy Schalnat0d580581995-07-20 02:43:20 -0500223}
224
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500225/* free a pointer allocated by png_malloc(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600226 configuration, png_ptr is not used, but is passed in case it
227 is needed. If ptr is NULL, return without taking any action. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500228void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500229png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500230{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500231 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600232 return;
233
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500234#ifdef PNG_USER_MEM_SUPPORTED
235 if (png_ptr->free_fn != NULL)
236 {
237 (*(png_ptr->free_fn))(png_ptr, ptr);
238 ptr = NULL;
239 return;
240 }
241 else png_free_default(png_ptr, ptr);
242}
243
244void
245png_free_default(png_structp png_ptr, png_voidp ptr)
246{
247#endif /* PNG_USER_MEM_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600248
Andreas Dilger47a0c421997-05-16 02:46:07 -0500249 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600250 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500251 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600252
Andreas Dilger47a0c421997-05-16 02:46:07 -0500253 for (i = 0; i < png_ptr->offset_table_count; i++)
254 {
255 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600256 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500257 ptr = NULL;
258 png_ptr->offset_table_count_free++;
259 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600260 }
261 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500262 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
263 {
264 farfree(png_ptr->offset_table);
265 farfree(png_ptr->offset_table_ptr);
266 png_ptr->offset_table = NULL;
267 png_ptr->offset_table_ptr = NULL;
268 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600269 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500270
271 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600272 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500273 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600274 ptr = NULL;
275 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600276}
277
278#else /* Not the Borland DOS special memory handler */
279
Guy Schalnate5a37791996-06-05 15:50:50 -0500280/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600281 memset can be replaced by a single call to calloc() if this is thought
282 to improve performance noticably.*/
Guy Schalnate5a37791996-06-05 15:50:50 -0500283png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600284png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500285{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500286#ifdef PNG_USER_MEM_SUPPORTED
287 return (png_create_struct_2(type, NULL));
288}
289
290/* Allocate memory for a png_struct or a png_info. The malloc and
291 memset can be replaced by a single call to calloc() if this is thought
292 to improve performance noticably.*/
293png_voidp
294png_create_struct_2(int type, png_malloc_ptr malloc_fn)
295{
296#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500298 png_voidp struct_ptr;
299
300 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500301 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500302 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500303 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500304 else
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600305 return ((png_voidp)NULL);
Guy Schalnate5a37791996-06-05 15:50:50 -0500306
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500307#ifdef PNG_USER_MEM_SUPPORTED
308 if(malloc_fn != NULL)
309 {
310 if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
311 png_memset(struct_ptr, 0, size);
312 return (struct_ptr);
313 }
314#endif /* PNG_USER_MEM_SUPPORTED */
315
Guy Schalnate5a37791996-06-05 15:50:50 -0500316#if defined(__TURBOC__) && !defined(__FLAT__)
317 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
318#else
319# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500320 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500321# else
322 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
323# endif
324#endif
325 {
326 png_memset(struct_ptr, 0, size);
327 }
328
329 return (struct_ptr);
330}
331
332
333/* Free memory allocated by a png_create_struct() call */
334void
335png_destroy_struct(png_voidp struct_ptr)
336{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500337#ifdef PNG_USER_MEM_SUPPORTED
338 png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
339}
340
341/* Free memory allocated by a png_create_struct() call */
342void
343png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
344{
345#endif /* PNG_USER_MEM_SUPPORTED */
Andreas Dilger47a0c421997-05-16 02:46:07 -0500346 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600347 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500348#ifdef PNG_USER_MEM_SUPPORTED
349 if(free_fn != NULL)
350 {
351 png_struct dummy_struct;
352 png_structp png_ptr = &dummy_struct;
353 (*(free_fn))(png_ptr, struct_ptr);
354 struct_ptr = NULL;
355 return;
356 }
357#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500358#if defined(__TURBOC__) && !defined(__FLAT__)
359 farfree(struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500360 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500361#else
362# if defined(_MSC_VER) && defined(MAXSEG_64K)
363 hfree(struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500364 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500365# else
366 free(struct_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500367 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500368# endif
369#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600370 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500371}
372
373
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600374/* Allocate memory. For reasonable files, size should never exceed
375 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500376 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600377 need to allocate exactly 64K, so whatever you call here must
378 have the ability to do that. */
379
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600380png_voidp
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500381png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600382{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500383#ifndef PNG_USER_MEM_SUPPORTED
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600384 png_voidp ret;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500385#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500386 if (png_ptr == NULL || size == 0)
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600387 return ((png_voidp)NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600388
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500389#ifdef PNG_USER_MEM_SUPPORTED
390 if(png_ptr->malloc_fn != NULL)
391 return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
392 else
393 return (png_malloc_default(png_ptr, size));
394}
395png_voidp
396png_malloc_default(png_structp png_ptr, png_uint_32 size)
397{
398 png_voidp ret;
399#endif /* PNG_USER_MEM_SUPPORTED */
400
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600401#ifdef PNG_MAX_MALLOC_64K
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600402 if (size > (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600403 png_error(png_ptr, "Cannot Allocate > 64K");
404#endif
405
406#if defined(__TURBOC__) && !defined(__FLAT__)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600407 ret = farmalloc(size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600408#else
409# if defined(_MSC_VER) && defined(MAXSEG_64K)
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600410 ret = halloc(size, 1);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600411# else
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -0600412 ret = malloc((size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600413# endif
414#endif
415
416 if (ret == NULL)
417 {
418 png_error(png_ptr, "Out of Memory");
419 }
420
Glenn Randers-Pehrsonb2120021998-01-31 20:07:59 -0600421 return (ret);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600422}
423
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500424/* Free a pointer allocated by png_malloc(). If ptr is NULL, return
425 without taking any action. */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600426void
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500427png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600428{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500429 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600430 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500431
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500432#ifdef PNG_USER_MEM_SUPPORTED
433 if (png_ptr->free_fn != NULL)
434 {
435 (*(png_ptr->free_fn))(png_ptr, ptr);
436 ptr = NULL;
437 return;
438 }
439 else png_free_default(png_ptr, ptr);
440}
441void
442png_free_default(png_structp png_ptr, png_voidp ptr)
443{
444#endif /* PNG_USER_MEM_SUPPORTED */
445
Guy Schalnat6d764711995-12-19 03:22:19 -0600446#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500447 farfree(ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500448 ptr = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500449#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600450# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451 hfree(ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500452 ptr = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600453# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500454 free(ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500455 ptr = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600456# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500457#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500458}
459
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600460#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600461
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600462png_voidp
463png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600464 png_uint_32 length)
465{
466 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600467
468 size = (png_size_t)length;
469 if ((png_uint_32)size != length)
470 png_error(png_ptr,"Overflow in png_memcpy_check.");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600471
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600472 return(png_memcpy (s1, s2, size));
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600473}
474
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600475png_voidp
476png_memset_check (png_structp png_ptr, png_voidp s1, int value,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600477 png_uint_32 length)
478{
479 png_size_t size;
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600480
481 size = (png_size_t)length;
482 if ((png_uint_32)size != length)
483 png_error(png_ptr,"Overflow in png_memset_check.");
484
485 return (png_memset (s1, value, size));
486
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600487}
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500488
489#ifdef PNG_USER_MEM_SUPPORTED
490/* This function is called when the application wants to use another method
491 * of allocating and freeing memory.
492 */
493void
494png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
495 malloc_fn, png_free_ptr free_fn)
496{
497 png_ptr->mem_ptr = mem_ptr;
498 png_ptr->malloc_fn = malloc_fn;
499 png_ptr->free_fn = free_fn;
500}
501
502/* This function returns a pointer to the mem_ptr associated with the user
503 * functions. The application should free any memory associated with this
504 * pointer before png_write_destroy and png_read_destroy are called.
505 */
506png_voidp
507png_get_mem_ptr(png_structp png_ptr)
508{
509 return ((png_voidp)png_ptr->mem_ptr);
510}
511#endif /* PNG_USER_MEM_SUPPORTED */