30 #define BLOCK_SIZE 512 // should be >8 and a power of 2 31 #define BLOCK_POINTER_SIZE sizeof(portable_off_t) 34 #define ASSERTS_ENABLED 36 #ifdef ASSERTS_ENABLED 37 #define STORE_ASSERT(x) assert(x) 39 #define STORE_ASSERT(x) 118 fprintf(
stderr,
"Store::alloc: Error seeking to end of file: %s\n",strerror(
errno));
143 fprintf(
stderr,
"Store::alloc: Error seeking to position %d: %s\n",
144 (
int)pos,strerror(
errno));
166 int bytesLeft = bytesInBlock<(
int)size ? (
int)size-bytesInBlock : 0;
167 int numBytes = size - bytesLeft;
172 if ((
int)fwrite(buf,1,numBytes,
m_file)!=numBytes)
174 fprintf(
stderr,
"Error writing: %s\n",strerror(
errno));
194 fprintf(
stderr,
"Error writing to store: %s\n",strerror(
errno));
206 fprintf(
stderr,
"Store::alloc: Error seeking to end of file: %s\n",strerror(
errno));
223 fprintf(
stderr,
"Error writing to store: %s\n",strerror(
errno));
234 fprintf(
stderr,
"Store::write: Error seeking to position %d: %s\n",
235 (
int)pos,strerror(
errno));
262 for (i=0;i<bytesInBlock;i++)
287 fprintf(
stderr,
"Store::release: Error seeking to position %d: %s\n",
294 fprintf(
stderr,
"Store::release: Error reading from store: %s\n",strerror(
errno));
311 fprintf(
stderr,
"Store::seek: Error seeking to position %d: %s\n",
312 (
int)pos,strerror(
errno));
331 int bytesLeft = bytesInBlock<(
int)size ? (
int)size-bytesInBlock : 0;
332 int numBytes = size - bytesLeft;
338 if ((
int)fread(buf,1,numBytes,
m_file)!=numBytes)
340 fprintf(
stderr,
"Error reading from store: %s\n",strerror(
errno));
357 fprintf(
stderr,
"Error reading from store: %s\n",strerror(
errno));
368 fprintf(
stderr,
"Store::read: Error seeking to position %d: %s\n",
369 (
int)curPos,strerror(
errno));
384 printf(
"FreeList: ");
388 printf(
"%x ",(
int)pos);
396 printf(
"ObjStore: block size %d bytes, total size %ld blocks, wrote %d blocks, read %d blocks\n",
405 if (fread(buf,size,1,
m_file)==(size_t)size)
408 for (i=0;i<
size;i+=16)
410 printf(
"%08x: ",(
int)s+i);
411 for (j=i;j<
QMIN(size,i+16);j++)
413 printf(
"%02x ",buf[i+j]);
416 for (j=i;j<
QMIN(size,i+16);j++)
418 printf(
"%c",(buf[i+j]>=32 && buf[i+j]<128)?buf[i+j]:
'.');
431 printf(
"sizeof(portable_off_t)=%d\n",(
int)
sizeof(
portable_off_t));
433 if (s.
open(
"test.db")==0)
435 const char *str1 =
"This is a test message... ";
436 const char *str2 =
"Another message. ";
444 for (i=0;i<1000000000;i++)
446 s.
write(str1,strlen(str1)+1);
452 s.
write(str2,strlen(str2)+1);
459 s.
read(buf,strlen(str1)+1);
460 printf(
"i=%d Read: %s\n",i,buf);
468 s.
read(buf,strlen(str2)+1);
469 printf(
"i=%d Read: %s\n",i,buf);
479 printf(
"Open failed! %s\n",strerror(
errno));
int read(char *buf, uint size)
#define BLOCK_POINTER_SIZE
int errno
Contains the last error code.
portable_off_t pos() const
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
The Store is a file based memory manager.
portable_off_t portable_fseek(FILE *f, portable_off_t offset, int whence)
portable_off_t portable_ftell(FILE *f)
void dumpBlock(portable_off_t start, portable_off_t end)
int write(const char *buf, uint size)
void seek(portable_off_t handle)
void release(portable_off_t handle)
int open(const char *name)
FILE * portable_fopen(const char *fileName, const char *mode)
Portable versions of functions that are platform dependent.