blob: d3ee01c7a95d1016222ecc6d24634688a3d9c089 [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
Guy Schalnat6d764711995-12-19 03:22:19 -06004 libpng 1.0 beta 2 - version 0.85
Guy Schalnat0d580581995-07-20 02:43:20 -05005 For conditions of distribution and use, see copyright notice in png.h
6 Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
Guy Schalnat6d764711995-12-19 03:22:19 -06007 December 19, 1995
Guy Schalnat0d580581995-07-20 02:43:20 -05008
Guy Schalnat51f0eb41995-09-26 05:22:39 -05009 This file provides a location for all memory allocation. Users which
Guy Schalnat6d764711995-12-19 03:22:19 -060010 need special memory handling are expected to modify the code in this file
Guy Schalnat51f0eb41995-09-26 05:22:39 -050011 to meet their needs. See the instructions at each function. */
Guy Schalnat0d580581995-07-20 02:43:20 -050012
13#define PNG_INTERNAL
14#include "png.h"
15
Guy Schalnat0d580581995-07-20 02:43:20 -050016/* Allocate memory. For reasonable files, size should never exceed
17 64K. However, zlib may allocate more then 64K if you don't tell
18 it not to. See zconf.h and png.h for more information. zlib does
19 need to allocate exactly 64K, so whatever you call here must
20 have the ability to do that. */
21
Guy Schalnat0d580581995-07-20 02:43:20 -050022
Guy Schalnat6d764711995-12-19 03:22:19 -060023png_voidp
24png_large_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat0d580581995-07-20 02:43:20 -050025{
Guy Schalnat6d764711995-12-19 03:22:19 -060026 png_voidp ret;
Guy Schalnat51f0eb41995-09-26 05:22:39 -050027 if (!png_ptr || !size)
Guy Schalnat6d764711995-12-19 03:22:19 -060028 return ((voidp)0);
Guy Schalnat0d580581995-07-20 02:43:20 -050029
30#ifdef PNG_MAX_MALLOC_64K
31 if (size > (png_uint_32)65536L)
Guy Schalnat6d764711995-12-19 03:22:19 -060032 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat0d580581995-07-20 02:43:20 -050033#endif
34
Guy Schalnat6d764711995-12-19 03:22:19 -060035#if defined(__TURBOC__) && !defined(__FLAT__)
36 ret = farmalloc(size);
37#else
38 ret = malloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -050039#endif
40
Guy Schalnat51f0eb41995-09-26 05:22:39 -050041 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -050042 {
Guy Schalnat6d764711995-12-19 03:22:19 -060043 png_error(png_ptr, "Out of Memory");
Guy Schalnat0d580581995-07-20 02:43:20 -050044 }
45
46 return ret;
47}
48
49/* free a pointer allocated by png_large_malloc(). In the default
50 configuration, png_ptr is not used, but is passed in case it
51 is needed. If ptr is NULL, return without taking any action. */
52void
Guy Schalnat6d764711995-12-19 03:22:19 -060053png_large_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -050054{
55 if (!png_ptr)
Guy Schalnat6d764711995-12-19 03:22:19 -060056 return;
Guy Schalnat0d580581995-07-20 02:43:20 -050057
Guy Schalnat6d764711995-12-19 03:22:19 -060058 if (ptr != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -050059 {
Guy Schalnat6d764711995-12-19 03:22:19 -060060#if defined(__TURBOC__) && !defined(__FLAT__)
61 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050062#else
Guy Schalnat6d764711995-12-19 03:22:19 -060063 free(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -050064#endif
65 }
66}
67
Guy Schalnat6d764711995-12-19 03:22:19 -060068
Guy Schalnat0d580581995-07-20 02:43:20 -050069/* Allocate memory. This is called for smallish blocks only It
Guy Schalnat6d764711995-12-19 03:22:19 -060070 should not get anywhere near 64K. On segmented machines, this
71 must come from the local heap (for zlib). */
Guy Schalnat0d580581995-07-20 02:43:20 -050072void *
Guy Schalnat6d764711995-12-19 03:22:19 -060073png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat0d580581995-07-20 02:43:20 -050074{
75 void *ret;
76
Guy Schalnat51f0eb41995-09-26 05:22:39 -050077 if (!png_ptr || !size)
Guy Schalnat6d764711995-12-19 03:22:19 -060078 {
Guy Schalnat0d580581995-07-20 02:43:20 -050079 return ((void *)0);
Guy Schalnat6d764711995-12-19 03:22:19 -060080 }
Guy Schalnat0d580581995-07-20 02:43:20 -050081
82#ifdef PNG_MAX_MALLOC_64K
83 if (size > (png_uint_32)65536L)
Guy Schalnat6d764711995-12-19 03:22:19 -060084 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat0d580581995-07-20 02:43:20 -050085#endif
86
Guy Schalnat6d764711995-12-19 03:22:19 -060087
Guy Schalnat0d580581995-07-20 02:43:20 -050088 ret = malloc((png_size_t)size);
89
Guy Schalnat6d764711995-12-19 03:22:19 -060090 if (!ret)
Guy Schalnat0d580581995-07-20 02:43:20 -050091 {
Guy Schalnat6d764711995-12-19 03:22:19 -060092 png_error(png_ptr, "Out of Memory");
Guy Schalnat0d580581995-07-20 02:43:20 -050093 }
94
95 return ret;
96}
97
98/* Reallocate memory. This will not get near 64K on a
Guy Schalnat6d764711995-12-19 03:22:19 -060099 even marginally reasonable file. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500100void *
Guy Schalnat6d764711995-12-19 03:22:19 -0600101png_realloc(png_structp png_ptr, void * ptr, png_uint_32 size,
102 png_uint_32 old_size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500103{
Guy Schalnat6d764711995-12-19 03:22:19 -0600104 void *ret;
Guy Schalnat0d580581995-07-20 02:43:20 -0500105
Guy Schalnat6d764711995-12-19 03:22:19 -0600106 if (!png_ptr || !old_size || !ptr || !size)
107 return ((void *)0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500108
109#ifdef PNG_MAX_MALLOC_64K
Guy Schalnat6d764711995-12-19 03:22:19 -0600110 if (size > (png_uint_32)65536L)
111 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat0d580581995-07-20 02:43:20 -0500112#endif
113
Guy Schalnat6d764711995-12-19 03:22:19 -0600114 ret = realloc(ptr, (png_size_t)size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500115
Guy Schalnat6d764711995-12-19 03:22:19 -0600116 if (!ret)
117 {
118 png_error(png_ptr, "Out of Memory 7");
119 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500120
Guy Schalnat6d764711995-12-19 03:22:19 -0600121 return ret;
Guy Schalnat0d580581995-07-20 02:43:20 -0500122}
123
124/* free a pointer allocated by png_malloc(). In the default
125 configuration, png_ptr is not used, but is passed incase it
126 is needed. If ptr is NULL, return without taking any action. */
127void
Guy Schalnat6d764711995-12-19 03:22:19 -0600128png_free(png_structp png_ptr, void * ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500129{
Guy Schalnat6d764711995-12-19 03:22:19 -0600130 if (!png_ptr)
131 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500132
Guy Schalnat6d764711995-12-19 03:22:19 -0600133 if (ptr != (void *)0)
134 free(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500135}
136
Guy Schalnat6d764711995-12-19 03:22:19 -0600137