2 /* pngmem.c - stub functions for memory allocation
4 * Last changed in libpng 1.4.2 [May 6, 2010]
5 * Copyright (c) 1998-2010 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.
20 #define PNG_NO_PEDANTIC_WARNINGS
22 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
25 /* Borland DOS special memory handler */
26 #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
27 /* If you change this, be sure to change the one in png.h also */
29 /* Allocate memory for a png_struct. The malloc and memset can be replaced
30 by a single call to calloc() if this is thought to improve performance. */
31 png_voidp /* PRIVATE */
32 png_create_struct(int type)
34 #ifdef PNG_USER_MEM_SUPPORTED
35 return (png_create_struct_2(type, NULL, NULL));
38 /* Alternate version of png_create_struct, for use with user-defined malloc. */
39 png_voidp /* PRIVATE */
40 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);
48 else if (type == PNG_STRUCT_PNG)
49 size = png_sizeof(png_struct);
51 return (png_get_copyright(NULL));
53 #ifdef PNG_USER_MEM_SUPPORTED
54 if (malloc_fn != NULL)
56 png_struct dummy_struct;
57 png_structp png_ptr = &dummy_struct;
58 png_ptr->mem_ptr=mem_ptr;
59 struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
62 #endif /* PNG_USER_MEM_SUPPORTED */
63 struct_ptr = (png_voidp)farmalloc(size);
64 if (struct_ptr != NULL)
65 png_memset(struct_ptr, 0, size);
69 /* Free memory allocated by a png_create_struct() call */
71 png_destroy_struct(png_voidp struct_ptr)
73 #ifdef PNG_USER_MEM_SUPPORTED
74 png_destroy_struct_2(struct_ptr, NULL, NULL);
77 /* Free memory allocated by a png_create_struct() call */
79 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
83 if (struct_ptr != NULL)
85 #ifdef PNG_USER_MEM_SUPPORTED
88 png_struct dummy_struct;
89 png_structp png_ptr = &dummy_struct;
90 png_ptr->mem_ptr=mem_ptr;
91 (*(free_fn))(png_ptr, struct_ptr);
94 #endif /* PNG_USER_MEM_SUPPORTED */
99 /* Allocate memory. For reasonable files, size should never exceed
100 * 64K. However, zlib may allocate more then 64K if you don't tell
101 * it not to. See zconf.h and png.h for more information. zlib does
102 * need to allocate exactly 64K, so whatever you call here must
103 * have the ability to do that.
105 * Borland seems to have a problem in DOS mode for exactly 64K.
106 * It gives you a segment with an offset of 8 (perhaps to store its
107 * memory stuff). zlib doesn't like this at all, so we have to
108 * detect and deal with it. This code should not be needed in
109 * Windows or OS/2 modes, and only in 16 bit mode. This code has
110 * been updated by Alexander Lehmann for version 0.89 to waste less
113 * Note that we can't use png_size_t for the "size" declaration,
114 * since on some systems a png_size_t is a 16-bit quantity, and as a
115 * result, we would be truncating potentially larger memory requests
116 * (which should cause a fatal error) and introducing major problems.
119 png_calloc(png_structp png_ptr, png_alloc_size_t size)
123 ret = (png_malloc(png_ptr, size));
125 png_memset(ret,0,(png_size_t)size);
130 png_malloc(png_structp png_ptr, png_alloc_size_t size)
134 if (png_ptr == NULL || size == 0)
137 #ifdef PNG_USER_MEM_SUPPORTED
138 if (png_ptr->malloc_fn != NULL)
139 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
141 ret = (png_malloc_default(png_ptr, size));
142 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
143 png_error(png_ptr, "Out of memory");
148 png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
151 #endif /* PNG_USER_MEM_SUPPORTED */
153 if (png_ptr == NULL || size == 0)
156 #ifdef PNG_MAX_MALLOC_64K
157 if (size > (png_uint_32)65536L)
159 png_warning(png_ptr, "Cannot Allocate > 64K");
165 if (size != (size_t)size)
167 else if (size == (png_uint_32)65536L)
169 if (png_ptr->offset_table == NULL)
171 /* Try to see if we need to do any of this fancy stuff */
172 ret = farmalloc(size);
173 if (ret == NULL || ((png_size_t)ret & 0xffff))
176 png_uint_32 total_size;
179 png_byte huge * hptr;
187 if (png_ptr->zlib_window_bits > 14)
188 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
191 if (png_ptr->zlib_mem_level >= 7)
192 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
196 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
198 table = farmalloc(total_size);
202 #ifndef PNG_USER_MEM_SUPPORTED
203 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
204 png_error(png_ptr, "Out Of Memory"); /* Note "O", "M" */
206 png_warning(png_ptr, "Out Of Memory");
211 if ((png_size_t)table & 0xfff0)
213 #ifndef PNG_USER_MEM_SUPPORTED
214 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
216 "Farmalloc didn't return normalized pointer");
219 "Farmalloc didn't return normalized pointer");
224 png_ptr->offset_table = table;
225 png_ptr->offset_table_ptr = farmalloc(num_blocks *
226 png_sizeof(png_bytep));
228 if (png_ptr->offset_table_ptr == NULL)
230 #ifndef PNG_USER_MEM_SUPPORTED
231 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
232 png_error(png_ptr, "Out Of memory"); /* Note "O", "m" */
234 png_warning(png_ptr, "Out Of memory");
239 hptr = (png_byte huge *)table;
240 if ((png_size_t)hptr & 0xf)
242 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
243 hptr = hptr + 16L; /* "hptr += 16L" fails on Turbo C++ 3.0 */
245 for (i = 0; i < num_blocks; i++)
247 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
248 hptr = hptr + (png_uint_32)65536L; /* "+=" fails on TC++3.0 */
251 png_ptr->offset_table_number = num_blocks;
252 png_ptr->offset_table_count = 0;
253 png_ptr->offset_table_count_free = 0;
257 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
259 #ifndef PNG_USER_MEM_SUPPORTED
260 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
261 png_error(png_ptr, "Out of Memory"); /* Note "o" and "M" */
263 png_warning(png_ptr, "Out of Memory");
268 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
271 ret = farmalloc(size);
273 #ifndef PNG_USER_MEM_SUPPORTED
276 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
277 png_error(png_ptr, "Out of memory"); /* Note "o" and "m" */
279 png_warning(png_ptr, "Out of memory"); /* Note "o" and "m" */
286 /* Free a pointer allocated by png_malloc(). In the default
287 * configuration, png_ptr is not used, but is passed in case it
288 * is needed. If ptr is NULL, return without taking any action.
291 png_free(png_structp png_ptr, png_voidp ptr)
293 if (png_ptr == NULL || ptr == NULL)
296 #ifdef PNG_USER_MEM_SUPPORTED
297 if (png_ptr->free_fn != NULL)
299 (*(png_ptr->free_fn))(png_ptr, ptr);
303 png_free_default(png_ptr, ptr);
307 png_free_default(png_structp png_ptr, png_voidp ptr)
309 #endif /* PNG_USER_MEM_SUPPORTED */
311 if (png_ptr == NULL || ptr == NULL)
314 if (png_ptr->offset_table != NULL)
318 for (i = 0; i < png_ptr->offset_table_count; i++)
320 if (ptr == png_ptr->offset_table_ptr[i])
323 png_ptr->offset_table_count_free++;
327 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
329 farfree(png_ptr->offset_table);
330 farfree(png_ptr->offset_table_ptr);
331 png_ptr->offset_table = NULL;
332 png_ptr->offset_table_ptr = NULL;
342 #else /* Not the Borland DOS special memory handler */
344 /* Allocate memory for a png_struct or a png_info. The malloc and
345 memset can be replaced by a single call to calloc() if this is thought
346 to improve performance noticably. */
347 png_voidp /* PRIVATE */
348 png_create_struct(int type)
350 #ifdef PNG_USER_MEM_SUPPORTED
351 return (png_create_struct_2(type, NULL, NULL));
354 /* Allocate memory for a png_struct or a png_info. The malloc and
355 memset can be replaced by a single call to calloc() if this is thought
356 to improve performance noticably. */
357 png_voidp /* PRIVATE */
358 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
360 #endif /* PNG_USER_MEM_SUPPORTED */
362 png_voidp struct_ptr;
364 if (type == PNG_STRUCT_INFO)
365 size = png_sizeof(png_info);
366 else if (type == PNG_STRUCT_PNG)
367 size = png_sizeof(png_struct);
371 #ifdef PNG_USER_MEM_SUPPORTED
372 if (malloc_fn != NULL)
374 png_struct dummy_struct;
375 png_structp png_ptr = &dummy_struct;
376 png_ptr->mem_ptr=mem_ptr;
377 struct_ptr = (*(malloc_fn))(png_ptr, size);
378 if (struct_ptr != NULL)
379 png_memset(struct_ptr, 0, size);
382 #endif /* PNG_USER_MEM_SUPPORTED */
384 #if defined(__TURBOC__) && !defined(__FLAT__)
385 struct_ptr = (png_voidp)farmalloc(size);
387 # if defined(_MSC_VER) && defined(MAXSEG_64K)
388 struct_ptr = (png_voidp)halloc(size, 1);
390 struct_ptr = (png_voidp)malloc(size);
393 if (struct_ptr != NULL)
394 png_memset(struct_ptr, 0, size);
400 /* Free memory allocated by a png_create_struct() call */
402 png_destroy_struct(png_voidp struct_ptr)
404 #ifdef PNG_USER_MEM_SUPPORTED
405 png_destroy_struct_2(struct_ptr, NULL, NULL);
408 /* Free memory allocated by a png_create_struct() call */
410 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
413 #endif /* PNG_USER_MEM_SUPPORTED */
414 if (struct_ptr != NULL)
416 #ifdef PNG_USER_MEM_SUPPORTED
419 png_struct dummy_struct;
420 png_structp png_ptr = &dummy_struct;
421 png_ptr->mem_ptr=mem_ptr;
422 (*(free_fn))(png_ptr, struct_ptr);
425 #endif /* PNG_USER_MEM_SUPPORTED */
426 #if defined(__TURBOC__) && !defined(__FLAT__)
429 # if defined(_MSC_VER) && defined(MAXSEG_64K)
438 /* Allocate memory. For reasonable files, size should never exceed
439 * 64K. However, zlib may allocate more then 64K if you don't tell
440 * it not to. See zconf.h and png.h for more information. zlib does
441 * need to allocate exactly 64K, so whatever you call here must
442 * have the ability to do that.
446 png_calloc(png_structp png_ptr, png_alloc_size_t size)
450 ret = (png_malloc(png_ptr, size));
452 png_memset(ret,0,(png_size_t)size);
457 png_malloc(png_structp png_ptr, png_alloc_size_t size)
461 #ifdef PNG_USER_MEM_SUPPORTED
462 if (png_ptr == NULL || size == 0)
465 if (png_ptr->malloc_fn != NULL)
466 ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
468 ret = (png_malloc_default(png_ptr, size));
469 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
470 png_error(png_ptr, "Out of Memory");
475 png_malloc_default(png_structp png_ptr, png_alloc_size_t size)
478 #endif /* PNG_USER_MEM_SUPPORTED */
480 if (png_ptr == NULL || size == 0)
483 #ifdef PNG_MAX_MALLOC_64K
484 if (size > (png_uint_32)65536L)
486 #ifndef PNG_USER_MEM_SUPPORTED
487 if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
488 png_error(png_ptr, "Cannot Allocate > 64K");
495 /* Check for overflow */
496 #if defined(__TURBOC__) && !defined(__FLAT__)
497 if (size != (unsigned long)size)
500 ret = farmalloc(size);
502 # if defined(_MSC_VER) && defined(MAXSEG_64K)
503 if (size != (unsigned long)size)
506 ret = halloc(size, 1);
508 if (size != (size_t)size)
511 ret = malloc((size_t)size);
515 #ifndef PNG_USER_MEM_SUPPORTED
516 if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
517 png_error(png_ptr, "Out of Memory");
523 /* Free a pointer allocated by png_malloc(). If ptr is NULL, return
524 * without taking any action.
527 png_free(png_structp png_ptr, png_voidp ptr)
529 if (png_ptr == NULL || ptr == NULL)
532 #ifdef PNG_USER_MEM_SUPPORTED
533 if (png_ptr->free_fn != NULL)
535 (*(png_ptr->free_fn))(png_ptr, ptr);
539 png_free_default(png_ptr, ptr);
542 png_free_default(png_structp png_ptr, png_voidp ptr)
544 if (png_ptr == NULL || ptr == NULL)
547 #endif /* PNG_USER_MEM_SUPPORTED */
549 #if defined(__TURBOC__) && !defined(__FLAT__)
552 # if defined(_MSC_VER) && defined(MAXSEG_64K)
560 #endif /* Not Borland DOS special memory handler */
562 /* This function was added at libpng version 1.2.3. The png_malloc_warn()
563 * function will set up png_malloc() to issue a png_warning and return NULL
564 * instead of issuing a png_error, if it fails to allocate the requested
568 png_malloc_warn(png_structp png_ptr, png_alloc_size_t size)
571 png_uint_32 save_flags;
575 save_flags = png_ptr->flags;
576 png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
577 ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
578 png_ptr->flags=save_flags;
583 #ifdef PNG_USER_MEM_SUPPORTED
584 /* This function is called when the application wants to use another method
585 * of allocating and freeing memory.
588 png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
589 malloc_fn, png_free_ptr free_fn)
593 png_ptr->mem_ptr = mem_ptr;
594 png_ptr->malloc_fn = malloc_fn;
595 png_ptr->free_fn = free_fn;
599 /* This function returns a pointer to the mem_ptr associated with the user
600 * functions. The application should free any memory associated with this
601 * pointer before png_write_destroy and png_read_destroy are called.
604 png_get_mem_ptr(png_structp png_ptr)
608 return ((png_voidp)png_ptr->mem_ptr);
610 #endif /* PNG_USER_MEM_SUPPORTED */
611 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */