blob: e3f317120b92226301af865d16e7cb5fe9b8062e [file] [log] [blame]
Guy Schalnat0f716451995-11-28 11:22:13 -06001/* pngmem.c - stub functions for memory allocation
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002 *
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06003 * libpng 0.99
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06004 * For conditions of distribution and use, see copyright notice in png.h
5 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
6 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06007 * Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06008 * January 30, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
10 * This file provides a location for all memory allocation. Users which
11 * need special memory handling are expected to modify the code in this file
12 * to meet their needs. See the instructions at each function.
13 */
Guy Schalnat0d580581995-07-20 02:43:20 -050014
15#define PNG_INTERNAL
16#include "png.h"
17
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060018/* The following "hides" PNG_MALLOC and PNG_FREE thus allowing the pngtest
19 application to put a wrapper on top of them. */
20#ifdef PNGTEST_MEMORY_DEBUG
21 #define PNG_MALLOC png_debug_malloc
22 #define PNG_FREE png_debug_free
23#else
24 #define PNG_MALLOC png_malloc
25 #define PNG_FREE png_free
26#endif
27
Guy Schalnat4ee97b01996-01-16 01:51:56 -060028/* Borland DOS special memory handler */
29#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
30/* if you change this, be sure to change the one in png.h also */
31
Guy Schalnate5a37791996-06-05 15:50:50 -050032/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060033 by a single call to calloc() if this is thought to improve performance. */
Guy Schalnate5a37791996-06-05 15:50:50 -050034png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060035png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050036{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060037 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050038 png_voidp struct_ptr;
39
40 if (type == PNG_STRUCT_INFO)
41 size = sizeof(png_info);
42 else if (type == PNG_STRUCT_PNG)
43 size = sizeof(png_struct);
44 else
45 return (png_voidp)NULL;
46
47 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
48 {
49 png_memset(struct_ptr, 0, size);
50 }
51
52 return (struct_ptr);
53}
54
55
56/* Free memory allocated by a png_create_struct() call */
57void
58png_destroy_struct(png_voidp struct_ptr)
59{
Andreas Dilger47a0c421997-05-16 02:46:07 -050060 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060061 {
Guy Schalnate5a37791996-06-05 15:50:50 -050062 farfree (struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -060063 struct_ptr = NULL;
64 }
Guy Schalnate5a37791996-06-05 15:50:50 -050065}
66
Guy Schalnat0d580581995-07-20 02:43:20 -050067/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050068 * 64K. However, zlib may allocate more then 64K if you don't tell
69 * it not to. See zconf.h and png.h for more information. zlib does
70 * need to allocate exactly 64K, so whatever you call here must
71 * have the ability to do that.
72 *
73 * Borland seems to have a problem in DOS mode for exactly 64K.
74 * It gives you a segment with an offset of 8 (perhaps to store it's
75 * memory stuff). zlib doesn't like this at all, so we have to
76 * detect and deal with it. This code should not be needed in
77 * Windows or OS/2 modes, and only in 16 bit mode. This code has
78 * been updated by Alexander Lehmann for version 0.89 to waste less
79 * memory.
80 *
81 * Note that we can't use png_size_t for the "size" declaration,
82 * since on some systems a png_size_t is a 16-bit quantity, and as a
83 * result, we would be truncating potentially larger memory requests
84 * (which should cause a fatal error) and introducing major problems.
85 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -060086png_voidp
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060087PNG_MALLOC(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -060088{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060089 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 if (png_ptr == NULL || size == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -050091 return ((voidp)NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -060092
93#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060094 if (size > (png_uint_32)65536L)
95 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -060096#endif
97
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060098 if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060099 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500100 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600101 {
102 /* try to see if we need to do any of this fancy stuff */
103 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500104 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600105 {
106 int num_blocks;
107 png_uint_32 total_size;
108 png_bytep table;
109 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600110 png_byte huge * hptr;
111
Andreas Dilger47a0c421997-05-16 02:46:07 -0500112 if (ret != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600113 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600114 farfree(ret);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600115 ret = NULL;
116 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600117
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600118 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
119 if (num_blocks < 1)
120 num_blocks = 1;
121 if (png_ptr->zlib_mem_level >= 7)
122 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
123 else
124 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600125
Guy Schalnate5a37791996-06-05 15:50:50 -0500126 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600127
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600128 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600129
Andreas Dilger47a0c421997-05-16 02:46:07 -0500130 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600131 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600132 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600133 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600134
Andreas Dilger47a0c421997-05-16 02:46:07 -0500135 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600136 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500137 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600138 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600139
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600140 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141 png_ptr->offset_table_ptr = farmalloc(num_blocks *
142 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500143
Andreas Dilger47a0c421997-05-16 02:46:07 -0500144 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600145 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600146 png_error(png_ptr, "Out Of memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500147 }
148
149 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500150 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500151 {
152 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
153 hptr += 16L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600154 }
155 for (i = 0; i < num_blocks; i++)
156 {
157 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600158 hptr += (png_uint_32)65536L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600159 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600160
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600161 png_ptr->offset_table_number = num_blocks;
162 png_ptr->offset_table_count = 0;
163 png_ptr->offset_table_count_free = 0;
164 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600165 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500166
167 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600168 png_error(png_ptr, "Out of Memory.");
Guy Schalnate5a37791996-06-05 15:50:50 -0500169
170 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600171 }
172 else
173 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500174
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500175 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500176 {
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600177 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
Guy Schalnat0d580581995-07-20 02:43:20 -0500178 }
179
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600180 return ret;
Guy Schalnat0d580581995-07-20 02:43:20 -0500181}
182
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600183/* free a pointer allocated by PNG_MALLOC(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600184 configuration, png_ptr is not used, but is passed in case it
185 is needed. If ptr is NULL, return without taking any action. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500186void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600187PNG_FREE(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500188{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600190 return;
191
Andreas Dilger47a0c421997-05-16 02:46:07 -0500192 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600193 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500194 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600195
Andreas Dilger47a0c421997-05-16 02:46:07 -0500196 for (i = 0; i < png_ptr->offset_table_count; i++)
197 {
198 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600199 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 ptr = NULL;
201 png_ptr->offset_table_count_free++;
202 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600203 }
204 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500205 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
206 {
207 farfree(png_ptr->offset_table);
208 farfree(png_ptr->offset_table_ptr);
209 png_ptr->offset_table = NULL;
210 png_ptr->offset_table_ptr = NULL;
211 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600212 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500213
214 if (ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600215 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600217 ptr = NULL;
218 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600219}
220
221#else /* Not the Borland DOS special memory handler */
222
Guy Schalnate5a37791996-06-05 15:50:50 -0500223/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600224 memset can be replaced by a single call to calloc() if this is thought
225 to improve performance noticably.*/
Guy Schalnate5a37791996-06-05 15:50:50 -0500226png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600227png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500228{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500230 png_voidp struct_ptr;
231
232 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500233 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500234 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500235 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500236 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500237 return (png_voidp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500238
239#if defined(__TURBOC__) && !defined(__FLAT__)
240 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
241#else
242# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500243 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500244# else
245 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
246# endif
247#endif
248 {
249 png_memset(struct_ptr, 0, size);
250 }
251
252 return (struct_ptr);
253}
254
255
256/* Free memory allocated by a png_create_struct() call */
257void
258png_destroy_struct(png_voidp struct_ptr)
259{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500260 if (struct_ptr != NULL)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600261 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500262#if defined(__TURBOC__) && !defined(__FLAT__)
263 farfree(struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600264 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500265#else
266# if defined(_MSC_VER) && defined(MAXSEG_64K)
267 hfree(struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600268 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500269# else
270 free(struct_ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600271 struct_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500272# endif
273#endif
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600274 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500275}
276
277
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600278/* Allocate memory. For reasonable files, size should never exceed
279 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500280 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600281 need to allocate exactly 64K, so whatever you call here must
282 have the ability to do that. */
283
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600284png_voidp
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600285PNG_MALLOC(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600286{
287 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500288 if (png_ptr == NULL || size == 0)
289 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600290
291#ifdef PNG_MAX_MALLOC_64K
292 if (size > (png_uint_32)65536L)
293 png_error(png_ptr, "Cannot Allocate > 64K");
294#endif
295
296#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500297 ret = farmalloc((png_size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600298#else
299# if defined(_MSC_VER) && defined(MAXSEG_64K)
300 ret = halloc(size, 1);
301# else
302 ret = malloc(size);
303# endif
304#endif
305
306 if (ret == NULL)
307 {
308 png_error(png_ptr, "Out of Memory");
309 }
310
311 return ret;
312}
313
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600314/* Free a pointer allocated by PNG_MALLOC(). In the default
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600315 configuration, png_ptr is not used, but is passed in case it
316 is needed. If ptr is NULL, return without taking any action. */
317void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600318PNG_FREE(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600319{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500320 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600321 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500322
Guy Schalnat6d764711995-12-19 03:22:19 -0600323#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324 farfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600325 ptr = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500326#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600327# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500328 hfree(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600329 ptr = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600330# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500331 free(ptr);
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600332 ptr = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600333# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500334#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500335}
336
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600337#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600338