vil_memory_chunk.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_memory_chunk.cxx
2 #include <cstring>
3 #include "vil_memory_chunk.h"
4 //:
5 // \file
6 // \brief Ref. counted block of data on the heap
7 // \author Tim Cootes
8 #ifdef _MSC_VER
9 # include <vcl_msvc_warnings.h>
10 #endif
11 #include <cassert>
12 
13 //: Dflt ctor
15 : data_(nullptr), size_(0), pixel_format_(VIL_PIXEL_FORMAT_UNKNOWN), ref_count_(0)
16 {
17 }
18 
19 //: Allocate n bytes of memory
21 : data_(new char[n]), size_(n), pixel_format_(pixel_form), ref_count_(0)
22 {
23  assert(vil_pixel_format_num_components(pixel_form)==1
24  || pixel_form==VIL_PIXEL_FORMAT_UNKNOWN );
25 }
26 
27 //: Destructor
29 {
30  delete [] reinterpret_cast<char*>(data_);
31 }
32 
33 //: Copy ctor
35 : data_(new char[d.size()]), size_(d.size()), pixel_format_(d.pixel_format_), ref_count_(0)
36 {
37  std::memcpy(data_,d.data_,size_);
38 }
39 
40 //: Assignment operator
42 {
43  if (this==&d) return *this;
44 
45  set_size(d.size(),d.pixel_format());
46  std::memcpy(data_,d.data_,size_);
47  return *this;
48 }
49 
50 //: Decrement reference count and call destructor when it becomes zero
52 {
53  assert (ref_count_ >0);
54  // Note: refcount decrement and zero comparison need to happen in the same
55  // statement for this to be thread safe. Otherwise a race condition can
56  // lead to multiple smart pointers deleting the memory.
57  if (--ref_count_==0)
58  {
59  delete [] reinterpret_cast<char*>(data_); data_=nullptr;
60  delete this;
61  }
62 }
63 
64 //: Pointer to first element of data
65 void* vil_memory_chunk::data() { return data_;}
66 
67 //: Pointer to first element of data
68 void* vil_memory_chunk::const_data() const { return data_;}
69 
70 //: Create empty space for n elements.
71 // Leave existing data untouched if the size is already n.
72 void vil_memory_chunk::set_size(unsigned long n, vil_pixel_format pixel_form)
73 {
74  if (size_==n) return;
75  delete [] reinterpret_cast<char*>(data_);
76  data_ = nullptr;
77  if (n>0)
78  data_ = new char[n];
79  size_ = n;
80  pixel_format_ = pixel_form;
81 }
virtual void * data()
Pointer to first element of data.
vil_pixel_format
Describes the type of the concrete data.
std::size_t size() const
Number of bytes allocated.
virtual void * const_data() const
Pointer to first element of data.
virtual void set_size(unsigned long n, vil_pixel_format pixel_format)
Create space for n bytes.
unsigned vil_pixel_format_num_components(enum vil_pixel_format f)
Return the number of components in pixel format f.
std::size_t size_
Number of elements (bytes).
vil_memory_chunk & operator=(const vil_memory_chunk &)
Copy operator.
Ref. counted block of data on the heap.
vcl_atomic_count ref_count_
Reference count.
vil_pixel_format pixel_format_
Indicate what format data is (used for binary IO).
vil_pixel_format pixel_format() const
Indicate what format data is to be saved as in binary IO.
virtual ~vil_memory_chunk()
Destructor.
void unref()
Decrement reference count.
vil_memory_chunk()
Dflt ctor.