Kaydet (Commit) 2c95c99a authored tarafından Tim Peters's avatar Tim Peters

_PyMalloc_Free(): As was already done for _PyMalloc_Malloc, rearranged

the code so that the most frequent cases come first.  Added comments.
Found a hidden assumption that a pool contains room for at least two
blocks, and added an assert to catch a violation if it ever happens in
a place where that matters.  Gave the normal "I allocated this block"
case a longer basic block to work with before it has to do its first
branch (via breaking apart an embedded assignment in an "if", and
hoisting common code out of both branches).
üst 1e16db6d
...@@ -667,6 +667,7 @@ void ...@@ -667,6 +667,7 @@ void
_PyMalloc_Free(void *p) _PyMalloc_Free(void *p)
{ {
poolp pool; poolp pool;
block *lastfree;
poolp next, prev; poolp next, prev;
uint size; uint size;
...@@ -679,57 +680,66 @@ _PyMalloc_Free(void *p) ...@@ -679,57 +680,66 @@ _PyMalloc_Free(void *p)
LOCK(); LOCK();
INCMINE; INCMINE;
/* /*
* At this point, the pool is not empty * Link p to the start of the pool's freeblock list. Since
* the pool had at least the p block outstanding, the pool
* wasn't empty (so it's already in a usedpools[] list, or
* was full and is in no list -- it's not in the freeblocks
* list in any case).
*/ */
if ((*(block **)p = pool->freeblock) == NULL) { *(block **)p = lastfree = pool->freeblock;
pool->freeblock = (block *)p;
if (lastfree) {
/* /*
* Pool was full * freeblock wasn't NULL, so the pool wasn't full,
* and the pool is in a usedpools[] list.
*/ */
pool->freeblock = (block *)p; assert(pool->ref.count < pool.capacity);
--pool->ref.count; if (--pool->ref.count != 0) {
/* pool isn't empty: leave it in usedpools */
UNLOCK();
return;
}
/* /*
* Frontlink to used pools * Pool is now empty: unlink from usedpools, and
* This mimics LRU pool usage for new allocations and * link to the front of usedpools. This ensures that
* targets optimal filling when several pools contain * previously freed pools will be allocated later
* blocks of the same size class. * (being not referenced, they are perhaps paged out).
*/ */
size = pool->szidx; next = pool->nextpool;
next = usedpools[size + size]; prev = pool->prevpool;
prev = next->prevpool; next->prevpool = prev;
pool->nextpool = next; prev->nextpool = next;
pool->prevpool = prev; /* Link to freepools. This is a singly-linked list,
next->prevpool = pool; * and pool->prevpool isn't used there.
prev->nextpool = pool; */
UNLOCK(); pool->nextpool = freepools;
return; freepools = pool;
}
/*
* Pool was not full
*/
pool->freeblock = (block *)p;
if (--pool->ref.count != 0) {
UNLOCK(); UNLOCK();
return; return;
} }
/* /*
* Pool is now empty, unlink from used pools * Pool was full, so doesn't currently live in any list:
*/ * link it to the front of the appropriate usedpools[] list.
next = pool->nextpool; * This mimics LRU pool usage for new allocations and
prev = pool->prevpool; * targets optimal filling when several pools contain
next->prevpool = prev; * blocks of the same size class.
prev->nextpool = next;
/*
* Frontlink to free pools
* This ensures that previously freed pools will be allocated
* later (being not referenced, they are perhaps paged out).
*/ */
pool->nextpool = freepools; assert(pool->ref.count == pool->capacity); /* else not full */
freepools = pool; --pool->ref.count;
assert(pool->ref.count > 0); /* else the pool is empty */
size = pool->szidx;
next = usedpools[size + size];
prev = next->prevpool;
/* insert pool before next: prev <-> pool <-> next */
pool->nextpool = next;
pool->prevpool = prev;
next->prevpool = pool;
prev->nextpool = pool;
UNLOCK(); UNLOCK();
return; return;
} }
/* We did not allocate this address. */ /* We didn't allocate this address. */
INCTHEIRS; INCTHEIRS;
PyMem_FREE(p); PyMem_FREE(p);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment