source: trunk/libdjvu/DjVuGlobalMemory.cpp @ 17

Last change on this file since 17 was 17, checked in by Eugene Romanenko, 15 years ago

update makefiles, remove absolute paths, update djvulibre to version 3.5.17

File size: 8.3 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, Version 2. The license should have
9//C- accompanied the software or you may obtain a copy of the license
10//C- from the Free Software Foundation at http://www.fsf.org .
11//C-
12//C- This program is distributed in the hope that it will be useful,
13//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
14//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15//C- GNU General Public License for more details.
16//C-
17//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
18//C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech
19//C- Software authorized us to replace the original DjVu(r) Reference
20//C- Library notice by the following text (see doc/lizard2002.djvu):
21//C-
22//C-  ------------------------------------------------------------------
23//C- | DjVu (r) Reference Library (v. 3.5)
24//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
25//C- | The DjVu Reference Library is protected by U.S. Pat. No.
26//C- | 6,058,214 and patents pending.
27//C- |
28//C- | This software is subject to, and may be distributed under, the
29//C- | GNU General Public License, Version 2. The license should have
30//C- | accompanied the software or you may obtain a copy of the license
31//C- | from the Free Software Foundation at http://www.fsf.org .
32//C- |
33//C- | The computer code originally released by LizardTech under this
34//C- | license and unmodified by other parties is deemed "the LIZARDTECH
35//C- | ORIGINAL CODE."  Subject to any third party intellectual property
36//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
37//C- | non-exclusive license to make, use, sell, or otherwise dispose of
38//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
39//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
40//C- | General Public License.   This grant only confers the right to
41//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
42//C- | the extent such infringement is reasonably necessary to enable
43//C- | recipient to make, have made, practice, sell, or otherwise dispose
44//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
45//C- | any greater extent that may be necessary to utilize further
46//C- | modifications or combinations.
47//C- |
48//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
49//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
50//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
51//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
52//C- +------------------------------------------------------------------
53//
54// $Id: DjVuGlobalMemory.cpp,v 1.6 2003/11/07 22:08:20 leonb Exp $
55// $Name:  $
56
57#ifdef HAVE_CONFIG_H
58# include "config.h"
59#endif
60#if NEED_GNUG_PRAGMAS
61# pragma implementation
62#endif
63
64#ifdef NEED_DJVU_MEMORY
65#ifndef NEED_DJVU_MEMORY_IMPLEMENTATION
66#define NEED_DJVU_MEMORY_IMPLEMENTATION
67#endif /* NEED_DJVU_MEMORY_IMPLEMENTATION */
68
69#include "DjVuGlobal.h"
70#include "GException.h"
71#include <stdlib.h>
72#include <string.h>
73#include "debug.h"
74
75#ifdef UNIX
76djvu_delete_callback *
77_djvu_delete_ptr=(djvu_delete_callback *)&(operator delete);
78djvu_delete_callback *
79_djvu_deleteArray_ptr=(djvu_delete_callback *)&(operator delete []);
80djvu_new_callback *
81_djvu_new_ptr=(djvu_new_callback *)&(operator new);
82djvu_new_callback *
83_djvu_newArray_ptr=(djvu_new_callback *)&(operator new []);
84#endif
85
86static djvu_delete_callback *_djvu_delete_handler = 0;
87static djvu_new_callback *_djvu_new_handler = 0;
88static djvu_delete_callback *deleteArray_handler = 0;
89static djvu_new_callback *newArray_handler = 0;
90
91static djvu_free_callback *_djvu_free_handler = 0;
92static djvu_realloc_callback *_djvu_realloc_handler = 0;
93static djvu_calloc_callback *_djvu_calloc_handler = 0;
94static djvu_malloc_callback *_djvu_malloc_handler = 0;
95
96int
97djvu_memoryObject_callback (
98  djvu_delete_callback* delete_handler,
99  djvu_new_callback* new_handler
100) {
101  if(delete_handler && new_handler)
102  {
103#ifdef UNIX
104    _djvu_new_ptr=&_djvu_new;
105    _djvu_delete_ptr=&_djvu_delete;
106#endif
107    _djvu_delete_handler=delete_handler;
108    _djvu_new_handler=new_handler;
109    return 1;
110  }else
111  {
112#ifdef UNIX
113    _djvu_new_ptr=(djvu_new_callback *)&(operator new);
114    _djvu_delete_ptr=(djvu_delete_callback *)&(operator delete);
115#endif
116    _djvu_delete_handler=0;
117    _djvu_new_handler=0;
118    return (delete_handler||new_handler)?0:1;
119  }
120  return 0;
121}
122
123int 
124djvu_set_memory_callbacks
125(
126  djvu_free_callback *free_handler,
127  djvu_realloc_callback *realloc_handler,
128  djvu_malloc_callback *malloc_handler,
129  djvu_calloc_callback *calloc_handler
130)
131{
132  if(free_handler && realloc_handler && malloc_handler)
133  {
134#ifdef UNIX
135    _djvu_new_ptr=(djvu_new_callback *)&_djvu_new;
136    _djvu_delete_ptr=(djvu_delete_callback *)&_djvu_delete;
137#endif
138    _djvu_new_handler=(djvu_new_callback *)malloc_handler;
139    _djvu_delete_handler=(djvu_delete_callback *)free_handler;
140    _djvu_malloc_handler=(djvu_malloc_callback *)malloc_handler;
141    _djvu_free_handler=(djvu_free_callback *)free_handler;
142    _djvu_realloc_handler=(djvu_realloc_callback *)realloc_handler;
143    if(calloc_handler)
144    {
145      _djvu_calloc_handler=(djvu_calloc_callback *)&calloc_handler;
146    }else
147    {
148      _djvu_calloc_handler=0;
149    }
150    return 1;
151  }else
152  {
153#ifdef UNIX
154    _djvu_new_ptr=(djvu_new_callback *)&(operator new);
155    _djvu_delete_ptr=(djvu_delete_callback *)&(operator delete);
156#endif
157    _djvu_delete_handler=0;
158    _djvu_new_handler=0;
159    _djvu_malloc_handler=0;
160    _djvu_free_handler=0;
161    _djvu_realloc_handler=0;
162    _djvu_calloc_handler=0;
163    return !(_djvu_malloc_handler
164      ||_djvu_free_handler
165      ||_djvu_realloc_handler
166      ||_djvu_calloc_handler);
167  }
168}
169
170DJVUAPI void *
171_djvu_new(size_t siz)
172{
173  void *ptr;
174#ifndef UNIX
175  if(_djvu_new_handler)
176  {
177#endif
178    if(!(ptr=(*_djvu_new_handler)(siz?siz:1)))
179    {
180      G_THROW( ERR_MSG("DjVuGlobalMemory.exhausted") );
181    }
182#ifndef UNIX
183  }else
184  {
185      ptr=::operator new(siz?siz:1);
186  }
187#endif
188  return ptr;
189}
190
191void 
192_djvu_delete(void *addr)
193{
194  if(addr)
195  {
196    if(_djvu_delete_handler)
197    {
198      (*_djvu_delete_handler)(addr);
199    }else
200    {
201      operator delete(addr);
202    }
203  }
204}
205
206void *
207_djvu_newArray(size_t siz)
208{
209  void *ptr;
210#ifndef UNIX
211  if(newArray_handler)
212  {
213#endif
214    if(!(ptr=(*newArray_handler)(siz?siz:1)))
215    {
216      G_THROW( ERR_MSG("DjVuGlobalMemory.exhausted") );
217    }
218#ifndef UNIX
219  }else
220  {
221      ptr=::new unsigned char[siz?siz:1];
222  }
223#endif
224  return ptr;
225}
226
227void
228_djvu_deleteArray(void *addr)
229{
230  if(addr)
231  {
232    if(deleteArray_handler)
233    {
234      (*deleteArray_handler)(addr);
235    }else
236    {
237#ifdef WIN32
238                delete [] (addr) ;
239#else
240        operator delete [] (addr);
241#endif
242    }
243  }
244}
245
246void *
247_djvu_malloc(size_t siz)
248{
249  DEBUG_MSG("_djvu_malloc: siz="<<siz<<"\n");
250  return _djvu_malloc_handler?(*_djvu_malloc_handler)(siz?siz:1):malloc(siz?siz:1);
251}
252
253void *
254_djvu_calloc(size_t siz, size_t items)
255{
256  DEBUG_MSG("_djvu_calloc: siz="<<siz<<" items="<<items<<"\n");
257  void *ptr;
258  if( _djvu_calloc_handler )
259  {
260    ptr = (*_djvu_calloc_handler)(siz?siz:1, items?items:1);
261  }else if( _djvu_malloc_handler )
262  {
263    if((ptr = (*_djvu_malloc_handler)((siz?siz:1)*(items?items:1)))&&siz&&items)
264    {
265      memset(ptr,0,siz*items);
266    }
267  }else
268  { 
269    ptr = calloc(siz?siz:1, items?items:1);
270  }
271  return ptr;   
272}
273
274void *
275_djvu_realloc(void* ptr, size_t siz)
276{
277  DEBUG_MSG("_djvu_realloc: ptr="<<ptr<<" siz="<<siz<<"\n");
278  void *newptr;
279  if( _djvu_realloc_handler )
280  {
281    newptr = (*_djvu_realloc_handler)(ptr, siz);
282  }else
283  {
284    newptr = realloc(ptr, siz?siz:1);
285  }
286  return newptr;
287}
288 
289void
290_djvu_free(void *ptr)
291{
292  DEBUG_MSG("_djvu_free: ptr="<<ptr<<"\n");
293  if(ptr)
294  {
295    if( _djvu_free_handler )
296    {
297      (*_djvu_free_handler)(ptr);
298    }else
299    {
300      free(ptr);
301    }
302  }
303}
304
305#endif
306
Note: See TracBrowser for help on using the repository browser.