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
BlobRef(const void * data_,const size_t length_)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
~BlobRef(void)33 Magick::BlobRef::~BlobRef(void)
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
decrease()44 size_t Magick::BlobRef::decrease()
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 }
56 count=--_refCount;
57 _mutexLock.unlock();
58 return(count);
59 }
60
increase()61 void Magick::BlobRef::increase()
62 {
63 _mutexLock.lock();
64 _refCount++;
65 _mutexLock.unlock();
66 }
67