Line data Source code
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 126 : ClassImp(AliHLTMemoryFile);
29 :
30 0 : AliHLTMemoryFile::AliHLTMemoryFile()
31 : :
32 0 : fpBuffer(NULL),
33 0 : fBufferSize(0),
34 0 : fPosition(0),
35 0 : fSize(0),
36 0 : fErrno(0),
37 0 : fbClosed(0),
38 0 : fHeaderSize(0),
39 0 : fTrailerSize(0)
40 0 : {
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 0 : }
47 :
48 : AliHLTMemoryFile::AliHLTMemoryFile(void* pBuffer, int iSize)
49 : :
50 0 : TFile("/dev/null", "CREATE"),
51 0 : AliHLTLogging(),
52 0 : fpBuffer((char*)pBuffer),
53 0 : fBufferSize(iSize),
54 0 : fPosition(0),
55 0 : fSize(0),
56 0 : fErrno(0),
57 0 : fbClosed(0),
58 0 : fHeaderSize(0),
59 0 : fTrailerSize(0)
60 0 : {
61 : // see header file for class documentation
62 : //HLTDebug("created memory file %p, capacity %d, ROOT version %d", this, fBufferSize, fVersion);
63 0 : }
64 :
65 0 : AliHLTMemoryFile::~AliHLTMemoryFile()
66 0 : {
67 : // see header file for function documentation
68 : //HLTDebug("deleting file %p size %d", this, fSize);
69 0 : if (!fbClosed) {
70 0 : HLTWarning("memory file not closed, possible data loss");
71 : }
72 0 : }
73 :
74 : void AliHLTMemoryFile::Close(const Option_t*)
75 : {
76 0 : CloseMemoryFile();
77 0 : }
78 :
79 : int AliHLTMemoryFile::CloseMemoryFile(int bFlush)
80 : {
81 0 : fErrno=0;
82 0 : if (fbClosed) return 0;
83 0 : if (bFlush) {
84 0 : TFile::Close();
85 0 : }
86 0 : fpBuffer=NULL;
87 0 : fBufferSize=fPosition=0;
88 0 : fbClosed=1;
89 0 : if (fErrno==ENOSPC) {
90 0 : HLTError("error flushing memory file, buffer too small");
91 0 : } else if (fErrno>0) {
92 0 : HLTError("error flushing memory file");
93 : }
94 0 : return -fErrno;
95 0 : }
96 :
97 : Int_t AliHLTMemoryFile::SysOpen(const char* /*pathname*/, Int_t /*flags*/, UInt_t /*mode*/)
98 : {
99 : // see header file for function documentation
100 0 : if (fpBuffer==NULL || fSize==0) return 1;
101 : //HLTDebug("opening file %p capacity %d", this, fSize);
102 0 : fErrno=0;
103 0 : errno=fErrno=ENOSPC;
104 0 : return -1;
105 0 : }
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 0 : 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 0 : if (buf==NULL) return 0;
118 0 : fErrno=0;
119 : //HLTDebug("reading buffer of size %d at position %d", len, fPosition);
120 0 : if (fpBuffer==NULL || fBufferSize==0) return 0;
121 0 : int read=len<fSize-fPosition?len:fSize-fPosition;
122 0 : memcpy(buf, fpBuffer+fPosition, read);
123 0 : fPosition+=read;
124 0 : if (fPosition>=fSize) fSize=fPosition+1;
125 : return read;
126 0 : }
127 :
128 : Int_t AliHLTMemoryFile::SysWrite(Int_t /*fd*/, const void *buf, Int_t len)
129 : {
130 : // see header file for function documentation
131 0 : if (buf==NULL) return 0;
132 0 : fErrno=0;
133 : //HLTDebug("writing buffer of size %d at position %d", len, fPosition);
134 0 : if (len<fBufferSize-fPosition) {
135 0 : memcpy(fpBuffer+fPosition, buf, len);
136 0 : fPosition+=len;
137 0 : if (fPosition>=fSize) fSize=fPosition+1;
138 0 : return len;
139 : }
140 0 : errno=fErrno=ENOSPC;
141 0 : return -1;
142 0 : }
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 0 : fErrno=0;
149 0 : int position=(int)offset;
150 0 : switch (whence) {
151 : case SEEK_SET:
152 : // nothing to do
153 : break;
154 : case SEEK_CUR:
155 0 : position+=fPosition;
156 0 : break;
157 : case SEEK_END:
158 0 : position+=fSize;
159 0 : break;
160 : default:
161 : position=-1;
162 0 : errno=EINVAL;
163 0 : }
164 0 : if (position>=0) {
165 0 : if (position<fBufferSize) {
166 0 : fPosition=position;
167 0 : } else {
168 : position=-1;
169 0 : errno=fErrno=ENOSPC;
170 : }
171 : }
172 0 : 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 0 : if (size) *size=fSize;
179 0 : return 0;
180 : }
181 :
182 : Int_t AliHLTMemoryFile::SysSync(Int_t /*fd*/)
183 : {
184 : // see header file for function documentation
185 0 : return 0;
186 : }
187 :
188 : int AliHLTMemoryFile::WriteHeaderBuffer(const char* pHeader, int size)
189 : {
190 : // see header file for function documentation
191 0 : fErrno=0;
192 0 : if (fHeaderSize==0) {
193 0 : if (fSize+size<fBufferSize) {
194 0 : if (fSize>0) {
195 : // move exiting data
196 0 : memcpy(fpBuffer+size, fpBuffer, fSize);
197 0 : }
198 0 : memcpy(fpBuffer, pHeader, size);
199 0 : fpBuffer+=size;
200 0 : fPosition+=size;
201 0 : fBufferSize-=size;
202 0 : fHeaderSize=size;
203 0 : } else {
204 0 : HLTError("no space left in memory file");
205 0 : fErrno=ENOSPC;
206 : }
207 : } else {
208 0 : HLTError("header exists");
209 0 : fErrno=EEXIST;
210 : }
211 0 : 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 : // }
|