core/vil/vil_memory_chunk.cxx

Go to the documentation of this file.
00001 // This is core/vil/vil_memory_chunk.cxx
00002 #include "vil_memory_chunk.h"
00003 //:
00004 // \file
00005 // \brief Ref. counted block of data on the heap
00006 // \author Tim Cootes
00007 #include <vcl_cstring.h>
00008 #include <vcl_cassert.h>
00009 
00010 //: Dflt ctor
00011 vil_memory_chunk::vil_memory_chunk()
00012 : data_(0), size_(0), pixel_format_(VIL_PIXEL_FORMAT_UNKNOWN), ref_count_(0)
00013 {
00014 }
00015 
00016 //: Allocate n bytes of memory
00017 vil_memory_chunk::vil_memory_chunk(unsigned long n, vil_pixel_format pixel_form)
00018 : data_(new char[n]), size_(n), pixel_format_(pixel_form), ref_count_(0)
00019 {
00020   assert(vil_pixel_format_num_components(pixel_form) == 1);
00021 }
00022 
00023 //: Destructor
00024 vil_memory_chunk::~vil_memory_chunk()
00025 {
00026   delete [] reinterpret_cast<char*>(data_);
00027 }
00028 
00029 //: Copy ctor
00030 vil_memory_chunk::vil_memory_chunk(const vil_memory_chunk& d)
00031 : data_(new char[d.size()]), size_(d.size()), pixel_format_(d.pixel_format_), ref_count_(0)
00032 {
00033   vcl_memcpy(data_,d.data_,size_);
00034 }
00035 
00036 //: Assignment operator
00037 vil_memory_chunk& vil_memory_chunk::operator=(const vil_memory_chunk& d)
00038 {
00039   if (this==&d) return *this;
00040 
00041   set_size(d.size(),d.pixel_format());
00042   vcl_memcpy(data_,d.data_,size_);
00043   return *this;
00044 }
00045 
00046 //: Decrement reference count and call destructor when it becomes zero
00047 void vil_memory_chunk::unref()
00048 {
00049   assert (ref_count_ >0);
00050   --ref_count_;
00051   if (ref_count_==0)
00052   {
00053     delete [] reinterpret_cast<char*>(data_); data_=0;
00054     delete this;
00055   }
00056 }
00057 
00058 //: Create empty space for n elements.
00059 //  Leave existing data untouched if the size is already n.
00060 void vil_memory_chunk::set_size(unsigned long n, vil_pixel_format pixel_form)
00061 {
00062   if (size_==n) return;
00063   delete [] reinterpret_cast<char*>(data_);
00064   data_ = 0;
00065   if (n>0)
00066     data_ = new char[n];
00067   size_ = n;
00068   pixel_format_ = pixel_form;
00069 }
00070 
00071 

Generated on Sun Sep 7 05:07:48 2008 for core/vil by  doxygen 1.5.1