virtmem
virtual memory library for Arduino
stdio_alloc.h
Go to the documentation of this file.
1 #ifndef VIRTMEM_STDIO_ALLOC_H
2 #define VIRTMEM_STDIO_ALLOC_H
3 
9 #include "internal/alloc.h"
10 #include "config/config.h"
11 
12 #include <errno.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 namespace virtmem {
17 
27 template <typename Properties = DefaultAllocProperties>
28 class StdioVAllocP : public VAlloc<Properties, StdioVAllocP<Properties> >
29 {
30  FILE *ramFile;
31 
32  void doStart(void)
33  {
34  ramFile = tmpfile();
35  if (!ramFile)
36  fprintf(stderr, "Unable to open ram file!");
37  this->writeZeros(0, this->getPoolSize()); // make sure it gets the right size
38  }
39 
40  void doSuspend(void) { }
41  void doStop(void) { if (ramFile) { fclose(ramFile); ramFile = 0; } }
42  void doRead(void *data, VPtrSize offset, VPtrSize size)
43  {
44  if (fseek(ramFile, offset, SEEK_SET) != 0)
45  fprintf(stderr, "fseek error: %s\n", strerror(errno));
46 
47  fread(data, size, 1, ramFile);
48  if (ferror(ramFile))
49  fprintf(stderr, "didn't read correctly: %s\n", strerror(errno));
50 
51  }
52 
53  void doWrite(const void *data, VPtrSize offset, VPtrSize size)
54  {
55  if (fseek(ramFile, offset, SEEK_SET) != 0)
56  fprintf(stderr, "fseek error: %s\n", strerror(errno));
57 
58  fwrite(data, size, 1, ramFile);
59  if (ferror(ramFile))
60  fprintf(stderr, "didn't write correctly: %s\n", strerror(errno));
61  }
62 
63 public:
70  ~StdioVAllocP(void) { doStop(); }
71 };
72 
74 
75 }
76 
77 #endif // VIRTMEM_STDIO_ALLOC_H
contains all code from virtmem
Definition: base_alloc.cpp:22
StdioVAllocP(VPtrSize ps=VIRTMEM_DEFAULT_POOLSIZE)
Constructs (but not initializes) the allocator.
Definition: stdio_alloc.h:69
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
void setPoolSize(VPtrSize ps)
Sets the total size of the memory pool.
Definition: base_alloc.h:155
Virtual memory allocator that uses a regular file (via stdio) as memory pool.
Definition: stdio_alloc.h:28
This header file contains several variables that can be used to customize virtmem.
StdioVAllocP StdioVAlloc
Shortcut to StdioVAllocP with default template arguments.
Definition: stdio_alloc.h:73
virtual memory class header
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
#define VIRTMEM_DEFAULT_POOLSIZE
The default poolsize for allocators supporting a variable sized pool.
Definition: config.h:55