2 /* pngmem.c - stub functions for memory allocation
4 * Last changed in libpng 1.5.4 [July 7, 2011]
5 * Copyright (c) 1998-2011 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.)
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
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.
22 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
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 */
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. */
30 PNG_FUNCTION(png_voidp
/* PRIVATE */,
31 png_create_struct
,(int type
),PNG_ALLOCATED
)
33 # ifdef PNG_USER_MEM_SUPPORTED
34 return (png_create_struct_2(type
, NULL
, NULL
));
37 /* Alternate version of png_create_struct, for use with user-defined malloc. */
38 PNG_FUNCTION(png_voidp
/* PRIVATE */,
39 png_create_struct_2
,(int type
, png_malloc_ptr malloc_fn
, png_voidp mem_ptr
),
42 # endif /* PNG_USER_MEM_SUPPORTED */
46 if (type
== PNG_STRUCT_INFO
)
47 size
= png_sizeof(png_info
);
49 else if (type
== PNG_STRUCT_PNG
)
50 size
= png_sizeof(png_struct
);
53 return (png_get_copyright(NULL
));
55 # ifdef PNG_USER_MEM_SUPPORTED
56 if (malloc_fn
!= NULL
)
58 png_struct dummy_struct
;
59 png_structp png_ptr
= &dummy_struct
;
60 png_ptr
->mem_ptr
=mem_ptr
;
61 struct_ptr
= (*(malloc_fn
))(png_ptr
, (png_uint_32
)size
);
65 # endif /* PNG_USER_MEM_SUPPORTED */
66 struct_ptr
= (png_voidp
)farmalloc(size
);
67 if (struct_ptr
!= NULL
)
68 png_memset(struct_ptr
, 0, size
);
73 /* Free memory allocated by a png_create_struct() call */
75 png_destroy_struct(png_voidp struct_ptr
)
77 # ifdef PNG_USER_MEM_SUPPORTED
78 png_destroy_struct_2(struct_ptr
, NULL
, NULL
);
81 /* Free memory allocated by a png_create_struct() call */
83 png_destroy_struct_2(png_voidp struct_ptr
, png_free_ptr free_fn
,
87 if (struct_ptr
!= NULL
)
89 # ifdef PNG_USER_MEM_SUPPORTED
92 png_struct dummy_struct
;
93 png_structp png_ptr
= &dummy_struct
;
94 png_ptr
->mem_ptr
=mem_ptr
;
95 (*(free_fn
))(png_ptr
, struct_ptr
);
99 # endif /* PNG_USER_MEM_SUPPORTED */
100 farfree (struct_ptr
);
104 /* Allocate memory. For reasonable files, size should never exceed
105 * 64K. However, zlib may allocate more then 64K if you don't tell
106 * it not to. See zconf.h and png.h for more information. zlib does
107 * need to allocate exactly 64K, so whatever you call here must
108 * have the ability to do that.
110 * Borland seems to have a problem in DOS mode for exactly 64K.
111 * It gives you a segment with an offset of 8 (perhaps to store its
112 * memory stuff). zlib doesn't like this at all, so we have to
113 * detect and deal with it. This code should not be needed in
114 * Windows or OS/2 modes, and only in 16 bit mode. This code has
115 * been updated by Alexander Lehmann for version 0.89 to waste less
118 * Note that we can't use png_size_t for the "size" declaration,
119 * since on some systems a png_size_t is a 16-bit quantity, and as a
120 * result, we would be truncating potentially larger memory requests
121 * (which should cause a fatal error) and introducing major problems.
123 PNG_FUNCTION(png_voidp
,PNGAPI
124 png_calloc
,(png_structp png_ptr
, png_alloc_size_t size
),PNG_ALLOCATED
)
128 ret
= (png_malloc(png_ptr
, size
));
131 png_memset(ret
,0,(png_size_t
)size
);
136 PNG_FUNCTION(png_voidp
,PNGAPI
137 png_malloc
,(png_structp png_ptr
, png_alloc_size_t size
),PNG_ALLOCATED
)
141 if (png_ptr
== NULL
|| size
== 0)
144 # ifdef PNG_USER_MEM_SUPPORTED
145 if (png_ptr
->malloc_fn
!= NULL
)
146 ret
= ((png_voidp
)(*(png_ptr
->malloc_fn
))(png_ptr
, (png_size_t
)size
));
149 ret
= (png_malloc_default(png_ptr
, size
));
151 if (ret
== NULL
&& (png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
152 png_error(png_ptr
, "Out of memory");
157 PNG_FUNCTION(png_voidp
,PNGAPI
158 png_malloc_default
,(png_structp png_ptr
, png_alloc_size_t size
),PNG_ALLOCATED
)
161 # endif /* PNG_USER_MEM_SUPPORTED */
163 if (png_ptr
== NULL
|| size
== 0)
166 # ifdef PNG_MAX_MALLOC_64K
167 if (size
> (png_uint_32
)65536L)
169 png_warning(png_ptr
, "Cannot Allocate > 64K");
176 if (size
!= (size_t)size
)
179 else if (size
== (png_uint_32
)65536L)
181 if (png_ptr
->offset_table
== NULL
)
183 /* Try to see if we need to do any of this fancy stuff */
184 ret
= farmalloc(size
);
185 if (ret
== NULL
|| ((png_size_t
)ret
& 0xffff))
188 png_uint_32 total_size
;
190 int i
, mem_level
, window_bits
;
191 png_byte huge
* hptr
;
201 png_ptr
->zlib_window_bits
>= png_ptr
->zlib_text_window_bits
?
202 png_ptr
->zlib_window_bits
: png_ptr
->zlib_text_window_bits
;
204 if (window_bits
> 14)
205 num_blocks
= (int)(1 << (window_bits
- 14));
211 png_ptr
->zlib_mem_level
>= png_ptr
->zlib_text_mem_level
?
212 png_ptr
->zlib_mem_level
: png_ptr
->zlib_text_mem_level
;
215 num_blocks
+= (int)(1 << (mem_level
- 7));
220 total_size
= ((png_uint_32
)65536L) * (png_uint_32
)num_blocks
+16;
222 table
= farmalloc(total_size
);
226 # ifndef PNG_USER_MEM_SUPPORTED
227 if ((png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
228 png_error(png_ptr
, "Out Of Memory"); /* Note "O", "M" */
231 png_warning(png_ptr
, "Out Of Memory");
236 if ((png_size_t
)table
& 0xfff0)
238 # ifndef PNG_USER_MEM_SUPPORTED
239 if ((png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
241 "Farmalloc didn't return normalized pointer");
245 "Farmalloc didn't return normalized pointer");
250 png_ptr
->offset_table
= table
;
251 png_ptr
->offset_table_ptr
= farmalloc(num_blocks
*
252 png_sizeof(png_bytep
));
254 if (png_ptr
->offset_table_ptr
== NULL
)
256 # ifndef PNG_USER_MEM_SUPPORTED
257 if ((png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
258 png_error(png_ptr
, "Out Of memory"); /* Note "O", "m" */
261 png_warning(png_ptr
, "Out Of memory");
266 hptr
= (png_byte huge
*)table
;
267 if ((png_size_t
)hptr
& 0xf)
269 hptr
= (png_byte huge
*)((long)(hptr
) & 0xfffffff0L
);
270 hptr
= hptr
+ 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
273 for (i
= 0; i
< num_blocks
; i
++)
275 png_ptr
->offset_table_ptr
[i
] = (png_bytep
)hptr
;
276 hptr
= hptr
+ (png_uint_32
)65536L; /* "+=" fails on TC++3.0 */
279 png_ptr
->offset_table_number
= num_blocks
;
280 png_ptr
->offset_table_count
= 0;
281 png_ptr
->offset_table_count_free
= 0;
285 if (png_ptr
->offset_table_count
>= png_ptr
->offset_table_number
)
287 # ifndef PNG_USER_MEM_SUPPORTED
288 if ((png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
289 png_error(png_ptr
, "Out of Memory"); /* Note "O" and "M" */
292 png_warning(png_ptr
, "Out of Memory");
297 ret
= png_ptr
->offset_table_ptr
[png_ptr
->offset_table_count
++];
301 ret
= farmalloc(size
);
303 # ifndef PNG_USER_MEM_SUPPORTED
306 if ((png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
307 png_error(png_ptr
, "Out of memory"); /* Note "o" and "m" */
310 png_warning(png_ptr
, "Out of memory"); /* Note "o" and "m" */
317 /* Free a pointer allocated by png_malloc(). In the default
318 * configuration, png_ptr is not used, but is passed in case it
319 * is needed. If ptr is NULL, return without taking any action.
322 png_free(png_structp png_ptr
, png_voidp ptr
)
324 if (png_ptr
== NULL
|| ptr
== NULL
)
327 # ifdef PNG_USER_MEM_SUPPORTED
328 if (png_ptr
->free_fn
!= NULL
)
330 (*(png_ptr
->free_fn
))(png_ptr
, ptr
);
335 png_free_default(png_ptr
, ptr
);
339 png_free_default(png_structp png_ptr
, png_voidp ptr
)
341 # endif /* PNG_USER_MEM_SUPPORTED */
343 if (png_ptr
== NULL
|| ptr
== NULL
)
346 if (png_ptr
->offset_table
!= NULL
)
350 for (i
= 0; i
< png_ptr
->offset_table_count
; i
++)
352 if (ptr
== png_ptr
->offset_table_ptr
[i
])
355 png_ptr
->offset_table_count_free
++;
359 if (png_ptr
->offset_table_count_free
== png_ptr
->offset_table_count
)
361 farfree(png_ptr
->offset_table
);
362 farfree(png_ptr
->offset_table_ptr
);
363 png_ptr
->offset_table
= NULL
;
364 png_ptr
->offset_table_ptr
= NULL
;
372 #else /* Not the Borland DOS special memory handler */
374 /* Allocate memory for a png_struct or a png_info. The malloc and
375 memset can be replaced by a single call to calloc() if this is thought
376 to improve performance noticably. */
377 PNG_FUNCTION(png_voidp
/* PRIVATE */,
378 png_create_struct
,(int type
),PNG_ALLOCATED
)
380 # ifdef PNG_USER_MEM_SUPPORTED
381 return (png_create_struct_2(type
, NULL
, NULL
));
384 /* Allocate memory for a png_struct or a png_info. The malloc and
385 memset can be replaced by a single call to calloc() if this is thought
386 to improve performance noticably. */
387 PNG_FUNCTION(png_voidp
/* PRIVATE */,
388 png_create_struct_2
,(int type
, png_malloc_ptr malloc_fn
, png_voidp mem_ptr
),
391 # endif /* PNG_USER_MEM_SUPPORTED */
393 png_voidp struct_ptr
;
395 if (type
== PNG_STRUCT_INFO
)
396 size
= png_sizeof(png_info
);
398 else if (type
== PNG_STRUCT_PNG
)
399 size
= png_sizeof(png_struct
);
404 # ifdef PNG_USER_MEM_SUPPORTED
405 if (malloc_fn
!= NULL
)
407 png_struct dummy_struct
;
408 png_structp png_ptr
= &dummy_struct
;
409 png_ptr
->mem_ptr
=mem_ptr
;
410 struct_ptr
= (*(malloc_fn
))(png_ptr
, size
);
412 if (struct_ptr
!= NULL
)
413 png_memset(struct_ptr
, 0, size
);
417 # endif /* PNG_USER_MEM_SUPPORTED */
419 # if defined(__TURBOC__) && !defined(__FLAT__)
420 struct_ptr
= (png_voidp
)farmalloc(size
);
422 # if defined(_MSC_VER) && defined(MAXSEG_64K)
423 struct_ptr
= (png_voidp
)halloc(size
, 1);
425 struct_ptr
= (png_voidp
)malloc(size
);
429 if (struct_ptr
!= NULL
)
430 png_memset(struct_ptr
, 0, size
);
436 /* Free memory allocated by a png_create_struct() call */
438 png_destroy_struct(png_voidp struct_ptr
)
440 # ifdef PNG_USER_MEM_SUPPORTED
441 png_destroy_struct_2(struct_ptr
, NULL
, NULL
);
444 /* Free memory allocated by a png_create_struct() call */
446 png_destroy_struct_2(png_voidp struct_ptr
, png_free_ptr free_fn
,
449 # endif /* PNG_USER_MEM_SUPPORTED */
450 if (struct_ptr
!= NULL
)
452 # ifdef PNG_USER_MEM_SUPPORTED
455 png_struct dummy_struct
;
456 png_structp png_ptr
= &dummy_struct
;
457 png_ptr
->mem_ptr
=mem_ptr
;
458 (*(free_fn
))(png_ptr
, struct_ptr
);
461 # endif /* PNG_USER_MEM_SUPPORTED */
462 # if defined(__TURBOC__) && !defined(__FLAT__)
466 # if defined(_MSC_VER) && defined(MAXSEG_64K)
477 /* Allocate memory. For reasonable files, size should never exceed
478 * 64K. However, zlib may allocate more then 64K if you don't tell
479 * it not to. See zconf.h and png.h for more information. zlib does
480 * need to allocate exactly 64K, so whatever you call here must
481 * have the ability to do that.
484 PNG_FUNCTION(png_voidp
,PNGAPI
485 png_calloc
,(png_structp png_ptr
, png_alloc_size_t size
),PNG_ALLOCATED
)
489 ret
= (png_malloc(png_ptr
, size
));
492 png_memset(ret
,0,(png_size_t
)size
);
497 PNG_FUNCTION(png_voidp
,PNGAPI
498 png_malloc
,(png_structp png_ptr
, png_alloc_size_t size
),PNG_ALLOCATED
)
502 # ifdef PNG_USER_MEM_SUPPORTED
503 if (png_ptr
== NULL
|| size
== 0)
506 if (png_ptr
->malloc_fn
!= NULL
)
507 ret
= ((png_voidp
)(*(png_ptr
->malloc_fn
))(png_ptr
, (png_size_t
)size
));
510 ret
= (png_malloc_default(png_ptr
, size
));
512 if (ret
== NULL
&& (png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
513 png_error(png_ptr
, "Out of Memory");
518 PNG_FUNCTION(png_voidp
,PNGAPI
519 png_malloc_default
,(png_structp png_ptr
, png_alloc_size_t size
),PNG_ALLOCATED
)
522 # endif /* PNG_USER_MEM_SUPPORTED */
524 if (png_ptr
== NULL
|| size
== 0)
527 # ifdef PNG_MAX_MALLOC_64K
528 if (size
> (png_uint_32
)65536L)
530 # ifndef PNG_USER_MEM_SUPPORTED
531 if ((png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
532 png_error(png_ptr
, "Cannot Allocate > 64K");
540 /* Check for overflow */
541 # if defined(__TURBOC__) && !defined(__FLAT__)
543 if (size
!= (unsigned long)size
)
547 ret
= farmalloc(size
);
550 # if defined(_MSC_VER) && defined(MAXSEG_64K)
551 if (size
!= (unsigned long)size
)
555 ret
= halloc(size
, 1);
558 if (size
!= (size_t)size
)
562 ret
= malloc((size_t)size
);
566 # ifndef PNG_USER_MEM_SUPPORTED
567 if (ret
== NULL
&& (png_ptr
->flags
&PNG_FLAG_MALLOC_NULL_MEM_OK
) == 0)
568 png_error(png_ptr
, "Out of Memory");
574 /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
575 * without taking any action.
578 png_free(png_structp png_ptr
, png_voidp ptr
)
580 if (png_ptr
== NULL
|| ptr
== NULL
)
583 # ifdef PNG_USER_MEM_SUPPORTED
584 if (png_ptr
->free_fn
!= NULL
)
586 (*(png_ptr
->free_fn
))(png_ptr
, ptr
);
591 png_free_default(png_ptr
, ptr
);
595 png_free_default(png_structp png_ptr
, png_voidp ptr
)
597 if (png_ptr
== NULL
|| ptr
== NULL
)
600 # endif /* PNG_USER_MEM_SUPPORTED */
602 # if defined(__TURBOC__) && !defined(__FLAT__)
606 # if defined(_MSC_VER) && defined(MAXSEG_64K)
615 #endif /* Not Borland DOS special memory handler */
617 /* This function was added at libpng version 1.2.3. The png_malloc_warn()
618 * function will set up png_malloc() to issue a png_warning and return NULL
619 * instead of issuing a png_error, if it fails to allocate the requested
622 PNG_FUNCTION(png_voidp
,PNGAPI
623 png_malloc_warn
,(png_structp png_ptr
, png_alloc_size_t size
),PNG_ALLOCATED
)
626 png_uint_32 save_flags
;
630 save_flags
= png_ptr
->flags
;
631 png_ptr
->flags
|=PNG_FLAG_MALLOC_NULL_MEM_OK
;
632 ptr
= (png_voidp
)png_malloc((png_structp
)png_ptr
, size
);
633 png_ptr
->flags
=save_flags
;
638 #ifdef PNG_USER_MEM_SUPPORTED
639 /* This function is called when the application wants to use another method
640 * of allocating and freeing memory.
643 png_set_mem_fn(png_structp png_ptr
, png_voidp mem_ptr
, png_malloc_ptr
644 malloc_fn
, png_free_ptr free_fn
)
648 png_ptr
->mem_ptr
= mem_ptr
;
649 png_ptr
->malloc_fn
= malloc_fn
;
650 png_ptr
->free_fn
= free_fn
;
654 /* This function returns a pointer to the mem_ptr associated with the user
655 * functions. The application should free any memory associated with this
656 * pointer before png_write_destroy and png_read_destroy are called.
659 png_get_mem_ptr(png_const_structp png_ptr
)
664 return ((png_voidp
)png_ptr
->mem_ptr
);
666 #endif /* PNG_USER_MEM_SUPPORTED */
667 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */