blob: 92d275675cda28812b27e8b3eb12f7e5b993c8f8 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Guy Schalnat0f716451995-11-28 11:22:13 -06002/* pngmem.c - stub functions for memory allocation
Guy Schalnat0d580581995-07-20 02:43:20 -05003
Andreas Dilger47a0c421997-05-16 02:46:07 -05004 libpng 1.0 beta 6 - version 0.96
Guy Schalnat0d580581995-07-20 02:43:20 -05005 For conditions of distribution and use, see copyright notice in png.h
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06006 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
Andreas Dilger47a0c421997-05-16 02:46:07 -05007 Copyright (c) 1996, 1997 Andreas Dilger
8 May 12, 1997
Guy Schalnat0d580581995-07-20 02:43:20 -05009
Guy Schalnat51f0eb41995-09-26 05:22:39 -050010 This file provides a location for all memory allocation. Users which
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060011 need special memory handling are expected to modify the code in this file
12 to meet their needs. See the instructions at each function. */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
14#define PNG_INTERNAL
15#include "png.h"
16
Guy Schalnat4ee97b01996-01-16 01:51:56 -060017/* Borland DOS special memory handler */
18#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
19/* if you change this, be sure to change the one in png.h also */
20
Guy Schalnate5a37791996-06-05 15:50:50 -050021/* Allocate memory for a png_struct. The malloc and memset can be replaced
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060022 by a single call to calloc() if this is thought to improve performance. */
Guy Schalnate5a37791996-06-05 15:50:50 -050023png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060024png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -050025{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060026 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -050027 png_voidp struct_ptr;
28
29 if (type == PNG_STRUCT_INFO)
30 size = sizeof(png_info);
31 else if (type == PNG_STRUCT_PNG)
32 size = sizeof(png_struct);
33 else
34 return (png_voidp)NULL;
35
36 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
37 {
38 png_memset(struct_ptr, 0, size);
39 }
40
41 return (struct_ptr);
42}
43
44
45/* Free memory allocated by a png_create_struct() call */
46void
47png_destroy_struct(png_voidp struct_ptr)
48{
Andreas Dilger47a0c421997-05-16 02:46:07 -050049 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -050050 farfree (struct_ptr);
51}
52
Guy Schalnat0d580581995-07-20 02:43:20 -050053/* Allocate memory. For reasonable files, size should never exceed
Andreas Dilger47a0c421997-05-16 02:46:07 -050054 * 64K. However, zlib may allocate more then 64K if you don't tell
55 * it not to. See zconf.h and png.h for more information. zlib does
56 * need to allocate exactly 64K, so whatever you call here must
57 * have the ability to do that.
58 *
59 * Borland seems to have a problem in DOS mode for exactly 64K.
60 * It gives you a segment with an offset of 8 (perhaps to store it's
61 * memory stuff). zlib doesn't like this at all, so we have to
62 * detect and deal with it. This code should not be needed in
63 * Windows or OS/2 modes, and only in 16 bit mode. This code has
64 * been updated by Alexander Lehmann for version 0.89 to waste less
65 * memory.
66 *
67 * Note that we can't use png_size_t for the "size" declaration,
68 * since on some systems a png_size_t is a 16-bit quantity, and as a
69 * result, we would be truncating potentially larger memory requests
70 * (which should cause a fatal error) and introducing major problems.
71 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -060072png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060073png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat4ee97b01996-01-16 01:51:56 -060074{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060075 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -050076 if (png_ptr == NULL || size == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -050077 return ((voidp)NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -060078
79#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060080 if (size > (png_uint_32)65536L)
81 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -060082#endif
83
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060084 if (size == (png_uint_32)(65536L))
85 {
Andreas Dilger47a0c421997-05-16 02:46:07 -050086 if (png_ptr->offset_table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060087 {
88 /* try to see if we need to do any of this fancy stuff */
89 ret = farmalloc(size);
Andreas Dilger47a0c421997-05-16 02:46:07 -050090 if (ret == NULL || ((png_size_t)ret & 0xffff))
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060091 {
92 int num_blocks;
93 png_uint_32 total_size;
94 png_bytep table;
95 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -060096 png_byte huge * hptr;
97
Andreas Dilger47a0c421997-05-16 02:46:07 -050098 if (ret != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060099 farfree(ret);
Guy Schalnate5a37791996-06-05 15:50:50 -0500100 ret = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600101
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600102 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
103 if (num_blocks < 1)
104 num_blocks = 1;
105 if (png_ptr->zlib_mem_level >= 7)
106 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
107 else
108 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600109
Guy Schalnate5a37791996-06-05 15:50:50 -0500110 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600111
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600112 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600113
Andreas Dilger47a0c421997-05-16 02:46:07 -0500114 if (table == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600115 {
116 png_error(png_ptr, "Out of Memory");
117 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600118
Andreas Dilger47a0c421997-05-16 02:46:07 -0500119 if ((png_size_t)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600120 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500121 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600122 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600123
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600124 png_ptr->offset_table = table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500125 png_ptr->offset_table_ptr = farmalloc(num_blocks *
126 sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500127
Andreas Dilger47a0c421997-05-16 02:46:07 -0500128 if (png_ptr->offset_table_ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600129 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500130 png_error(png_ptr, "Out of memory");
131 }
132
133 hptr = (png_byte huge *)table;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500134 if ((png_size_t)hptr & 0xf)
Guy Schalnate5a37791996-06-05 15:50:50 -0500135 {
136 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
137 hptr += 16L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600138 }
139 for (i = 0; i < num_blocks; i++)
140 {
141 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
142 hptr += 65536L;
143 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600144
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600145 png_ptr->offset_table_number = num_blocks;
146 png_ptr->offset_table_count = 0;
147 png_ptr->offset_table_count_free = 0;
148 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600149 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500150
151 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
152 png_error(png_ptr, "Out of Memory");
153
154 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600155 }
156 else
157 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500158
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500159 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500160 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600161 png_error(png_ptr, "Out of Memory");
Guy Schalnat0d580581995-07-20 02:43:20 -0500162 }
163
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600164 return ret;
Guy Schalnat0d580581995-07-20 02:43:20 -0500165}
166
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600167/* free a pointer allocated by png_malloc(). In the default
168 configuration, png_ptr is not used, but is passed in case it
169 is needed. If ptr is NULL, return without taking any action. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500170void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600171png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500172{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500173 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600174 return;
175
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176 if (png_ptr->offset_table != NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600177 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500178 int i;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600179
Andreas Dilger47a0c421997-05-16 02:46:07 -0500180 for (i = 0; i < png_ptr->offset_table_count; i++)
181 {
182 if (ptr == png_ptr->offset_table_ptr[i])
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600183 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500184 ptr = NULL;
185 png_ptr->offset_table_count_free++;
186 break;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600187 }
188 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500189 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
190 {
191 farfree(png_ptr->offset_table);
192 farfree(png_ptr->offset_table_ptr);
193 png_ptr->offset_table = NULL;
194 png_ptr->offset_table_ptr = NULL;
195 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600196 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500197
198 if (ptr != NULL)
199 farfree(ptr);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600200}
201
202#else /* Not the Borland DOS special memory handler */
203
Guy Schalnate5a37791996-06-05 15:50:50 -0500204/* Allocate memory for a png_struct or a png_info. The malloc and
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600205 memset can be replaced by a single call to calloc() if this is thought
206 to improve performance noticably.*/
Guy Schalnate5a37791996-06-05 15:50:50 -0500207png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600208png_create_struct(int type)
Guy Schalnate5a37791996-06-05 15:50:50 -0500209{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500210 png_size_t size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500211 png_voidp struct_ptr;
212
213 if (type == PNG_STRUCT_INFO)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214 size = sizeof(png_info);
Guy Schalnate5a37791996-06-05 15:50:50 -0500215 else if (type == PNG_STRUCT_PNG)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500216 size = sizeof(png_struct);
Guy Schalnate5a37791996-06-05 15:50:50 -0500217 else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218 return (png_voidp)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500219
220#if defined(__TURBOC__) && !defined(__FLAT__)
221 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
222#else
223# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500224 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500225# else
226 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
227# endif
228#endif
229 {
230 png_memset(struct_ptr, 0, size);
231 }
232
233 return (struct_ptr);
234}
235
236
237/* Free memory allocated by a png_create_struct() call */
238void
239png_destroy_struct(png_voidp struct_ptr)
240{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500241 if (struct_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500242#if defined(__TURBOC__) && !defined(__FLAT__)
243 farfree(struct_ptr);
244#else
245# if defined(_MSC_VER) && defined(MAXSEG_64K)
246 hfree(struct_ptr);
247# else
248 free(struct_ptr);
249# endif
250#endif
251}
252
253
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600254/* Allocate memory. For reasonable files, size should never exceed
255 64K. However, zlib may allocate more then 64K if you don't tell
Andreas Dilger47a0c421997-05-16 02:46:07 -0500256 it not to. See zconf.h and png.h for more information. zlib does
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600257 need to allocate exactly 64K, so whatever you call here must
258 have the ability to do that. */
259
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600260png_voidp
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600261png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600262{
263 png_voidp ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500264 if (png_ptr == NULL || size == 0)
265 return (NULL);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600266
267#ifdef PNG_MAX_MALLOC_64K
268 if (size > (png_uint_32)65536L)
269 png_error(png_ptr, "Cannot Allocate > 64K");
270#endif
271
272#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500273 ret = farmalloc((png_size_t)size);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600274#else
275# if defined(_MSC_VER) && defined(MAXSEG_64K)
276 ret = halloc(size, 1);
277# else
278 ret = malloc(size);
279# endif
280#endif
281
282 if (ret == NULL)
283 {
284 png_error(png_ptr, "Out of Memory");
285 }
286
287 return ret;
288}
289
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290/* Free a pointer allocated by png_malloc(). In the default
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600291 configuration, png_ptr is not used, but is passed in case it
292 is needed. If ptr is NULL, return without taking any action. */
293void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600294png_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600295{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500296 if (png_ptr == NULL || ptr == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600297 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500298
Guy Schalnat6d764711995-12-19 03:22:19 -0600299#if defined(__TURBOC__) && !defined(__FLAT__)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500301#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600302# if defined(_MSC_VER) && defined(MAXSEG_64K)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500303 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600304# else
Andreas Dilger47a0c421997-05-16 02:46:07 -0500305 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600306# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500307#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500308}
309
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600310#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600311