blob: a79c78b10557e787f9d0436a90d483696b35b3b0 [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-Pehrsonc4a2ae61998-01-16 22:06:18 -06003 * libpng 0.98
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-Pehrsonc4a2ae61998-01-16 22:06:18 -06008 * January 16, 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)
Guy Schalnate5a37791996-06-05 15:50:50 -050061 farfree (struct_ptr);
62}
63
Guy Schalnat0d580581995-07-20 02:43:20 -050064/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050065 * 64K. However, zlib may allocate more then 64K if you don't tell
66 * it not to. See zconf.h and png.h for more information. zlib does
67 * need to allocate exactly 64K, so whatever you call here must
68 * have the ability to do that.
69 *
70 * Borland seems to have a problem in DOS mode for exactly 64K.
71 * It gives you a segment with an offset of 8 (perhaps to store it's
72 * memory stuff). zlib doesn't like this at all, so we have to
73 * detect and deal with it. This code should not be needed in
74 * Windows or OS/2 modes, and only in 16 bit mode. This code has
75 * been updated by Alexander Lehmann for version 0.89 to waste less
76 * memory.
77 *
78 * Note that we can't use png_size_t for the "size" declaration,
79 * since on some systems a png_size_t is a 16-bit quantity, and as a
80 * result, we would be truncating potentially larger memory requests
81 * (which should cause a fatal error) and introducing major problems.
82 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -060083png_voidp
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -060084PNG_MALLOC(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -060085{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060086 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -050087 if (png_ptr == NULL || size == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -050088 return ((voidp)NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -060089
90#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060091 if (size > (png_uint_32)65536L)
92 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -060093#endif
94
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060095 if (size == (png_uint_32)65536L)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060096 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050097 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060098 {
99 /* try to see if we need to do any of this fancy stuff */
100 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500101 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600102 {
103 int num_blocks;
104 png_uint_32 total_size;
105 png_bytep table;
106 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600107 png_byte huge * hptr;
108
Andreas Dilger47a0c421997-05-16 02:46:07 -0500109 if (ret != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600110 farfree(ret);
Guy Schalnate5a37791996-06-05 15:50:50 -0500111 ret = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600112
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600113 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
114 if (num_blocks < 1)
115 num_blocks = 1;
116 if (png_ptr->zlib_mem_level >= 7)
117 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
118 else
119 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600120
Guy Schalnate5a37791996-06-05 15:50:50 -0500121 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600122
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600123 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600124
Andreas Dilger47a0c421997-05-16 02:46:07 -0500125 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600126 {
127 png_error(png_ptr, "Out of Memory");
128 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600129
Andreas Dilger47a0c421997-05-16 02:46:07 -0500130 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600131 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500132 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600133 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600134
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600135 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500136 png_ptr->offset_table_ptr = farmalloc(num_blocks *
137 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500138
Andreas Dilger47a0c421997-05-16 02:46:07 -0500139 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600140 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500141 png_error(png_ptr, "Out of memory");
142 }
143
144 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500145 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500146 {
147 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
148 hptr += 16L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600149 }
150 for (i = 0; i < num_blocks; i++)
151 {
152 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600153 hptr += (png_uint_32)65536L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600154 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600155
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600156 png_ptr->offset_table_number = num_blocks;
157 png_ptr->offset_table_count = 0;
158 png_ptr->offset_table_count_free = 0;
159 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600160 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500161
162 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
163 png_error(png_ptr, "Out of Memory");
164
165 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600166 }
167 else
168 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500169
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500170 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500171 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600172 png_error(png_ptr, "Out of Memory");
Guy Schalnat0d580581995-07-20 02:43:20 -0500173 }
174
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600175 return ret;
Guy Schalnat0d580581995-07-20 02:43:20 -0500176}
177
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600178/* free a pointer allocated by PNG_MALLOC(). In the default
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600179 configuration, png_ptr is not used, but is passed in case it
180 is needed. If ptr is NULL, return without taking any action. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500181void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600182PNG_FREE(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500183{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600185 return;
186
Andreas Dilger47a0c421997-05-16 02:46:07 -0500187 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600188 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600190
Andreas Dilger47a0c421997-05-16 02:46:07 -0500191 for (i = 0; i < png_ptr->offset_table_count; i++)
192 {
193 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600194 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500195 ptr = NULL;
196 png_ptr->offset_table_count_free++;
197 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600198 }
199 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500200 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
201 {
202 farfree(png_ptr->offset_table);
203 farfree(png_ptr->offset_table_ptr);
204 png_ptr->offset_table = NULL;
205 png_ptr->offset_table_ptr = NULL;
206 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600207 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500208
209 if (ptr != NULL)
210 farfree(ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600211}
212
213#else /* Not the Borland DOS special memory handler */
214
Guy Schalnate5a37791996-06-05 15:50:50 -0500215/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600216 memset can be replaced by a single call to calloc() if this is thought
217 to improve performance noticably.*/
Guy Schalnate5a37791996-06-05 15:50:50 -0500218png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600219png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500220{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500221 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500222 png_voidp struct_ptr;
223
224 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500225 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500226 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500228 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500229 return (png_voidp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500230
231#if defined(__TURBOC__) && !defined(__FLAT__)
232 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
233#else
234# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500235 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500236# else
237 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
238# endif
239#endif
240 {
241 png_memset(struct_ptr, 0, size);
242 }
243
244 return (struct_ptr);
245}
246
247
248/* Free memory allocated by a png_create_struct() call */
249void
250png_destroy_struct(png_voidp struct_ptr)
251{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500252 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500253#if defined(__TURBOC__) && !defined(__FLAT__)
254 farfree(struct_ptr);
255#else
256# if defined(_MSC_VER) && defined(MAXSEG_64K)
257 hfree(struct_ptr);
258# else
259 free(struct_ptr);
260# endif
261#endif
262}
263
264
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600265/* Allocate memory. For reasonable files, size should never exceed
266 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500267 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600268 need to allocate exactly 64K, so whatever you call here must
269 have the ability to do that. */
270
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600271png_voidp
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600272PNG_MALLOC(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600273{
274 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275 if (png_ptr == NULL || size == 0)
276 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600277
278#ifdef PNG_MAX_MALLOC_64K
279 if (size > (png_uint_32)65536L)
280 png_error(png_ptr, "Cannot Allocate > 64K");
281#endif
282
283#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500284 ret = farmalloc((png_size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600285#else
286# if defined(_MSC_VER) && defined(MAXSEG_64K)
287 ret = halloc(size, 1);
288# else
289 ret = malloc(size);
290# endif
291#endif
292
293 if (ret == NULL)
294 {
295 png_error(png_ptr, "Out of Memory");
296 }
297
298 return ret;
299}
300
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600301/* Free a pointer allocated by PNG_MALLOC(). In the default
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600302 configuration, png_ptr is not used, but is passed in case it
303 is needed. If ptr is NULL, return without taking any action. */
304void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600305PNG_FREE(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600306{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500307 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600308 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500309
Guy Schalnat6d764711995-12-19 03:22:19 -0600310#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500311 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500312#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600313# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500314 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600315# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500316 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600317# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500318#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500319}
320
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600321#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600322