-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMemoryHandler.h
More file actions
99 lines (77 loc) · 2.81 KB
/
MemoryHandler.h
File metadata and controls
99 lines (77 loc) · 2.81 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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include <Common/Fifo.h>
#include <memory>
#include <mutex>
#include <stdint.h>
#include <string>
#include "DataBlockContainer.h"
#include "ReadoutUtils.h"
struct MemoryRegion {
unsigned long long size; // size of memory block
void* ptr; // pointer to memory block
std::string name; // name of the region
// todo: add callback to release region afterwards
unsigned long long usedSize; // amount of memory in use
};
extern std::unique_ptr<MemoryRegion> bigBlock;
// todo: named list of big blocks, with get/set functions
// todo: big blocks: one different per equipment to avoid lock
// a big block of memory for I/O
class MemoryHandler
{
public:
MemoryHandler(int pageSize, int numberOfPages);
~MemoryHandler();
void* getPage();
void freePage(void*);
void* getBaseAddress();
size_t getSize();
size_t getPageSize();
private:
size_t memorySize; // total size of buffer
size_t pageSize; // size of each superpage in buffer (not the one of getpagesize())
size_t numberOfPages; // number of pages allocated
uint8_t* baseAddress; // base address of buffer
std::unique_ptr<AliceO2::Common::Fifo<long>> pagesAvailable; // a buffer to keep track of individual pages. storing offset (with respect to base address) of pages available
};
class DataBlockContainerFromMemoryHandler : public DataBlockContainer
{
private:
std::shared_ptr<MemoryHandler> mMemoryHandler;
public:
DataBlockContainerFromMemoryHandler(std::shared_ptr<MemoryHandler> const& h) : DataBlockContainer((DataBlock*)nullptr)
{
mMemoryHandler = h;
data = nullptr;
try {
data = new DataBlock;
} catch (...) {
throw __LINE__;
}
data->data = (char*)h->getPage();
if (data->data == nullptr) {
delete data;
throw __LINE__;
}
// printf("In use: page %p\n",data->data);
}
~DataBlockContainerFromMemoryHandler()
{
if (data != nullptr) {
// printf("~DataBlockContainerFromMemoryHandler(%p) : header=%p data=%p\n",this,data,data->data);
if (data->data != nullptr) {
mMemoryHandler->freePage(data->data);
}
delete data;
}
}
};