virtmem
virtual memory library for Arduino
sd_alloc.h
Go to the documentation of this file.
1 #ifndef VIRTMEM_SD_ALLOC_H
2 #define VIRTMEM_SD_ALLOC_H
3 
9 #include "internal/alloc.h"
10 
11 #include <SdFat.h>
12 
13 namespace virtmem {
14 
31 template <typename Properties=DefaultAllocProperties>
32 class SDVAllocP : public VAlloc<Properties, SDVAllocP<Properties> >
33 {
34  SdFile sdFile;
35 
36  void doStart(void)
37  {
38  // file does not exist yet (can we create it)?
39  if (sdFile.open("ramfile.vm", O_CREAT | O_EXCL))
40  this->writeZeros(0, this->getPoolSize()); // make it the right size
41  else // already exists, check size
42  {
43  if (!sdFile.open("ramfile.vm", O_CREAT | O_RDWR))
44  {
45  Serial.println("opening ram file failed");
46  while (true)
47  ;
48  }
49 
50  const uint32_t size = sdFile.fileSize();
51  if (size < this->getPoolSize())
52  this->writeZeros(size, this->getPoolSize() - size);
53  }
54  }
55 
56  void doStop(void)
57  {
58  sdFile.close();
59  }
60  void doRead(void *data, VPtrSize offset, VPtrSize size)
61  {
62 // const uint32_t t = micros();
63  sdFile.seekSet(offset);
64  sdFile.read(data, size);
65 // Serial.print("read: "); Serial.print(size); Serial.print("/"); Serial.println(micros() - t);
66  }
67 
68  void doWrite(const void *data, VPtrSize offset, VPtrSize size)
69  {
70 // const uint32_t t = micros();
71  sdFile.seekSet(offset);
72  sdFile.write(data, size);
73 // Serial.print("write: "); Serial.print(size); Serial.print("/"); Serial.println(micros() - t);
74  }
75 
76 public:
82  ~SDVAllocP(void) { doStop(); }
83 
88  void removeTempFile(void) { sdFile.remove(); }
89 };
90 
92 
98 }
99 
100 #endif // VIRTMEM_SD_ALLOC_H
contains all code from virtmem
Definition: base_alloc.cpp:22
void writeZeros(VPtrNum start, VPtrSize n)
Writes zeros to raw virtual memory. Can be used to initialize the memory pool.
Definition: base_alloc.cpp:489
Base template class for virtual memory allocators.
Definition: alloc.h:28
Virtual memory allocator class that uses SD card as virtual pool.
Definition: sd_alloc.h:32
void setPoolSize(VPtrSize ps)
Sets the total size of the memory pool.
Definition: base_alloc.h:155
SDVAllocP(VPtrSize ps=VIRTMEM_DEFAULT_POOLSIZE)
Definition: sd_alloc.h:81
virtual memory class header
void removeTempFile(void)
Definition: sd_alloc.h:88
uint32_t VPtrSize
Numeric type used to store the size of a virtual memory block.
Definition: base_alloc.h:22
VPtrSize getPoolSize(void) const
Returns the size the memory pool.
Definition: base_alloc.h:187
SDVAllocP SDVAlloc
Shortcut to SDVAllocP with default template arguments.
Definition: sd_alloc.h:91
#define VIRTMEM_DEFAULT_POOLSIZE
The default poolsize for allocators supporting a variable sized pool.
Definition: config.h:55