JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
VideoBuf.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #include <sys/mman.h>
19 #include <fstream>
20 
21 #include <jevois/Core/VideoBuf.H>
22 #include <jevois/Debug/Log.H>
23 
24 // ####################################################################################################
25 jevois::VideoBuf::VideoBuf(int const fd, size_t const length, unsigned int offset) :
26  itsFd(fd), itsLength(length), itsBytesUsed(0)
27 {
28  if (itsFd > 0)
29  {
30  // mmap the buffer to any address:
31 #ifdef JEVOIS_PLATFORM
32  // PROT_EXEC needed for clearcache() to work:
33  itsAddr = mmap(NULL, length, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, fd, offset);
34 #else
35  itsAddr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
36 #endif
37  if (itsAddr == MAP_FAILED) PLFATAL("Unable to map buffer");
38  }
39  else
40  {
41  // Simple memory allocation:
42  itsAddr = reinterpret_cast<void *>(new char[length]);
43  }
44 }
45 
46 // ####################################################################################################
48 {
49  if (itsFd > 0)
50  {
51  if (munmap(itsAddr, itsLength) < 0) PLERROR("munmap failed");
52  }
53  else
54  {
55  delete [] reinterpret_cast<char *>(itsAddr);
56  }
57 }
58 
59 #ifdef JEVOIS_PLATFORM
60 void clearcache(char* begin, char *end)
61 {
62  const int syscall = 0xf0002;
63  __asm __volatile (
64  "mov r0, %0\n"
65  "mov r1, %1\n"
66  "mov r7, %2\n"
67  "mov r2, #0x0\n"
68  "svc 0x00000000\n"
69  :
70  : "r" (begin), "r" (end), "r" (syscall)
71  : "r0", "r1", "r7"
72  );
73 }
74 #endif
75 
76 // ####################################################################################################
78 {
79 #ifdef JEVOIS_PLATFORM
80  if (itsFd > 0)
81  {
82  // This does nothing useful for us:
83  // msync(itsAddr, itsLength, MS_SYNC | MS_INVALIDATE);
84 
85  // This works ok but a bit brutal:
86  std::ofstream ofs("/proc/sys/vm/drop_caches");
87  if (ofs.is_open() == false) { LERROR("Cannot flush cache -- IGNORED"); return; }
88  ofs << "1" << std::endl;
89 
90  // Here is some arm-specific code:
91  clearcache(reinterpret_cast<char *>(itsAddr), reinterpret_cast<char *>(itsAddr) + itsLength);
92  }
93 #endif
94 }
95 
96 // ####################################################################################################
97 void * jevois::VideoBuf::data() const
98 {
99  return itsAddr;
100 }
101 
102 // ####################################################################################################
104 {
105  return itsLength;
106 }
107 
108 // ####################################################################################################
110 {
111  itsBytesUsed = n;
112 }
113 
114 // ####################################################################################################
116 {
117  return itsBytesUsed;
118 }
void sync()
Sync the data.
Definition: VideoBuf.C:77
#define PLFATAL(msg)
Like LDEBUG but appends errno and strerror(errno), to be used when some system call fails...
Definition: Log.H:214
~VideoBuf()
Destructor unmaps the memory.
Definition: VideoBuf.C:47
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level. ...
Definition: Log.H:186
VideoBuf(int const fd, size_t const length, unsigned int offset)
Construct and allocate MMAP'd memory.
Definition: VideoBuf.C:25
void setBytesUsed(size_t n)
Set the number of bytes used, eg, for MJPEG images that application code compressed into the buffer...
Definition: VideoBuf.C:109
size_t length() const
Get the allocated memory length.
Definition: VideoBuf.C:103
void * data() const
Get a pointer to the buffer data.
Definition: VideoBuf.C:97
#define PLERROR(msg)
Like LERROR but appends errno and strerror(errno), to be used when some system call fails...
Definition: Log.H:194
size_t bytesUsed() const
Get the number of bytes used, valid only for MJPEG images.
Definition: VideoBuf.C:115