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 | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 4 | libpng 1.0 beta 2 - version 0.88 |
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. |
| 7 | January 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 | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 20 | /* Allocate memory. For reasonable files, size should never exceed |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -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. */ |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 25 | |
| 26 | /* Borland seems to have a problem in DOS mode for exactly 64K. |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 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 |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 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 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 36 | png_voidp ret; |
| 37 | if (!png_ptr || !size) |
| 38 | return ((voidp)0); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 39 | |
| 40 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 41 | if (size > (png_uint_32)65536L) |
| 42 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 43 | #endif |
| 44 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 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; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 57 | png_byte huge * hptr; |
| 58 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 59 | if (ret) |
| 60 | farfree(ret); |
| 61 | ret = 0; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 62 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 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++; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 70 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 71 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 72 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 73 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 74 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 75 | if (!table) |
| 76 | { |
| 77 | png_error(png_ptr, "Out of Memory"); |
| 78 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 79 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 80 | if ((long)table & 0xffff) |
| 81 | { |
| 82 | farfree(table); |
| 83 | total_size += (png_uint_32)65536L; |
| 84 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 85 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 86 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 87 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 88 | if (!table) |
| 89 | { |
| 90 | png_error(png_ptr, "Out of Memory"); |
| 91 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 92 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 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 | } |
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 | png_ptr->offset_table_number = num_blocks; |
| 109 | png_ptr->offset_table_count = 0; |
| 110 | png_ptr->offset_table_count_free = 0; |
| 111 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 112 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 113 | if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
| 114 | png_error(png_ptr, "Out of Memory"); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 115 | |
| 116 | ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 117 | } |
| 118 | } |
| 119 | else |
| 120 | ret = farmalloc(size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 121 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 122 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 123 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 124 | png_error(png_ptr, "Out of Memory"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 127 | return ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 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 |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 134 | png_large_free(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 135 | { |
| 136 | if (!png_ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 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. */ |
| 175 | |
| 176 | |
| 177 | png_voidp |
| 178 | png_large_malloc(png_structp png_ptr, png_uint_32 size) |
| 179 | { |
| 180 | png_voidp ret; |
| 181 | if (!png_ptr || !size) |
| 182 | return ((voidp)0); |
| 183 | |
| 184 | #ifdef PNG_MAX_MALLOC_64K |
| 185 | if (size > (png_uint_32)65536L) |
| 186 | png_error(png_ptr, "Cannot Allocate > 64K"); |
| 187 | #endif |
| 188 | |
| 189 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 190 | ret = farmalloc(size); |
| 191 | #else |
| 192 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 193 | ret = halloc(size, 1); |
| 194 | # else |
| 195 | ret = malloc(size); |
| 196 | # endif |
| 197 | #endif |
| 198 | |
| 199 | if (ret == NULL) |
| 200 | { |
| 201 | png_error(png_ptr, "Out of Memory"); |
| 202 | } |
| 203 | |
| 204 | return ret; |
| 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 |
| 211 | png_large_free(png_structp png_ptr, png_voidp ptr) |
| 212 | { |
| 213 | if (!png_ptr) |
| 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__) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 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) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 222 | hfree(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 223 | # else |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -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 | b2e01bd | 1996-01-26 01:38:47 -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 | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 233 | should not get anywhere near 64K. On segmented machines, this |
| 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 | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 255 | if (!ret) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 256 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -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 | b2e01bd | 1996-01-26 01:38:47 -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, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 268 | png_uint_32 old_size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 269 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 270 | void *ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 271 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -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 | b2e01bd | 1996-01-26 01:38:47 -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 | b2e01bd | 1996-01-26 01:38:47 -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 | b2e01bd | 1996-01-26 01:38:47 -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 | b2e01bd | 1996-01-26 01:38:47 -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 | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame^] | 296 | if (!png_ptr) |
| 297 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 298 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -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 | |