collector.cpp 12.4 KB
Newer Older
1
// -*- c-basic-offset: 2 -*-
2 3 4
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 *
21 22
 */

mjs's avatar
mjs committed
23 24 25 26 27 28
#if APPLE_CHANGES
#define _COLLECTOR
#include <CoreFoundation/CoreFoundation.h>
#include <cxxabi.h>
#endif

mjs's avatar
mjs committed
29 30 31
#include <collector.h>
#include <value.h>
#include <internal.h>
32 33 34

using namespace KJS;

mjs's avatar
mjs committed
35 36 37 38 39 40 41 42 43 44 45
// tunable parameters
static const int CELL_SIZE = 64;
static const int BLOCK_SIZE = (4 * 4096);
static const int SPARE_EMPTY_BLOCKS = 1;
static const int MIN_ARRAY_SIZE = 14;
static const int GROWTH_FACTOR = 2;
static const int LOW_WATER_FACTOR = 4;

// derived constants
static const int WORD_SIZE = sizeof(uint32_t);
static const int BITS_PER_WORD = WORD_SIZE * 8;
46
static const uint32_t ALL_BITS_ON = 0xffffffff;
mjs's avatar
mjs committed
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
static const int CELLS_PER_BLOCK = ((BLOCK_SIZE * 8 - 32) / (CELL_SIZE * 8 + 1));
static const int BITMAP_SIZE = (CELLS_PER_BLOCK / BITS_PER_WORD) + (CELLS_PER_BLOCK % BITS_PER_WORD != 0 ? 1 : 0);


struct CollectorCell {
  char memory[CELL_SIZE];
};

static const int ALLOCATIONS_PER_COLLECTION = 1000;

struct CollectorBlock {
  CollectorBlock() : usedCells(0) { memset(bitmap, 0, BITMAP_SIZE * WORD_SIZE); }
  
  uint32_t bitmap[BITMAP_SIZE];
  int32_t usedCells;
  CollectorCell cells[CELLS_PER_BLOCK];
};

struct CollectorHeap {
  CollectorBlock **blocks;
  int numBlocks;
  int usedBlocks;
  
  CollectorCell **oversizeCells;
  int numOversizeCells;
  int usedOversizeCells;

  int numLiveObjects;
  int numAllocationsSinceLastCollect;
};

static CollectorHeap heap = {NULL, 0, 0, NULL, 0, 0, 0, 0};
79

80 81 82
bool Collector::memoryFull = false;


83 84 85 86
void* Collector::allocate(size_t s)
{
  if (s == 0)
    return 0L;
mjs's avatar
mjs committed
87 88 89
  
  // collect if needed
  if (++heap.numAllocationsSinceLastCollect >= ALLOCATIONS_PER_COLLECTION)
90
    collect();
mjs's avatar
mjs committed
91 92 93 94 95 96 97 98 99 100 101
  
  if (s > (unsigned)CELL_SIZE) {
    // oversize allocator
    if (heap.usedOversizeCells == heap.numOversizeCells) {
      heap.numOversizeCells = MAX(MIN_ARRAY_SIZE, heap.numOversizeCells * GROWTH_FACTOR);
      heap.oversizeCells = (CollectorCell **)realloc(heap.oversizeCells, heap.numOversizeCells * sizeof(CollectorCell *));
    }
    
    void *newCell = malloc(s);
    heap.oversizeCells[heap.usedOversizeCells] = (CollectorCell *)newCell;
    heap.usedOversizeCells++;
102
    heap.numLiveObjects++;
mjs's avatar
mjs committed
103 104 105

    ((ValueImp *)(newCell))->_flags = 0;
    return newCell;
106
  }
mjs's avatar
mjs committed
107 108 109 110 111 112 113 114 115 116 117
  
  // slab allocator
  
  CollectorBlock *targetBlock = NULL;
  
  // try to find free space in an existing block
  for (int i = 0; i < heap.usedBlocks; i++) {
    if (heap.blocks[i]->usedCells < CELLS_PER_BLOCK) {
      targetBlock = heap.blocks[i];
      break;
    }
118
  }
mjs's avatar
mjs committed
119 120 121 122 123 124 125 126 127 128 129 130
  
  if (targetBlock == NULL) {
    // didn't find one, need to allocate a new block
    
    if (heap.usedBlocks == heap.numBlocks) {
      heap.numBlocks = MAX(MIN_ARRAY_SIZE, heap.numBlocks * GROWTH_FACTOR);
      heap.blocks = (CollectorBlock **)realloc(heap.blocks, heap.numBlocks * sizeof(CollectorBlock *));
    }
    
    targetBlock = new CollectorBlock();
    heap.blocks[heap.usedBlocks] = targetBlock;
    heap.usedBlocks++;
131
  }
mjs's avatar
mjs committed
132 133 134 135
  
  // find a free spot in the block
  for (int wordInBitmap = 0; wordInBitmap < BITMAP_SIZE; wordInBitmap++) {
    uint32_t word = targetBlock->bitmap[wordInBitmap];
136 137 138
    if (word == ALL_BITS_ON) {
      continue;
    }
mjs's avatar
mjs committed
139 140 141 142 143 144
    for (int bitInWord = 0; bitInWord < BITS_PER_WORD; bitInWord++) {
      if ((word & (1 << bitInWord)) == 0) {
	int cellPos = BITS_PER_WORD * wordInBitmap + bitInWord;
	if (cellPos < CELLS_PER_BLOCK) {
	  targetBlock->bitmap[wordInBitmap] |= (1 << bitInWord);
	  targetBlock->usedCells++;
145
          heap.numLiveObjects++;
mjs's avatar
mjs committed
146 147

	  ((ValueImp *)(targetBlock->cells + cellPos))->_flags = 0;
mjs's avatar
mjs committed
148 149 150 151 152 153 154 155
	  return (void *)(targetBlock->cells + cellPos);
	}
      }
    }
  }
  // unreachable, if the free count wasn't 0 there has to be a free
  // cell in the block
  assert(false);
156

mjs's avatar
mjs committed
157
  return false;
158 159
}

160
bool Collector::collect()
161
{
162 163
  bool deleted = false;
  // MARK: first unmark everything
mjs's avatar
mjs committed
164 165 166 167
  for (int block = 0; block < heap.usedBlocks; block++) {
    for (int cell = 0; cell < CELLS_PER_BLOCK; cell++) {
      ((ValueImp *)(heap.blocks[block]->cells + cell))->_flags &= ~ValueImp::VI_MARKED;
    }
168
  }
mjs's avatar
mjs committed
169 170 171 172
  for (int cell = 0; cell < heap.usedOversizeCells; cell++) {
    ((ValueImp *)heap.oversizeCells[cell])->_flags &= ~ValueImp::VI_MARKED;
  }
  
173
  // mark all referenced objects recursively
174
  // starting out from the set of root objects
175 176
  if (InterpreterImp::s_hook) {
    InterpreterImp *scr = InterpreterImp::s_hook;
177
    do {
178
      //fprintf( stderr, "Collector marking interpreter %p\n",(void*)scr);
179 180
      scr->mark();
      scr = scr->next;
181
    } while (scr != InterpreterImp::s_hook);
182
  }
mjs's avatar
mjs committed
183
  
184
  // mark any other objects that we wouldn't delete anyway
mjs's avatar
mjs committed
185 186 187 188 189 190 191 192 193 194 195
  for (int block = 0; block < heap.usedBlocks; block++) {
    for (int wordInBitmap = 0; wordInBitmap < BITMAP_SIZE; wordInBitmap++) {
      uint32_t word = heap.blocks[block]->bitmap[wordInBitmap];
      for (int bitInWord = 0; bitInWord < BITS_PER_WORD; bitInWord++) {
	ValueImp *imp = (ValueImp *)(heap.blocks[block]->cells + BITS_PER_WORD * wordInBitmap + bitInWord);
	
	if ((word & (1 << bitInWord)) &&
	    (imp->_flags & (ValueImp::VI_CREATED|ValueImp::VI_MARKED)) == ValueImp::VI_CREATED &&
	    ((imp->_flags & ValueImp::VI_GCALLOWED) == 0 || imp->refcount != 0)) {
	  imp->mark();
	}
196 197
      }
    }
198
  }
mjs's avatar
mjs committed
199 200 201 202 203 204 205 206 207
  
  for (int cell = 0; cell < heap.usedOversizeCells; cell++) {
    ValueImp *imp = (ValueImp *)heap.oversizeCells[cell];
    if ((imp->_flags & (ValueImp::VI_CREATED|ValueImp::VI_MARKED)) == ValueImp::VI_CREATED &&
	((imp->_flags & ValueImp::VI_GCALLOWED) == 0 || imp->refcount != 0)) {
      imp->mark();
    }
  }
  
208
  // SWEEP: delete everything with a zero refcount (garbage)
mjs's avatar
mjs committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  
  int emptyBlocks = 0;

  for (int block = 0; block < heap.usedBlocks; block++) {
    for (int wordInBitmap = 0; wordInBitmap < BITMAP_SIZE; wordInBitmap++) {
      uint32_t word = heap.blocks[block]->bitmap[wordInBitmap];
      for (int bitInWord = 0; bitInWord < BITS_PER_WORD; bitInWord++) {
	ValueImp *imp = (ValueImp *)(heap.blocks[block]->cells + BITS_PER_WORD * wordInBitmap + bitInWord);
	
	if ((word & (1 << bitInWord)) &&
	    !imp->refcount && imp->_flags == (ValueImp::VI_GCALLOWED | ValueImp::VI_CREATED)) {
	  // emulate destructing part of 'operator delete()'
	  //fprintf( stderr, "Collector::deleting ValueImp %p (%s)\n", (void*)imp, typeid(*imp).name());
	  imp->~ValueImp();
	  heap.blocks[block]->bitmap[wordInBitmap] &= ~(1 << bitInWord);
	  heap.blocks[block]->usedCells--;
	  heap.numLiveObjects--;
	  deleted = true;
	}
228 229
      }
    }
mjs's avatar
mjs committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

    if (heap.blocks[block]->usedCells == 0) {
      emptyBlocks++;
      if (emptyBlocks > SPARE_EMPTY_BLOCKS) {
	delete heap.blocks[block];

	// swap with the last block so we compact as we go
	heap.blocks[block] = heap.blocks[heap.usedBlocks - 1];
	heap.usedBlocks--;
	block--; // Don't move forward a step in this case

	if (heap.numBlocks > MIN_ARRAY_SIZE && heap.usedBlocks < heap.numBlocks / LOW_WATER_FACTOR) {
	  heap.numBlocks = heap.numBlocks / GROWTH_FACTOR; 
	  heap.blocks = (CollectorBlock **)realloc(heap.blocks, heap.numBlocks * sizeof(CollectorBlock *));
	}
      } 
246 247
    }
  }
248

249

mjs's avatar
mjs committed
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 275 276 277 278 279 280 281 282
  
  int cell = 0;
  while (cell < heap.usedOversizeCells) {
    ValueImp *imp = (ValueImp *)heap.oversizeCells[cell];
    
    if (!imp->refcount && 
	imp->_flags == (ValueImp::VI_GCALLOWED | ValueImp::VI_CREATED)) {
      
      imp->~ValueImp();
      free((void *)imp);

      // swap with the last oversize cell so we compact as we go
      heap.oversizeCells[cell] = heap.oversizeCells[heap.usedOversizeCells - 1];

      heap.usedOversizeCells--;
      deleted = true;
      heap.numLiveObjects--;

      if (heap.numOversizeCells > MIN_ARRAY_SIZE && heap.usedOversizeCells < heap.numOversizeCells / LOW_WATER_FACTOR) {
	heap.numOversizeCells = heap.numOversizeCells / GROWTH_FACTOR; 
	heap.oversizeCells = (CollectorCell **)realloc(heap.oversizeCells, heap.numOversizeCells * sizeof(CollectorCell *));
      }

    } else {
      cell++;
    }
  }
  
  // possibly free some completely empty collector blocks ?
  // compact array of collector blocks
  
  heap.numAllocationsSinceLastCollect = 0;
  
283 284
  memoryFull = (heap.numLiveObjects >= KJS_MEM_LIMIT);

285 286
  return deleted;
}
287

mjs's avatar
mjs committed
288 289 290 291 292
int Collector::size() 
{
  return heap.numLiveObjects; 
}

293
#ifdef KJS_DEBUG_MEM
294 295
void Collector::finalCheck()
{
296
}
297
#endif
darin's avatar
darin committed
298

299
#if APPLE_CHANGES
300

darin's avatar
darin committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
int Collector::numInterpreters()
{
  int count = 0;
  if (InterpreterImp::s_hook) {
    InterpreterImp *scr = InterpreterImp::s_hook;
    do {
      ++count;
      scr = scr->next;
    } while (scr != InterpreterImp::s_hook);
  }
  return count;
}

int Collector::numGCNotAllowedObjects()
{
  int count = 0;
mjs's avatar
mjs committed
317 318 319 320 321 322 323 324 325 326
  for (int block = 0; block < heap.usedBlocks; block++) {
    for (int wordInBitmap = 0; wordInBitmap < BITMAP_SIZE; wordInBitmap++) {
      uint32_t word = heap.blocks[block]->bitmap[wordInBitmap];
      for (int bitInWord = 0; bitInWord < BITS_PER_WORD; bitInWord++) {
	ValueImp *imp = (ValueImp *)(heap.blocks[block]->cells + BITS_PER_WORD * wordInBitmap + bitInWord);
	
	if ((word & (1 << bitInWord)) &&
	    (imp->_flags & ValueImp::VI_GCALLOWED) == 0) {
	  ++count;
	}
darin's avatar
darin committed
327 328 329
      }
    }
  }
mjs's avatar
mjs committed
330 331 332 333 334 335 336 337
  
  for (int cell = 0; cell < heap.usedOversizeCells; cell++) {
    ValueImp *imp = (ValueImp *)heap.oversizeCells[cell];
      if ((imp->_flags & ValueImp::VI_GCALLOWED) == 0) {
        ++count;
      }
  }

darin's avatar
darin committed
338 339 340 341 342 343
  return count;
}

int Collector::numReferencedObjects()
{
  int count = 0;
mjs's avatar
mjs committed
344 345 346 347 348 349 350
  for (int block = 0; block < heap.usedBlocks; block++) {
    for (int wordInBitmap = 0; wordInBitmap < BITMAP_SIZE; wordInBitmap++) {
      uint32_t word = heap.blocks[block]->bitmap[wordInBitmap];
      for (int bitInWord = 0; bitInWord < BITS_PER_WORD; bitInWord++) {
	ValueImp *imp = (ValueImp *)(heap.blocks[block]->cells + BITS_PER_WORD * wordInBitmap + bitInWord);
	
	if ((word & (1 << bitInWord)) &&
351
	    imp->refcount != 0) {
mjs's avatar
mjs committed
352 353
	  ++count;
	}
darin's avatar
darin committed
354 355 356
      }
    }
  }
mjs's avatar
mjs committed
357 358 359
  
  for (int cell = 0; cell < heap.usedOversizeCells; cell++) {
    ValueImp *imp = (ValueImp *)heap.oversizeCells[cell];
360
      if (imp->refcount != 0) {
mjs's avatar
mjs committed
361 362 363 364
        ++count;
      }
  }

darin's avatar
darin committed
365 366
  return count;
}
367

368
// FIXME: Rename. Root object classes are more useful than live object classes.
mjs's avatar
mjs committed
369 370 371 372
CFSetRef Collector::liveObjectClasses()
{
  CFMutableSetRef classes = CFSetCreateMutable(NULL, 0, &kCFTypeSetCallBacks);

mjs's avatar
mjs committed
373 374 375 376 377 378
  for (int block = 0; block < heap.usedBlocks; block++) {
    for (int wordInBitmap = 0; wordInBitmap < BITMAP_SIZE; wordInBitmap++) {
      uint32_t word = heap.blocks[block]->bitmap[wordInBitmap];
      for (int bitInWord = 0; bitInWord < BITS_PER_WORD; bitInWord++) {
	ValueImp *imp = (ValueImp *)(heap.blocks[block]->cells + BITS_PER_WORD * wordInBitmap + bitInWord);
	
379 380
	if (word & (1 << bitInWord)
                && ((imp->_flags & ValueImp::VI_GCALLOWED) == 0 || imp->refcount != 0)) {
mjs's avatar
mjs committed
381 382 383 384 385 386 387 388 389
	  const char *mangled_name = typeid(*imp).name();
	  int status;
	  char *demangled_name = __cxxabiv1::__cxa_demangle (mangled_name, NULL, NULL, &status);
	  
	  CFStringRef className = CFStringCreateWithCString(NULL, demangled_name, kCFStringEncodingASCII);
	  free(demangled_name);
	  CFSetAddValue(classes, className);
	  CFRelease(className);
	}
mjs's avatar
mjs committed
390 391 392
      }
    }
  }
mjs's avatar
mjs committed
393 394 395 396
  
  for (int cell = 0; cell < heap.usedOversizeCells; cell++) {
    ValueImp *imp = (ValueImp *)heap.oversizeCells[cell];
    
397 398 399 400 401 402 403 404 405 406
    if ((imp->_flags & ValueImp::VI_GCALLOWED) == 0 || imp->refcount != 0) {
        const char *mangled_name = typeid(*imp).name();
        int status;
        char *demangled_name = __cxxabiv1::__cxa_demangle (mangled_name, NULL, NULL, &status);
        
        CFStringRef className = CFStringCreateWithCString(NULL, demangled_name, kCFStringEncodingASCII);
        free(demangled_name);
        CFSetAddValue(classes, className);
        CFRelease(className);
    }
mjs's avatar
mjs committed
407 408
  }

mjs's avatar
mjs committed
409 410 411
  return classes;
}

412
#endif // APPLE_CHANGES