source: trunk/libdjvu/GSmartPointer.cpp @ 101

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

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

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