]>
git.saurik.com Git - wxWidgets.git/blob - src/png/pngmem.c
2 /* pngmem.c - stub functions for memory allocation
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * Copyright (c) 1998, Glenn Randers-Pehrson
11 * This file provides a location for all memory allocation. Users which
12 * need special memory handling are expected to modify the code in this file
13 * to meet their needs. See the instructions at each function.
17 #include "../png/png.h"
19 /* The following "hides" PNG_MALLOC and PNG_FREE thus allowing the pngtest
20 application to put a wrapper on top of them. */
21 #ifdef PNGTEST_MEMORY_DEBUG
22 #define PNG_MALLOC png_debug_malloc
23 #define PNG_FREE png_debug_free
25 #define PNG_MALLOC png_malloc
26 #define PNG_FREE png_free
29 /* Borland DOS special memory handler */
30 #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
31 /* if you change this, be sure to change the one in png.h also */
33 /* Allocate memory for a png_struct. The malloc and memset can be replaced
34 by a single call to calloc() if this is thought to improve performance. */
36 png_create_struct(int type
)
41 if (type
== PNG_STRUCT_INFO
)
42 size
= sizeof(png_info
);
43 else if (type
== PNG_STRUCT_PNG
)
44 size
= sizeof(png_struct
);
46 return ((png_voidp
)NULL
);
48 if ((struct_ptr
= (png_voidp
)farmalloc(size
)) != NULL
)
50 png_memset(struct_ptr
, 0, size
);
57 /* Free memory allocated by a png_create_struct() call */
59 png_destroy_struct(png_voidp struct_ptr
)
61 if (struct_ptr
!= NULL
)
68 /* Allocate memory. For reasonable files, size should never exceed
69 * 64K. However, zlib may allocate more then 64K if you don't tell
70 * it not to. See zconf.h and png.h for more information. zlib does
71 * need to allocate exactly 64K, so whatever you call here must
72 * have the ability to do that.
74 * Borland seems to have a problem in DOS mode for exactly 64K.
75 * It gives you a segment with an offset of 8 (perhaps to store it's
76 * memory stuff). zlib doesn't like this at all, so we have to
77 * detect and deal with it. This code should not be needed in
78 * Windows or OS/2 modes, and only in 16 bit mode. This code has
79 * been updated by Alexander Lehmann for version 0.89 to waste less
82 * Note that we can't use png_size_t for the "size" declaration,
83 * since on some systems a png_size_t is a 16-bit quantity, and as a
84 * result, we would be truncating potentially larger memory requests
85 * (which should cause a fatal error) and introducing major problems.
88 PNG_MALLOC(png_structp png_ptr
, png_uint_32 size
)
91 if (png_ptr
== NULL
|| size
== 0)
92 return ((png_voidp
)NULL
);
94 #ifdef PNG_MAX_MALLOC_64K
95 if (size
> (png_uint_32
)65536L)
96 png_error(png_ptr
, "Cannot Allocate > 64K");
99 if (size
== (png_uint_32
)65536L)
101 if (png_ptr
->offset_table
== NULL
)
103 /* try to see if we need to do any of this fancy stuff */
104 ret
= farmalloc(size
);
105 if (ret
== NULL
|| ((png_size_t
)ret
& 0xffff))
108 png_uint_32 total_size
;
111 png_byte huge
* hptr
;
119 num_blocks
= (int)(1 << (png_ptr
->zlib_window_bits
- 14));
122 if (png_ptr
->zlib_mem_level
>= 7)
123 num_blocks
+= (int)(1 << (png_ptr
->zlib_mem_level
- 7));
127 total_size
= ((png_uint_32
)65536L) * (png_uint_32
)num_blocks
+16;
129 table
= farmalloc(total_size
);
133 png_error(png_ptr
, "Out Of Memory."); /* Note "O" and "M" */
136 if ((png_size_t
)table
& 0xfff0)
138 png_error(png_ptr
, "Farmalloc didn't return normalized pointer");
141 png_ptr
->offset_table
= table
;
142 png_ptr
->offset_table_ptr
= farmalloc(num_blocks
*
145 if (png_ptr
->offset_table_ptr
== NULL
)
147 png_error(png_ptr
, "Out Of memory.");
150 hptr
= (png_byte huge
*)table
;
151 if ((png_size_t
)hptr
& 0xf)
153 hptr
= (png_byte huge
*)((long)(hptr
) & 0xfffffff0L
);
156 for (i
= 0; i
< num_blocks
; i
++)
158 png_ptr
->offset_table_ptr
[i
] = (png_bytep
)hptr
;
159 hptr
+= (png_uint_32
)65536L;
162 png_ptr
->offset_table_number
= num_blocks
;
163 png_ptr
->offset_table_count
= 0;
164 png_ptr
->offset_table_count_free
= 0;
168 if (png_ptr
->offset_table_count
>= png_ptr
->offset_table_number
)
169 png_error(png_ptr
, "Out of Memory.");
171 ret
= png_ptr
->offset_table_ptr
[png_ptr
->offset_table_count
++];
174 ret
= farmalloc(size
);
178 png_error(png_ptr
, "Out of memory."); /* Note "o" and "m" */
184 /* free a pointer allocated by PNG_MALLOC(). In the default
185 configuration, png_ptr is not used, but is passed in case it
186 is needed. If ptr is NULL, return without taking any action. */
188 PNG_FREE(png_structp png_ptr
, png_voidp ptr
)
190 if (png_ptr
== NULL
|| ptr
== NULL
)
193 if (png_ptr
->offset_table
!= NULL
)
197 for (i
= 0; i
< png_ptr
->offset_table_count
; i
++)
199 if (ptr
== png_ptr
->offset_table_ptr
[i
])
202 png_ptr
->offset_table_count_free
++;
206 if (png_ptr
->offset_table_count_free
== png_ptr
->offset_table_count
)
208 farfree(png_ptr
->offset_table
);
209 farfree(png_ptr
->offset_table_ptr
);
210 png_ptr
->offset_table
= NULL
;
211 png_ptr
->offset_table_ptr
= NULL
;
222 #else /* Not the Borland DOS special memory handler */
224 /* Allocate memory for a png_struct or a png_info. The malloc and
225 memset can be replaced by a single call to calloc() if this is thought
226 to improve performance noticably.*/
228 png_create_struct(int type
)
231 png_voidp struct_ptr
;
233 if (type
== PNG_STRUCT_INFO
)
234 size
= sizeof(png_info
);
235 else if (type
== PNG_STRUCT_PNG
)
236 size
= sizeof(png_struct
);
238 return ((png_voidp
)NULL
);
240 #if defined(__TURBOC__) && !defined(__FLAT__)
241 if ((struct_ptr
= (png_voidp
)farmalloc(size
)) != NULL
)
243 # if defined(_MSC_VER) && defined(MAXSEG_64K)
244 if ((struct_ptr
= (png_voidp
)halloc(size
,1)) != NULL
)
246 if ((struct_ptr
= (png_voidp
)malloc(size
)) != NULL
)
250 png_memset(struct_ptr
, 0, size
);
257 /* Free memory allocated by a png_create_struct() call */
259 png_destroy_struct(png_voidp struct_ptr
)
261 if (struct_ptr
!= NULL
)
263 #if defined(__TURBOC__) && !defined(__FLAT__)
266 # if defined(_MSC_VER) && defined(MAXSEG_64K)
276 /* Allocate memory. For reasonable files, size should never exceed
277 64K. However, zlib may allocate more then 64K if you don't tell
278 it not to. See zconf.h and png.h for more information. zlib does
279 need to allocate exactly 64K, so whatever you call here must
280 have the ability to do that. */
283 PNG_MALLOC(png_structp png_ptr
, png_uint_32 size
)
287 if (png_ptr
== NULL
|| size
== 0)
288 return ((png_voidp
)NULL
);
290 #ifdef PNG_MAX_MALLOC_64K
291 if (size
> (png_uint_32
)65536L)
292 png_error(png_ptr
, "Cannot Allocate > 64K");
295 #if defined(__TURBOC__) && !defined(__FLAT__)
296 ret
= farmalloc(size
);
298 # if defined(_MSC_VER) && defined(MAXSEG_64K)
299 ret
= halloc(size
, 1);
301 ret
= malloc((size_t)size
);
307 png_error(png_ptr
, "Out of Memory");
313 /* Free a pointer allocated by PNG_MALLOC(). In the default
314 configuration, png_ptr is not used, but is passed in case it
315 is needed. If ptr is NULL, return without taking any action. */
317 PNG_FREE(png_structp png_ptr
, png_voidp ptr
)
319 if (png_ptr
== NULL
|| ptr
== NULL
)
322 #if defined(__TURBOC__) && !defined(__FLAT__)
325 # if defined(_MSC_VER) && defined(MAXSEG_64K)
333 #endif /* Not Borland DOS special memory handler */
336 png_memcpy_check (png_structp png_ptr
, png_voidp s1
, png_voidp s2
,
341 size
= (png_size_t
)length
;
342 if ((png_uint_32
)size
!= length
)
343 png_error(png_ptr
,"Overflow in png_memcpy_check.");
345 return(png_memcpy (s1
, s2
, size
));
349 png_memset_check (png_structp png_ptr
, png_voidp s1
, int value
,
354 size
= (png_size_t
)length
;
355 if ((png_uint_32
)size
!= length
)
356 png_error(png_ptr
,"Overflow in png_memset_check.");
358 return (png_memset (s1
, value
, size
));