17e8cc4212a944d59f5514efb73062711606342b
[IRC.git] / Robust / src / Runtime / memPool.h
1 #ifndef ___MEMPOOL_H__
2 #define ___MEMPOOL_H__
3
4 //////////////////////////////////////////////////////////
5 //
6 //  A memory pool implements POOLCREATE, POOLALLOC and 
7 //  POOLFREE to improve memory allocation by reusing records.
8 //
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.
14 //
15 //  poolfree adds newly freed records to the list BACK
16 //
17 //  poolalloc either takes records from FRONT or mallocs
18 //
19 //////////////////////////////////////////////////////////
20
21 #include <stdlib.h>
22 #include "runtime.h"
23 #include "mem.h"
24 #include "mlp_lock.h"
25
26
27
28 typedef struct MemPoolItem_t {
29   void* next;
30 } MemPoolItem;
31
32
33 typedef struct MemPool_t {
34   int itemSize;
35   MemPoolItem* head;
36
37   // avoid cache line contention between producer/consumer...
38   char buffer[CACHELINESIZE - sizeof(void*)];
39
40   MemPoolItem* tail;
41 } MemPool;
42
43
44 // the memory pool must always have at least one
45 // item in it
46 static MemPool* poolcreate( int itemSize ) {
47   MemPool* p    = calloc( 1, sizeof( MemPool ) );
48   p->itemSize   = itemSize;
49   p->head       = calloc( 1, itemSize );
50   p->head->next = NULL;
51   p->tail       = p->head;
52   return p;
53 }
54
55
56 // CAS
57 // in: a ptr, expected old, desired new
58 // return: actual old
59 //
60 // Pass in a ptr, what you expect the old value is and
61 // what you want the new value to be.
62 // The CAS returns what the value is actually: if it matches
63 // your proposed old value then you assume the update was successful,
64 // otherwise someone did CAS before you, so try again (the return
65 // value is the old value you will pass next time.)
66
67 static inline void poolfreeinto( MemPool* p, void* ptr ) {
68
69   MemPoolItem* tailCurrent;
70   MemPoolItem* tailActual;
71   
72   // set up the now unneeded record to as the tail of the
73   // free list by treating its first bytes as next pointer,
74   MemPoolItem* tailNew = (MemPoolItem*) ptr;
75   tailNew->next = NULL;
76
77   while( 1 ) {
78     // make sure the null happens before the insertion,
79     // also makes sure that we reload tailCurrent, etc..
80     BARRIER();
81
82     tailCurrent = p->tail;
83     tailActual = (MemPoolItem*)
84       CAS( &(p->tail),         // ptr to set
85            (long) tailCurrent, // current tail's next should be NULL
86            (long) tailNew      // try set to our new tail
87            );   
88     if( tailActual == tailCurrent ) {
89       // success, update tail
90       tailCurrent->next = tailNew;
91       return;
92     }
93
94     // if CAS failed, retry entire operation
95   }
96 }
97
98
99
100 static inline void* poolalloc( MemPool* p ) {
101
102   // to protect CAS in poolfree from dereferencing
103   // null, treat the queue as empty when there is
104   // only one item.  The dequeue operation is only
105   // executed by the thread that owns the pool, so
106   // it doesn't require an atomic op
107   MemPoolItem* headCurrent = p->head;
108   MemPoolItem* next=headCurrent->next;
109
110   if(next == NULL) {
111     // only one item, so don't take from pool
112     return (void*) RUNMALLOC( p->itemSize );
113   }
114  
115   p->head = next;
116
117   //////////////////////////////////////////////////////////
118   //
119   //
120   //  static inline void prefetch(void *x) 
121   //  { 
122   //    asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
123   //  } 
124   //
125   //
126   //  but this built-in gcc one seems the most portable:
127   //////////////////////////////////////////////////////////
128   //__builtin_prefetch( &(p->head->next) );
129   asm volatile( "prefetcht0 (%0)" :: "r" (next));
130
131   return (void*)headCurrent;
132 }
133
134
135 static void pooldestroy( MemPool* p ) {
136   MemPoolItem* i = p->head;
137   MemPoolItem* n;
138
139   while( i != NULL ) {
140     n = i->next;
141     free( i );
142     i = n;
143   }
144
145   free( p );
146 }
147
148
149 #endif // ___MEMPOOL_H__
150
151
152
153
154
155
156
157
158
159