source: trunk/libdjvu/GSmartPointer.cpp @ 269

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

DJVU plugin: djvulibre updated to version 3.5.19

File size: 6.0 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: GSmartPointer.cpp,v 1.12 2007/03/25 20:48:32 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// - Author: Leon Bottou, 05/1997
67
68// From: Leon Bottou, 1/31/2002
69// Class GPBuffer has been added (but not documented) by Lizardtech.
70// Our original implementation consisted of multiple classes.
71// <http://prdownloads.sourceforge.net/djvu/DjVu2_2b-src.tgz>.
72
73#include <string.h>
74#include "GThreads.h"
75#include "GSmartPointer.h"
76#include "GException.h"
77
78
79#ifdef HAVE_NAMESPACES
80namespace DJVU {
81# ifdef NOT_DEFINED // Just to fool emacs c++ mode
82}
83#endif
84#endif
85
86
87// ------ STATIC CRITICAL SECTION
88
89static GCriticalSection gcsCounter;
90
91
92// ------ GPENABLED
93
94
95GPEnabled::~GPEnabled()
96{
97  if (count > 0)
98    G_THROW( ERR_MSG("GSmartPointer.suspicious") );
99}
100
101void
102GPEnabled::destroy()
103{
104  if (count >= 0)
105    G_THROW( ERR_MSG("GSmartPointer.suspicious") );
106  delete this;
107}
108
109void 
110GPEnabled::ref()
111{
112  gcsCounter.lock();
113  count++;
114  gcsCounter.unlock();
115}
116
117void 
118GPEnabled::unref()
119{
120  gcsCounter.lock();
121  if (! --count) 
122    count = -1;
123  gcsCounter.unlock();
124  if (count < 0)
125    destroy();
126}
127
128
129// ------ GPBASE
130
131
132GPBase&
133GPBase::assign (GPEnabled *nptr)
134{
135  gcsCounter.lock();
136  if (nptr)
137    {
138      if (nptr->count >= 0) 
139        nptr->count++;
140      else
141        nptr = 0;
142    }
143  if (ptr)
144    {
145      GPEnabled *old = ptr;
146      ptr = nptr;
147      if (! --old->count) 
148        old->count = -1;
149      gcsCounter.unlock();     
150      if (old->count < 0)
151        old->destroy();
152    }
153  else
154    {
155      ptr = nptr;
156      gcsCounter.unlock();
157    }
158  return *this;
159}
160
161GPBase&
162GPBase::assign (const GPBase &sptr)
163{
164  gcsCounter.lock();
165  if (sptr.ptr) 
166    {
167      sptr.ptr->count++;
168    }
169  if (ptr)
170    {
171      GPEnabled *old = ptr;
172      ptr = sptr.ptr;
173      if (! --old->count) 
174        old->count = -1;
175      gcsCounter.unlock();     
176      if (old->count < 0)
177        old->destroy();
178    }
179  else
180    {
181      ptr = sptr.ptr;
182      gcsCounter.unlock();
183    }
184  return *this;
185}
186
187
188// ------ GPBUFFERBASE
189
190
191void
192GPBufferBase::replace(void *nptr,const size_t n)
193{
194  resize(0,0);
195  ptr=nptr;
196  num=n;
197}
198
199GPBufferBase::GPBufferBase(void *&xptr,const size_t n,const size_t t) 
200  : ptr(xptr), num(n)
201{
202  if (n)
203    xptr = ::operator new(n*t);
204  else
205    xptr = 0;
206}
207
208GPBufferBase::~GPBufferBase()
209{
210  ::operator delete(ptr);
211}
212
213void 
214GPBufferBase::swap(GPBufferBase &other)
215{
216  void * const temp_ptr=ptr;
217  ptr=other.ptr;
218  other.ptr=temp_ptr;
219  const size_t temp_num=num;
220  num=other.num;
221  other.num=temp_num;
222}
223
224void
225GPBufferBase::resize(const size_t n, const size_t t)
226{
227  if(!n && !ptr)
228    {
229      num=0;
230    }
231  else
232    {
233      const size_t s=ptr?(((num<n)?num:n)*t):0;
234      void *nptr;
235      GPBufferBase gnptr(nptr, n, t);
236      if(s)
237        {
238          memcpy(nptr, ptr, s);
239        }
240      swap(gnptr);
241    }
242}
243
244void
245GPBufferBase::set(const size_t t,const char c)
246{
247  if(num)
248    memset(ptr,c,num*t);
249}
250
251
252#ifdef HAVE_NAMESPACES
253}
254# ifndef NOT_USING_DJVU_NAMESPACE
255using namespace DJVU;
256# endif
257#endif
258
Note: See TracBrowser for help on using the repository browser.