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 | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 4 | libpng 1.0 beta 2 - version 0.87 |
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 | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 6 | Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 7 | January 15, 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 | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 10 | need special memory handling are expected to modify the code in this file |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 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 | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 20 | /* Allocate memory. For reasonable files, size should never exceed |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 21 | 64K. However, zlib may allocate more then 64K if you don't tell |
| 22 | it not to. See zconf.h and png.h for more information. zlib does |
| 23 | need to allocate exactly 64K, so whatever you call here must |
| 24 | have the ability to do that. */ |
| 25 | |
| 26 | /* Borland seems to have a problem in DOS mode for exactly 64K. |
| 27 | It gives you a segment with an offset of 8 (perhaps to store it's |
| 28 | memory stuff). zlib doesn't like this at all, so we have to |
| 29 | detect and deal with it. This code should not be needed in |
| 30 | Windows or OS/2 modes, and only in 16 bit mode. |
| 31 | */ |
| 32 | |
| 33 | png_voidp |
| 34 | png_large_malloc(png_structp png_ptr, png_uint_32 size) |
| 35 | { |
| 36 | png_voidp ret; |
| 37 | if (!png_ptr || !size) |
| 38 | return ((voidp)0); |
| 39 | |
| 40 | #ifdef PNG_MAX_MALLOC_64K |
| 41 | if (size > (png_uint_32)65536L) |
| 42 | png_error(png_ptr, "Cannot Allocate > 64K"); |
| 43 | #endif |
| 44 | |
| 45 | if (size == (png_uint_32)(65536L)) |
| 46 | { |
| 47 | if (!png_ptr->offset_table) |
| 48 | { |
| 49 | /* try to see if we need to do any of this fancy stuff */ |
| 50 | ret = farmalloc(size); |
| 51 | if (!ret || ((long)ret & 0xffff)) |
| 52 | { |
| 53 | int num_blocks; |
| 54 | png_uint_32 total_size; |
| 55 | png_bytep table; |
| 56 | int i; |
| 57 | png_byte huge * hptr; |
| 58 | |
| 59 | if (ret) |
| 60 | farfree(ret); |
| 61 | ret = 0; |
| 62 | |
| 63 | num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); |
| 64 | if (num_blocks < 1) |
| 65 | num_blocks = 1; |
| 66 | if (png_ptr->zlib_mem_level >= 7) |
| 67 | num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); |
| 68 | else |
| 69 | num_blocks++; |
| 70 | |
| 71 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks; |
| 72 | |
| 73 | table = farmalloc(total_size); |
| 74 | |
| 75 | if (!table) |
| 76 | { |
| 77 | png_error(png_ptr, "Out of Memory"); |
| 78 | } |
| 79 | |
| 80 | if ((long)table & 0xffff) |
| 81 | { |
| 82 | farfree(table); |
| 83 | total_size += (png_uint_32)65536L; |
| 84 | } |
| 85 | |
| 86 | table = farmalloc(total_size); |
| 87 | |
| 88 | if (!table) |
| 89 | { |
| 90 | png_error(png_ptr, "Out of Memory"); |
| 91 | } |
| 92 | |
| 93 | png_ptr->offset_table = table; |
| 94 | png_ptr->offset_table_ptr = farmalloc( |
| 95 | num_blocks * sizeof (png_bytep)); |
| 96 | hptr = (png_byte huge *)table; |
| 97 | if ((long)hptr & 0xffff) |
| 98 | { |
| 99 | hptr = (png_byte huge *)((long)(hptr) & 0xffff0000L); |
| 100 | hptr += 65536L; |
| 101 | } |
| 102 | for (i = 0; i < num_blocks; i++) |
| 103 | { |
| 104 | png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
| 105 | hptr += 65536L; |
| 106 | } |
| 107 | |
| 108 | png_ptr->offset_table_number = num_blocks; |
| 109 | png_ptr->offset_table_count = 0; |
| 110 | png_ptr->offset_table_count_free = 0; |
| 111 | } |
| 112 | |
| 113 | if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
| 114 | png_error(png_ptr, "Out of Memory"); |
| 115 | |
| 116 | ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
| 117 | } |
| 118 | } |
| 119 | else |
| 120 | ret = farmalloc(size); |
| 121 | |
| 122 | if (ret == NULL) |
| 123 | { |
| 124 | png_error(png_ptr, "Out of Memory"); |
| 125 | } |
| 126 | |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | /* free a pointer allocated by png_large_malloc(). In the default |
| 131 | configuration, png_ptr is not used, but is passed in case it |
| 132 | is needed. If ptr is NULL, return without taking any action. */ |
| 133 | void |
| 134 | png_large_free(png_structp png_ptr, png_voidp ptr) |
| 135 | { |
| 136 | if (!png_ptr) |
| 137 | return; |
| 138 | |
| 139 | if (ptr != NULL) |
| 140 | { |
| 141 | if (png_ptr->offset_table) |
| 142 | { |
| 143 | int i; |
| 144 | |
| 145 | for (i = 0; i < png_ptr->offset_table_count; i++) |
| 146 | { |
| 147 | if (ptr == png_ptr->offset_table_ptr[i]) |
| 148 | { |
| 149 | ptr = 0; |
| 150 | png_ptr->offset_table_count_free++; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) |
| 155 | { |
| 156 | farfree(png_ptr->offset_table); |
| 157 | farfree(png_ptr->offset_table_ptr); |
| 158 | png_ptr->offset_table = 0; |
| 159 | png_ptr->offset_table_ptr = 0; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (ptr) |
| 164 | farfree(ptr); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | #else /* Not the Borland DOS special memory handler */ |
| 169 | |
| 170 | /* Allocate memory. For reasonable files, size should never exceed |
| 171 | 64K. However, zlib may allocate more then 64K if you don't tell |
| 172 | it not to. See zconf.h and png.h for more information. zlib does |
| 173 | need to allocate exactly 64K, so whatever you call here must |
| 174 | have the ability to do that. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 175 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 176 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 177 | png_voidp |
| 178 | png_large_malloc(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 179 | { |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 180 | png_voidp ret; |
| 181 | if (!png_ptr || !size) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 182 | return ((voidp)0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 183 | |
| 184 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 185 | if (size > (png_uint_32)65536L) |
| 186 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 187 | #endif |
| 188 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 189 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 190 | ret = farmalloc(size); |
| 191 | #else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 192 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 193 | ret = halloc(size, 1); |
| 194 | # else |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 195 | ret = malloc(size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 196 | # endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 197 | #endif |
| 198 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 199 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 200 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 201 | png_error(png_ptr, "Out of Memory"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 202 | } |
| 203 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 204 | return ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* free a pointer allocated by png_large_malloc(). In the default |
| 208 | configuration, png_ptr is not used, but is passed in case it |
| 209 | is needed. If ptr is NULL, return without taking any action. */ |
| 210 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 211 | png_large_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 212 | { |
| 213 | if (!png_ptr) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 214 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 215 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 216 | if (ptr != NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 217 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 218 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 219 | farfree(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 220 | #else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 221 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 222 | hfree(ptr); |
| 223 | # else |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 224 | free(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 225 | # endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 226 | #endif |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 227 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 230 | #endif /* Not Borland DOS special memory handler */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 231 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 232 | /* Allocate memory. This is called for smallish blocks only It |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 233 | should not get anywhere near 64K. On segmented machines, this |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 234 | must come from the local heap (for zlib). Currently, zlib is |
| 235 | the only one that uses this, so you should only get one call |
| 236 | to this, and that a small block. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 237 | void * |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 238 | png_malloc(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 239 | { |
| 240 | void *ret; |
| 241 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 242 | if (!png_ptr || !size) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 243 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 244 | return ((void *)0); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 245 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 246 | |
| 247 | #ifdef PNG_MAX_MALLOC_64K |
| 248 | if (size > (png_uint_32)65536L) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 249 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 250 | #endif |
| 251 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 252 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 253 | ret = malloc((png_size_t)size); |
| 254 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 255 | if (!ret) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 256 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 257 | png_error(png_ptr, "Out of Memory"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | /* Reallocate memory. This will not get near 64K on a |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame^] | 264 | even marginally reasonable file. This is not used in |
| 265 | the current version of the library. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 266 | void * |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 267 | png_realloc(png_structp png_ptr, void * ptr, png_uint_32 size, |
| 268 | png_uint_32 old_size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 269 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 270 | void *ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 271 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 272 | if (!png_ptr || !old_size || !ptr || !size) |
| 273 | return ((void *)0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 274 | |
| 275 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 276 | if (size > (png_uint_32)65536L) |
| 277 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 278 | #endif |
| 279 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 280 | ret = realloc(ptr, (png_size_t)size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 281 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 282 | if (!ret) |
| 283 | { |
| 284 | png_error(png_ptr, "Out of Memory 7"); |
| 285 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 286 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 287 | return ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* free a pointer allocated by png_malloc(). In the default |
| 291 | configuration, png_ptr is not used, but is passed incase it |
| 292 | is needed. If ptr is NULL, return without taking any action. */ |
| 293 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 294 | png_free(png_structp png_ptr, void * ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 295 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 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 != (void *)0) |
| 300 | free(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 301 | } |
| 302 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 303 | |