This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Well managed file descriptor and mmap-ed memory #454
Merged
+41
−151
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3460cce
Introduce FDDeleter
7751359
Remove mapPhysicalMemory
6b14514
Implement FDManager
8da7d3f
Remove old MappedMem
f30347c
Remane FDManager to FileDescriptor
6334905
Merge branch 'master' into feature/fd-manager
primenumber ee95be0
Fix renamed class name
bda8583
Merge branch 'master' into feature/fd-manager
tkng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,55 @@ limitations under the License. | |
#include <stdint.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <memory> | ||
#include <system_error> | ||
|
||
class FDManager { | ||
public: | ||
FDManager() : fd(-1) {} | ||
FDManager(int fd) : fd(fd) {} | ||
~FDManager() { | ||
if (fd >= 0) { | ||
close(fd); | ||
} | ||
} | ||
operator int() const { return fd; } | ||
private: | ||
int fd; | ||
}; | ||
|
||
class SimpleMappedMem { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
public: | ||
SimpleMappedMem(std::size_t base, std::size_t size) : length(0) { | ||
FDManager fd(open("/dev/mem", O_RDWR | O_SYNC)); | ||
if (fd == -1) { | ||
return; | ||
} | ||
int rw = PROT_READ | PROT_WRITE; | ||
ptr = mmap(nullptr, size, rw, MAP_SHARED, fd, base); | ||
if (ptr == MAP_FAILED) { | ||
throw std::system_error(errno, std::generic_category()); | ||
} | ||
length = size; | ||
} | ||
~SimpleMappedMem() { | ||
if (ptr != MAP_FAILED) { | ||
munmap(ptr, length); | ||
} | ||
} | ||
void* get() const { return ptr; } | ||
private: | ||
void* ptr; | ||
std::size_t length; | ||
}; | ||
|
||
class MappedMem | ||
{ | ||
public: | ||
using memtype = volatile void; | ||
MappedMem() = delete; | ||
MappedMem(const MappedMem &) = delete; | ||
MappedMem& operator=(const MappedMem &) = delete; | ||
|
||
MappedMem(unsigned long g_paddr, | ||
uint32_t g_count, | ||
|
@@ -42,9 +86,10 @@ class MappedMem | |
aligned_size = g_paddr - aligned_paddr + (g_count * g_size); | ||
aligned_size = (aligned_size + 4096 - 1) & ~(4096 - 1); | ||
|
||
int fd = -1; | ||
if ((fd = open("/dev/mem", O_RDWR, 0)) < 0) | ||
FDManager fd(open("/dev/mem", O_RDWR)); | ||
if (fd == -1) { | ||
return; | ||
} | ||
|
||
aligned_vaddr = mmap(nullptr, | ||
aligned_size, | ||
|
@@ -58,7 +103,6 @@ class MappedMem | |
} | ||
|
||
mem = (memtype *)((uint32_t)aligned_vaddr + (uint32_t)(g_paddr - aligned_paddr)); | ||
close(fd); | ||
} | ||
|
||
~MappedMem() | ||
|
@@ -132,12 +176,6 @@ class MappedMem | |
return mem; | ||
} | ||
|
||
|
||
private: | ||
MappedMem(); | ||
MappedMem(const MappedMem &); | ||
MappedMem& operator=(const MappedMem &); | ||
|
||
private: | ||
memtype *mem; | ||
uint32_t aligned_size; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me, this
FDManager
class represents file descriptor itself, rather than a manager. How about renaming this class to justFD
orFileDescriptor
or something?If you are uncomfortable using such a common name, you can also use namespace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, fixed.