From 6aad3d55426925ed6dbc4df32bf30ce164cf06fe Mon Sep 17 00:00:00 2001 From: zhangyang Date: Wed, 19 Sep 2018 10:11:54 +0800 Subject: [PATCH] correct memory management for FPGA track --- src/fpga/api.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/fpga/api.cpp b/src/fpga/api.cpp index 36a54eb3a8..11b26fe689 100644 --- a/src/fpga/api.cpp +++ b/src/fpga/api.cpp @@ -17,17 +17,21 @@ limitations under the License. */ #include #include #include +#include #include "bias_scale.h" #include "filter.h" #include "image.h" #define FPGA_TEST_MODE -//#define PADDLE_MOBILE_OS_LINUX +#define PADDLE_MOBILE_OS_LINUX namespace paddle_mobile { namespace fpga { static int fd = -1; static const char *device_path = "/dev/fpgadrv0"; +#ifdef PADDLE_MOBILE_OS_LINUX +static std::map memory_map; +#endif static inline int do_ioctl(int req, const void *arg) { #ifdef PADDLE_MOBILE_OS_LINUX @@ -48,10 +52,13 @@ int open_device() { // memory management; void *fpga_malloc(size_t size) { - DLOG << size << " bytes allocated"; + static uint64_t counter = 0; + counter += size; + DLOG << size << " bytes allocated. Total " << counter << " bytes"; #ifdef PADDLE_MOBILE_OS_LINUX - return reinterpret_cast( - mmap64(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); + auto ptr = mmap64(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + memory_map.insert(std::make_pair(ptr, size)); + return ptr; #else return malloc(size); #endif @@ -59,7 +66,16 @@ void *fpga_malloc(size_t size) { void fpga_free(void *ptr) { #ifdef PADDLE_MOBILE_OS_LINUX - munmap(ptr, 0); + static uint64_t counter = 0; + size_t size = 0; + auto iter = memory_map.find(ptr); // std::map::iterator + if (iter != memory_map.end()) { + size = iter->second; + munmap(ptr, size); + memory_map.erase(iter); + } + counter += size; + DLOG << size << " bytes freed. Total " << counter << " bytes"; #else free(ptr); #endif -- GitLab