source: trunk/libdjvu/GSmartPointer.cpp @ 280

Last change on this file since 280 was 280, checked in by rbri, 11 years ago

DJVU plugin: djvulibre updated to version 3.5.22

File size: 5.9 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.26 2008/08/03 03:54:01 leonb Exp $
57// $Name: release_3_5_22 $
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#if PARANOID_DEBUG
75# include <assert.h>
76#endif
77
78#include "GThreads.h"
79#include "GSmartPointer.h"
80#include "GException.h"
81
82#ifdef HAVE_NAMESPACES
83namespace DJVU {
84# ifdef NOT_DEFINED // Just to fool emacs c++ mode
85}
86#endif
87#endif
88
89
90// ------ GPENABLED
91
92GPEnabled::~GPEnabled()
93{
94  if (count > 0)
95    G_THROW( ERR_MSG("GSmartPointer.suspicious") );
96}
97
98void
99GPEnabled::destroy()
100{
101  // Only delete if the counter is still zero.
102  // because someone may have rescued the object...
103  // If yes, set the counter to -0x7fff to mark
104  // the object as doomed and make sure things
105  // will work if the destructor uses a GP...
106  if (atomicCompareAndSwap(&count, 0, -0x7fff))
107    delete this;
108}
109
110
111// ------ GPBASE
112
113
114#define LOCKMASK 0x3f
115#define LOCKIDX(addr) ((((size_t)(addr))/sizeof(void*))&LOCKMASK)
116static int volatile locks[LOCKMASK+1];
117
118
119GPBase&
120GPBase::assign (const GPBase &sptr)
121{
122  int volatile *lockb = locks+LOCKIDX(&sptr);
123  atomicAcquireOrSpin(lockb);
124  GPEnabled *nptr = sptr.ptr;
125  if (nptr)
126    nptr->ref();
127  atomicRelease(lockb);
128  int volatile *locka = locks+LOCKIDX(this);
129  atomicAcquireOrSpin(locka);
130  GPEnabled *old = ptr;
131  ptr = nptr;
132  atomicRelease(locka);
133  if (old)
134    old->unref();
135  return *this;
136}
137
138GPBase&
139GPBase::assign (GPEnabled *nptr)
140{
141  if (nptr)
142    nptr->ref();
143  int volatile *locka = locks+LOCKIDX(this);
144  atomicAcquireOrSpin(locka);
145  GPEnabled *old = ptr;
146  ptr = nptr;
147  atomicRelease(locka);
148  if (old)
149    old->unref();
150  return *this;
151}
152
153
154
155
156// ------ GPBUFFERBASE
157
158
159void
160GPBufferBase::replace(void *nptr,const size_t n)
161{
162  resize(0,0);
163  ptr=nptr;
164  num=n;
165}
166
167GPBufferBase::GPBufferBase(void *&xptr,const size_t n,const size_t t) 
168  : ptr(xptr), num(n)
169{
170  if (n)
171    xptr = ::operator new(n*t);
172  else
173    xptr = 0;
174}
175
176GPBufferBase::~GPBufferBase()
177{
178  ::operator delete(ptr);
179}
180
181void 
182GPBufferBase::swap(GPBufferBase &other)
183{
184  void * const temp_ptr=ptr;
185  ptr=other.ptr;
186  other.ptr=temp_ptr;
187  const size_t temp_num=num;
188  num=other.num;
189  other.num=temp_num;
190}
191
192void
193GPBufferBase::resize(const size_t n, const size_t t)
194{
195  if(!n && !ptr)
196    {
197      num=0;
198    }
199  else
200    {
201      const size_t s=ptr?(((num<n)?num:n)*t):0;
202      void *nptr;
203      GPBufferBase gnptr(nptr, n, t);
204      if(s)
205        {
206          memcpy(nptr, ptr, s);
207        }
208      swap(gnptr);
209    }
210}
211
212void
213GPBufferBase::set(const size_t t,const char c)
214{
215  if(num)
216    memset(ptr,c,num*t);
217}
218
219
220#ifdef HAVE_NAMESPACES
221}
222# ifndef NOT_USING_DJVU_NAMESPACE
223using namespace DJVU;
224# endif
225#endif
226
Note: See TracBrowser for help on using the repository browser.