This repository has been archived on 2023-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
libpng/pngmem.c

314 lines
7.9 KiB
C
Raw Normal View History

1995-07-20 07:43:20 +00:00
/* pngstub.c - stub functions for i/o and memory allocation
1995-09-26 10:22:39 +00:00
libpng 1.0 beta 2 - version 0.81
1995-07-20 07:43:20 +00:00
For conditions of distribution and use, see copyright notice in png.h
Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
1995-09-26 10:22:39 +00:00
August 24, 1995
1995-07-20 07:43:20 +00:00
1995-09-26 10:22:39 +00:00
This file provides a location for all memory allocation. Users which
need special memory handling are expected to modify the code in this file
to meet their needs. See the instructions at each function. */
1995-07-20 07:43:20 +00:00
#define PNG_INTERNAL
#include "png.h"
/* Allocate memory. For reasonable files, size should never exceed
64K. However, zlib may allocate more then 64K if you don't tell
it not to. See zconf.h and png.h for more information. zlib does
need to allocate exactly 64K, so whatever you call here must
have the ability to do that. */
/* Borland compilers have this habit of not giving you 64K chunks
that start on the segment in DOS mode. This has not been observed
in Windows, and of course it doesn't matter in 32 bit mode, as there
are no segments. Now libpng doesn't need that much memory normally,
but zlib does, so we have to normalize it, if necessary. It would be
better if zlib worked in less then 64K, but it doesn't, so we
have to deal with it. Truely, we are misusing farmalloc here,
as it is designed for use with huge pointers, which don't care
about segments. So we allocate a large amount of memory, and
divvy off segments when needed.
*/
#ifdef __TURBOC__
#ifndef __WIN32__
1995-09-26 10:22:39 +00:00
#ifndef __FLAT__
1995-07-20 07:43:20 +00:00
/* NUM_SEG is the number of segments allocated at once */
#define NUM_SEG 4
typedef struct borland_seg_struct
{
void *mem_ptr;
void *seg_ptr[NUM_SEG];
int seg_used[NUM_SEG];
int num_used;
} borland_seg;
borland_seg *save_array;
int num_save_array;
int max_save_array;
1995-09-26 10:22:39 +00:00
#endif
1995-07-20 07:43:20 +00:00
#endif
#endif
1995-09-26 10:22:39 +00:00
voidpf
png_large_malloc(png_structf *png_ptr, png_uint_32 size)
1995-07-20 07:43:20 +00:00
{
1995-09-26 10:22:39 +00:00
voidpf ret;
if (!png_ptr || !size)
return ((void *)0);
1995-07-20 07:43:20 +00:00
#ifdef PNG_MAX_MALLOC_64K
if (size > (png_uint_32)65536L)
png_error(png_ptr, "Cannot Allocate > 64K");
#endif
#ifdef __TURBOC__
1995-09-26 10:22:39 +00:00
# if defined(__WIN32__) || defined(__FLAT__)
ret = malloc(size);
1995-07-20 07:43:20 +00:00
# else
if (size == 65536L)
{
unsigned long offset;
if (!save_array)
{
ret = farmalloc(size);
offset = (unsigned long)(ret);
offset &= 0xffffL;
}
else
{
ret = (void *)0;
}
if (save_array || offset)
{
int i, j;
if (ret)
farfree(ret);
ret = (void *)0;
if (!save_array)
{
unsigned long offset;
png_byte huge *ptr;
int i;
num_save_array = 1;
save_array = malloc(num_save_array * sizeof (borland_seg));
if (!save_array)
1995-09-26 10:22:39 +00:00
png_error(png_ptr, "Out of Memory 1");
1995-07-20 07:43:20 +00:00
save_array->mem_ptr = farmalloc(
1995-09-26 10:22:39 +00:00
(unsigned long)(NUM_SEG) * 65536L + 65532L);
1995-07-20 07:43:20 +00:00
if (!save_array->mem_ptr)
1995-09-26 10:22:39 +00:00
png_error(png_ptr, "Out of Memory 2");
1995-07-20 07:43:20 +00:00
offset = (unsigned long)(ret);
offset &= 0xffffL;
ptr = save_array->mem_ptr;
if (offset)
ptr += 65536L - offset;
for (i = 0; i < NUM_SEG; i++, ptr += 65536L)
{
save_array->seg_ptr[i] = ptr;
save_array->seg_used[i] = 0;
}
save_array->num_used = 0;
}
for (i = 0; i < num_save_array; i++)
{
for (j = 0; j < NUM_SEG; j++)
{
if (!save_array[i].seg_used[j])
{
ret = save_array[i].seg_ptr[j];
save_array[i].seg_used[j] = 1;
save_array[i].num_used++;
break;
}
}
if (ret)
break;
}
if (!ret)
{
unsigned long offset;
png_byte huge *ptr;
save_array = realloc(save_array,
(num_save_array + 1) * sizeof (borland_seg));
if (!save_array)
1995-09-26 10:22:39 +00:00
png_error(png_ptr, "Out of Memory 3");
1995-07-20 07:43:20 +00:00
save_array[num_save_array].mem_ptr = farmalloc(
1995-09-26 10:22:39 +00:00
(unsigned long)(NUM_SEG) * 65536L + 65532L);
1995-07-20 07:43:20 +00:00
if (!save_array[num_save_array].mem_ptr)
1995-09-26 10:22:39 +00:00
png_error(png_ptr, "Out of Memory 4");
1995-07-20 07:43:20 +00:00
offset = (unsigned long)(ret);
offset &= 0xffffL;
ptr = save_array[num_save_array].mem_ptr;
if (offset)
ptr += 65536L - offset;
for (i = 0; i < NUM_SEG; i++, ptr += 65536L)
{
save_array[num_save_array].seg_ptr[i] = ptr;
save_array[num_save_array].seg_used[i] = 0;
}
ret = save_array[num_save_array].seg_ptr[0];
save_array[num_save_array].seg_used[0] = 1;
save_array[num_save_array].num_used = 1;
num_save_array++;
}
}
}
else
{
ret = farmalloc(size);
}
# endif /* __WIN32__ */
#else /* __TURBOC__ */
# ifdef _MSC_VER
ret = halloc(size, 1);
# else
/* everybody else, so normal malloc should do it. */
ret = malloc(size);
# endif
#endif
1995-09-26 10:22:39 +00:00
if (ret == NULL)
1995-07-20 07:43:20 +00:00
{
png_error(png_ptr, "Out of Memory");
}
return ret;
}
/* free a pointer allocated by png_large_malloc(). In the default
configuration, png_ptr is not used, but is passed in case it
is needed. If ptr is NULL, return without taking any action. */
void
1995-09-26 10:22:39 +00:00
png_large_free(png_structf *png_ptr, voidpf ptr)
1995-07-20 07:43:20 +00:00
{
if (!png_ptr)
return;
if (ptr != (void *)0)
{
#ifdef __TURBOC__
1995-09-26 10:22:39 +00:00
# if defined(__WIN32__) || defined(__FLAT__)
if (ptr)
free(ptr);
# else
1995-07-20 07:43:20 +00:00
int i, j;
for (i = 0; i < num_save_array; i++)
{
for (j = 0; j < NUM_SEG; j++)
{
if (ptr == save_array[i].seg_ptr[j])
{
save_array[i].seg_used[j] = 0;
ptr = 0;
save_array[i].num_used--;
if (!save_array[i].num_used)
{
int k;
1995-09-26 10:22:39 +00:00
1995-07-20 07:43:20 +00:00
num_save_array--;
farfree(save_array[i].mem_ptr);
for (k = i; k < num_save_array; k++)
save_array[k] = save_array[k + 1];
if (!num_save_array)
{
free(save_array);
save_array = 0;
}
}
break;
}
}
if (!ptr)
break;
}
if (ptr)
farfree(ptr);
1995-09-26 10:22:39 +00:00
# endif
1995-07-20 07:43:20 +00:00
#else
# ifdef _MSC_VER
hfree(ptr);
# else
free(ptr);
# endif
#endif
}
}
/* Allocate memory. This is called for smallish blocks only It
should not get anywhere near 64K. */
void *
png_malloc(png_struct *png_ptr, png_uint_32 size)
{
void *ret;
1995-09-26 10:22:39 +00:00
if (!png_ptr || !size)
1995-07-20 07:43:20 +00:00
return ((void *)0);
#ifdef PNG_MAX_MALLOC_64K
if (size > (png_uint_32)65536L)
png_error(png_ptr, "Cannot Allocate > 64K");
#endif
ret = malloc((png_size_t)size);
if (!ret)
{
1995-09-26 10:22:39 +00:00
png_error(png_ptr, "Out of Memory 6");
1995-07-20 07:43:20 +00:00
}
return ret;
}
/* Reallocate memory. This will not get near 64K on a
even marginally reasonable file. */
void *
1995-09-26 10:22:39 +00:00
png_realloc(png_struct *png_ptr, void *ptr, png_uint_32 size,
png_uint_32 old_size)
1995-07-20 07:43:20 +00:00
{
void *ret;
1995-09-26 10:22:39 +00:00
if (!png_ptr || !old_size || !ptr || !size)
1995-07-20 07:43:20 +00:00
return ((void *)0);
#ifdef PNG_MAX_MALLOC_64K
if (size > (png_uint_32)65536L)
png_error(png_ptr, "Cannot Allocate > 64K");
#endif
ret = realloc(ptr, (png_size_t)size);
if (!ret)
{
1995-09-26 10:22:39 +00:00
png_error(png_ptr, "Out of Memory 7");
1995-07-20 07:43:20 +00:00
}
return ret;
}
/* free a pointer allocated by png_malloc(). In the default
configuration, png_ptr is not used, but is passed incase it
is needed. If ptr is NULL, return without taking any action. */
void
png_free(png_struct *png_ptr, void *ptr)
{
if (!png_ptr)
return;
if (ptr != (void *)0)
free(ptr);
}