Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TFree.cxx
Go to the documentation of this file.
1// @(#)root/io:$Id$
2// Author: Rene Brun 28/12/94
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "TFree.h"
13#include "TList.h"
14#include "TFile.h"
15#include "Bytes.h"
16#include <iostream>
17
18
19/**
20\class TFree
21\ingroup IO
22Service class for TFile.
23
24Each file has a linked list of free segments. Each free segment is described
25by its firts and last address.
26When an object is written to a file, a new Key (see TKey)
27is created. The first free segment big enough to accomodate the object
28is used.
29If the object size has a length corresponding to the size of the free segment,
30the free segment is deleted from the list of free segments.
31When an object is deleted from a file, a new TFree object is generated.
32If the deleted object is contiguous to an already deleted object, the free
33segments are merged in one single segment.
34*/
35
36////////////////////////////////////////////////////////////////////////////////
37/// Default constructor.
38
40{
41 fFirst = fLast = 0;
42}
43
44////////////////////////////////////////////////////////////////////////////////
45/// Constructor for a free segment.
46
48{
49 fFirst = first;
50 fLast = last;
51 lfree->Add(this);
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Add a new free segment to the list of free segments.
56///
57/// - if last just precedes an existing free segment, then first becomes
58/// the new starting location of the free segment.
59/// - if first just follows an existing free segment, then last becomes
60/// the new ending location of the free segment.
61/// - if first just follows an existing free segment AND last just precedes
62/// an existing free segment, these two segments are merged into
63/// one single segment.
64///
65
67{
68 TFree *idcur = this;
69 while (idcur) {
70 Long64_t curfirst = idcur->GetFirst();
71 Long64_t curlast = idcur->GetLast();
72 if (curlast == first-1) {
73 idcur->SetLast(last);
74 TFree *idnext = (TFree*)lfree->After(idcur);
75 if (idnext == 0) return idcur;
76 if (idnext->GetFirst() > last+1) return idcur;
77 idcur->SetLast( idnext->GetLast() );
78 lfree->Remove(idnext);
79 delete idnext;
80 return idcur;
81 }
82 if (curfirst == last+1) {
83 idcur->SetFirst(first);
84 return idcur;
85 }
86 if (first < curfirst) {
87 TFree * newfree = new TFree();
88 newfree->SetFirst(first);
89 newfree->SetLast(last);
90 lfree->AddBefore(idcur, newfree);
91 return newfree;
92 }
93 idcur = (TFree*)lfree->After(idcur);
94 }
95 return 0;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Destructor.
100
102{
103}
104
105////////////////////////////////////////////////////////////////////////////////
106/// Encode fre structure into output buffer.
107
108void TFree::FillBuffer(char *&buffer)
109{
111 if (fLast > TFile::kStartBigFile) version += 1000;
112 tobuf(buffer, version);
113 // printf("TFree::fillBuffer, fFirst=%lld, fLast=%lld, version=%d\n",fFirst,fLast,version);
114 if (version > 1000) {
115 tobuf(buffer, fFirst);
116 tobuf(buffer, fLast);
117 } else {
118 tobuf(buffer, (Int_t)fFirst);
119 tobuf(buffer, (Int_t)fLast);
120 }
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// Return the best free segment where to store nbytes.
125
127{
128 TFree *idcur = this;
129 if (idcur == 0) return 0;
130 TFree *idcur1 = 0;
131 do {
132 Long64_t nleft = Long64_t(idcur->fLast - idcur->fFirst +1);
133 if (nleft == nbytes) {
134 // Found an exact match
135 return idcur;
136 }
137 if(nleft > (Long64_t)(nbytes+3)) {
138 if (idcur1 == 0) {
140 }
141 }
142 idcur = (TFree*)lfree->After(idcur);
143 } while (idcur !=0);
144
145 // return first segment >nbytes
146 if (idcur1) return idcur1;
147
148 // try big file
149 idcur = (TFree*)lfree->Last();
150 Long64_t last = idcur->fLast+1000000000LL;
151 idcur->SetLast(last);
152 return idcur;
153}
154
155////////////////////////////////////////////////////////////////////////////////
156/// List free segment contents.
157
158void TFree::ls(Option_t *) const
159{
160 std::cout <<"Free Segment: "<<fFirst<<"\t"<<fLast<<std::endl;
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Decode one free structure from input buffer
165
166void TFree::ReadBuffer(char *&buffer)
167{
169 frombuf(buffer, &version);
170 if (version > 1000) {
171 frombuf(buffer, &fFirst);
172 frombuf(buffer, &fLast);
173 } else {
174 Int_t first,last;
175 frombuf(buffer, &first); fFirst = (Long64_t)first;
176 frombuf(buffer, &last); fLast = (Long64_t)last;
177 }
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// return number of bytes occupied by this TFree on permanent storage
182
184{
185 // printf("TFree::Sizeof, fFirst=%lld, fLast=%lld, version=%d\n",fFirst,fLast, (fLast > TFile::kStartBigFile));
186 if (fLast > TFile::kStartBigFile) return 18;
187 else return 10;
188}
189
void frombuf(char *&buf, Bool_t *x)
Definition Bytes.h:278
void tobuf(char *&buf, Bool_t x)
Definition Bytes.h:55
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
@ kStartBigFile
Definition TFile.h:279
Service class for TFile.
Definition TFree.h:27
TFree()
Default constructor.
Definition TFree.cxx:39
Int_t Sizeof() const
return number of bytes occupied by this TFree on permanent storage
Definition TFree.cxx:183
virtual void ReadBuffer(char *&buffer)
Decode one free structure from input buffer.
Definition TFree.cxx:166
void ls(Option_t *="") const override
List free segment contents.
Definition TFree.cxx:158
~TFree() override
Destructor.
Definition TFree.cxx:101
Long64_t fLast
Last free word of segment.
Definition TFree.h:31
static constexpr Version_t Class_Version()
Definition TFree.h:48
virtual void FillBuffer(char *&buffer)
Encode fre structure into output buffer.
Definition TFree.cxx:108
Long64_t fFirst
First free word of segment.
Definition TFree.h:30
TFree * AddFree(TList *lfree, Long64_t first, Long64_t last)
Add a new free segment to the list of free segments.
Definition TFree.cxx:66
TFree * GetBestFree(TList *lfree, Int_t nbytes)
Return the best free segment where to store nbytes.
Definition TFree.cxx:126
A doubly linked list.
Definition TList.h:38