Use a shared_ptr<vector> for ArrayData rather than a shared_array.
authorCarl Hetherington <cth@carlh.net>
Sun, 16 Apr 2023 20:07:10 +0000 (22:07 +0200)
committerCarl Hetherington <cth@carlh.net>
Sun, 16 Apr 2023 20:14:26 +0000 (22:14 +0200)
This is simpler and allows us to remove the hack of allocating some
"maximum" buffer for incoming JPEG2000 data.

It does mean that the buffer is zero-initialized before being written
to, but hopefully that doesn't matter too much.

src/array_data.cc
src/array_data.h
src/j2k_transcode.cc
src/smpte_subtitle_asset.cc

index 1234ba22383ec1fc13a8af539c32ad3e242f0365..52a22cf67b706e04bbb5ffc3a11dd8df288e91f7 100644 (file)
 #include <cstdio>
 
 
-using boost::shared_array;
 using namespace dcp;
 
 
 ArrayData::ArrayData ()
+       : _data(std::make_shared<std::vector<uint8_t>>())
 {
 
 }
 
 
 ArrayData::ArrayData (int size)
-       : _data (new uint8_t[size])
-       , _size (size)
+       : _data(std::make_shared<std::vector<uint8_t>>(size))
 {
 
 }
 
 
 ArrayData::ArrayData (uint8_t const * data, int size)
-       : _data (new uint8_t[size])
-       , _size (size)
-{
-       memcpy (_data.get(), data, size);
-}
-
-
-ArrayData::ArrayData (shared_array<uint8_t> data, int size)
-       : _data (data)
-       , _size (size)
+       : _data(std::make_shared<std::vector<uint8_t>>(data, data + size))
 {
 
 }
@@ -81,15 +71,15 @@ ArrayData::ArrayData (shared_array<uint8_t> data, int size)
 
 ArrayData::ArrayData (boost::filesystem::path file)
 {
-       _size = boost::filesystem::file_size (file);
-       _data.reset (new uint8_t[_size]);
+       auto const size = boost::filesystem::file_size (file);
+       _data = std::make_shared<std::vector<uint8_t>>(size);
 
        File f(file, "rb");
        if (!f) {
                throw FileError ("could not open file for reading", file, errno);
        }
 
-       if (f.read(_data.get(), 1, _size) != static_cast<size_t>(_size)) {
+       if (f.read(_data->data(), 1, size) != static_cast<size_t>(size)) {
                throw FileError ("could not read from file", file, errno);
        }
 }
index da7229bbcce646a3a96b9c2f4d2684e4ff0ef61a..7a425f4471495581ca3b39360bed3fc52fb425ab 100644 (file)
@@ -58,43 +58,33 @@ public:
        explicit ArrayData (int size);
        ArrayData (uint8_t const * data, int size);
 
-       /** Create an ArrayData by copying a shared_array<>
-        *  @param data shared_array<> to copy (the shared_array<> is copied, not the data)
-        *  @param size Size of data in bytes
-        */
-       ArrayData (boost::shared_array<uint8_t> data, int size);
-
        /** Create an ArrayData by reading the contents of a file
         *  @param file Filename to read
         */
-       explicit ArrayData (boost::filesystem::path file);
-
-       virtual ~ArrayData () {}
+       explicit ArrayData(boost::filesystem::path file);
 
        uint8_t const * data () const override {
-               return _data.get();
+               return _data->data();
        }
 
        uint8_t * data () override {
-               return _data.get();
+               return _data->data();
        }
 
        /** @return size of the data in _data, or whatever was last
         *  passed to a set_size() call
         */
        int size () const override {
-               return _size;
+               return _data->size();
        }
 
        /** Set the size that will be returned from size() */
        void set_size (int s) {
-               _size = s;
+               _data->resize(s);
        }
 
 private:
-       boost::shared_array<uint8_t> _data;
-       /** amount of `valid' data in _data; the array may be larger */
-       int _size = 0;
+       std::shared_ptr<std::vector<uint8_t>> _data;
 };
 
 
index 686bc0409c31445dca7162fe8d4b9c47e265a19e..664d18a34b2fc08378353da460229e0e09eff5a8 100644 (file)
@@ -188,23 +188,14 @@ dcp::decompress_j2k (uint8_t const * data, int64_t size, int reduce)
 class WriteBuffer
 {
 public:
-/* XXX: is there a better strategy for this? */
-#define MAX_J2K_SIZE (1024 * 1024 * 2)
-       WriteBuffer ()
-               : _data (shared_array<uint8_t>(new uint8_t[MAX_J2K_SIZE]), MAX_J2K_SIZE)
-               , _offset (0)
-       {
-               _data.set_size (0);
-       }
-
        OPJ_SIZE_T write (void* buffer, OPJ_SIZE_T nb_bytes)
        {
-               DCP_ASSERT ((_offset + nb_bytes) < MAX_J2K_SIZE);
-               memcpy (_data.data() + _offset, buffer, nb_bytes);
-               _offset += nb_bytes;
-               if (_offset > OPJ_SIZE_T(_data.size())) {
-                       _data.set_size (_offset);
+               auto const new_offset = _offset + nb_bytes;
+               if (new_offset > OPJ_SIZE_T(_data.size())) {
+                       _data.set_size(new_offset);
                }
+               memcpy(_data.data() + _offset, buffer, nb_bytes);
+               _offset = new_offset;
                return nb_bytes;
        }
 
@@ -221,7 +212,7 @@ public:
 
 private:
        ArrayData _data;
-       OPJ_SIZE_T _offset;
+       OPJ_SIZE_T _offset = 0;
 };
 
 
index 4b2ae8cd40e8fd03bdac0dc84447b49a76bf9ea9..6db90b2eeb4699440017273db77de303905cda51 100644 (file)
@@ -238,9 +238,6 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                char id[64];
                Kumu::bin2UUIDhex (i->ResourceID, ASDCP::UUIDlen, id, sizeof(id));
 
-               shared_array<uint8_t> data (new uint8_t[buffer.Size()]);
-               memcpy (data.get(), buffer.RoData(), buffer.Size());
-
                switch (i->Type) {
                case ASDCP::TimedText::MT_OPENTYPE:
                {
@@ -250,7 +247,7 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                        }
 
                        if (j != _load_font_nodes.end ()) {
-                               _fonts.push_back (Font ((*j)->id, (*j)->urn, ArrayData (data, buffer.Size ())));
+                               _fonts.push_back(Font((*j)->id, (*j)->urn, ArrayData(buffer.RoData(), buffer.Size())));
                        }
                        break;
                }
@@ -262,7 +259,7 @@ SMPTESubtitleAsset::read_mxf_resources (shared_ptr<ASDCP::TimedText::MXFReader>
                        }
 
                        if (j != _subtitles.end()) {
-                               dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image (ArrayData(data, buffer.Size()));
+                               dynamic_pointer_cast<SubtitleImage>(*j)->set_png_image(ArrayData(buffer.RoData(), buffer.Size()));
                        }
                        break;
                }