-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPMEMAllocator.cpp
More file actions
149 lines (130 loc) · 4.23 KB
/
Copy pathPMEMAllocator.cpp
File metadata and controls
149 lines (130 loc) · 4.23 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
// SPDX-License-Identifier: BSD-3-Clause-Clear
#include "QC/Infras/Memory/PMEMAllocator.hpp"
#include <pmem.h>
#include <unistd.h>
namespace QC
{
namespace Memory
{
PMEMAllocator::PMEMAllocator( const QCMemoryAllocatorConfigInit_t &config,
const QCMemoryAllocator_e allocator )
: QCMemoryAllocatorIfs( config, allocator )
{
(void) QC_LOGGER_INIT( GetConfiguration().name.c_str(), LOGGER_LEVEL_ERROR );
pmem_init();
}
PMEMAllocator::~PMEMAllocator()
{
(void) QC_LOGGER_DEINIT();
pmem_deinit();
}
QCStatus_e PMEMAllocator::Allocate( const QCBufferPropBase_t &request,
QCBufferDescriptorBase_t &response )
{
QCStatus_e status = QC_STATUS_OK;
uint32_t pmemId = 0;
uint32_t pmemFlags = PMEM_FLAGS_CACHE_NONE | PMEM_FLAGS_PHYS_NON_CONTIG | PMEM_FLAGS_SHMEM;
pmem_handle_t pmemHandle = nullptr;
if ( QC_CACHEABLE_NON != request.cache )
{
pmemFlags |= PMEM_FLAGS_CACHE_WB_WA;
}
if ( 0 == request.size )
{
QC_LOG_ERROR( "0 == request.size" );
status = QC_STATUS_BAD_ARGUMENTS;
}
else if ( QC_STATUS_OK != AllocaotrorToPMEM_ID( GetConfiguration().type, pmemId ) )
{
QC_LOG_ERROR( "AllocaotrorToPMEM_ID failed" );
status = QC_STATUS_BAD_ARGUMENTS;
}
if ( QC_STATUS_OK == status )
{
response.pBuf = pmem_malloc_ext_v2( static_cast<uint32_t>(request.size), pmemId, pmemFlags, static_cast<uint32_t>(request.alignment), 0x0,
&pmemHandle, NULL );
if ( nullptr == response.pBuf )
{
QC_LOG_ERROR( "pmem_malloc_ext_v2 allocate failed" );
status = QC_STATUS_NOMEM;
}
else
{
response.dmaHandle = static_cast<uint64_t>( (uintptr_t) pmemHandle );
response.size = request.size;
response.allocatorType = GetConfiguration().type;
response.alignment = request.alignment;
response.cache = request.cache;
response.pid = getpid();
response.name = GetConfiguration().name + "-" + std::to_string( response.dmaHandle );
}
}
return status;
}
QCStatus_e PMEMAllocator::Free( const QCBufferDescriptorBase_t &BufferDescriptor )
{
QCStatus_e status = QC_STATUS_OK;
if ( nullptr == BufferDescriptor.pBuf )
{
status = QC_STATUS_NULL_PTR;
QC_ERROR( "nullptr == BufferDescriptor.pBuf" );
}
else if ( 0 == BufferDescriptor.dmaHandle )
{
QC_ERROR( "0 == BufferDescriptor.dmaHandle" );
status = QC_STATUS_BAD_ARGUMENTS;
}
else if ( 0 == BufferDescriptor.size )
{
QC_ERROR( "BufferDescriptor.size != 0" );
status = QC_STATUS_BAD_ARGUMENTS;
}
else if ( BufferDescriptor.allocatorType != GetConfiguration().type )
{
status = QC_STATUS_BAD_ARGUMENTS;
QC_ERROR( "BufferDescriptor.allocatorType !=GetConfiguration().type" );
}
else
{
int rc = pmem_free( BufferDescriptor.pBuf );
if ( 0 != rc )
{
QC_LOG_ERROR( "Free failed to do free for buffer %p: %d", BufferDescriptor.pBuf, rc );
status = QC_STATUS_FAIL;
}
}
return status;
}
QCStatus_e PMEMAllocator::AllocaotrorToPMEM_ID( const QCMemoryAllocator_e &allocatorType,
uint32_t &pmemId )
{
QCStatus_e status = QC_STATUS_OK;
switch ( allocatorType )
{
case QC_MEMORY_ALLOCATOR_DMA:
pmemId = PMEM_DMA_ID;
break;
case QC_MEMORY_ALLOCATOR_DMA_CAMERA:
pmemId = PMEM_CAMERA_ID;
break;
case QC_MEMORY_ALLOCATOR_DMA_GPU:
pmemId = PMEM_GRAPHICS_FRAMEBUFFER_ID;
break;
case QC_MEMORY_ALLOCATOR_DMA_VPU:
pmemId = PMEM_VIDEO_ID;
break;
case QC_MEMORY_ALLOCATOR_DMA_EVA:
pmemId = PMEM_EVA_ID;
break;
case QC_MEMORY_ALLOCATOR_DMA_HTP:
pmemId = PMEM_DSP_ID;
break;
default:
status = QC_STATUS_BAD_ARGUMENTS;
break;
}
return status;
}
} // namespace Memory
} // namespace QC