Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 1 | /* pngmem.c - stub functions for memory allocation |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2 | * |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 3 | * libpng 0.99c |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 4 | * For conditions of distribution and use, see copyright notice in png.h |
| 5 | * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
| 6 | * Copyright (c) 1996, 1997 Andreas Dilger |
Glenn Randers-Pehrson | 2687fcc | 1998-01-07 20:54:20 -0600 | [diff] [blame] | 7 | * Copyright (c) 1998, Glenn Randers-Pehrson |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 8 | * February 7, 1998 |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 9 | * |
| 10 | * This file provides a location for all memory allocation. Users which |
| 11 | * need special memory handling are expected to modify the code in this file |
| 12 | * to meet their needs. See the instructions at each function. |
| 13 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 14 | |
| 15 | #define PNG_INTERNAL |
| 16 | #include "png.h" |
| 17 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 18 | /* The following "hides" PNG_MALLOC and PNG_FREE thus allowing the pngtest |
| 19 | application to put a wrapper on top of them. */ |
| 20 | #ifdef PNGTEST_MEMORY_DEBUG |
| 21 | #define PNG_MALLOC png_debug_malloc |
| 22 | #define PNG_FREE png_debug_free |
| 23 | #else |
| 24 | #define PNG_MALLOC png_malloc |
| 25 | #define PNG_FREE png_free |
| 26 | #endif |
| 27 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 28 | /* Borland DOS special memory handler */ |
| 29 | #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) |
| 30 | /* if you change this, be sure to change the one in png.h also */ |
| 31 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 32 | /* Allocate memory for a png_struct. The malloc and memset can be replaced |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 33 | by a single call to calloc() if this is thought to improve performance. */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 34 | png_voidp |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 35 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 36 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 37 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 38 | png_voidp struct_ptr; |
| 39 | |
| 40 | if (type == PNG_STRUCT_INFO) |
| 41 | size = sizeof(png_info); |
| 42 | else if (type == PNG_STRUCT_PNG) |
| 43 | size = sizeof(png_struct); |
| 44 | else |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 45 | return ((png_voidp)NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 46 | |
| 47 | if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL) |
| 48 | { |
| 49 | png_memset(struct_ptr, 0, size); |
| 50 | } |
| 51 | |
| 52 | return (struct_ptr); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* Free memory allocated by a png_create_struct() call */ |
| 57 | void |
| 58 | png_destroy_struct(png_voidp struct_ptr) |
| 59 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 60 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 61 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 62 | farfree (struct_ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 63 | struct_ptr = NULL; |
| 64 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 67 | /* Allocate memory. For reasonable files, size should never exceed |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 68 | * 64K. However, zlib may allocate more then 64K if you don't tell |
| 69 | * it not to. See zconf.h and png.h for more information. zlib does |
| 70 | * need to allocate exactly 64K, so whatever you call here must |
| 71 | * have the ability to do that. |
| 72 | * |
| 73 | * Borland seems to have a problem in DOS mode for exactly 64K. |
| 74 | * It gives you a segment with an offset of 8 (perhaps to store it's |
| 75 | * memory stuff). zlib doesn't like this at all, so we have to |
| 76 | * detect and deal with it. This code should not be needed in |
| 77 | * Windows or OS/2 modes, and only in 16 bit mode. This code has |
| 78 | * been updated by Alexander Lehmann for version 0.89 to waste less |
| 79 | * memory. |
| 80 | * |
| 81 | * Note that we can't use png_size_t for the "size" declaration, |
| 82 | * since on some systems a png_size_t is a 16-bit quantity, and as a |
| 83 | * result, we would be truncating potentially larger memory requests |
| 84 | * (which should cause a fatal error) and introducing major problems. |
| 85 | */ |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 86 | png_voidp |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 87 | PNG_MALLOC(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 88 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 89 | png_voidp ret; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 90 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 91 | return ((png_voidp)NULL); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 92 | |
| 93 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 94 | if (size > (png_uint_32)65536L) |
| 95 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 96 | #endif |
| 97 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 98 | if (size == (png_uint_32)65536L) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 99 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 100 | if (png_ptr->offset_table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 101 | { |
| 102 | /* try to see if we need to do any of this fancy stuff */ |
| 103 | ret = farmalloc(size); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 104 | if (ret == NULL || ((png_size_t)ret & 0xffff)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 105 | { |
| 106 | int num_blocks; |
| 107 | png_uint_32 total_size; |
| 108 | png_bytep table; |
| 109 | int i; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 110 | png_byte huge * hptr; |
| 111 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 112 | if (ret != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 113 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 114 | farfree(ret); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 115 | ret = NULL; |
| 116 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 117 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 118 | num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); |
| 119 | if (num_blocks < 1) |
| 120 | num_blocks = 1; |
| 121 | if (png_ptr->zlib_mem_level >= 7) |
| 122 | num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); |
| 123 | else |
| 124 | num_blocks++; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 125 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 126 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 127 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 128 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 129 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 130 | if (table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 131 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 132 | png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 133 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 134 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 135 | if ((png_size_t)table & 0xfff0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 136 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 137 | png_error(png_ptr, "Farmalloc didn't return normalized pointer"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 138 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 139 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 140 | png_ptr->offset_table = table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 141 | png_ptr->offset_table_ptr = farmalloc(num_blocks * |
| 142 | sizeof (png_bytep)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 143 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 144 | if (png_ptr->offset_table_ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 145 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 146 | png_error(png_ptr, "Out Of memory."); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | hptr = (png_byte huge *)table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 150 | if ((png_size_t)hptr & 0xf) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 151 | { |
| 152 | hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); |
| 153 | hptr += 16L; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 154 | } |
| 155 | for (i = 0; i < num_blocks; i++) |
| 156 | { |
| 157 | png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 158 | hptr += (png_uint_32)65536L; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 159 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 160 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 161 | png_ptr->offset_table_number = num_blocks; |
| 162 | png_ptr->offset_table_count = 0; |
| 163 | png_ptr->offset_table_count_free = 0; |
| 164 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 165 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 166 | |
| 167 | if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 168 | png_error(png_ptr, "Out of Memory."); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 169 | |
| 170 | ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 171 | } |
| 172 | else |
| 173 | ret = farmalloc(size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 174 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 175 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 176 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 177 | png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 180 | return (ret); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 183 | /* free a pointer allocated by PNG_MALLOC(). In the default |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 184 | configuration, png_ptr is not used, but is passed in case it |
| 185 | is needed. If ptr is NULL, return without taking any action. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 186 | void |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 187 | PNG_FREE(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 188 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 189 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 190 | return; |
| 191 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 192 | if (png_ptr->offset_table != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 193 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 194 | int i; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 195 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 196 | for (i = 0; i < png_ptr->offset_table_count; i++) |
| 197 | { |
| 198 | if (ptr == png_ptr->offset_table_ptr[i]) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 199 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 200 | ptr = NULL; |
| 201 | png_ptr->offset_table_count_free++; |
| 202 | break; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 203 | } |
| 204 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 205 | if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) |
| 206 | { |
| 207 | farfree(png_ptr->offset_table); |
| 208 | farfree(png_ptr->offset_table_ptr); |
| 209 | png_ptr->offset_table = NULL; |
| 210 | png_ptr->offset_table_ptr = NULL; |
| 211 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 212 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 213 | |
| 214 | if (ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 215 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 216 | farfree(ptr); |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 217 | ptr = NULL; |
| 218 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | #else /* Not the Borland DOS special memory handler */ |
| 222 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 223 | /* Allocate memory for a png_struct or a png_info. The malloc and |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 224 | memset can be replaced by a single call to calloc() if this is thought |
| 225 | to improve performance noticably.*/ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 226 | png_voidp |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 227 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 228 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 229 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 230 | png_voidp struct_ptr; |
| 231 | |
| 232 | if (type == PNG_STRUCT_INFO) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 233 | size = sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 234 | else if (type == PNG_STRUCT_PNG) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 235 | size = sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 236 | else |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 237 | return ((png_voidp)NULL); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 238 | |
| 239 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 240 | if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL) |
| 241 | #else |
| 242 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 243 | if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 244 | # else |
| 245 | if ((struct_ptr = (png_voidp)malloc(size)) != NULL) |
| 246 | # endif |
| 247 | #endif |
| 248 | { |
| 249 | png_memset(struct_ptr, 0, size); |
| 250 | } |
| 251 | |
| 252 | return (struct_ptr); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /* Free memory allocated by a png_create_struct() call */ |
| 257 | void |
| 258 | png_destroy_struct(png_voidp struct_ptr) |
| 259 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 260 | if (struct_ptr != NULL) |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 261 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 262 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 263 | farfree(struct_ptr); |
| 264 | #else |
| 265 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 266 | hfree(struct_ptr); |
| 267 | # else |
| 268 | free(struct_ptr); |
| 269 | # endif |
| 270 | #endif |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 271 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 275 | /* Allocate memory. For reasonable files, size should never exceed |
| 276 | 64K. However, zlib may allocate more then 64K if you don't tell |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 277 | it not to. See zconf.h and png.h for more information. zlib does |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 278 | need to allocate exactly 64K, so whatever you call here must |
| 279 | have the ability to do that. */ |
| 280 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 281 | png_voidp |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 282 | PNG_MALLOC(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 283 | { |
| 284 | png_voidp ret; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 285 | png_size_t length; |
| 286 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 287 | if (png_ptr == NULL || size == 0) |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 288 | return ((png_voidp)NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 289 | |
| 290 | #ifdef PNG_MAX_MALLOC_64K |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 291 | if (png_size > (png_uint_32)65536L) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 292 | png_error(png_ptr, "Cannot Allocate > 64K"); |
| 293 | #endif |
| 294 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 295 | length = (png_size_t)size; |
| 296 | if ((png_uint_32)length != size) |
| 297 | png_error(png_ptr, "Cannot Allocate > size_t"); |
| 298 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 299 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 300 | ret = farmalloc(length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 301 | #else |
| 302 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 303 | ret = halloc(length, 1); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 304 | # else |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 305 | ret = malloc(length); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 306 | # endif |
| 307 | #endif |
| 308 | |
| 309 | if (ret == NULL) |
| 310 | { |
| 311 | png_error(png_ptr, "Out of Memory"); |
| 312 | } |
| 313 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 314 | return (ret); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 315 | } |
| 316 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 317 | /* Free a pointer allocated by PNG_MALLOC(). In the default |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 318 | configuration, png_ptr is not used, but is passed in case it |
| 319 | is needed. If ptr is NULL, return without taking any action. */ |
| 320 | void |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 321 | PNG_FREE(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 322 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 323 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 324 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 325 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 326 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 327 | farfree(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 328 | #else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 329 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 330 | hfree(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 331 | # else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 332 | free(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 333 | # endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 334 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 335 | } |
| 336 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 337 | #endif /* Not Borland DOS special memory handler */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 338 | |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame^] | 339 | void |
| 340 | png_buffered_memcpy (png_structp png_ptr, png_voidp s1, png_voidp s2, |
| 341 | png_uint_32 length) |
| 342 | { |
| 343 | png_size_t size; |
| 344 | png_uint_32 i; |
| 345 | for (i=0; i<length; i+=65530) |
| 346 | { |
| 347 | size = (png_size_t)(length - i); |
| 348 | if (size > (png_size_t)65530L) |
| 349 | size=(png_size_t)65530L; |
| 350 | png_memcpy (s1, s2, size); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void |
| 355 | png_buffered_memset (png_structp png_ptr, png_voidp s1, int value, |
| 356 | png_uint_32 length) |
| 357 | { |
| 358 | png_size_t size; |
| 359 | png_uint_32 i; |
| 360 | for (i=0; i<length; i+=65530) |
| 361 | { |
| 362 | size = length - i; |
| 363 | if (size > 65530) |
| 364 | size=65530; |
| 365 | png_memset (s1, value, size); |
| 366 | } |
| 367 | } |