Magick++  7.0.8
Convert, Edit, Or Compose Bitmap Images
BlobRef.cpp
Go to the documentation of this file.
1 // This may look like C code, but it is really -*- C++ -*-
2 //
3 // Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2004
4 // Copyright Dirk Lemstra 2014-2015
5 //
6 // Implementation of Blob
7 //
8 
9 #define MAGICKCORE_IMPLEMENTATION 1
10 #define MAGICK_PLUSPLUS_IMPLEMENTATION 1
11 
12 #include "Magick++/Include.h"
13 #include "Magick++/BlobRef.h"
14 #include "Magick++/Exception.h"
15 #include "Magick++/Thread.h"
16 
17 #include <string.h>
18 
19 Magick::BlobRef::BlobRef(const void* data_,const size_t length_)
20  : allocator(Magick::Blob::NewAllocator),
21  length(length_),
22  data((void*) NULL),
23  _mutexLock(),
24  _refCount(1)
25 {
26  if (data_ != (const void*) NULL)
27  {
28  data=new unsigned char[length_];
29  memcpy(data,data_,length_);
30  }
31 }
32 
34 {
35  if (allocator == Magick::Blob::NewAllocator)
36  {
37  delete[] static_cast<unsigned char*>(data);
38  data=(void *) NULL;
39  }
40  else if (allocator == Magick::Blob::MallocAllocator)
41  data=(void *) RelinquishMagickMemory(data);
42 }
43 
45 {
46  size_t
47  count;
48 
49  _mutexLock.lock();
50  if (_refCount == 0)
51  {
52  _mutexLock.unlock();
53  throwExceptionExplicit(MagickCore::OptionError,
54  "Invalid call to decrease");
55  return(0);
56  }
57  count=--_refCount;
58  _mutexLock.unlock();
59  return(count);
60 }
61 
63 {
64  _mutexLock.lock();
65  _refCount++;
66  _mutexLock.unlock();
67 }
BlobRef(const void *data_, const size_t length_)
Definition: BlobRef.cpp:19
void * data
Definition: BlobRef.h:39
void increase()
Definition: BlobRef.cpp:62
MagickPPExport void throwExceptionExplicit(const MagickCore::ExceptionType severity_, const char *reason_, const char *description_=(char *) NULL)
Definition: Exception.cpp:806
size_t decrease()
Definition: BlobRef.cpp:44
Definition: Blob.h:15
~BlobRef(void)
Definition: BlobRef.cpp:33