]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTMemoryFile.cxx
HLTHOMER module
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTMemoryFile.cxx
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 */
28 ClassImp(AliHLTMemoryFile);
29
30 AliHLTMemoryFile::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
48 AliHLTMemoryFile::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
65 AliHLTMemoryFile::~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
74 void AliHLTMemoryFile::Close(const Option_t*)
75 {
76   CloseMemoryFile();
77 }
78
79 int AliHLTMemoryFile::CloseMemoryFile(int bFlush)
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
97 Int_t    AliHLTMemoryFile::SysOpen(const char* /*pathname*/, Int_t /*flags*/, UInt_t /*mode*/)
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
107 Int_t    AliHLTMemoryFile::SysClose(Int_t /*fd*/)
108 {
109   // see header file for function documentation
110   //HLTDebug("closing file %p size %d", this, fSize);
111   return 0;
112 }
113
114 Int_t    AliHLTMemoryFile::SysRead(Int_t /*fd*/, void *buf, Int_t len)
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
128 Int_t    AliHLTMemoryFile::SysWrite(Int_t /*fd*/, const void *buf, Int_t len)
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
144 Long64_t AliHLTMemoryFile::SysSeek(Int_t /*fd*/, Long64_t offset, Int_t whence)
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     break;
160   default:
161     position=-1;
162     errno=EINVAL;
163   }
164   if (position>=0) {
165     if (position<fBufferSize) {
166       fPosition=position;
167     } else {
168       position=-1;
169       errno=fErrno=ENOSPC;
170     }
171   }
172   return position;
173 }
174
175 Int_t    AliHLTMemoryFile::SysStat(Int_t /*fd*/, Long_t */*id*/, Long64_t *size, Long_t */*flags*/, Long_t */*modtime*/)
176 {
177   // see header file for function documentation
178   if (size) *size=fSize;
179   return 0;
180 }
181
182 Int_t    AliHLTMemoryFile::SysSync(Int_t /*fd*/)
183 {
184   // see header file for function documentation
185   return 0;
186 }
187
188 int AliHLTMemoryFile::WriteHeaderBuffer(const char* pHeader, int size)
189 {
190   // see header file for function documentation
191   fErrno=0;
192   if (fHeaderSize==0) {
193     if (fSize+size<fBufferSize) {
194       if (fSize>0) {
195         // move exiting data
196         memcpy(fpBuffer+size, fpBuffer, fSize);
197       }
198       memcpy(fpBuffer, pHeader, size);
199       fpBuffer+=size;
200       fPosition+=size;
201       fBufferSize-=size;
202       fHeaderSize=size;
203     } else {
204       HLTError("no space left in memory file");
205       fErrno=ENOSPC;
206     }
207   } else {
208     HLTError("header exists");
209     fErrno=EEXIST;
210   }
211   return -fErrno;
212 }
213
214 // int AliHLTMemoryFile::WriteTrailerBuffer(const char* pTrailer, int size)
215 // {
216 //   // see header file for function documentation
217 //   fErrno=0;
218 //   if (fD>0) {
219 //     HLTError("file must be closed to write trailer");
220 //     return EPERM;
221 //   }
222 //   if (fSize+size<fBufferSize) {
223 //     memcpy(fpBuffer+fSize, pTrailer, size);
224 //   } else {
225 //     HLTError("no space left in memory file");
226 //     fErrno=ENOSPC;
227 //   }
228 //   return fErrno;
229 // }