Title / Description
Code #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 };
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code