source: trunk/src/3rdparty/libpng/pngmem.c@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 16.7 KB
Line 
1
2/* pngmem.c - stub functions for memory allocation
3 *
4 * Last changed in libpng 1.2.37 [June 4, 2009]
5 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
12 *
13 * This file provides a location for all memory allocation. Users who
14 * 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.
18 */
19
20#define PNG_INTERNAL
21#include "png.h"
22#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
23
24/* Borland DOS special memory handler */
25#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
26/* If you change this, be sure to change the one in png.h also */
27
28/* Allocate memory for a png_struct. The malloc and memset can be replaced
29 by a single call to calloc() if this is thought to improve performance. */
30png_voidp /* PRIVATE */
31png_create_struct(int type)
32{
33#ifdef PNG_USER_MEM_SUPPORTED
34 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
35}
36
37/* Alternate version of png_create_struct, for use with user-defined malloc. */
38png_voidp /* PRIVATE */
39png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
40{
41#endif /* PNG_USER_MEM_SUPPORTED */
42 png_size_t size;
43 png_voidp struct_ptr;
44
45 if (type == PNG_STRUCT_INFO)
46 size = png_sizeof(png_info);
47 else if (type == PNG_STRUCT_PNG)
48 size = png_sizeof(png_struct);
49 else
50 return (png_get_copyright(NULL));
51
52#ifdef PNG_USER_MEM_SUPPORTED
53 if (malloc_fn != NULL)
54 {
55 png_struct dummy_struct;
56 png_structp png_ptr = &dummy_struct;
57 png_ptr->mem_ptr=mem_ptr;
58 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
59 }
60 else
61#endif /* PNG_USER_MEM_SUPPORTED */
62 struct_ptr = (png_voidp)farmalloc(size);
63 if (struct_ptr != NULL)
64 png_memset(struct_ptr, 0, size);
65 return (struct_ptr);
66}
67
68/* Free memory allocated by a png_create_struct() call */
69void /* PRIVATE */
70png_destroy_struct(png_voidp struct_ptr)
71{
72#ifdef PNG_USER_MEM_SUPPORTED
73 png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
74}
75
76/* Free memory allocated by a png_create_struct() call */
77void /* PRIVATE */
78png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
79 png_voidp mem_ptr)
80{
81#endif
82 if (struct_ptr != NULL)
83 {
84#ifdef PNG_USER_MEM_SUPPORTED
85 if (free_fn != NULL)
86 {
87 png_struct dummy_struct;
88 png_structp png_ptr = &dummy_struct;
89 png_ptr->mem_ptr=mem_ptr;
90 (*(free_fn))(png_ptr, struct_ptr);
91 return;
92 }
93#endif /* PNG_USER_MEM_SUPPORTED */
94 farfree (struct_ptr);
95 }
96}
97
98/* Allocate memory. For reasonable files, size should never exceed
99 * 64K. However, zlib may allocate more then 64K if you don't tell
100 * it not to. See zconf.h and png.h for more information. zlib does
101 * need to allocate exactly 64K, so whatever you call here must
102 * have the ability to do that.
103 *
104 * Borland seems to have a problem in DOS mode for exactly 64K.
105 * It gives you a segment with an offset of 8 (perhaps to store its
106 * memory stuff). zlib doesn't like this at all, so we have to
107 * detect and deal with it. This code should not be needed in
108 * Windows or OS/2 modes, and only in 16 bit mode. This code has
109 * been updated by Alexander Lehmann for version 0.89 to waste less
110 * memory.
111 *
112 * Note that we can't use png_size_t for the "size" declaration,
113 * since on some systems a png_size_t is a 16-bit quantity, and as a
114 * result, we would be truncating potentially larger memory requests
115 * (which should cause a fatal error) and introducing major problems.
116 */
117
118png_voidp PNGAPI
119png_malloc(png_structp png_ptr, png_uint_32 size)
120{
121 png_voidp ret;
122
123 if (png_ptr == NULL || size == 0)
124 return (NULL);
125
126#ifdef PNG_USER_MEM_SUPPORTED
127 if (png_ptr->malloc_fn != NULL)
128 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
129 else
130 ret = (png_malloc_default(png_ptr, size));
131 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
132 png_error(png_ptr, "Out of memory!");
133 return (ret);
134}
135
136png_voidp PNGAPI
137png_malloc_default(png_structp png_ptr, png_uint_32 size)
138{
139 png_voidp ret;
140#endif /* PNG_USER_MEM_SUPPORTED */
141
142 if (png_ptr == NULL || size == 0)
143 return (NULL);
144
145#ifdef PNG_MAX_MALLOC_64K
146 if (size > (png_uint_32)65536L)
147 {
148 png_warning(png_ptr, "Cannot Allocate > 64K");
149 ret = NULL;
150 }
151 else
152#endif
153
154 if (size != (size_t)size)
155 ret = NULL;
156 else if (size == (png_uint_32)65536L)
157 {
158 if (png_ptr->offset_table == NULL)
159 {
160 /* Try to see if we need to do any of this fancy stuff */
161 ret = farmalloc(size);
162 if (ret == NULL || ((png_size_t)ret & 0xffff))
163 {
164 int num_blocks;
165 png_uint_32 total_size;
166 png_bytep table;
167 int i;
168 png_byte huge * hptr;
169
170 if (ret != NULL)
171 {
172 farfree(ret);
173 ret = NULL;
174 }
175
176 if (png_ptr->zlib_window_bits > 14)
177 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
178 else
179 num_blocks = 1;
180 if (png_ptr->zlib_mem_level >= 7)
181 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
182 else
183 num_blocks++;
184
185 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
186
187 table = farmalloc(total_size);
188
189 if (table == NULL)
190 {
191#ifndef PNG_USER_MEM_SUPPORTED
192 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
193 png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
194 else
195 png_warning(png_ptr, "Out Of Memory.");
196#endif
197 return (NULL);
198 }
199
200 if ((png_size_t)table & 0xfff0)
201 {
202#ifndef PNG_USER_MEM_SUPPORTED
203 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
204 png_error(png_ptr,
205 "Farmalloc didn't return normalized pointer");
206 else
207 png_warning(png_ptr,
208 "Farmalloc didn't return normalized pointer");
209#endif
210 return (NULL);
211 }
212
213 png_ptr->offset_table = table;
214 png_ptr->offset_table_ptr = farmalloc(num_blocks *
215 png_sizeof(png_bytep));
216
217 if (png_ptr->offset_table_ptr == NULL)
218 {
219#ifndef PNG_USER_MEM_SUPPORTED
220 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
221 png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
222 else
223 png_warning(png_ptr, "Out Of memory.");
224#endif
225 return (NULL);
226 }
227
228 hptr = (png_byte huge *)table;
229 if ((png_size_t)hptr & 0xf)
230 {
231 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
232 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
233 }
234 for (i = 0; i < num_blocks; i++)
235 {
236 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
237 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
238 }
239
240 png_ptr->offset_table_number = num_blocks;
241 png_ptr->offset_table_count = 0;
242 png_ptr->offset_table_count_free = 0;
243 }
244 }
245
246 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
247 {
248#ifndef PNG_USER_MEM_SUPPORTED
249 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
250 png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
251 else
252 png_warning(png_ptr, "Out of Memory.");
253#endif
254 return (NULL);
255 }
256
257 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
258 }
259 else
260 ret = farmalloc(size);
261
262#ifndef PNG_USER_MEM_SUPPORTED
263 if (ret == NULL)
264 {
265 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
266 png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
267 else
268 png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
269 }
270#endif
271
272 return (ret);
273}
274
275/* Free a pointer allocated by png_malloc(). In the default
276 * configuration, png_ptr is not used, but is passed in case it
277 * is needed. If ptr is NULL, return without taking any action.
278 */
279void PNGAPI
280png_free(png_structp png_ptr, png_voidp ptr)
281{
282 if (png_ptr == NULL || ptr == NULL)
283 return;
284
285#ifdef PNG_USER_MEM_SUPPORTED
286 if (png_ptr->free_fn != NULL)
287 {
288 (*(png_ptr->free_fn))(png_ptr, ptr);
289 return;
290 }
291 else
292 png_free_default(png_ptr, ptr);
293}
294
295void PNGAPI
296png_free_default(png_structp png_ptr, png_voidp ptr)
297{
298#endif /* PNG_USER_MEM_SUPPORTED */
299
300 if (png_ptr == NULL || ptr == NULL)
301 return;
302
303 if (png_ptr->offset_table != NULL)
304 {
305 int i;
306
307 for (i = 0; i < png_ptr->offset_table_count; i++)
308 {
309 if (ptr == png_ptr->offset_table_ptr[i])
310 {
311 ptr = NULL;
312 png_ptr->offset_table_count_free++;
313 break;
314 }
315 }
316 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
317 {
318 farfree(png_ptr->offset_table);
319 farfree(png_ptr->offset_table_ptr);
320 png_ptr->offset_table = NULL;
321 png_ptr->offset_table_ptr = NULL;
322 }
323 }
324
325 if (ptr != NULL)
326 {
327 farfree(ptr);
328 }
329}
330
331#else /* Not the Borland DOS special memory handler */
332
333/* Allocate memory for a png_struct or a png_info. The malloc and
334 memset can be replaced by a single call to calloc() if this is thought
335 to improve performance noticably. */
336png_voidp /* PRIVATE */
337png_create_struct(int type)
338{
339#ifdef PNG_USER_MEM_SUPPORTED
340 return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
341}
342
343/* Allocate memory for a png_struct or a png_info. The malloc and
344 memset can be replaced by a single call to calloc() if this is thought
345 to improve performance noticably. */
346png_voidp /* PRIVATE */
347png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
348{
349#endif /* PNG_USER_MEM_SUPPORTED */
350 png_size_t size;
351 png_voidp struct_ptr;
352
353 if (type == PNG_STRUCT_INFO)
354 size = png_sizeof(png_info);
355 else if (type == PNG_STRUCT_PNG)
356 size = png_sizeof(png_struct);
357 else
358 return (NULL);
359
360#ifdef PNG_USER_MEM_SUPPORTED
361 if (malloc_fn != NULL)
362 {
363 png_struct dummy_struct;
364 png_structp png_ptr = &dummy_struct;
365 png_ptr->mem_ptr=mem_ptr;
366 struct_ptr = (*(malloc_fn))(png_ptr, size);
367 if (struct_ptr != NULL)
368 png_memset(struct_ptr, 0, size);
369 return (struct_ptr);
370 }
371#endif /* PNG_USER_MEM_SUPPORTED */
372
373#if defined(__TURBOC__) && !defined(__FLAT__)
374 struct_ptr = (png_voidp)farmalloc(size);