~ubuntu-branches/ubuntu/raring/firebird2.5/raring-proposed

« back to all changes in this revision

Viewing changes to src/jrd/TempSpace.h

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2011-09-24 14:12:19 UTC
  • mfrom: (15.1.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110924141219-pkxk7486f3d8ut9f
Tags: 2.5.1.26349-0~rc1.ds4-5
* Medium urgency for fixing a serious bug in testing

* Import a patch from upstream SVN fixing problems in poll() usage when
  process receives signals like SIGALRM.
  Closes: #642555 -- segfault in the remote interface when using alarm() in
  the client program

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
class TempSpace : public Firebird::File
34
34
{
35
35
public:
36
 
        TempSpace(MemoryPool& pool, const Firebird::PathName& prefix);
 
36
        TempSpace(MemoryPool& pool, const Firebird::PathName& prefix, bool dynamic = true);
37
37
        virtual ~TempSpace();
38
38
 
39
39
        size_t read(offset_t offset, void* buffer, size_t length);
51
51
        offset_t allocateSpace(size_t size);
52
52
        void releaseSpace(offset_t offset, size_t size);
53
53
 
54
 
        char* inMemory(offset_t offset, size_t size) const;
 
54
        UCHAR* inMemory(offset_t offset, size_t size) const;
55
55
 
56
56
        struct SegmentInMemory
57
57
        {
58
 
                char* memory;
 
58
                UCHAR* memory;
59
59
                offset_t position;
60
60
                size_t size;
61
61
        };
71
71
        class Block
72
72
        {
73
73
        public:
74
 
                Block(Block* tail, size_t length);
 
74
                Block(Block* tail, size_t length)
 
75
                        : next(NULL), size(length)
 
76
                {
 
77
                        if (tail)
 
78
                        {
 
79
                                tail->next = this;
 
80
                        }
 
81
                        prev = tail;
 
82
                }
 
83
 
75
84
                virtual ~Block() {}
76
85
 
77
86
                virtual size_t read(offset_t offset, void* buffer, size_t length) = 0;
78
87
                virtual size_t write(offset_t offset, const void* buffer, size_t length) = 0;
79
88
 
80
 
                virtual char* inMemory(offset_t offset, size_t size) const = 0;
 
89
                virtual UCHAR* inMemory(offset_t offset, size_t size) const = 0;
81
90
                virtual bool sameFile(const Firebird::TempFile* file) const = 0;
82
91
 
83
92
                Block *prev;
88
97
        class MemoryBlock : public Block
89
98
        {
90
99
        public:
91
 
                MemoryBlock(MemoryPool& pool, Block* tail, size_t length);
92
 
                ~MemoryBlock();
 
100
                MemoryBlock(UCHAR* memory, Block* tail, size_t length)
 
101
                        : Block(tail, length), ptr(memory)
 
102
                {}
 
103
 
 
104
                ~MemoryBlock()
 
105
                {
 
106
                        delete[] ptr;
 
107
                }
93
108
 
94
109
                size_t read(offset_t offset, void* buffer, size_t length);
95
110
                size_t write(offset_t offset, const void* buffer, size_t length);
96
111
 
97
 
                char* inMemory(offset_t offset, size_t _size) const
 
112
                UCHAR* inMemory(offset_t offset, size_t _size) const
98
113
                {
99
114
                        if ((offset < this->size) && (offset + _size <= this->size))
100
115
                                return ptr + offset;
107
122
                        return false;
108
123
                }
109
124
 
110
 
        private:
111
 
                char* ptr;
 
125
        protected:
 
126
                UCHAR* ptr;
 
127
        };
 
128
 
 
129
        class InitialBlock : public MemoryBlock
 
130
        {
 
131
        public:
 
132
                InitialBlock(UCHAR* memory, size_t length)
 
133
                        : MemoryBlock(memory, NULL, length)
 
134
                {}
 
135
 
 
136
                ~InitialBlock()
 
137
                {
 
138
                        ptr = NULL;
 
139
                }
112
140
        };
113
141
 
114
142
        class FileBlock : public Block
115
143
        {
116
144
        public:
117
 
                FileBlock(Firebird::TempFile* file, Block* tail, size_t length);
118
 
                ~FileBlock();
 
145
                FileBlock(Firebird::TempFile* f, Block* tail, size_t length)
 
146
                        : Block(tail, length), file(f)
 
147
                {
 
148
                        fb_assert(file);
 
149
 
 
150
                        // FileBlock is created after file was extended by length (look at
 
151
                        // TempSpace::extend) so this FileBlock is already inside the file
 
152
                        seek = file->getSize() - length;
 
153
                }
 
154
 
 
155
                ~FileBlock() {}
119
156
 
120
157
                size_t read(offset_t offset, void* buffer, size_t length);
121
158
                size_t write(offset_t offset, const void* buffer, size_t length);
122
159
 
123
 
                char* inMemory(offset_t /*offset*/, size_t /*a_size*/) const
 
160
                UCHAR* inMemory(offset_t /*offset*/, size_t /*a_size*/) const
124
161
                {
125
162
                        return NULL;
126
163
                }
138
175
        Block* findBlock(offset_t& offset) const;
139
176
        Firebird::TempFile* setupFile(size_t size);
140
177
 
141
 
        virtual bool adjustCacheSize(long) const
142
 
        {
143
 
                return false;
144
 
        }
145
 
 
146
 
        char* findMemory(offset_t& begin, offset_t end, size_t size) const;
 
178
        UCHAR* findMemory(offset_t& begin, offset_t end, size_t size) const;
147
179
 
148
180
        //  free/used segments management
149
181
        class Segment
150
182
        {
151
183
        public:
152
 
                Segment(Segment* _next, offset_t _position, offset_t _size) :
153
 
                        next(_next), position(_position), size(_size)
154
 
                {}
155
 
 
156
 
                Segment* next;
 
184
                Segment() : position(0), size(0)
 
185
                {}
 
186
 
 
187
                Segment(offset_t _position, offset_t _size) :
 
188
                        position(_position), size(_size)
 
189
                {}
 
190
 
157
191
                offset_t position;
158
192
                offset_t size;
 
193
 
 
194
                static const offset_t& generate(const void* /*sender*/, const Segment& segment)
 
195
                {
 
196
                        return segment.position;
 
197
                }
159
198
        };
160
199
 
161
 
        Segment* getSegment(offset_t position, size_t size);
162
 
        void joinSegment(Segment* seg, offset_t position, size_t size);
163
 
 
164
200
        MemoryPool& pool;
165
201
        Firebird::PathName filePrefix;
166
202
        offset_t logicalSize;
169
205
        Block* head;
170
206
        Block* tail;
171
207
        Firebird::Array<Firebird::TempFile*> tempFiles;
 
208
        Firebird::Array<UCHAR> initialBuffer;
 
209
        bool initiallyDynamic;
172
210
 
173
 
        Segment* freeSegments;
174
 
        Segment* notUsedSegments;
 
211
        typedef Firebird::BePlusTree<Segment, offset_t, MemoryPool, Segment> FreeSegmentTree;
 
212
        FreeSegmentTree freeSegments;
175
213
 
176
214
        static Firebird::GlobalPtr<Firebird::Mutex> initMutex;
177
215
        static Firebird::TempDirectoryList* tempDirs;