2 * mlt_pool.c -- memory pooling functionality
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "mlt_properties.h"
22 #include "mlt_deque.h"
28 /** Singleton repositories
31 static mlt_properties pools
= NULL
;
33 /** Private pooling structure.
36 typedef struct mlt_pool_s
45 typedef struct mlt_release_s
54 static mlt_pool
pool_init( int size
)
57 mlt_pool
this = calloc( 1, sizeof( struct mlt_pool_s
) );
62 // Initialise the mutex
63 pthread_mutex_init( &this->lock
, NULL
);
66 this->stack
= mlt_deque_init( );
76 /** Get an item from the pool.
79 static void *pool_fetch( mlt_pool
this )
81 // We will generate a release object
88 pthread_mutex_lock( &this->lock
);
90 // Check if the stack is empty
91 if ( mlt_deque_count( this->stack
) != 0 )
93 // Pop the top of the stack
94 ptr
= mlt_deque_pop_back( this->stack
);
98 // We need to generate a release item
99 mlt_release release
= malloc( sizeof( struct mlt_release_s
) + this->size
);
102 if ( release
!= NULL
)
105 release
->pool
= this;
108 ptr
= ( void * )release
+ sizeof( struct mlt_release_s
);
113 pthread_mutex_unlock( &this->lock
);
116 // Return the generated release object
120 /** Return an item to the pool.
123 static void pool_return( void *ptr
)
128 // Get the release pointer
129 mlt_release that
= ( void * )ptr
- sizeof( struct mlt_release_s
);
132 mlt_pool
this = that
->pool
;
137 pthread_mutex_lock( &this->lock
);
139 // Push the that back back on to the stack
140 mlt_deque_push_back( this->stack
, ptr
);
143 pthread_mutex_unlock( &this->lock
);
145 // Ensure that we don't clean up
150 // Tidy up - this will only occur if the returned item is incorrect
153 // Free the release itself
154 free( ptr
- sizeof( struct mlt_release_s
) );
161 static void pool_close( mlt_pool
this )
165 // We need to free up all items in the pool
166 void *release
= NULL
;
168 // Iterate through the stack until depleted
169 while ( ( release
= mlt_deque_pop_back( this->stack
) ) != NULL
)
171 // We'll free this item now
172 free( release
- sizeof( struct mlt_release_s
) );
175 // We can now close the stack
176 mlt_deque_close( this->stack
);
179 pthread_mutex_destroy( &this->lock
);
186 /** Initialise the pool.
189 void mlt_pool_init( )
191 // Loop variable used to create the pools
195 pools
= mlt_properties_new( );
198 for ( i
= 8; i
< 31; i
++ )
200 // Each properties item needs a name
204 mlt_pool pool
= pool_init( 1 << i
);
207 sprintf( name
, "%d", i
);
209 // Register with properties
210 mlt_properties_set_data( pools
, name
, pool
, 0, ( mlt_destructor
)pool_close
, NULL
);
214 /** Allocate size bytes from the pool.
217 void *mlt_pool_alloc( int size
)
219 // This will be used to obtain the pool to use
220 mlt_pool pool
= NULL
;
222 // Determines the index of the pool to use
225 // Minimum size pooled is 256 bytes
227 while ( ( 1 << index
) < size
)
230 // Now get the pool at the index
231 pool
= mlt_properties_get_data_at( pools
, index
- 8, NULL
);
233 // Now get the real item
234 return pool_fetch( pool
);
237 /** Allocate size bytes from the pool.
240 void *mlt_pool_allocate( int size
, void **release
)
242 // This is the real release structure we'll return
245 // This will be used to obtain the pool to use
246 mlt_pool pool
= NULL
;
248 // Determines the index of the pool to use
251 // Minimum size pooled is 256 bytes
253 while ( ( 1 << index
) < size
)
256 // Now get the pool at the index
257 pool
= mlt_properties_get_data_at( pools
, index
+ 1, NULL
);
259 // Now get the real item
260 real
= pool_fetch( pool
);
265 // Otherwise return a NULL to indicate failure
269 /** Release the allocated memory.
272 void mlt_pool_release( void *release
)
274 // Return to the pool
275 pool_return( release
);
281 void mlt_pool_close( )
283 // Close the properties
284 mlt_properties_close( pools
);