]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTMemoryFile.cxx
more bugfixes on data type operator semantics: earlier workarounds have been cleaned now
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTMemoryFile.cxx
CommitLineData
79c114b5 1// $Id$
2
3/**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
6 * *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
9 * *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
18
19/** @file AliHLTMemoryFile.cxx
20 @author Matthias Richter
21 @date
22 @brief ROOT file in memory. */
23
24#include "AliHLTMemoryFile.h"
25#include <cerrno>
26
27/** ROOT macro for the implementation of ROOT specific class methods */
28ClassImp(AliHLTMemoryFile);
29
30AliHLTMemoryFile::AliHLTMemoryFile()
31 :
32 fpBuffer(NULL),
33 fBufferSize(0),
34 fPosition(0),
35 fSize(0),
36 fErrno(0),
37 fbClosed(0),
38 fHeaderSize(0),
39 fTrailerSize(0)
40{
41 // see header file for class documentation
42 // or
43 // refer to README to build package
44 // or
45 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
46}
47
48AliHLTMemoryFile::AliHLTMemoryFile(void* pBuffer, int iSize)
49 :
50 TFile("/dev/null", "CREATE"),
51 AliHLTLogging(),
52 fpBuffer((char*)pBuffer),
53 fBufferSize(iSize),
54 fPosition(0),
55 fSize(0),
56 fErrno(0),
57 fbClosed(0),
58 fHeaderSize(0),
59 fTrailerSize(0)
60{
61 // see header file for class documentation
62 //HLTDebug("created memory file %p, capacity %d, ROOT version %d", this, fBufferSize, fVersion);
63}
64
79c114b5 65AliHLTMemoryFile::~AliHLTMemoryFile()
66{
67 // see header file for function documentation
68 //HLTDebug("deleting file %p size %d", this, fSize);
69 if (!fbClosed) {
70 HLTWarning("memory file not closed, possible data loss");
71 }
72}
73
29312178 74void AliHLTMemoryFile::Close(const Option_t*)
75{
76 CloseMemoryFile();
77}
78
79int AliHLTMemoryFile::CloseMemoryFile(int bFlush)
79c114b5 80{
81 fErrno=0;
82 if (fbClosed) return 0;
83 if (bFlush) {
84 TFile::Close();
85 }
86 fpBuffer=NULL;
87 fBufferSize=fPosition=0;
88 fbClosed=1;
89 if (fErrno==ENOSPC) {
90 HLTError("error flushing memory file, buffer too small");
91 } else if (fErrno>0) {
92 HLTError("error flushing memory file");
93 }
94 return -fErrno;
95}
96
298ef463 97Int_t AliHLTMemoryFile::SysOpen(const char* /*pathname*/, Int_t /*flags*/, UInt_t /*mode*/)
79c114b5 98{
99 // see header file for function documentation
100 if (fpBuffer==NULL || fSize==0) return 1;
101 //HLTDebug("opening file %p capacity %d", this, fSize);
102 fErrno=0;
103 errno=fErrno=ENOSPC;
104 return -1;
105}
106
298ef463 107Int_t AliHLTMemoryFile::SysClose(Int_t /*fd*/)
79c114b5 108{
109 // see header file for function documentation
110 //HLTDebug("closing file %p size %d", this, fSize);
111 return 0;
112}
113
298ef463 114Int_t AliHLTMemoryFile::SysRead(Int_t /*fd*/, void *buf, Int_t len)
79c114b5 115{
116 // see header file for function documentation
117 if (buf==NULL) return 0;
118 fErrno=0;
119 //HLTDebug("reading buffer of size %d at position %d", len, fPosition);
120 if (fpBuffer==NULL || fBufferSize==0) return 0;
121 int read=len<fSize-fPosition?len:fSize-fPosition;
122 memcpy(buf, fpBuffer+fPosition, read);
123 fPosition+=read;
124 if (fPosition>=fSize) fSize=fPosition+1;
125 return read;
126}
127
298ef463 128Int_t AliHLTMemoryFile::SysWrite(Int_t /*fd*/, const void *buf, Int_t len)
79c114b5 129{
130 // see header file for function documentation
131 if (buf==NULL) return 0;
132 fErrno=0;
133 //HLTDebug("writing buffer of size %d at position %d", len, fPosition);
134 if (len<fBufferSize-fPosition) {
135 memcpy(fpBuffer+fPosition, buf, len);
136 fPosition+=len;
137 if (fPosition>=fSize) fSize=fPosition+1;
138 return len;
139 }
140 errno=fErrno=ENOSPC;
141 return -1;
142}
143
298ef463 144Long64_t AliHLTMemoryFile::SysSeek(Int_t /*fd*/, Long64_t offset, Int_t whence)
79c114b5 145{
146 // see header file for function documentation
147 //HLTDebug("seek %d from %d", offset, whence);
148 fErrno=0;
149 int position=(int)offset;
150 switch (whence) {
151 case SEEK_SET:
152 // nothing to do
153 break;
154 case SEEK_CUR:
155 position+=fPosition;
156 break;
157 case SEEK_END:
158 position+=fSize;
159 default:
160 position=-1;
161 errno=EINVAL;
162 }
163 if (position>=0) {
164 if (position<fBufferSize) {
165 fPosition=position;
166 } else {
167 position=-1;
168 errno=fErrno=ENOSPC;
169 }
170 }
171 return position;
172}
173
298ef463 174Int_t AliHLTMemoryFile::SysStat(Int_t /*fd*/, Long_t */*id*/, Long64_t *size, Long_t */*flags*/, Long_t */*modtime*/)
79c114b5 175{
176 // see header file for function documentation
177 if (size) *size=fSize;
178 return 0;
179}
180
298ef463 181Int_t AliHLTMemoryFile::SysSync(Int_t /*fd*/)
79c114b5 182{
183 // see header file for function documentation
184 return 0;
185}
186
29312178 187int AliHLTMemoryFile::WriteHeaderBuffer(const char* pHeader, int size)
79c114b5 188{
189 // see header file for function documentation
190 fErrno=0;
191 if (fHeaderSize==0) {
192 if (fSize+size<fBufferSize) {
193 if (fSize>0) {
194 // move exiting data
195 memcpy(fpBuffer+size, fpBuffer, fSize);
196 }
197 memcpy(fpBuffer, pHeader, size);
198 fpBuffer+=size;
199 fPosition+=size;
200 fBufferSize-=size;
201 fHeaderSize=size;
202 } else {
203 HLTError("no space left in memory file");
204 fErrno=ENOSPC;
205 }
206 } else {
207 HLTError("header exists");
208 fErrno=EEXIST;
209 }
210 return -fErrno;
211}
212
29312178 213// int AliHLTMemoryFile::WriteTrailerBuffer(const char* pTrailer, int size)
79c114b5 214// {
215// // see header file for function documentation
216// fErrno=0;
217// if (fD>0) {
218// HLTError("file must be closed to write trailer");
219// return EPERM;
220// }
221// if (fSize+size<fBufferSize) {
222// memcpy(fpBuffer+fSize, pTrailer, size);
223// } else {
224// HLTError("no space left in memory file");
225// fErrno=ENOSPC;
226// }
227// return fErrno;
228// }