Blame libdjvu/GSmartPointer.cpp

Packit df99a1
//C-  -*- C++ -*-
Packit df99a1
//C- -------------------------------------------------------------------
Packit df99a1
//C- DjVuLibre-3.5
Packit df99a1
//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
Packit df99a1
//C- Copyright (c) 2001  AT&T
Packit df99a1
//C-
Packit df99a1
//C- This software is subject to, and may be distributed under, the
Packit df99a1
//C- GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- or (at your option) any later version. The license should have
Packit df99a1
//C- accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C-
Packit df99a1
//C- This program is distributed in the hope that it will be useful,
Packit df99a1
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit df99a1
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit df99a1
//C- GNU General Public License for more details.
Packit df99a1
//C- 
Packit df99a1
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
Packit df99a1
//C- Lizardtech Software.  Lizardtech Software has authorized us to
Packit df99a1
//C- replace the original DjVu(r) Reference Library notice by the following
Packit df99a1
//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
Packit df99a1
//C-
Packit df99a1
//C-  ------------------------------------------------------------------
Packit df99a1
//C- | DjVu (r) Reference Library (v. 3.5)
Packit df99a1
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
Packit df99a1
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
Packit df99a1
//C- | 6,058,214 and patents pending.
Packit df99a1
//C- |
Packit df99a1
//C- | This software is subject to, and may be distributed under, the
Packit df99a1
//C- | GNU General Public License, either Version 2 of the license,
Packit df99a1
//C- | or (at your option) any later version. The license should have
Packit df99a1
//C- | accompanied the software or you may obtain a copy of the license
Packit df99a1
//C- | from the Free Software Foundation at http://www.fsf.org .
Packit df99a1
//C- |
Packit df99a1
//C- | The computer code originally released by LizardTech under this
Packit df99a1
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
Packit df99a1
//C- | ORIGINAL CODE."  Subject to any third party intellectual property
Packit df99a1
//C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
Packit df99a1
//C- | non-exclusive license to make, use, sell, or otherwise dispose of 
Packit df99a1
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
Packit df99a1
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
Packit df99a1
//C- | General Public License.   This grant only confers the right to 
Packit df99a1
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
Packit df99a1
//C- | the extent such infringement is reasonably necessary to enable 
Packit df99a1
//C- | recipient to make, have made, practice, sell, or otherwise dispose 
Packit df99a1
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
Packit df99a1
//C- | any greater extent that may be necessary to utilize further 
Packit df99a1
//C- | modifications or combinations.
Packit df99a1
//C- |
Packit df99a1
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
Packit df99a1
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
Packit df99a1
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
Packit df99a1
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit df99a1
//C- +------------------------------------------------------------------
Packit df99a1
Packit df99a1
#ifdef HAVE_CONFIG_H
Packit df99a1
# include "config.h"
Packit df99a1
#endif
Packit df99a1
#if NEED_GNUG_PRAGMAS
Packit df99a1
# pragma implementation
Packit df99a1
#endif
Packit df99a1
Packit df99a1
// - Author: Leon Bottou, 05/1997
Packit df99a1
Packit df99a1
// From: Leon Bottou, 1/31/2002
Packit df99a1
// Class GPBuffer has been added (but not documented) by Lizardtech.
Packit df99a1
// Our original implementation consisted of multiple classes.
Packit df99a1
// <http://prdownloads.sourceforge.net/djvu/DjVu2_2b-src.tgz>.
Packit df99a1
Packit df99a1
#include <stddef.h>
Packit df99a1
#include <string.h>
Packit df99a1
#if PARANOID_DEBUG
Packit df99a1
# include <assert.h>
Packit df99a1
#endif
Packit df99a1
Packit df99a1
#include "GThreads.h"
Packit df99a1
#include "GSmartPointer.h"
Packit df99a1
#include "GException.h"
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
namespace DJVU {
Packit df99a1
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
Packit df99a1
}
Packit df99a1
#endif
Packit df99a1
#endif
Packit df99a1
Packit df99a1
Packit df99a1
// ------ GPENABLED
Packit df99a1
Packit df99a1
GPEnabled::~GPEnabled()
Packit df99a1
{
Packit df99a1
  if (count > 0)
Packit df99a1
    G_THROW( ERR_MSG("GSmartPointer.suspicious") );
Packit df99a1
}
Packit df99a1
Packit df99a1
void
Packit df99a1
GPEnabled::destroy()
Packit df99a1
{
Packit df99a1
  // Only delete if the counter is still zero.
Packit df99a1
  // because someone may have rescued the object...
Packit df99a1
  // If yes, set the counter to -0x7fff to mark 
Packit df99a1
  // the object as doomed and make sure things
Packit df99a1
  // will work if the destructor uses a GP...
Packit df99a1
  if (! atomicCompareAndSwap(&count, 0, -0x7fff))
Packit df99a1
    delete this;
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
// ------ GPBASE
Packit df99a1
Packit df99a1
Packit df99a1
GPBase&
Packit df99a1
GPBase::assign (const GPBase &sptr)
Packit df99a1
{
Packit df99a1
  GPEnabled *nptr = sptr.ptr;
Packit df99a1
  if (nptr && atomicIncrement(&nptr->count) <= 0)
Packit df99a1
    nptr = 0;
Packit df99a1
  GPEnabled *optr = (GPEnabled*)atomicExchangePointer((void**)&ptr, (void*)nptr);
Packit df99a1
  if (optr)
Packit df99a1
    optr->unref();
Packit df99a1
  return *this;
Packit df99a1
}
Packit df99a1
Packit df99a1
GPBase&
Packit df99a1
GPBase::assign (GPEnabled *nptr)
Packit df99a1
{
Packit df99a1
  if (nptr && atomicIncrement(&nptr->count) <= 0)
Packit df99a1
    nptr = 0;
Packit df99a1
  GPEnabled *optr = (GPEnabled*)atomicExchangePointer((void**)&ptr, (void*)nptr);
Packit df99a1
  if (optr)
Packit df99a1
    optr->unref();
Packit df99a1
  return *this;
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
Packit df99a1
// ------ GPBUFFERBASE
Packit df99a1
Packit df99a1
Packit df99a1
void
Packit df99a1
GPBufferBase::replace(void *nptr,const size_t n)
Packit df99a1
{
Packit df99a1
  resize(0,0);
Packit df99a1
  ptr=nptr;
Packit df99a1
  num=n;
Packit df99a1
}
Packit df99a1
Packit df99a1
GPBufferBase::GPBufferBase(void *&xptr,const size_t n,const size_t t) 
Packit df99a1
  : ptr(xptr), num(n)
Packit df99a1
{
Packit df99a1
  if (n)
Packit df99a1
    xptr = ::operator new(n*t);
Packit df99a1
  else
Packit df99a1
    xptr = 0;
Packit df99a1
}
Packit df99a1
Packit df99a1
GPBufferBase::~GPBufferBase()
Packit df99a1
{
Packit df99a1
  ::operator delete(ptr);
Packit df99a1
}
Packit df99a1
Packit df99a1
void 
Packit df99a1
GPBufferBase::swap(GPBufferBase &other)
Packit df99a1
{
Packit df99a1
  void * const temp_ptr=ptr;
Packit df99a1
  ptr=other.ptr;
Packit df99a1
  other.ptr=temp_ptr;
Packit df99a1
  const size_t temp_num=num;
Packit df99a1
  num=other.num;
Packit df99a1
  other.num=temp_num;
Packit df99a1
}
Packit df99a1
Packit df99a1
void
Packit df99a1
GPBufferBase::resize(const size_t n, const size_t t)
Packit df99a1
{
Packit df99a1
  if(!n && !ptr)
Packit df99a1
    {
Packit df99a1
      num=0;
Packit df99a1
    }
Packit df99a1
  else
Packit df99a1
    {
Packit df99a1
      const size_t s=ptr?(((num
Packit df99a1
      void *nptr;
Packit df99a1
      GPBufferBase gnptr(nptr, n, t);
Packit df99a1
      if(s)
Packit df99a1
        {
Packit df99a1
          memcpy(nptr, ptr, s);
Packit df99a1
        }
Packit df99a1
      swap(gnptr);
Packit df99a1
    }
Packit df99a1
}
Packit df99a1
Packit df99a1
void
Packit df99a1
GPBufferBase::set(const size_t t,const char c)
Packit df99a1
{
Packit df99a1
  if(num)
Packit df99a1
    memset(ptr,c,num*t);
Packit df99a1
}
Packit df99a1
Packit df99a1
Packit df99a1
#ifdef HAVE_NAMESPACES
Packit df99a1
}
Packit df99a1
# ifndef NOT_USING_DJVU_NAMESPACE
Packit df99a1
using namespace DJVU;
Packit df99a1
# endif
Packit df99a1
#endif
Packit df99a1