blob: 60645f0a988d6cc3fe561e10ff625c3da72f6d60 [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 Schalnate5a37791996-06-05 15:50:50 -05004 libpng 1.0 beta 3 - version 0.89
Guy Schalnat0d580581995-07-20 02:43:20 -05005 For conditions of distribution and use, see copyright notice in png.h
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06006 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
Guy Schalnate5a37791996-06-05 15:50:50 -05007 May 25, 1996
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 Schalnatb2e01bd1996-01-26 01:38:47 -060010 need special memory handling are expected to modify the code in this file
11 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 Schalnat4ee97b01996-01-16 01:51:56 -060016/* Borland DOS special memory handler */
17#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
18/* if you change this, be sure to change the one in png.h also */
19
Guy Schalnate5a37791996-06-05 15:50:50 -050020/* Allocate memory for a png_struct. The malloc and memset can be replaced
21 * by a single call to calloc() if this is thought to improve performance.
22 */
23png_voidp
24png_create_struct(uInt type)
25{
26 png_size_t type;
27 png_voidp struct_ptr;
28
29 if (type == PNG_STRUCT_INFO)
30 size = sizeof(png_info);
31 else if (type == PNG_STRUCT_PNG)
32 size = sizeof(png_struct);
33 else
34 return (png_voidp)NULL;
35
36 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
37 {
38 png_memset(struct_ptr, 0, size);
39 }
40
41 return (struct_ptr);
42}
43
44
45/* Free memory allocated by a png_create_struct() call */
46void
47png_destroy_struct(png_voidp struct_ptr)
48{
49 if (struct_ptr)
50 farfree (struct_ptr);
51}
52
Guy Schalnat0d580581995-07-20 02:43:20 -050053/* Allocate memory. For reasonable files, size should never exceed
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060054 64K. However, zlib may allocate more then 64K if you don't tell
55 it not to. See zconf.h and png.h for more information. zlib does
56 need to allocate exactly 64K, so whatever you call here must
57 have the ability to do that. */
Guy Schalnat4ee97b01996-01-16 01:51:56 -060058
59/* Borland seems to have a problem in DOS mode for exactly 64K.
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060060 It gives you a segment with an offset of 8 (perhaps to store it's
61 memory stuff). zlib doesn't like this at all, so we have to
62 detect and deal with it. This code should not be needed in
Guy Schalnate5a37791996-06-05 15:50:50 -050063 Windows or OS/2 modes, and only in 16 bit mode. This code has
64 been updated by Alexander Lehmann for version 0.89 to waste less
65 memory.
Guy Schalnat4ee97b01996-01-16 01:51:56 -060066*/
67
68png_voidp
69png_large_malloc(png_structp png_ptr, png_uint_32 size)
70{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060071 png_voidp ret;
72 if (!png_ptr || !size)
Guy Schalnate5a37791996-06-05 15:50:50 -050073 return ((voidp)NULL);
Guy Schalnat4ee97b01996-01-16 01:51:56 -060074
75#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060076 if (size > (png_uint_32)65536L)
77 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat4ee97b01996-01-16 01:51:56 -060078#endif
79
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060080 if (size == (png_uint_32)(65536L))
81 {
82 if (!png_ptr->offset_table)
83 {
84 /* try to see if we need to do any of this fancy stuff */
85 ret = farmalloc(size);
86 if (!ret || ((long)ret & 0xffff))
87 {
88 int num_blocks;
89 png_uint_32 total_size;
90 png_bytep table;
91 int i;
Guy Schalnat4ee97b01996-01-16 01:51:56 -060092 png_byte huge * hptr;
93
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060094 if (ret)
95 farfree(ret);
Guy Schalnate5a37791996-06-05 15:50:50 -050096 ret = NULL;
Guy Schalnat4ee97b01996-01-16 01:51:56 -060097
Guy Schalnatb2e01bd1996-01-26 01:38:47 -060098 num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
99 if (num_blocks < 1)
100 num_blocks = 1;
101 if (png_ptr->zlib_mem_level >= 7)
102 num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
103 else
104 num_blocks++;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600105
Guy Schalnate5a37791996-06-05 15:50:50 -0500106 total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600107
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600108 table = farmalloc(total_size);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600109
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600110 if (!table)
111 {
112 png_error(png_ptr, "Out of Memory");
113 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600114
Guy Schalnate5a37791996-06-05 15:50:50 -0500115 if ((long)table & 0xfff0)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600116 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500117 png_error(png_ptr, "Farmalloc didn't return normalized pointer");
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600118 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600119
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600120 png_ptr->offset_table = table;
121 png_ptr->offset_table_ptr = farmalloc(
122 num_blocks * sizeof (png_bytep));
Guy Schalnate5a37791996-06-05 15:50:50 -0500123
124 if (!png_ptr->offset_table_ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600125 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500126 png_error(png_ptr, "Out of memory");
127 }
128
129 hptr = (png_byte huge *)table;
130 if ((long)hptr & 0xf)
131 {
132 hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
133 hptr += 16L;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600134 }
135 for (i = 0; i < num_blocks; i++)
136 {
137 png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
138 hptr += 65536L;
139 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600140
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600141 png_ptr->offset_table_number = num_blocks;
142 png_ptr->offset_table_count = 0;
143 png_ptr->offset_table_count_free = 0;
144 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600145 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500146
147 if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
148 png_error(png_ptr, "Out of Memory");
149
150 ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600151 }
152 else
153 ret = farmalloc(size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500154
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500155 if (ret == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500156 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600157 png_error(png_ptr, "Out of Memory");
Guy Schalnat0d580581995-07-20 02:43:20 -0500158 }
159
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600160 return ret;
Guy Schalnat0d580581995-07-20 02:43:20 -0500161}
162
163/* free a pointer allocated by png_large_malloc(). In the default
164 configuration, png_ptr is not used, but is passed in case it
165 is needed. If ptr is NULL, return without taking any action. */
166void
Guy Schalnat6d764711995-12-19 03:22:19 -0600167png_large_free(png_structp png_ptr, png_voidp ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500168{
169 if (!png_ptr)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600170 return;
171
172 if (ptr != NULL)
173 {
174 if (png_ptr->offset_table)
175 {
176 int i;
177
178 for (i = 0; i < png_ptr->offset_table_count; i++)
179 {
180 if (ptr == png_ptr->offset_table_ptr[i])
181 {
182 ptr = 0;
183 png_ptr->offset_table_count_free++;
184 break;
185 }
186 }
187 if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
188 {
189 farfree(png_ptr->offset_table);
190 farfree(png_ptr->offset_table_ptr);
191 png_ptr->offset_table = 0;
192 png_ptr->offset_table_ptr = 0;
193 }
194 }
195
196 if (ptr)
197 farfree(ptr);
198 }
199}
200
201#else /* Not the Borland DOS special memory handler */
202
Guy Schalnate5a37791996-06-05 15:50:50 -0500203/* Allocate memory for a png_struct or a png_info. The malloc and
204 * memset can be replaced by a single call to calloc() if this is thought
205 * to improve performance noticably.
206 */
207png_voidp
208png_create_struct(uInt type)
209{
210 size_t size;
211 png_voidp struct_ptr;
212
213 if (type == PNG_STRUCT_INFO)
214 size = sizeof(png_info);
215 else if (type == PNG_STRUCT_PNG)
216 size = sizeof(png_struct);
217 else
218 return (png_voidp)NULL;
219
220#if defined(__TURBOC__) && !defined(__FLAT__)
221 if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
222#else
223# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatc21f90c1996-06-17 16:24:45 -0500224 if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500225# else
226 if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
227# endif
228#endif
229 {
230 png_memset(struct_ptr, 0, size);
231 }
232
233 return (struct_ptr);
234}
235
236
237/* Free memory allocated by a png_create_struct() call */
238void
239png_destroy_struct(png_voidp struct_ptr)
240{
241 if (struct_ptr)
242#if defined(__TURBOC__) && !defined(__FLAT__)
243 farfree(struct_ptr);
244#else
245# if defined(_MSC_VER) && defined(MAXSEG_64K)
246 hfree(struct_ptr);
247# else
248 free(struct_ptr);
249# endif
250#endif
251}
252
253
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600254/* Allocate memory. For reasonable files, size should never exceed
255 64K. However, zlib may allocate more then 64K if you don't tell
256 it not to. See zconf.h and png.h for more information. zlib does
257 need to allocate exactly 64K, so whatever you call here must
258 have the ability to do that. */
259
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600260png_voidp
261png_large_malloc(png_structp png_ptr, png_uint_32 size)
262{
263 png_voidp ret;
264 if (!png_ptr || !size)
265 return ((voidp)0);
266
267#ifdef PNG_MAX_MALLOC_64K
268 if (size > (png_uint_32)65536L)
269 png_error(png_ptr, "Cannot Allocate > 64K");
270#endif
271
272#if defined(__TURBOC__) && !defined(__FLAT__)
273 ret = farmalloc(size);
274#else
275# if defined(_MSC_VER) && defined(MAXSEG_64K)
276 ret = halloc(size, 1);
277# else
278 ret = malloc(size);
279# endif
280#endif
281
282 if (ret == NULL)
283 {
284 png_error(png_ptr, "Out of Memory");
285 }
286
287 return ret;
288}
289
290/* free a pointer allocated by png_large_malloc(). In the default
291 configuration, png_ptr is not used, but is passed in case it
292 is needed. If ptr is NULL, return without taking any action. */
293void
294png_large_free(png_structp png_ptr, png_voidp ptr)
295{
296 if (!png_ptr)
297 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500298
Guy Schalnat6d764711995-12-19 03:22:19 -0600299 if (ptr != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500300 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600301#if defined(__TURBOC__) && !defined(__FLAT__)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600302 farfree(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500303#else
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600304# if defined(_MSC_VER) && defined(MAXSEG_64K)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600305 hfree(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600306# else
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600307 free(ptr);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600308# endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500309#endif
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600310 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500311}
312
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600313#endif /* Not Borland DOS special memory handler */
Guy Schalnat6d764711995-12-19 03:22:19 -0600314
Guy Schalnat0d580581995-07-20 02:43:20 -0500315/* Allocate memory. This is called for smallish blocks only It
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600316 should not get anywhere near 64K. On segmented machines, this
317 must come from the local heap (for zlib). Currently, zlib is
318 the only one that uses this, so you should only get one call
319 to this, and that a small block. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500320void *
Guy Schalnat6d764711995-12-19 03:22:19 -0600321png_malloc(png_structp png_ptr, png_uint_32 size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500322{
323 void *ret;
324
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500325 if (!png_ptr || !size)
Guy Schalnat6d764711995-12-19 03:22:19 -0600326 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500327 return ((void *)0);
Guy Schalnat6d764711995-12-19 03:22:19 -0600328 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500329
330#ifdef PNG_MAX_MALLOC_64K
331 if (size > (png_uint_32)65536L)
Guy Schalnat6d764711995-12-19 03:22:19 -0600332 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat0d580581995-07-20 02:43:20 -0500333#endif
334
Guy Schalnat6d764711995-12-19 03:22:19 -0600335
Guy Schalnat0d580581995-07-20 02:43:20 -0500336 ret = malloc((png_size_t)size);
337
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600338 if (!ret)
Guy Schalnat0d580581995-07-20 02:43:20 -0500339 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600340 png_error(png_ptr, "Out of Memory");
Guy Schalnat0d580581995-07-20 02:43:20 -0500341 }
342
343 return ret;
344}
345
346/* Reallocate memory. This will not get near 64K on a
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600347 even marginally reasonable file. This is not used in
348 the current version of the library. */
Guy Schalnat0d580581995-07-20 02:43:20 -0500349void *
Guy Schalnat6d764711995-12-19 03:22:19 -0600350png_realloc(png_structp png_ptr, void * ptr, png_uint_32 size,
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600351 png_uint_32 old_size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500352{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600353 void *ret;
Guy Schalnat0d580581995-07-20 02:43:20 -0500354
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600355 if (!png_ptr || !old_size || !ptr || !size)
356 return ((void *)0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500357
358#ifdef PNG_MAX_MALLOC_64K
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600359 if (size > (png_uint_32)65536L)
360 png_error(png_ptr, "Cannot Allocate > 64K");
Guy Schalnat0d580581995-07-20 02:43:20 -0500361#endif
362
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600363 ret = realloc(ptr, (png_size_t)size);
Guy Schalnat0d580581995-07-20 02:43:20 -0500364
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600365 if (!ret)
366 {
367 png_error(png_ptr, "Out of Memory 7");
368 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500369
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600370 return ret;
Guy Schalnat0d580581995-07-20 02:43:20 -0500371}
372
373/* free a pointer allocated by png_malloc(). In the default
374 configuration, png_ptr is not used, but is passed incase it
375 is needed. If ptr is NULL, return without taking any action. */
376void
Guy Schalnat6d764711995-12-19 03:22:19 -0600377png_free(png_structp png_ptr, void * ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500378{
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600379 if (!png_ptr)
380 return;
Guy Schalnat0d580581995-07-20 02:43:20 -0500381
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600382 if (ptr != (void *)0)
383 free(ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500384}
385