18 #ifndef __MEMORYMAPARRAY_H
19 #define __MEMORYMAPARRAY_H
21 #ifndef __STDC_LIMIT_MACROS
22 #define __STDC_LIMIT_MACROS
35 #include <sys/types.h>
43 #include "MemoryMap.h"
67 void constructorClear()
69 memset(
this, 0,
sizeof(*
this));
73 uint32_t contentCookie;
74 uint32_t contentVersion;
79 char creationUser[32];
80 char creationHost[32];
84 void debugPrint(FILE *);
85 size_t getHeaderSize(
int i)
90 void setApplication(
const char *s)
92 strncpy(application, s,
sizeof(application)-1);
93 application[
sizeof(application)-1] =
'\0';
95 void setCreationUser(
const char *s)
97 strncpy(creationUser, s,
sizeof(creationUser)-1);
98 creationUser[
sizeof(creationUser)-1] =
'\0';
100 void setCreationHost(
const char *s)
102 strncpy(creationHost, s,
sizeof(creationHost)-1);
103 creationHost[
sizeof(creationHost)-1] =
'\0';
125 size_t getHeaderSize()
135 unsigned int cookieVal,
136 unsigned int versionVal,
137 elementT accessorFunc(
char *base, indexT),
138 void setterFunc(
char *base, indexT, elementT),
139 size_t elementCount2BytesFunc(indexT),
140 class arrayHeaderClass>
144 arrayHeaderClass *header;
146 std::string errorStr;
148 void constructorClear()
163 const std::string &getErrorString()
168 arrayHeaderClass &getHeader()
173 void setContentCookie(uint32_t c)
175 header->contentCookie = c;
177 void setContentVersion(uint32_t v)
179 header->contentVersion = v;
183 inline elementT operator[](indexT i)
185 return accessorFunc(data, i);
187 inline void set(indexT i, elementT v)
189 setterFunc(data, i, v);
208 int create(
const char *file, indexT elementCount,
int optionalHeaderCount = 0)
210 size_t len = elementCount2BytesFunc(elementCount) +
211 header->getHeaderSize(optionalHeaderCount);
216 std::ostringstream buf;
217 buf << file <<
": failed to create file";
218 errorStr = buf.str();
222 header = (arrayHeaderClass *) MemoryMap::data;
223 header->constructorClear();
224 header->typeCookie = cookieVal;
225 header->typeVersion = versionVal;
226 header->headerSize = header->getHeaderSize(optionalHeaderCount);
227 header->elementCount = elementCount;
228 data = (
char *)((
char *) MemoryMap::data + header->headerSize);
232 env = getenv(
"USER");
233 if (env) header->setCreationUser(env);
234 header->creationDate = time(NULL);
238 gethostname(hostname,
sizeof(hostname));
240 header->setCreationHost(hostname);
250 int create(indexT elementCount,
int optionalHeaderCount = 0)
252 return create(NULL, elementCount, optionalHeaderCount);
269 bool open(
const char *file,
int flags = O_RDONLY)
274 std::ostringstream buf;
275 buf << file <<
": open() failed (error=" << strerror(errno) <<
").";
276 errorStr = buf.str();
279 header = (arrayHeaderClass *) MemoryMap::data;
280 data = (
char *)((
char *) MemoryMap::data + header->headerSize);
281 if (header->typeCookie!=cookieVal)
283 std::ostringstream buf;
284 buf << file <<
": wrong type of file (expected type "
285 << cookieVal <<
" but got " << header->typeCookie <<
")";
286 errorStr = buf.str();
291 if (header->typeVersion!=versionVal)
293 std::ostringstream buf;
294 buf << file <<
": wrong version of file (expected version "
295 << versionVal <<
" but got " << header->typeVersion <<
")";
296 errorStr = buf.str();
307 return MemoryMap::close();
309 void debugPrint(FILE *f)
311 if (header) header->debugPrint(f);
314 size_t getElementCount()
const
316 return header->elementCount;
324 size_t getHeaderSize()
326 return sizeof(*this);
333 inline uint32_t mmapUint32Access(
char *base, uint32_t index)
335 return ((uint32_t *)base)[index];
337 inline void mmapUint32Set(
char *base, uint32_t index, uint32_t v)
339 ((uint32_t *)base)[index] = v;
341 inline size_t mmapUint32elementCount2Bytes(uint32_t i)
343 return sizeof(uint32_t) * i;
353 mmapUint32elementCount2Bytes,
369 Packed1BitElementCount2Bytes,
384 Packed2BitElementCount2Bytes,
395 Packed4BitElementCount2Bytes,
404 unsigned int qScore:7;
405 unsigned int conflicts:5;
411 inline baseRecord &mmapBaseRecordAccess(
void *base, uint32_t index)
413 return *((baseRecord *)((
char *)base + index*
sizeof(baseRecord)));
415 inline void mmapBaseRecordSet(
void *base, uint32_t index, baseRecord &v)
417 mmapBaseRecordAccess(base, index) = v;
419 inline size_t mmapBaseRecordElementCount2Bytes(uint32_t i)
421 return sizeof(baseRecord) * i;
429 &mmapBaseRecordAccess,
431 mmapBaseRecordElementCount2Bytes,
433 > mmapArrayBaseRecord_t;
bool open(const char *file, int flags=O_RDONLY)
open a previously created mapped vector
int create(indexT elementCount, int optionalHeaderCount=0)
allow anonymous (malloc) create.
int create(const char *file, indexT elementCount, int optionalHeaderCount=0)
Create a vector with elementCount memebers.
There are a pair of related data structures in the operating system, and also a few simple algorithms...
virtual bool open(const char *file, int flags=O_RDONLY)
open a previously created mapped vector
virtual bool create(const char *file, size_t size)
create the memory mapped file on disk