EPICS ARCHIVER V4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
FileAllocator.h
1 // -*- c++ -*-
2 
3 #ifndef __FILE_ALLOCATOR_H__
4 #define __FILE_ALLOCATOR_H__
5 
6 // system
7 #include <stdio.h>
8 
9 // storage
10 #include "storage/StorageTypes.h"
11 
15 
23 {
24 public:
25 
31  FileAllocator();
32 
35 
56  bool attach(FILE *f, FileOffset reserved_space, bool init);
57 
59  FILE *getFile() const;
60 
64  void detach();
65 
70  FileOffset allocate(FileOffset num_bytes);
71 
73  FileOffset size() const
74  { return file_size; }
75 
85  void free(FileOffset offset);
86 
94 
98 
114  bool dump(int level=1, FILE *out=stdout);
115 
116 private:
117 
118  PROHIBIT_DEFAULT_COPY(FileAllocator);
119 
121  class Node
122  {
123  public:
124  Node();
125 
127  static FileOffset size()
128  { return 12; }
129 
131  FileOffset getNext() const
132  { return next; }
133 
135  void setNext(FileOffset new_next)
136  { next = new_next; }
137 
139  FileOffset getPrev() const
140  { return prev; }
141 
143  void setPrev(FileOffset new_prev)
144  { prev = new_prev; }
145 
147  FileOffset getBytes() const
148  { return bytes; }
149 
151  void setBytes(FileOffset new_bytes)
152  { bytes = new_bytes; }
153 
155  void addBytes(int bytes_to_add);
156 
160  void read(FILE *f, FileOffset offset);
161 
165  void write(FILE *f, FileOffset offset) const;
166 
170  void write() const;
171 
173  FileOffset getOffset() const
174  { return offset; }
175 
176  private:
177  mutable FILE *f;
178  mutable FileOffset offset;
179  FileOffset prev;
180  FileOffset next;
181  FileOffset bytes;
182  };
183 
184  FILE *f;
185  FileOffset reserved_space; // Bytes we ignore in header
186  FileOffset file_size; // Total # of bytes in file
187  // For the head nodes,
188  // 'prev' = last entry, tail of list,
189  // 'next' = first entry, head of list!
190  // ! These nodes are always a current copy
191  // of what's on the disk!
192  Node allocated_head, free_head;
193 
194  // Unlink node from list, node itself remains unchanged
195  void remove_node(Node &head, const Node &node);
196  // Insert node (sorted), node's prev/next get changed
197  void insert_node(Node &head,
198  FileOffset node_offset, Node &node);
199 };
200 
204 
205 #endif
206 
~FileAllocator()
Destructor checks if detach has been called.
FileOffset allocate(FileOffset num_bytes)
Allocate a block with given size, returning a file offset (for fseek).
static FileOffset minimum_size
To avoid allocating tiny areas, also to avoid splitting free blocks into pieces that are then too sma...
Definition: FileAllocator.h:93
bool attach(FILE *f, FileOffset reserved_space, bool init)
Must be invoked to attach (&amp; initialize) a file.
static FileOffset file_size_increment
Setting file_size_increment will cause the file size to jump in the given increments.
Definition: FileAllocator.h:97
FileOffset size() const
Definition: FileAllocator.h:73
FileAllocator()
Constructor/destructor check if attach/detach have been called.
uint32_t FileOffset
FileOffset is used as a system independent type for, well, offsets into files.
Definition: StorageTypes.h:13
void detach()
Must be called before destroying the FileAllocator and closing the file.
Maintains memory blocks within a file.
Definition: FileAllocator.h:22
FILE * getFile() const
void free(FileOffset offset)
Release a file block (will be placed in free list).
bool dump(int level=1, FILE *out=stdout)
Show ASCII-type info about the file structure.