pngmem.c
Go to the documentation of this file.
00001 
00002 /* pngmem.c - stub functions for memory allocation
00003  *
00004  * Last changed in libpng 1.2.30 [August 15, 2008]
00005  * For conditions of distribution and use, see copyright notice in png.h
00006  * Copyright (c) 1998-2008 Glenn Randers-Pehrson
00007  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
00008  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
00009  *
00010  * This file provides a location for all memory allocation.  Users who
00011  * need special memory handling are expected to supply replacement
00012  * functions for png_malloc() and png_free(), and to use
00013  * png_create_read_struct_2() and png_create_write_struct_2() to
00014  * identify the replacement functions.
00015  */
00016 
00017 #define PNG_INTERNAL
00018 #include "png.h"
00019 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
00020 
00021 /* Borland DOS special memory handler */
00022 #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
00023 /* if you change this, be sure to change the one in png.h also */
00024 
00025 /* Allocate memory for a png_struct.  The malloc and memset can be replaced
00026    by a single call to calloc() if this is thought to improve performance. */
00027 png_voidp /* PRIVATE */
00028 png_create_struct(int type)
00029 {
00030 #ifdef PNG_USER_MEM_SUPPORTED
00031    return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
00032 }
00033 
00034 /* Alternate version of png_create_struct, for use with user-defined malloc. */
00035 png_voidp /* PRIVATE */
00036 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
00037 {
00038 #endif /* PNG_USER_MEM_SUPPORTED */
00039    png_size_t size;
00040    png_voidp struct_ptr;
00041 
00042    if (type == PNG_STRUCT_INFO)
00043      size = png_sizeof(png_info);
00044    else if (type == PNG_STRUCT_PNG)
00045      size = png_sizeof(png_struct);
00046    else
00047      return (png_get_copyright(NULL));
00048 
00049 #ifdef PNG_USER_MEM_SUPPORTED
00050    if (malloc_fn != NULL)
00051    {
00052       png_struct dummy_struct;
00053       png_structp png_ptr = &dummy_struct;
00054       png_ptr->mem_ptr=mem_ptr;
00055       struct_ptr = (*(malloc_fn))(png_ptr, (png_uint_32)size);
00056    }
00057    else
00058 #endif /* PNG_USER_MEM_SUPPORTED */
00059       struct_ptr = (png_voidp)farmalloc(size);
00060    if (struct_ptr != NULL)
00061       png_memset(struct_ptr, 0, size);
00062    return (struct_ptr);
00063 }
00064 
00065 /* Free memory allocated by a png_create_struct() call */
00066 void /* PRIVATE */
00067 png_destroy_struct(png_voidp struct_ptr)
00068 {
00069 #ifdef PNG_USER_MEM_SUPPORTED
00070    png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
00071 }
00072 
00073 /* Free memory allocated by a png_create_struct() call */
00074 void /* PRIVATE */
00075 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
00076     png_voidp mem_ptr)
00077 {
00078 #endif
00079    if (struct_ptr != NULL)
00080    {
00081 #ifdef PNG_USER_MEM_SUPPORTED
00082       if (free_fn != NULL)
00083       {
00084          png_struct dummy_struct;
00085          png_structp png_ptr = &dummy_struct;
00086          png_ptr->mem_ptr=mem_ptr;
00087          (*(free_fn))(png_ptr, struct_ptr);
00088          return;
00089       }
00090 #endif /* PNG_USER_MEM_SUPPORTED */
00091       farfree (struct_ptr);
00092    }
00093 }
00094 
00095 /* Allocate memory.  For reasonable files, size should never exceed
00096  * 64K.  However, zlib may allocate more then 64K if you don't tell
00097  * it not to.  See zconf.h and png.h for more information. zlib does
00098  * need to allocate exactly 64K, so whatever you call here must
00099  * have the ability to do that.
00100  *
00101  * Borland seems to have a problem in DOS mode for exactly 64K.
00102  * It gives you a segment with an offset of 8 (perhaps to store its
00103  * memory stuff).  zlib doesn't like this at all, so we have to
00104  * detect and deal with it.  This code should not be needed in
00105  * Windows or OS/2 modes, and only in 16 bit mode.  This code has
00106  * been updated by Alexander Lehmann for version 0.89 to waste less
00107  * memory.
00108  *
00109  * Note that we can't use png_size_t for the "size" declaration,
00110  * since on some systems a png_size_t is a 16-bit quantity, and as a
00111  * result, we would be truncating potentially larger memory requests
00112  * (which should cause a fatal error) and introducing major problems.
00113  */
00114 
00115 png_voidp PNGAPI
00116 png_malloc(png_structp png_ptr, png_uint_32 size)
00117 {
00118    png_voidp ret;
00119 
00120    if (png_ptr == NULL || size == 0)
00121       return (NULL);
00122 
00123 #ifdef PNG_USER_MEM_SUPPORTED
00124    if (png_ptr->malloc_fn != NULL)
00125        ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
00126    else
00127        ret = (png_malloc_default(png_ptr, size));
00128    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00129        png_error(png_ptr, "Out of memory!");
00130    return (ret);
00131 }
00132 
00133 png_voidp PNGAPI
00134 png_malloc_default(png_structp png_ptr, png_uint_32 size)
00135 {
00136    png_voidp ret;
00137 #endif /* PNG_USER_MEM_SUPPORTED */
00138 
00139    if (png_ptr == NULL || size == 0)
00140       return (NULL);
00141 
00142 #ifdef PNG_MAX_MALLOC_64K
00143    if (size > (png_uint_32)65536L)
00144    {
00145       png_warning(png_ptr, "Cannot Allocate > 64K");
00146       ret = NULL;
00147    }
00148    else
00149 #endif
00150 
00151    if (size != (size_t)size)
00152      ret = NULL;
00153    else if (size == (png_uint_32)65536L)
00154    {
00155       if (png_ptr->offset_table == NULL)
00156       {
00157          /* try to see if we need to do any of this fancy stuff */
00158          ret = farmalloc(size);
00159          if (ret == NULL || ((png_size_t)ret & 0xffff))
00160          {
00161             int num_blocks;
00162             png_uint_32 total_size;
00163             png_bytep table;
00164             int i;
00165             png_byte huge * hptr;
00166 
00167             if (ret != NULL)
00168             {
00169                farfree(ret);
00170                ret = NULL;
00171             }
00172 
00173             if (png_ptr->zlib_window_bits > 14)
00174                num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
00175             else
00176                num_blocks = 1;
00177             if (png_ptr->zlib_mem_level >= 7)
00178                num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
00179             else
00180                num_blocks++;
00181 
00182             total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
00183 
00184             table = farmalloc(total_size);
00185 
00186             if (table == NULL)
00187             {
00188 #ifndef PNG_USER_MEM_SUPPORTED
00189                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00190                   png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
00191                else
00192                   png_warning(png_ptr, "Out Of Memory.");
00193 #endif
00194                return (NULL);
00195             }
00196 
00197             if ((png_size_t)table & 0xfff0)
00198             {
00199 #ifndef PNG_USER_MEM_SUPPORTED
00200                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00201                   png_error(png_ptr,
00202                     "Farmalloc didn't return normalized pointer");
00203                else
00204                   png_warning(png_ptr,
00205                     "Farmalloc didn't return normalized pointer");
00206 #endif
00207                return (NULL);
00208             }
00209 
00210             png_ptr->offset_table = table;
00211             png_ptr->offset_table_ptr = farmalloc(num_blocks *
00212                png_sizeof(png_bytep));
00213 
00214             if (png_ptr->offset_table_ptr == NULL)
00215             {
00216 #ifndef PNG_USER_MEM_SUPPORTED
00217                if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00218                   png_error(png_ptr, "Out Of memory."); /* Note "O" and "M" */
00219                else
00220                   png_warning(png_ptr, "Out Of memory.");
00221 #endif
00222                return (NULL);
00223             }
00224 
00225             hptr = (png_byte huge *)table;
00226             if ((png_size_t)hptr & 0xf)
00227             {
00228                hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
00229                hptr = hptr + 16L;  /* "hptr += 16L" fails on Turbo C++ 3.0 */
00230             }
00231             for (i = 0; i < num_blocks; i++)
00232             {
00233                png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
00234                hptr = hptr + (png_uint_32)65536L;  /* "+=" fails on TC++3.0 */
00235             }
00236 
00237             png_ptr->offset_table_number = num_blocks;
00238             png_ptr->offset_table_count = 0;
00239             png_ptr->offset_table_count_free = 0;
00240          }
00241       }
00242 
00243       if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
00244       {
00245 #ifndef PNG_USER_MEM_SUPPORTED
00246          if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00247             png_error(png_ptr, "Out of Memory."); /* Note "o" and "M" */
00248          else
00249             png_warning(png_ptr, "Out of Memory.");
00250 #endif
00251          return (NULL);
00252       }
00253 
00254       ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
00255    }
00256    else
00257       ret = farmalloc(size);
00258 
00259 #ifndef PNG_USER_MEM_SUPPORTED
00260    if (ret == NULL)
00261    {
00262       if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00263          png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
00264       else
00265          png_warning(png_ptr, "Out of memory."); /* Note "o" and "m" */
00266    }
00267 #endif
00268 
00269    return (ret);
00270 }
00271 
00272 /* free a pointer allocated by png_malloc().  In the default
00273    configuration, png_ptr is not used, but is passed in case it
00274    is needed.  If ptr is NULL, return without taking any action. */
00275 
00276 void PNGAPI
00277 png_free(png_structp png_ptr, png_voidp ptr)
00278 {
00279    if (png_ptr == NULL || ptr == NULL)
00280       return;
00281 
00282 #ifdef PNG_USER_MEM_SUPPORTED
00283    if (png_ptr->free_fn != NULL)
00284    {
00285       (*(png_ptr->free_fn))(png_ptr, ptr);
00286       return;
00287    }
00288    else png_free_default(png_ptr, ptr);
00289 }
00290 
00291 void PNGAPI
00292 png_free_default(png_structp png_ptr, png_voidp ptr)
00293 {
00294 #endif /* PNG_USER_MEM_SUPPORTED */
00295 
00296    if (png_ptr == NULL || ptr == NULL) return;
00297 
00298    if (png_ptr->offset_table != NULL)
00299    {
00300       int i;
00301 
00302       for (i = 0; i < png_ptr->offset_table_count; i++)
00303       {
00304          if (ptr == png_ptr->offset_table_ptr[i])
00305          {
00306             ptr = NULL;
00307             png_ptr->offset_table_count_free++;
00308             break;
00309          }
00310       }
00311       if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
00312       {
00313          farfree(png_ptr->offset_table);
00314          farfree(png_ptr->offset_table_ptr);
00315          png_ptr->offset_table = NULL;
00316          png_ptr->offset_table_ptr = NULL;
00317       }
00318    }
00319 
00320    if (ptr != NULL)
00321    {
00322       farfree(ptr);
00323    }
00324 }
00325 
00326 #else /* Not the Borland DOS special memory handler */
00327 
00328 /* Allocate memory for a png_struct or a png_info.  The malloc and
00329    memset can be replaced by a single call to calloc() if this is thought
00330    to improve performance noticably. */
00331 png_voidp /* PRIVATE */
00332 png_create_struct(int type)
00333 {
00334 #ifdef PNG_USER_MEM_SUPPORTED
00335    return (png_create_struct_2(type, png_malloc_ptr_NULL, png_voidp_NULL));
00336 }
00337 
00338 /* Allocate memory for a png_struct or a png_info.  The malloc and
00339    memset can be replaced by a single call to calloc() if this is thought
00340    to improve performance noticably. */
00341 png_voidp /* PRIVATE */
00342 png_create_struct_2(int type, png_malloc_ptr malloc_fn, png_voidp mem_ptr)
00343 {
00344 #endif /* PNG_USER_MEM_SUPPORTED */
00345    png_size_t size;
00346    png_voidp struct_ptr;
00347 
00348    if (type == PNG_STRUCT_INFO)
00349       size = png_sizeof(png_info);
00350    else if (type == PNG_STRUCT_PNG)
00351       size = png_sizeof(png_struct);
00352    else
00353       return (NULL);
00354 
00355 #ifdef PNG_USER_MEM_SUPPORTED
00356    if (malloc_fn != NULL)
00357    {
00358       png_struct dummy_struct;
00359       png_structp png_ptr = &dummy_struct;
00360       png_ptr->mem_ptr=mem_ptr;
00361       struct_ptr = (*(malloc_fn))(png_ptr, size);
00362       if (struct_ptr != NULL)
00363          png_memset(struct_ptr, 0, size);
00364       return (struct_ptr);
00365    }
00366 #endif /* PNG_USER_MEM_SUPPORTED */
00367 
00368 #if defined(__TURBOC__) && !defined(__FLAT__)
00369    struct_ptr = (png_voidp)farmalloc(size);
00370 #else
00371 # if defined(_MSC_VER) && defined(MAXSEG_64K)
00372    struct_ptr = (png_voidp)halloc(size, 1);
00373 # else
00374    struct_ptr = (png_voidp)malloc(size);
00375 # endif
00376 #endif
00377    if (struct_ptr != NULL)
00378       png_memset(struct_ptr, 0, size);
00379 
00380    return (struct_ptr);
00381 }
00382 
00383 
00384 /* Free memory allocated by a png_create_struct() call */
00385 void /* PRIVATE */
00386 png_destroy_struct(png_voidp struct_ptr)
00387 {
00388 #ifdef PNG_USER_MEM_SUPPORTED
00389    png_destroy_struct_2(struct_ptr, png_free_ptr_NULL, png_voidp_NULL);
00390 }
00391 
00392 /* Free memory allocated by a png_create_struct() call */
00393 void /* PRIVATE */
00394 png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn,
00395     png_voidp mem_ptr)
00396 {
00397 #endif /* PNG_USER_MEM_SUPPORTED */
00398    if (struct_ptr != NULL)
00399    {
00400 #ifdef PNG_USER_MEM_SUPPORTED
00401       if (free_fn != NULL)
00402       {
00403          png_struct dummy_struct;
00404          png_structp png_ptr = &dummy_struct;
00405          png_ptr->mem_ptr=mem_ptr;
00406          (*(free_fn))(png_ptr, struct_ptr);
00407          return;
00408       }
00409 #endif /* PNG_USER_MEM_SUPPORTED */
00410 #if defined(__TURBOC__) && !defined(__FLAT__)
00411       farfree(struct_ptr);
00412 #else
00413 # if defined(_MSC_VER) && defined(MAXSEG_64K)
00414       hfree(struct_ptr);
00415 # else
00416       free(struct_ptr);
00417 # endif
00418 #endif
00419    }
00420 }
00421 
00422 /* Allocate memory.  For reasonable files, size should never exceed
00423    64K.  However, zlib may allocate more then 64K if you don't tell
00424    it not to.  See zconf.h and png.h for more information.  zlib does
00425    need to allocate exactly 64K, so whatever you call here must
00426    have the ability to do that. */
00427 
00428 png_voidp PNGAPI
00429 png_malloc(png_structp png_ptr, png_uint_32 size)
00430 {
00431    png_voidp ret;
00432 
00433 #ifdef PNG_USER_MEM_SUPPORTED
00434    if (png_ptr == NULL || size == 0)
00435       return (NULL);
00436 
00437    if (png_ptr->malloc_fn != NULL)
00438        ret = ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, (png_size_t)size));
00439    else
00440        ret = (png_malloc_default(png_ptr, size));
00441    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00442        png_error(png_ptr, "Out of Memory!");
00443    return (ret);
00444 }
00445 
00446 png_voidp PNGAPI
00447 png_malloc_default(png_structp png_ptr, png_uint_32 size)
00448 {
00449    png_voidp ret;
00450 #endif /* PNG_USER_MEM_SUPPORTED */
00451 
00452    if (png_ptr == NULL || size == 0)
00453       return (NULL);
00454 
00455 #ifdef PNG_MAX_MALLOC_64K
00456    if (size > (png_uint_32)65536L)
00457    {
00458 #ifndef PNG_USER_MEM_SUPPORTED
00459       if ((png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00460          png_error(png_ptr, "Cannot Allocate > 64K");
00461       else
00462 #endif
00463          return NULL;
00464    }
00465 #endif
00466 
00467  /* Check for overflow */
00468 #if defined(__TURBOC__) && !defined(__FLAT__)
00469  if (size != (unsigned long)size)
00470    ret = NULL;
00471  else
00472    ret = farmalloc(size);
00473 #else
00474 # if defined(_MSC_VER) && defined(MAXSEG_64K)
00475  if (size != (unsigned long)size)
00476    ret = NULL;
00477  else
00478    ret = halloc(size, 1);
00479 # else
00480  if (size != (size_t)size)
00481    ret = NULL;
00482  else
00483    ret = malloc((size_t)size);
00484 # endif
00485 #endif
00486 
00487 #ifndef PNG_USER_MEM_SUPPORTED
00488    if (ret == NULL && (png_ptr->flags&PNG_FLAG_MALLOC_NULL_MEM_OK) == 0)
00489       png_error(png_ptr, "Out of Memory");
00490 #endif
00491 
00492    return (ret);
00493 }
00494 
00495 /* Free a pointer allocated by png_malloc().  If ptr is NULL, return
00496    without taking any action. */
00497 void PNGAPI
00498 png_free(png_structp png_ptr, png_voidp ptr)
00499 {
00500    if (png_ptr == NULL || ptr == NULL)
00501       return;
00502 
00503 #ifdef PNG_USER_MEM_SUPPORTED
00504    if (png_ptr->free_fn != NULL)
00505    {
00506       (*(png_ptr->free_fn))(png_ptr, ptr);
00507       return;
00508    }
00509    else png_free_default(png_ptr, ptr);
00510 }
00511 void PNGAPI
00512 png_free_default(png_structp png_ptr, png_voidp ptr)
00513 {
00514    if (png_ptr == NULL || ptr == NULL)
00515       return;
00516 
00517 #endif /* PNG_USER_MEM_SUPPORTED */
00518 
00519 #if defined(__TURBOC__) && !defined(__FLAT__)
00520    farfree(ptr);
00521 #else
00522 # if defined(_MSC_VER) && defined(MAXSEG_64K)
00523    hfree(ptr);
00524 # else
00525    free(ptr);
00526 # endif
00527 #endif
00528 }
00529 
00530 #endif /* Not Borland DOS special memory handler */
00531 
00532 #if defined(PNG_1_0_X)
00533 #  define png_malloc_warn png_malloc
00534 #else
00535 /* This function was added at libpng version 1.2.3.  The png_malloc_warn()
00536  * function will set up png_malloc() to issue a png_warning and return NULL
00537  * instead of issuing a png_error, if it fails to allocate the requested
00538  * memory.
00539  */
00540 png_voidp PNGAPI
00541 png_malloc_warn(png_structp png_ptr, png_uint_32 size)
00542 {
00543    png_voidp ptr;
00544    png_uint_32 save_flags;
00545    if (png_ptr == NULL) return (NULL);
00546 
00547    save_flags = png_ptr->flags;
00548    png_ptr->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
00549    ptr = (png_voidp)png_malloc((png_structp)png_ptr, size);
00550    png_ptr->flags=save_flags;
00551    return(ptr);
00552 }
00553 #endif
00554 
00555 png_voidp PNGAPI
00556 png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
00557    png_uint_32 length)
00558 {
00559    png_size_t size;
00560 
00561    size = (png_size_t)length;
00562    if ((png_uint_32)size != length)
00563       png_error(png_ptr, "Overflow in png_memcpy_check.");
00564 
00565    return(png_memcpy (s1, s2, size));
00566 }
00567 
00568 png_voidp PNGAPI
00569 png_memset_check (png_structp png_ptr, png_voidp s1, int value,
00570    png_uint_32 length)
00571 {
00572    png_size_t size;
00573 
00574    size = (png_size_t)length;
00575    if ((png_uint_32)size != length)
00576       png_error(png_ptr, "Overflow in png_memset_check.");
00577 
00578    return (png_memset (s1, value, size));
00579 
00580 }
00581 
00582 #ifdef PNG_USER_MEM_SUPPORTED
00583 /* This function is called when the application wants to use another method
00584  * of allocating and freeing memory.
00585  */
00586 void PNGAPI
00587 png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
00588   malloc_fn, png_free_ptr free_fn)
00589 {
00590    if (png_ptr != NULL)
00591    {
00592       png_ptr->mem_ptr = mem_ptr;
00593       png_ptr->malloc_fn = malloc_fn;
00594       png_ptr->free_fn = free_fn;
00595    }
00596 }
00597 
00598 /* This function returns a pointer to the mem_ptr associated with the user
00599  * functions.  The application should free any memory associated with this
00600  * pointer before png_write_destroy and png_read_destroy are called.
00601  */
00602 png_voidp PNGAPI
00603 png_get_mem_ptr(png_structp png_ptr)
00604 {
00605    if (png_ptr == NULL) return (NULL);
00606    return ((png_voidp)png_ptr->mem_ptr);
00607 }
00608 #endif /* PNG_USER_MEM_SUPPORTED */
00609 #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:18