Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1 | |
Guy Schalnat | 0f71645 | 1995-11-28 11:22:13 -0600 | [diff] [blame] | 2 | /* pngmem.c - stub functions for memory allocation |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 3 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 4 | libpng 1.0 beta 3 - version 0.89 |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 5 | For conditions of distribution and use, see copyright notice in png.h |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 6 | Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 7 | May 25, 1996 |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 8 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 9 | This file provides a location for all memory allocation. Users which |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 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. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 12 | |
| 13 | #define PNG_INTERNAL |
| 14 | #include "png.h" |
| 15 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 16 | /* 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 Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 20 | /* 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 | */ |
| 23 | png_voidp |
| 24 | png_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 */ |
| 46 | void |
| 47 | png_destroy_struct(png_voidp struct_ptr) |
| 48 | { |
| 49 | if (struct_ptr) |
| 50 | farfree (struct_ptr); |
| 51 | } |
| 52 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 53 | /* Allocate memory. For reasonable files, size should never exceed |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 54 | 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 Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 58 | |
| 59 | /* Borland seems to have a problem in DOS mode for exactly 64K. |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 60 | 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 Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 63 | 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 Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 66 | */ |
| 67 | |
| 68 | png_voidp |
| 69 | png_large_malloc(png_structp png_ptr, png_uint_32 size) |
| 70 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 71 | png_voidp ret; |
| 72 | if (!png_ptr || !size) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 73 | return ((voidp)NULL); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 74 | |
| 75 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 76 | if (size > (png_uint_32)65536L) |
| 77 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 78 | #endif |
| 79 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 80 | 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 Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 92 | png_byte huge * hptr; |
| 93 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 94 | if (ret) |
| 95 | farfree(ret); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 96 | ret = NULL; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 97 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 98 | 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 Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 105 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 106 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 107 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 108 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 109 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 110 | if (!table) |
| 111 | { |
| 112 | png_error(png_ptr, "Out of Memory"); |
| 113 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 114 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 115 | if ((long)table & 0xfff0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 116 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 117 | png_error(png_ptr, "Farmalloc didn't return normalized pointer"); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 118 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 119 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 120 | png_ptr->offset_table = table; |
| 121 | png_ptr->offset_table_ptr = farmalloc( |
| 122 | num_blocks * sizeof (png_bytep)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 123 | |
| 124 | if (!png_ptr->offset_table_ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 125 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 126 | 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 Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 134 | } |
| 135 | for (i = 0; i < num_blocks; i++) |
| 136 | { |
| 137 | png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
| 138 | hptr += 65536L; |
| 139 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 140 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 141 | png_ptr->offset_table_number = num_blocks; |
| 142 | png_ptr->offset_table_count = 0; |
| 143 | png_ptr->offset_table_count_free = 0; |
| 144 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 145 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 146 | |
| 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 Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 151 | } |
| 152 | else |
| 153 | ret = farmalloc(size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 154 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 155 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 156 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 157 | png_error(png_ptr, "Out of Memory"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 160 | return ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 161 | } |
| 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. */ |
| 166 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 167 | png_large_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 168 | { |
| 169 | if (!png_ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 170 | 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 Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame^] | 203 | /* 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 | */ |
| 207 | png_voidp |
| 208 | png_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) |
| 224 | if ((struct_ptr = (png_voidp)halloc(size)) != NULL) |
| 225 | # 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 */ |
| 238 | void |
| 239 | png_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 Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 254 | /* 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 Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 260 | png_voidp |
| 261 | png_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. */ |
| 293 | void |
| 294 | png_large_free(png_structp png_ptr, png_voidp ptr) |
| 295 | { |
| 296 | if (!png_ptr) |
| 297 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 298 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 299 | if (ptr != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 300 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 301 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 302 | farfree(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 303 | #else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 304 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 305 | hfree(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 306 | # else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 307 | free(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 308 | # endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 309 | #endif |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 310 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 311 | } |
| 312 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 313 | #endif /* Not Borland DOS special memory handler */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 314 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 315 | /* Allocate memory. This is called for smallish blocks only It |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 316 | 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 Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 320 | void * |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 321 | png_malloc(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 322 | { |
| 323 | void *ret; |
| 324 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 325 | if (!png_ptr || !size) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 326 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 327 | return ((void *)0); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 328 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 329 | |
| 330 | #ifdef PNG_MAX_MALLOC_64K |
| 331 | if (size > (png_uint_32)65536L) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 332 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 333 | #endif |
| 334 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 335 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 336 | ret = malloc((png_size_t)size); |
| 337 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 338 | if (!ret) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 339 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 340 | png_error(png_ptr, "Out of Memory"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | /* Reallocate memory. This will not get near 64K on a |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 347 | even marginally reasonable file. This is not used in |
| 348 | the current version of the library. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 349 | void * |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 350 | png_realloc(png_structp png_ptr, void * ptr, png_uint_32 size, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 351 | png_uint_32 old_size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 352 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 353 | void *ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 354 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 355 | if (!png_ptr || !old_size || !ptr || !size) |
| 356 | return ((void *)0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 357 | |
| 358 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 359 | if (size > (png_uint_32)65536L) |
| 360 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 361 | #endif |
| 362 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 363 | ret = realloc(ptr, (png_size_t)size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 364 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 365 | if (!ret) |
| 366 | { |
| 367 | png_error(png_ptr, "Out of Memory 7"); |
| 368 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 369 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 370 | return ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 371 | } |
| 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. */ |
| 376 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 377 | png_free(png_structp png_ptr, void * ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 378 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 379 | if (!png_ptr) |
| 380 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 381 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 382 | if (ptr != (void *)0) |
| 383 | free(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 384 | } |
| 385 | |