@@ -13,74 +13,78 @@ namespace intel_npu {
13
13
14
14
class BlobContainer {
15
15
public:
16
- virtual void * get_ptr () = 0 ;
16
+ BlobContainer () = default ;
17
17
18
- virtual size_t size () const = 0;
18
+ BlobContainer (std::vector< uint8_t > blob) : _blob(std::move(blob)) {}
19
19
20
- virtual bool release_from_memory () = 0;
21
-
22
- virtual std::vector<uint8_t > get_ownership_blob () = 0;
23
-
24
- virtual ~BlobContainer () = default ;
25
- };
26
-
27
- class BlobContainerVector : public BlobContainer {
28
- public:
29
- BlobContainerVector (std::vector<uint8_t > blob) : _ownershipBlob(std::move(blob)) {}
30
-
31
- void * get_ptr () override {
32
- return reinterpret_cast <void *>(_ownershipBlob.data ());
20
+ virtual const void * get_ptr () const {
21
+ return _blob.data ();
33
22
}
34
23
35
- size_t size () const override {
36
- return _ownershipBlob .size ();
24
+ virtual size_t size () const {
25
+ return _blob .size ();
37
26
}
38
27
39
- bool release_from_memory () override {
40
- _ownershipBlob.clear ();
41
- _ownershipBlob.shrink_to_fit ();
42
- return true ;
28
+ virtual bool release_from_memory () const {
29
+ if (_shouldDeallocate) {
30
+ _blob.clear ();
31
+ _blob.shrink_to_fit ();
32
+ return true ;
33
+ }
34
+ _shouldDeallocate = true ;
35
+ return false ;
43
36
}
44
37
45
- std::vector<uint8_t > get_ownership_blob () override {
46
- return _ownershipBlob;
38
+ virtual const std::vector<uint8_t >& get_blob () const {
39
+ // when unerlying blob object was accessed,
40
+ // prevent deallocation on next `release_from_memory` call
41
+ _shouldDeallocate = false ;
42
+ return _blob;
47
43
}
48
44
45
+ virtual ~BlobContainer () = default ;
46
+
47
+ protected:
48
+ mutable std::vector<uint8_t > _blob;
49
+
49
50
private:
50
- std::vector< uint8_t > _ownershipBlob ;
51
+ mutable bool _shouldDeallocate = true ;
51
52
};
52
53
53
54
class BlobContainerAlignedBuffer : public BlobContainer {
54
55
public:
55
56
BlobContainerAlignedBuffer (const std::shared_ptr<ov::AlignedBuffer>& blobSO,
56
57
size_t ovHeaderOffset,
57
58
uint64_t blobSize)
58
- : _blobSize (blobSize),
59
+ : _size (blobSize),
59
60
_ovHeaderOffset (ovHeaderOffset),
60
- _ownershipBlob (blobSO) {}
61
+ _blobSO (blobSO) {}
61
62
62
- void * get_ptr () override {
63
- return _ownershipBlob ->get_ptr (_ovHeaderOffset);
63
+ const void * get_ptr () const override {
64
+ return _blobSO ->get_ptr (_ovHeaderOffset);
64
65
}
65
66
66
67
size_t size () const override {
67
- return _blobSize ;
68
+ return _size ;
68
69
}
69
70
70
- bool release_from_memory () override {
71
+ bool release_from_memory () const override {
72
+ BlobContainer::release_from_memory ();
71
73
return false ;
72
74
}
73
75
74
- std::vector<uint8_t > get_ownership_blob () override {
75
- std::vector<uint8_t > blob (_blobSize);
76
- blob.assign (reinterpret_cast <const uint8_t *>(this ->get_ptr ()), reinterpret_cast <const uint8_t *>(this ->get_ptr ()) + this ->size ());
77
- return blob;
76
+ const std::vector<uint8_t >& get_blob () const override {
77
+ BlobContainer::release_from_memory ();
78
+ _blob.resize (_size);
79
+ _blob.assign (reinterpret_cast <const uint8_t *>(this ->get_ptr ()),
80
+ reinterpret_cast <const uint8_t *>(this ->get_ptr ()) + _size);
81
+ return _blob;
78
82
}
79
83
80
84
private:
81
- uint64_t _blobSize ;
85
+ uint64_t _size ;
82
86
size_t _ovHeaderOffset;
83
- std::shared_ptr<ov::AlignedBuffer> _ownershipBlob ;
87
+ std::shared_ptr<ov::AlignedBuffer> _blobSO ;
84
88
};
85
89
86
90
} // namespace intel_npu
0 commit comments