@@ -33,30 +33,17 @@ typedef struct list_head k_list_head;
3333
3434typedef struct _RtMemPool
3535{
36- char name [RTSAFE_MEMORY_POOL_NAME_MAX ];
37-
38- size_t dataSize ;
39- size_t maxPreallocated ;
40-
4136 k_list_head used ;
42- unsigned int usedCount ;
43-
4437 k_list_head unused ;
45- unsigned int unusedCount ;
46-
4738 pthread_mutex_t mutex ;
48-
4939} RtMemPool ;
5040
5141// ------------------------------------------------------------------------------------------------
5242
5343bool rtsafe_memory_pool_create (RtMemPool_Handle * handlePtr ,
54- const char * poolName ,
5544 size_t dataSize ,
5645 size_t maxPreallocated )
5746{
58- assert (poolName == NULL || strlen (poolName ) < RTSAFE_MEMORY_POOL_NAME_MAX );
59-
6047 k_list_head * nodePtr ;
6148 RtMemPool * poolPtr ;
6249
@@ -67,23 +54,8 @@ bool rtsafe_memory_pool_create(RtMemPool_Handle* handlePtr,
6754 return false;
6855 }
6956
70- if (poolName != NULL )
71- {
72- strcpy (poolPtr -> name , poolName );
73- }
74- else
75- {
76- sprintf (poolPtr -> name , "%p" , poolPtr );
77- }
78-
79- poolPtr -> dataSize = dataSize ;
80- poolPtr -> maxPreallocated = maxPreallocated ;
81-
8257 INIT_LIST_HEAD (& poolPtr -> used );
83- poolPtr -> usedCount = 0 ;
84-
8558 INIT_LIST_HEAD (& poolPtr -> unused );
86- poolPtr -> unusedCount = 0 ;
8759
8860 pthread_mutexattr_t atts ;
8961 pthread_mutexattr_init (& atts );
@@ -93,17 +65,16 @@ bool rtsafe_memory_pool_create(RtMemPool_Handle* handlePtr,
9365 pthread_mutex_init (& poolPtr -> mutex , & atts );
9466 pthread_mutexattr_destroy (& atts );
9567
96- while ( poolPtr -> unusedCount < poolPtr -> maxPreallocated )
68+ for ( unsigned int unusedCount = 0 ; unusedCount < maxPreallocated ; unusedCount ++ )
9769 {
98- nodePtr = malloc (sizeof (k_list_head ) + poolPtr -> dataSize );
70+ nodePtr = malloc (sizeof (k_list_head ) + dataSize );
9971
10072 if (nodePtr == NULL )
10173 {
10274 break ;
10375 }
10476
10577 list_add_tail (nodePtr , & poolPtr -> unused );
106- poolPtr -> unusedCount ++ ;
10778 }
10879
10980 * handlePtr = (RtMemPool_Handle )poolPtr ;
@@ -120,21 +91,10 @@ void rtsafe_memory_pool_destroy(RtMemPool_Handle handle)
12091 k_list_head * nodePtr ;
12192 RtMemPool * poolPtr = (RtMemPool * )handle ;
12293
123- // caller should deallocate all chunks prior releasing pool itself
124- if (poolPtr -> usedCount != 0 )
125- {
126- assert (0 );
127- }
128-
129- while (poolPtr -> unusedCount != 0 )
94+ while (! list_empty (& poolPtr -> unused ))
13095 {
131- assert (! list_empty (& poolPtr -> unused ));
132-
13396 nodePtr = poolPtr -> unused .next ;
134-
13597 list_del (nodePtr );
136- poolPtr -> unusedCount -- ;
137-
13898 free (nodePtr );
13999 }
140100
@@ -166,9 +126,6 @@ void* rtsafe_memory_pool_allocate_atomic(RtMemPool_Handle handle)
166126 nodePtr = poolPtr -> unused .next ;
167127 list_del (nodePtr );
168128
169- poolPtr -> unusedCount -- ;
170- poolPtr -> usedCount ++ ;
171-
172129 list_add_tail (nodePtr , & poolPtr -> used );
173130
174131 pthread_mutex_unlock (& poolPtr -> mutex );
@@ -189,8 +146,6 @@ void rtsafe_memory_pool_deallocate(RtMemPool_Handle handle, void* memoryPtr)
189146
190147 list_del ((k_list_head * )memoryPtr - 1 );
191148 list_add_tail ((k_list_head * )memoryPtr - 1 , & poolPtr -> unused );
192- poolPtr -> usedCount -- ;
193- poolPtr -> unusedCount ++ ;
194149
195150 pthread_mutex_unlock (& poolPtr -> mutex );
196151}
0 commit comments