cvs: pecl /apc apc_sma.c
| From: | Rasmus Lerdorf | Date: | Sat, 25 Feb 2006 00:02:47 +0000 |
| Subject: | cvs: pecl /apc apc_sma.c | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-5262@lists.php.net to get a copy of this message | ||
rasmus Sat Feb 25 00:02:47 2006 UTC
Modified files:
/pecl/apc apc_sma.c
Log:
Change the allocator to always allocate power of 2 sized chunks and
nothing less than 128 bytes. It also makes sure that an existing chunk
isn't split if it would leave less than 128 bytes. Need to play a bit
with the numbers. Perhaps 128 is too large. This will increase
memory usage, of course, but it should also increase performance as
the allocator won't have to search through as much fragmented memory
as it did before.
http://cvs.php.net/viewcvs.cgi/pecl/apc/apc_sma.c?r1=1.30&r2=1.31&diff_format=u
Index: pecl/apc/apc_sma.c
diff -u pecl/apc/apc_sma.c:1.30 pecl/apc/apc_sma.c:1.31
--- pecl/apc/apc_sma.c:1.30 Mon Jan 9 20:35:56 2006
+++ pecl/apc/apc_sma.c Sat Feb 25 00:02:47 2006
@@ -26,7 +26,7 @@
*/
-/* $Id: apc_sma.c,v 1.30 2006/01/09 20:35:56 helly Exp $ */
+/* $Id: apc_sma.c,v 1.31 2006/02/25 00:02:47 rasmus Exp $ */
#include "apc_sma.h"
#include "apc.h"
@@ -45,7 +45,7 @@
#define UNLOCK(c) { apc_lck_unlock(c); HANDLE_UNBLOCK_INTERRUPTIONS(); }
/* }}} */
-enum { POWER_OF_TWO_BLOCKSIZE=0 }; /* force allocated blocks to 2^n? */
+enum { POWER_OF_TWO_BLOCKSIZE=1 }; /* force allocated blocks to 2^n? */
enum { DEFAULT_NUMSEG=1, DEFAULT_SEGSIZE=30*1024*1024 };
@@ -90,31 +90,32 @@
/* }}} */
/* {{{ sma_allocate: tries to allocate size bytes in a segment */
-static int sma_allocate(void* shmaddr, int size)
+static int sma_allocate(void* shmaddr, unsigned int size)
{
header_t* header; /* header of shared memory segment */
block_t* prv; /* block prior to working block */
block_t* cur; /* working block in list */
block_t* prvbestfit; /* block before best fit */
- int realsize; /* actual size of block needed, including header */
- int minsize; /* for finding best fit */
+ unsigned int realsize; /* actual size of block needed, including header */
+ unsigned int minsize; /* for finding best fit */
- /* Realsize must be aligned to a word boundary on some architectures. */
- realsize = alignword(max(size + alignword(sizeof(int)), sizeof(block_t)));
-
- /*
- * Set realsize to the smallest power of 2 greater than or equal to
- * realsize. This increases the likelihood that neighboring blocks can be
- * coalesced, reducing memory fragmentation.
- */
- if (POWER_OF_TWO_BLOCKSIZE) {
- int p = 1;
-
- while (p < realsize) {
- p <<= 1;
+ if(size<128) {
+ realsize = 128;
+ } else {
+ /*
+ * Set realsize to the smallest power of 2 greater than or equal to
+ * realsize. This increases the likelihood that neighboring blocks can be
+ * coalesced, reducing memory fragmentation.
+ */
+ if (POWER_OF_TWO_BLOCKSIZE) {
+ unsigned int p = size - 1;
+ p|=(p>>1); p|=(p>>2); p|=(p>>4); p|=(p>>8); p|=(p>>16);
+ realsize = p + 1;
+ } else {
+ realsize = size;
}
- realsize = p;
}
+ realsize = alignword(max(realsize + alignword(sizeof(int)), sizeof(block_t)));
/*
* First, insure that the segment contains at least realsize free bytes,
@@ -156,7 +157,7 @@
/* update the block header */
header->avail -= realsize;
- if (cur->size == realsize) {
+ if (cur->size == realsize || ((cur->size - realsize) < 128)) {
/* cur is a perfect fit for realsize; just unlink it */
prv->next = cur->next;
}