source: svn/newcon3bcm2_21bu/dst/dlib/src/PNG/pngmem.c

Last change on this file was 76, checked in by megakiss, 10 years ago

1W 대기전력을 만족시키기 위하여 POWEROFF시 튜너를 Standby 상태로 함

  • Property svn:executable set to *
File size: 14.4 KB
Line 
1/*
2 * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/PNG/lpng102/pngmem.c#1 $
3 * $Revision: #1 $
4 * $DateTime: 2006/02/24 17:51:46 $
5 * $Change: 42566 $
6 * $Author: pryush.sharma $
7 */
8
9
10/* pngmem.c - stub functions for memory allocation
11 *
12 * libpng 1.0.2 - June 14, 1998
13 * For conditions of distribution and use, see copyright notice in png.h
14 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
15 * Copyright (c) 1996, 1997 Andreas Dilger
16 * Copyright (c) 1998, Glenn Randers-Pehrson
17 *
18 * This file provides a location for all memory allocation.  Users who
19 * need special memory handling are expected to supply replacement
20 * functions for png_malloc() and png_free(), and to use
21 * png_create_read_struct_2() and png_create_write_struct_2() to
22 * identify the replacement functions.
23 */
24
25#define PNG_INTERNAL
26#include "png.h"
27
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
32/* Allocate memory for a png_struct.  The malloc and memset can be replaced
33   by a single call to calloc() if this is thought to improve performance. */
34png_voidp
35png_create_struct(int type)
36{
37#ifdef PNG_USER_MEM_SUPPORTED
38   return (png_create_struct_2(type, NULL));
39}
40
41/* Alternate version of png_create_struct, for use with user-defined malloc. */
42png_voidp
43png_create_struct_2(int type, png_malloc_ptr malloc_fn)
44{
45#endif /* PNG_USER_MEM_SUPPORTED */
46   png_size_t size;
47   png_voidp struct_ptr;
48
49   if (type == PNG_STRUCT_INFO)
50     size = sizeof(png_info);
51   else if (type == PNG_STRUCT_PNG)
52     size = sizeof(png_struct);
53   else
54     return ((png_voidp)NULL);
55
56#ifdef PNG_USER_MEM_SUPPORTED
57   if(malloc_fn != NULL)
58   {
59      if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
60         png_memset(struct_ptr, 0, size);
61         return (struct_ptr);
62   }
63#endif /* PNG_USER_MEM_SUPPORTED */
64   if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
65   {
66      png_memset(struct_ptr, 0, size);
67   }
68   return (struct_ptr);
69}
70
71
72/* Free memory allocated by a png_create_struct() call */
73void
74png_destroy_struct(png_voidp struct_ptr)
75{
76#ifdef PNG_USER_MEM_SUPPORTED
77   png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
78}
79
80/* Free memory allocated by a png_create_struct() call */
81void
82png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
83{
84#endif
85   if (struct_ptr != NULL)
86   {
87#ifdef PNG_USER_MEM_SUPPORTED
88      if(free_fn != NULL)
89      {
90         png_struct dummy_struct;
91         png_structp png_ptr = &dummy_struct;
92         (*(free_fn))(png_ptr, struct_ptr);
93         struct_ptr = NULL;
94         return;
95      }
96#endif /* PNG_USER_MEM_SUPPORTED */
97      farfree (struct_ptr);
98      struct_ptr = NULL;
99   }
100}
101
102/* Allocate memory.  For reasonable files, size should never exceed
103 * 64K.  However, zlib may allocate more then 64K if you don't tell
104 * it not to.  See zconf.h and png.h for more information. zlib does
105 * need to allocate exactly 64K, so whatever you call here must
106 * have the ability to do that.
107 *
108 * Borland seems to have a problem in DOS mode for exactly 64K.
109 * It gives you a segment with an offset of 8 (perhaps to store its
110 * memory stuff).  zlib doesn't like this at all, so we have to
111 * detect and deal with it.  This code should not be needed in
112 * Windows or OS/2 modes, and only in 16 bit mode.  This code has
113 * been updated by Alexander Lehmann for version 0.89 to waste less
114 * memory.
115 *
116 * Note that we can't use png_size_t for the "size" declaration,
117 * since on some systems a png_size_t is a 16-bit quantity, and as a
118 * result, we would be truncating potentially larger memory requests
119 * (which should cause a fatal error) and introducing major problems.
120 */
121png_voidp
122png_malloc(png_structp png_ptr, png_uint_32 size)
123{
124#ifndef PNG_USER_MEM_SUPPORTED
125   png_voidp ret;
126#endif
127   if (png_ptr == NULL || size == 0)
128      return ((png_voidp)NULL);
129
130#ifdef PNG_USER_MEM_SUPPORTED
131   if(png_ptr->malloc_fn != NULL)
132       return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
133   else
134       return png_malloc_default(png_ptr, size);
135}
136
137png_voidp
138png_malloc_default(png_structp png_ptr, png_uint_32 size)
139{
140   png_voidp ret;
141#endif /* PNG_USER_MEM_SUPPORTED */
142
143#ifdef PNG_MAX_MALLOC_64K
144   if (size > (png_uint_32)65536L)
145      png_error(png_ptr, "Cannot Allocate > 64K");
146#endif
147
148   if (size == (png_uint_32)65536L)
149   {
150      if (png_ptr->offset_table == NULL)
151      {
152         /* try to see if we need to do any of this fancy stuff */
153         ret = farmalloc(size);
154         if (ret == NULL || ((png_size_t)ret & 0xffff))
155         {
156            int num_blocks;
157            png_uint_32 total_size;
158            png_bytep table;
159            int i;
160            png_byte huge * hptr;
161
162            if (ret != NULL)
163            {
164               farfree(ret);
165               ret = NULL;
166            }
167
168            num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
169            if (num_blocks < 1)
170               num_blocks = 1;
171            if (png_ptr->zlib_mem_level >= 7)
172               num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
173            else
174               num_blocks++;
175
176            total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
177
178            table = farmalloc(total_size);
179
180            if (table == NULL)
181            {
182               png_error(png_ptr, "Out Of Memory."); /* Note "O" and "M" */
183            }
184
185            if ((png_size_t)table & 0xfff0)
186            {
187               png_error(png_ptr, "Farmalloc didn't return normalized pointer");
188            }
189
190            png_ptr->offset_table = table;
191            png_ptr->offset_table_ptr = farmalloc(num_blocks *
192               sizeof (png_bytep));
193
194            if (png_ptr->offset_table_ptr == NULL)
195            {
196               png_error(png_ptr, "Out Of memory.");
197            }
198
199            hptr = (png_byte huge *)table;
200            if ((png_size_t)hptr & 0xf)
201            {
202               hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
203               hptr += 16L;
204            }
205            for (i = 0; i < num_blocks; i++)
206            {
207               png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
208               hptr += (png_uint_32)65536L;
209            }
210
211            png_ptr->offset_table_number = num_blocks;
212            png_ptr->offset_table_count = 0;
213            png_ptr->offset_table_count_free = 0;
214         }
215      }
216
217      if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
218         png_error(png_ptr, "Out of Memory.");
219
220      ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
221   }
222   else
223      ret = farmalloc(size);
224
225   if (ret == NULL)
226   {
227      png_error(png_ptr, "Out of memory."); /* Note "o" and "m" */
228   }
229
230   return (ret);
231}
232
233/* free a pointer allocated by png_malloc().  In the default
234   configuration, png_ptr is not used, but is passed in case it
235   is needed.  If ptr is NULL, return without taking any action. */
236void
237png_free(png_structp png_ptr, png_voidp ptr)
238{
239   if (png_ptr == NULL || ptr == NULL)
240      return;
241
242#ifdef PNG_USER_MEM_SUPPORTED
243   if (png_ptr->free_fn != NULL)
244   {
245      (*(png_ptr->free_fn))(png_ptr, ptr);
246      ptr = NULL;
247      return;
248   }
249   else png_free_default(png_ptr, ptr);
250}
251
252void
253png_free_default(png_structp png_ptr, png_voidp ptr)
254{
255#endif /* PNG_USER_MEM_SUPPORTED */
256     
257   if (png_ptr->offset_table != NULL)
258   {
259      int i;
260
261      for (i = 0; i < png_ptr->offset_table_count; i++)
262      {
263         if (ptr == png_ptr->offset_table_ptr[i])
264         {
265            ptr = NULL;
266            png_ptr->offset_table_count_free++;
267            break;
268         }
269      }
270      if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
271      {
272         farfree(png_ptr->offset_table);
273         farfree(png_ptr->offset_table_ptr);
274         png_ptr->offset_table = NULL;
275         png_ptr->offset_table_ptr = NULL;
276      }
277   }
278
279   if (ptr != NULL)
280   {
281      farfree(ptr);
282      ptr = NULL;
283   }
284}
285
286#else /* Not the Borland DOS special memory handler */
287
288/* Allocate memory for a png_struct or a png_info.  The malloc and
289   memset can be replaced by a single call to calloc() if this is thought
290   to improve performance noticably.*/
291png_voidp
292png_create_struct(int type)
293{
294#ifdef PNG_USER_MEM_SUPPORTED
295   return (png_create_struct_2(type, NULL));
296}
297
298/* Allocate memory for a png_struct or a png_info.  The malloc and
299   memset can be replaced by a single call to calloc() if this is thought
300   to improve performance noticably.*/
301png_voidp
302png_create_struct_2(int type, png_malloc_ptr malloc_fn)
303{
304#endif /* PNG_USER_MEM_SUPPORTED */
305   png_size_t size;
306   png_voidp struct_ptr;
307
308   if (type == PNG_STRUCT_INFO)
309      size = sizeof(png_info);
310   else if (type == PNG_STRUCT_PNG)
311      size = sizeof(png_struct);
312   else
313      return ((png_voidp)NULL);
314
315#ifdef PNG_USER_MEM_SUPPORTED
316   if(malloc_fn != NULL)
317   {
318      if ((struct_ptr = (*(malloc_fn))(NULL, size)) != NULL)
319         png_memset(struct_ptr, 0, size);
320      return (struct_ptr);
321   }
322#endif /* PNG_USER_MEM_SUPPORTED */
323
324#if defined(__TURBOC__) && !defined(__FLAT__)
325   if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
326#else
327# if defined(_MSC_VER) && defined(MAXSEG_64K)
328   if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
329# else
330   if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
331# endif
332#endif
333   {
334      png_memset(struct_ptr, 0, size);
335   }
336
337   return (struct_ptr);
338}
339
340
341/* Free memory allocated by a png_create_struct() call */
342void
343png_destroy_struct(png_voidp struct_ptr)
344{
345#ifdef PNG_USER_MEM_SUPPORTED
346   png_destroy_struct_2(struct_ptr, (png_free_ptr)NULL);
347}
348
349/* Free memory allocated by a png_create_struct() call */
350void
351png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn)
352{
353#endif /* PNG_USER_MEM_SUPPORTED */
354   if (struct_ptr != NULL)
355   {
356#ifdef PNG_USER_MEM_SUPPORTED
357      if(free_fn != NULL)
358      {
359         png_struct dummy_struct;
360         png_structp png_ptr = &dummy_struct;
361         (*(free_fn))(png_ptr, struct_ptr);
362         struct_ptr = NULL;
363         return;
364      }
365#endif /* PNG_USER_MEM_SUPPORTED */
366#if defined(__TURBOC__) && !defined(__FLAT__)
367      farfree(struct_ptr);
368      struct_ptr = NULL;
369#else
370# if defined(_MSC_VER) && defined(MAXSEG_64K)
371      hfree(struct_ptr);
372      struct_ptr = NULL;
373# else
374      free(struct_ptr);
375      struct_ptr = NULL;
376# endif
377#endif
378   }
379}
380
381
382/* Allocate memory.  For reasonable files, size should never exceed
383   64K.  However, zlib may allocate more then 64K if you don't tell
384   it not to.  See zconf.h and png.h for more information.  zlib does
385   need to allocate exactly 64K, so whatever you call here must
386   have the ability to do that. */
387
388//BKTEMP void * MsOS_AllocateMemory2 (png_uint_32 u32Size);
389
390
391
392png_voidp
393png_malloc(png_structp png_ptr, png_uint_32 size)
394{
395        png_voidp ret;
396//BKTEMP        ret = MsOS_AllocateMemory2(size);       //BKTODO: cached memory ÇÊ¿äÇÑÁö.. BMEM_Heap_ConvertAddressToCached()..
397        ret = malloc(size);
398
399        if (ret == NULL)
400    {
401          png_error(png_ptr, "Out of Memory");
402    }
403        return (ret);
404
405
406/*
407#ifndef PNG_USER_MEM_SUPPORTED
408   png_voidp ret;
409#endif
410   if (png_ptr == NULL || size == 0)
411      return ((png_voidp)NULL);
412
413#ifdef PNG_USER_MEM_SUPPORTED
414   if(png_ptr->malloc_fn != NULL)
415       return ((png_voidp)(*(png_ptr->malloc_fn))(png_ptr, size));
416   else
417       return (png_malloc_default(png_ptr, size));
418}
419png_voidp
420png_malloc_default(png_structp png_ptr, png_uint_32 size)
421{
422   png_voidp ret;
423#endif /* PNG_USER_MEM_SUPPORTED */
424/*
425#ifdef PNG_MAX_MALLOC_64K
426   if (size > (png_uint_32)65536L)
427      png_error(png_ptr, "Cannot Allocate > 64K");
428#endif
429
430#if defined(__TURBOC__) && !defined(__FLAT__)
431   ret = farmalloc(size);
432#else
433# if defined(_MSC_VER) && defined(MAXSEG_64K)
434   ret = halloc(size, 1);
435# else
436   ret = malloc((size_t)size);
437# endif
438#endif
439
440   if (ret == NULL)
441   {
442      png_error(png_ptr, "Out of Memory");
443   }
444
445   return (ret);
446   */
447}
448
449/* Free a pointer allocated by png_malloc().  If ptr is NULL, return
450   without taking any action. */
451   
452//void MsOS_FreeMemory2(void *ptr);
453
454
455void
456png_free(png_structp png_ptr, png_voidp ptr)
457{
458//BKTEMP        MsOS_FreeMemory2(ptr);
459        free(ptr);
460
461        ptr = NULL;
462
463/*
464   if (png_ptr == NULL || ptr == NULL)
465      return;
466
467#ifdef PNG_USER_MEM_SUPPORTED
468   if (png_ptr->free_fn != NULL)
469   {
470      (*(png_ptr->free_fn))(png_ptr, ptr);
471      ptr = NULL;
472      return;
473   }
474   else png_free_default(png_ptr, ptr);
475}
476void
477png_free_default(png_structp png_ptr, png_voidp ptr)
478{
479#endif /* PNG_USER_MEM_SUPPORTED */
480/*
481#if defined(__TURBOC__) && !defined(__FLAT__)
482   farfree(ptr);
483   ptr = NULL;
484#else
485# if defined(_MSC_VER) && defined(MAXSEG_64K)
486   hfree(ptr);
487   ptr = NULL;
488# else
489   free(ptr);
490   ptr = NULL;
491# endif
492#endif
493*/
494}
495
496#endif /* Not Borland DOS special memory handler */
497
498png_voidp
499png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
500   png_uint_32 length)
501{
502   png_size_t size;
503
504   size = (png_size_t)length;
505   if ((png_uint_32)size != length)
506      png_error(png_ptr,"Overflow in png_memcpy_check.");
507 
508   return(png_memcpy (s1, s2, size));
509}
510
511png_voidp
512png_memset_check (png_structp png_ptr, png_voidp s1, int value,
513   png_uint_32 length)
514{
515   png_size_t size;
516
517   size = (png_size_t)length;
518   if ((png_uint_32)size != length)
519      png_error(png_ptr,"Overflow in png_memset_check.");
520
521   return (png_memset (s1, value, size));
522
523}
524
525#ifdef PNG_USER_MEM_SUPPORTED
526/* This function is called when the application wants to use another method
527 * of allocating and freeing memory.
528 */
529void
530png_set_mem_fn(png_structp png_ptr, png_voidp mem_ptr, png_malloc_ptr
531  malloc_fn, png_free_ptr free_fn)
532{
533   png_ptr->mem_ptr = mem_ptr;
534   png_ptr->malloc_fn = malloc_fn;
535   png_ptr->free_fn = free_fn;
536}
537
538/* This function returns a pointer to the mem_ptr associated with the user
539 * functions.  The application should free any memory associated with this
540 * pointer before png_write_destroy and png_read_destroy are called.
541 */
542png_voidp
543png_get_mem_ptr(png_structp png_ptr)
544{
545   return ((png_voidp)png_ptr->mem_ptr);
546}
547#endif /* PNG_USER_MEM_SUPPORTED */
Note: See TracBrowser for help on using the repository browser.