test

C++ code posted
created at 22 Oct 20:49

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cassert>
#include <cstddef>
#include <new>
#include <opencv2/opencv.hpp>

class MatAllocator : public cv::MatAllocator {
 public:
  using cv::MatAllocator::MatAllocator;

  virtual void allocate(int dims, const int *sizes, int type, int *& refcount,
                        uchar *& datastart, uchar *& data, std::size_t *step) {
    assert(dims <= 2);
    const std::size_t total_size =
        cv::alignSize(step[0] * sizes[0], sizeof(*refcount));
    datastart = static_cast<uchar *>(alloc(total_size + sizeof(*refcount)));
    data = datastart;
    refcount = reinterpret_cast<int *>(data + total_size);
    *refcount = 1;
  }

  virtual void deallocate(int *refcount, uchar *datastart, uchar *data) {
    assert(refcount != nullptr);
    assert(*refcount == 0);
    free(datastart);
  }

  virtual void * alloc(std::size_t size) throw(std::bad_alloc) {
    assert(size > 0);
    void *address;
    try {
      address = cv::fastMalloc(size);
    } catch (cv::Exception) {
      throw std::bad_alloc();
    }
    assert(address != nullptr);
    return address;
  }

  virtual void free(void *address) {
    cv::fastFree(address);
  }

  static MatAllocator * get_shared();  // Define in source file
};
1.23 KB in 3 ms with coderay