4 //////////////////////////////////////////////////////////
6 // A memory pool implements POOLCREATE, POOLALLOC and
7 // POOLFREE to improve memory allocation by reusing records.
9 // This implementation uses a lock-free singly-linked list
10 // to store reusable records. The list is initialized with
11 // one valid record, and the list is considered empty when
12 // it has only one record; this allows the enqueue operation's
13 // CAS to assume tail can always be dereferenced.
15 // poolfree adds newly freed records to the list BACK
17 // poolalloc either takes records from FRONT or mallocs
19 //////////////////////////////////////////////////////////
25 // The cache line size is set for the AMD Opteron 6168 (dc-10)
26 // that has L1 and L2 cache line sizes of 64 bytes. Source:
27 // http://www.cs.virginia.edu/~skadron/cs451/opteron/opteron.ppt
28 #define CACHELINESIZE 64
31 typedef struct MemPoolItem_t {
36 typedef struct MemPool_t {
40 // avoid cache line contention between producer/consumer...
41 char buffer[CACHELINESIZE - sizeof(void*)];
47 // the memory pool must always have at least one
49 static MemPool* poolcreate( int itemSize ) {
50 MemPool* p = calloc( 1, sizeof( MemPool ) );
51 p->itemSize = itemSize;
52 p->head = calloc( 1, itemSize );
59 // in: a ptr, expected old, desired new
62 // Pass in a ptr, what you expect the old value is and
63 // what you want the new value to be.
64 // The CAS returns what the value is actually: if it matches
65 // your proposed old value then you assume the update was successful,
66 // otherwise someone did CAS before you, so try again (the return
67 // value is the old value you will pass next time.)
69 static inline void poolfreeinto( MemPool* p, void* ptr ) {
71 MemPoolItem* tailCurrent;
72 MemPoolItem* tailActual;
74 // set up the now unneeded record to as the tail of the
75 // free list by treating its first bytes as next pointer,
76 MemPoolItem* tailNew = (MemPoolItem*) ptr;
80 // make sure the null happens before the insertion,
81 // also makes sure that we reload tailCurrent, etc..
84 tailCurrent = p->tail;
85 tailActual = (MemPoolItem*)
86 CAS( &(p->tail), // ptr to set
87 (long) tailCurrent, // current tail's next should be NULL
88 (long) tailNew // try set to our new tail
90 if( tailActual == tailCurrent ) {
91 // success, update tail
92 tailCurrent->next = tailNew;
96 // if CAS failed, retry entire operation
101 static inline void* poolalloc( MemPool* p ) {
103 // to protect CAS in poolfree from dereferencing
104 // null, treat the queue as empty when there is
105 // only one item. The dequeue operation is only
106 // executed by the thread that owns the pool, so
107 // it doesn't require an atomic op
108 MemPoolItem* headCurrent = p->head;
110 if( headCurrent->next == NULL ) {
111 // only one item, so don't take from pool
112 return calloc( 1, p->itemSize );
115 p->head = headCurrent->next;
120 static void pooldestroy( MemPool* p ) {
121 MemPoolItem* i = p->head;
134 #endif // ___MEMPOOL_H__