pngmem.c 2.9 KB
Newer Older
1

2
/* pngmem.c - stub functions for memory allocation
3

4
	libpng 1.0 beta 2 - version 0.85
5 6
   For conditions of distribution and use, see copyright notice in png.h
   Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
7
   December 19, 1995
8

9
   This file provides a location for all memory allocation.  Users which
10
	need special memory handling are expected to modify the code in this file
11
   to meet their needs.  See the instructions at each function. */
12 13 14 15 16 17 18 19 20 21 22

#define PNG_INTERNAL
#include "png.h"

/* Allocate memory.  For reasonable files, size should never exceed
   64K.  However, zlib may allocate more then 64K if you don't tell
   it not to.  See zconf.h and png.h for more information. zlib does
   need to allocate exactly 64K, so whatever you call here must
   have the ability to do that. */


23 24
png_voidp
png_large_malloc(png_structp png_ptr, png_uint_32 size)
25
{
26
   png_voidp ret;
27
   if (!png_ptr || !size)
28
		return ((voidp)0);
29 30 31

#ifdef PNG_MAX_MALLOC_64K
   if (size > (png_uint_32)65536L)
32
      png_error(png_ptr, "Cannot Allocate > 64K");
33 34
#endif

35 36 37 38
#if defined(__TURBOC__) && !defined(__FLAT__)
	ret = farmalloc(size);
#else
	ret = malloc(size);
39 40
#endif

41
   if (ret == NULL)
42
   {
43
      png_error(png_ptr, "Out of Memory");
44 45 46 47 48 49 50 51 52
   }

   return ret;
}

/* free a pointer allocated by png_large_malloc().  In the default
  configuration, png_ptr is not used, but is passed in case it
  is needed.  If ptr is NULL, return without taking any action. */
void
53
png_large_free(png_structp png_ptr, png_voidp ptr)
54 55
{
   if (!png_ptr)
56
		return;
57

58
   if (ptr != NULL)
59
   {
60 61
#if defined(__TURBOC__) && !defined(__FLAT__)
		farfree(ptr);
62
#else
63
		free(ptr);
64 65 66 67
#endif
   }
}

68

69
/* Allocate memory.  This is called for smallish blocks only  It
70 71
	should not get anywhere near 64K.  On segmented machines, this
	must come from the local heap (for zlib). */
72
void *
73
png_malloc(png_structp png_ptr, png_uint_32 size)
74 75 76
{
   void *ret;

77
   if (!png_ptr || !size)
78
   {
79
      return ((void *)0);
80
   }
81 82 83

#ifdef PNG_MAX_MALLOC_64K
   if (size > (png_uint_32)65536L)
84
      png_error(png_ptr, "Cannot Allocate > 64K");
85 86
#endif

87

88 89
   ret = malloc((png_size_t)size);

90
	if (!ret)
91
   {
92
		png_error(png_ptr, "Out of Memory");
93 94 95 96 97 98
   }

   return ret;
}

/* Reallocate memory.  This will not get near 64K on a
99
	even marginally reasonable file. */
100
void *
101 102
png_realloc(png_structp png_ptr, void * ptr, png_uint_32 size,
	png_uint_32 old_size)
103
{
104
	void *ret;
105

106 107
	if (!png_ptr || !old_size || !ptr || !size)
		return ((void *)0);
108 109

#ifdef PNG_MAX_MALLOC_64K
110 111
	if (size > (png_uint_32)65536L)
		png_error(png_ptr, "Cannot Allocate > 64K");
112 113
#endif

114
	ret = realloc(ptr, (png_size_t)size);
115

116 117 118 119
	if (!ret)
	{
		png_error(png_ptr, "Out of Memory 7");
	}
120

121
	return ret;
122 123 124 125 126 127
}

/* free a pointer allocated by png_malloc().  In the default
  configuration, png_ptr is not used, but is passed incase it
  is needed.  If ptr is NULL, return without taking any action. */
void
128
png_free(png_structp png_ptr, void * ptr)
129
{
130 131
	if (!png_ptr)
		return;
132

133 134
	if (ptr != (void *)0)
		free(ptr);
135 136
}

137
新手
引导
客服 返回
顶部