source: trunk/libdjvu/DjVuGlobalMemory.cpp @ 206

Last change on this file since 206 was 206, checked in by Eugene Romanenko, 14 years ago

DJVU plugin: djvulibre updated to version 3.5.19

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