-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuni_mem_allocator.cpp
More file actions
85 lines (70 loc) · 1.92 KB
/
uni_mem_allocator.cpp
File metadata and controls
85 lines (70 loc) · 1.92 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// ***********************************************************************
//
// Demo program for education in subject
// Computer Architectures and Parallel Systems.
// Petr Olivka, dep. of Computer Science, FEI, VSB-TU Ostrava, 2020/11
// email:petr.olivka@vsb.cz
//
// Uniform memory allocator for class Mat
//
// ***********************************************************************
#include "uni_mem_allocator.h"
cv::UMatData* UniformAllocator::allocate(int dims, const int* sizes, int type,
void* data0, size_t* step,
AccessFlag_v_3_4 /*flags*/, cv::UMatUsageFlags /*usageFlags*/) const
{
size_t total = CV_ELEM_SIZE(type);
for (int i = dims-1; i >= 0; i--)
{
if (step)
{
if (data0 && step[i] != CV_AUTOSTEP)
{
CV_Assert(total <= step[i]);
total = step[i];
}
else
{
step[i] = total;
}
}
total *= sizes[i];
}
cv::UMatData* u = new cv::UMatData(this);
u->size = total;
if (data0)
{
u->data = u->origdata = static_cast<uchar*>(data0);
u->flags |= cv::UMatData::USER_ALLOCATED;
}
else
{
void* ptr = 0;
if(cudaMallocManaged(&ptr, total) != cudaSuccess)
{
abort();
}
u->data = u->origdata = static_cast<uchar*>(ptr);
}
return u;
}
bool UniformAllocator::allocate(cv::UMatData* u, AccessFlag_v_3_4 /*accessFlags*/, cv::UMatUsageFlags /*usageFlags*/) const
{
return (u != NULL);
}
void UniformAllocator::deallocate(cv::UMatData* u) const
{
if (!u)
return;
CV_Assert(u->urefcount >= 0);
CV_Assert(u->refcount >= 0);
if (u->refcount == 0)
{
if ( !(u->flags & cv::UMatData::USER_ALLOCATED) )
{
cudaFree(u->origdata);
u->origdata = 0;
}
delete u;
}
}