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 | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame^] | 3 | * libpng 0.98 |
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 | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame^] | 8 | * January 16, 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 |
| 45 | return (png_voidp)NULL; |
| 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) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 61 | farfree (struct_ptr); |
| 62 | } |
| 63 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 64 | /* Allocate memory. For reasonable files, size should never exceed |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 65 | * 64K. However, zlib may allocate more then 64K if you don't tell |
| 66 | * it not to. See zconf.h and png.h for more information. zlib does |
| 67 | * need to allocate exactly 64K, so whatever you call here must |
| 68 | * have the ability to do that. |
| 69 | * |
| 70 | * Borland seems to have a problem in DOS mode for exactly 64K. |
| 71 | * It gives you a segment with an offset of 8 (perhaps to store it's |
| 72 | * memory stuff). zlib doesn't like this at all, so we have to |
| 73 | * detect and deal with it. This code should not be needed in |
| 74 | * Windows or OS/2 modes, and only in 16 bit mode. This code has |
| 75 | * been updated by Alexander Lehmann for version 0.89 to waste less |
| 76 | * memory. |
| 77 | * |
| 78 | * Note that we can't use png_size_t for the "size" declaration, |
| 79 | * since on some systems a png_size_t is a 16-bit quantity, and as a |
| 80 | * result, we would be truncating potentially larger memory requests |
| 81 | * (which should cause a fatal error) and introducing major problems. |
| 82 | */ |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 83 | png_voidp |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame^] | 84 | PNG_MALLOC(png_structp png_ptr, png_uint_32 size) |
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 | png_voidp ret; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 87 | if (png_ptr == NULL || size == 0) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 88 | return ((voidp)NULL); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 89 | |
| 90 | #ifdef PNG_MAX_MALLOC_64K |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 91 | if (size > (png_uint_32)65536L) |
| 92 | png_error(png_ptr, "Cannot Allocate > 64K"); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 93 | #endif |
| 94 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 95 | if (size == (png_uint_32)65536L) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 96 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 97 | if (png_ptr->offset_table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 98 | { |
| 99 | /* try to see if we need to do any of this fancy stuff */ |
| 100 | ret = farmalloc(size); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 101 | if (ret == NULL || ((png_size_t)ret & 0xffff)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 102 | { |
| 103 | int num_blocks; |
| 104 | png_uint_32 total_size; |
| 105 | png_bytep table; |
| 106 | int i; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 107 | png_byte huge * hptr; |
| 108 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 109 | if (ret != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 110 | farfree(ret); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 111 | ret = NULL; |
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 | num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14)); |
| 114 | if (num_blocks < 1) |
| 115 | num_blocks = 1; |
| 116 | if (png_ptr->zlib_mem_level >= 7) |
| 117 | num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7)); |
| 118 | else |
| 119 | num_blocks++; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 120 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 121 | total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16; |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 122 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 123 | table = farmalloc(total_size); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 124 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 125 | if (table == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 126 | { |
| 127 | png_error(png_ptr, "Out of Memory"); |
| 128 | } |
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 ((png_size_t)table & 0xfff0) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 131 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 132 | png_error(png_ptr, "Farmalloc didn't return normalized pointer"); |
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 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 135 | png_ptr->offset_table = table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 136 | png_ptr->offset_table_ptr = farmalloc(num_blocks * |
| 137 | sizeof (png_bytep)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 138 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 139 | if (png_ptr->offset_table_ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 140 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 141 | png_error(png_ptr, "Out of memory"); |
| 142 | } |
| 143 | |
| 144 | hptr = (png_byte huge *)table; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 145 | if ((png_size_t)hptr & 0xf) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 146 | { |
| 147 | hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L); |
| 148 | hptr += 16L; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 149 | } |
| 150 | for (i = 0; i < num_blocks; i++) |
| 151 | { |
| 152 | png_ptr->offset_table_ptr[i] = (png_bytep)hptr; |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 153 | hptr += (png_uint_32)65536L; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 154 | } |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 155 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 156 | png_ptr->offset_table_number = num_blocks; |
| 157 | png_ptr->offset_table_count = 0; |
| 158 | png_ptr->offset_table_count_free = 0; |
| 159 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 160 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 161 | |
| 162 | if (png_ptr->offset_table_count >= png_ptr->offset_table_number) |
| 163 | png_error(png_ptr, "Out of Memory"); |
| 164 | |
| 165 | ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++]; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 166 | } |
| 167 | else |
| 168 | ret = farmalloc(size); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 169 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 170 | if (ret == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 171 | { |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 172 | png_error(png_ptr, "Out of Memory"); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 175 | return ret; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame^] | 178 | /* free a pointer allocated by PNG_MALLOC(). In the default |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 179 | configuration, png_ptr is not used, but is passed in case it |
| 180 | is needed. If ptr is NULL, return without taking any action. */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 181 | void |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame^] | 182 | PNG_FREE(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 183 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 184 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 185 | return; |
| 186 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 187 | if (png_ptr->offset_table != NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 188 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 189 | int i; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 190 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 191 | for (i = 0; i < png_ptr->offset_table_count; i++) |
| 192 | { |
| 193 | if (ptr == png_ptr->offset_table_ptr[i]) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 194 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 195 | ptr = NULL; |
| 196 | png_ptr->offset_table_count_free++; |
| 197 | break; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 198 | } |
| 199 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 200 | if (png_ptr->offset_table_count_free == png_ptr->offset_table_count) |
| 201 | { |
| 202 | farfree(png_ptr->offset_table); |
| 203 | farfree(png_ptr->offset_table_ptr); |
| 204 | png_ptr->offset_table = NULL; |
| 205 | png_ptr->offset_table_ptr = NULL; |
| 206 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 207 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 208 | |
| 209 | if (ptr != NULL) |
| 210 | farfree(ptr); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | #else /* Not the Borland DOS special memory handler */ |
| 214 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 215 | /* 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] | 216 | memset can be replaced by a single call to calloc() if this is thought |
| 217 | to improve performance noticably.*/ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 218 | png_voidp |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 219 | png_create_struct(int type) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 220 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 221 | png_size_t size; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 222 | png_voidp struct_ptr; |
| 223 | |
| 224 | if (type == PNG_STRUCT_INFO) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 225 | size = sizeof(png_info); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 226 | else if (type == PNG_STRUCT_PNG) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 227 | size = sizeof(png_struct); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 228 | else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 229 | return (png_voidp)NULL; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 230 | |
| 231 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 232 | if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL) |
| 233 | #else |
| 234 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Guy Schalnat | c21f90c | 1996-06-17 16:24:45 -0500 | [diff] [blame] | 235 | if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 236 | # else |
| 237 | if ((struct_ptr = (png_voidp)malloc(size)) != NULL) |
| 238 | # endif |
| 239 | #endif |
| 240 | { |
| 241 | png_memset(struct_ptr, 0, size); |
| 242 | } |
| 243 | |
| 244 | return (struct_ptr); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | /* Free memory allocated by a png_create_struct() call */ |
| 249 | void |
| 250 | png_destroy_struct(png_voidp struct_ptr) |
| 251 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 252 | if (struct_ptr != NULL) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 253 | #if defined(__TURBOC__) && !defined(__FLAT__) |
| 254 | farfree(struct_ptr); |
| 255 | #else |
| 256 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 257 | hfree(struct_ptr); |
| 258 | # else |
| 259 | free(struct_ptr); |
| 260 | # endif |
| 261 | #endif |
| 262 | } |
| 263 | |
| 264 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 265 | /* Allocate memory. For reasonable files, size should never exceed |
| 266 | 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] | 267 | 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] | 268 | need to allocate exactly 64K, so whatever you call here must |
| 269 | have the ability to do that. */ |
| 270 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 271 | png_voidp |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame^] | 272 | PNG_MALLOC(png_structp png_ptr, png_uint_32 size) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 273 | { |
| 274 | png_voidp ret; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 275 | if (png_ptr == NULL || size == 0) |
| 276 | return (NULL); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 277 | |
| 278 | #ifdef PNG_MAX_MALLOC_64K |
| 279 | if (size > (png_uint_32)65536L) |
| 280 | png_error(png_ptr, "Cannot Allocate > 64K"); |
| 281 | #endif |
| 282 | |
| 283 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 284 | ret = farmalloc((png_size_t)size); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 285 | #else |
| 286 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
| 287 | ret = halloc(size, 1); |
| 288 | # else |
| 289 | ret = malloc(size); |
| 290 | # endif |
| 291 | #endif |
| 292 | |
| 293 | if (ret == NULL) |
| 294 | { |
| 295 | png_error(png_ptr, "Out of Memory"); |
| 296 | } |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame^] | 301 | /* Free a pointer allocated by PNG_MALLOC(). In the default |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 302 | configuration, png_ptr is not used, but is passed in case it |
| 303 | is needed. If ptr is NULL, return without taking any action. */ |
| 304 | void |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame^] | 305 | PNG_FREE(png_structp png_ptr, png_voidp ptr) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 306 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 307 | if (png_ptr == NULL || ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 308 | return; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 309 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 310 | #if defined(__TURBOC__) && !defined(__FLAT__) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 311 | farfree(ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 312 | #else |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 313 | # if defined(_MSC_VER) && defined(MAXSEG_64K) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 314 | hfree(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 315 | # else |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 316 | free(ptr); |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 317 | # endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 318 | #endif |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 319 | } |
| 320 | |
Guy Schalnat | 4ee97b0 | 1996-01-16 01:51:56 -0600 | [diff] [blame] | 321 | #endif /* Not Borland DOS special memory handler */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 322 | |