summaryrefslogtreecommitdiffstats
path: root/kdecore/kvmallocator.cpp
blob: 21d4184347cde2e68a3adb4b82d23cf0425344a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
    This file is part of the KDE libraries

    Copyright (C) 1999 Waldo Bastian (bastian@kde.org)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/
//----------------------------------------------------------------------------
//
// Virtual Memory Allocator

// TODO: Add large file support.
// TODO: Error reporting. (e.g. disk-full)

#include <unistd.h>
#include <sys/mman.h>

#include <qintdict.h>
#include <qmap.h>

#include <ktempfile.h>
#include <kdebug.h>

#include "kvmallocator.h"


#define KVM_ALIGN 4095

struct KVMAllocator::Block
{
   off_t start;
   size_t length; // Requested length
   size_t size; // Actual size
   void *mmap;
};


class KVMAllocatorPrivate
{
public:
   KTempFile *tempfile;
   off_t max_length;
   QMap<off_t, KVMAllocator::Block> used_blocks;
   QMap<off_t, KVMAllocator::Block> free_blocks;
};

/**
 * Create a KVMAllocator
 */
KVMAllocator::KVMAllocator()
{
   d = new KVMAllocatorPrivate;
   d->tempfile = 0;
   d->max_length = 0;
}

/**
 * Destruct the KVMAllocator and release all memory.
 */
KVMAllocator::~KVMAllocator()
{
   delete d->tempfile;
   delete d;
}

/**
 * Allocate a virtual memory block.
 * @param _size Size in bytes of the memory block.
 */
KVMAllocator::Block *
KVMAllocator::allocate(size_t _size)
{
   if (!d->tempfile)
   {
      d->tempfile = new KTempFile(QString::null, "vmdata");
      d->tempfile->unlink();
   }
   // Search in free list
   QMap<off_t,KVMAllocator::Block>::iterator it;
   it = d->free_blocks.begin();
   while (it != d->free_blocks.end())
   {
      if (it.data().size > _size)
      {
         Block &free_block = it.data();
         Block block;
         kdDebug(180)<<"VM alloc: using block from free list "<<(long)free_block.start<<" size ="<<(long)free_block.size<<" request = "<<_size<< endl;
         block.start = free_block.start;
         block.length = _size;
         block.size = (_size + KVM_ALIGN) & ~KVM_ALIGN;
         block.mmap = 0;
         free_block.size -= block.size;
         free_block.start += block.size;
         if (!free_block.size)
            d->free_blocks.remove(it);
         it = d->used_blocks.replace(block.start, block);
         return &(it.data());
      }
      ++it;
   } 


   // Create new block
   Block block;
   block.start = d->max_length;
   block.length = _size;
   block.size = (_size + KVM_ALIGN) & ~KVM_ALIGN;
   block.mmap = 0;
   kdDebug(180)<<"VM alloc: using new block "<<(long)block.start<<" size ="<<(long)block.size<<" request = "<<_size<< endl;
   it = d->used_blocks.replace(block.start, block);
   d->max_length += block.size;
   return &(it.data());
}

/**
 * Free a virtual memory block
 */
void
KVMAllocator::free(Block *block_p)
{
   Block block = *block_p;
   if (block.mmap)
   {
      kdDebug(180)<<"VM free: Block "<<(long)block.start<<" is still mmapped!"<<endl;
      return;
   }
   QMap<off_t,KVMAllocator::Block>::iterator it;
   it = d->used_blocks.find(block.start);
   if (it == d->used_blocks.end())
   {
      kdDebug(180)<<"VM free: Block "<<(long)block.start<<" is not allocated."<<endl;
      return;
   }
   d->used_blocks.remove(it);
   it = d->free_blocks.replace(block.start, block);
   QMap<off_t,KVMAllocator::Block>::iterator before = it;
   --before;
   if (before != d->free_blocks.end())
   {
      Block &block_before = before.data();
      if ((block_before.start + off_t(block_before.size)) == block.start)
      {
         // Merge blocks.
         kdDebug(180) << "VM merging: Block "<< (long)block_before.start<<
                           " with "<< (long)block.start<< " (before)" << endl;
         block.size += block_before.size;
         block.start = block_before.start;
         it.data() = block;
         d->free_blocks.remove(before);
      }
   }
   
   QMap<off_t,KVMAllocator::Block>::iterator after = it;
   ++after;
   if (after != d->free_blocks.end())
   {
      Block &block_after = after.data();
      if ((block.start + off_t(block.size)) == block_after.start)
      {
         // Merge blocks.
         kdDebug(180) << "VM merging: Block "<< (long)block.start<<
                           " with "<< (long)block_after.start<< " (after)" << endl;
         block.size += block_after.size;
         it.data() = block;
         d->free_blocks.remove(after);
      }
   }
}

/**
 * Copy data from a virtual memory block to normal memory
 */
void
KVMAllocator::copy(void *dest, Block *src, int _offset, size_t length)
{
   (void) copyBlock(dest, src, _offset, length);
}

bool
KVMAllocator::copyBlock(void *dest, Block *src, int _offset, size_t length)
{
   //kdDebug(180)<<"VM read: seek "<<(long)src->start<<" +"<<_offset<<":"<<length<<endl;
   lseek(d->tempfile->handle(), src->start+_offset, SEEK_SET);
   if (length == 0)
      length = src->length - _offset;
   int to_go = length;
   int done = 0;
   char *buf = (char *) dest;
   while(to_go > 0)
   {
      int n = read(d->tempfile->handle(), buf+done, to_go);
      if (n <= 0) 
      {
         if (n < 0)
            return false; // Error
         else
            return true; // End of data
      }
      done += n;
      to_go -= n;
   }
   // Done.
   return true;
}

/**
 * Copy data from normal memory to a virtual memory block
 */
void
KVMAllocator::copy(Block *dest, void *src, int _offset, size_t length)
{
   (void) copyBlock(dest, src, _offset, length);
}

bool
KVMAllocator::copyBlock(Block *dest, void *src, int _offset, size_t length)
{
   //kdDebug(180)<<"VM write: seek "<<(long)dest->start<<" +"<<_offset<< ":" << length << endl;
   lseek(d->tempfile->handle(), dest->start+_offset, SEEK_SET);
   if (length == 0)
      length = dest->length - _offset;
   int to_go = length;
   int done = 0;
   char *buf = (char *) src;
   while(to_go > 0)
   {
      int n = write(d->tempfile->handle(), buf+done, to_go);
      if (n <= 0) return false; // Error
      done += n;
      to_go -= n;
   }
   // Done.
   return true;
}

/**
 * Map a virtual memory block in memory
 */
void *
KVMAllocator::map(Block *block)
{
   if (block->mmap)
      return block->mmap;

   void *result = mmap(0, block->length, PROT_READ| PROT_WRITE,
                       MAP_SHARED, d->tempfile->handle(), block->start);
   block->mmap = result;
   return block->mmap;
}

/**
 * Unmap a virtual memory block
 */
void
KVMAllocator::unmap(Block *block)
{
   // The following cast is necassery for Solaris.
   // (touch it and die). --Waba
   munmap((char *)block->mmap, block->length);
   block->mmap = 0;
}