@@ -212,6 +212,13 @@ umfDefaultCtlPoolHandle(void *hPool, umf_ctl_query_source_t operationType,
212212 return UMF_RESULT_ERROR_NOT_SUPPORTED ;
213213}
214214
215+ static umf_result_t umfDefaultTrimMemory (void * provider ,
216+ size_t minBytesToKeep ) {
217+ (void )provider ;
218+ (void )minBytesToKeep ;
219+ return UMF_RESULT_ERROR_NOT_SUPPORTED ;
220+ }
221+
215222// logical sum (OR) of all umf_pool_create_flags_t flags
216223static const umf_pool_create_flags_t UMF_POOL_CREATE_FLAG_ALL =
217224 UMF_POOL_CREATE_FLAG_OWN_PROVIDER | UMF_POOL_CREATE_FLAG_DISABLE_TRACKING ;
@@ -233,9 +240,9 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
233240 const void * params ,
234241 umf_pool_create_flags_t flags ,
235242 umf_memory_pool_handle_t * hPool ) {
236- if (! ops || ! provider || ! hPool ) {
237- return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
238- }
243+ UMF_CHECK (( ops != NULL ), UMF_RESULT_ERROR_INVALID_ARGUMENT );
244+ UMF_CHECK (( provider != NULL ), UMF_RESULT_ERROR_INVALID_ARGUMENT ) ;
245+ UMF_CHECK (( hPool != NULL ), UMF_RESULT_ERROR_INVALID_ARGUMENT );
239246
240247 // validate flags
241248 if (flags & ~UMF_POOL_CREATE_FLAG_ALL ) {
@@ -245,10 +252,26 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
245252
246253 umf_result_t ret = UMF_RESULT_SUCCESS ;
247254
255+ umf_memory_pool_ops_t compatible_ops ;
248256 if (ops -> version != UMF_POOL_OPS_VERSION_CURRENT ) {
249257 LOG_WARN ("Memory Pool ops version \"%d\" is different than the current "
250258 "version \"%d\"" ,
251259 ops -> version , UMF_POOL_OPS_VERSION_CURRENT );
260+
261+ // Create a new ops compatible structure with the current version
262+ memset (& compatible_ops , 0 , sizeof (compatible_ops ));
263+ if (UMF_MINOR_VERSION (ops -> version ) == 0 ) {
264+ LOG_INFO ("Detected 1.0 version of Memory Pool ops, "
265+ "upgrading to current version" );
266+ memcpy (& compatible_ops , ops ,
267+ offsetof(umf_memory_pool_ops_t , ext_trim_memory ));
268+ } else {
269+ LOG_ERR ("Memory Pool ops unknown version, which \"%d\" is not "
270+ "supported" ,
271+ ops -> version );
272+ return UMF_RESULT_ERROR_NOT_SUPPORTED ;
273+ }
274+ ops = & compatible_ops ;
252275 }
253276
254277 umf_memory_pool_handle_t pool =
@@ -278,6 +301,10 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
278301 pool -> ops .ext_ctl = umfDefaultCtlPoolHandle ;
279302 }
280303
304+ if (NULL == pool -> ops .ext_trim_memory ) {
305+ pool -> ops .ext_trim_memory = umfDefaultTrimMemory ;
306+ }
307+
281308 if (NULL == utils_mutex_init (& pool -> lock )) {
282309 LOG_ERR ("Failed to initialize mutex for pool" );
283310 ret = UMF_RESULT_ERROR_UNKNOWN ;
@@ -326,10 +353,7 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
326353}
327354
328355umf_result_t umfPoolDestroy (umf_memory_pool_handle_t hPool ) {
329- if (hPool == NULL ) {
330- LOG_ERR ("memory pool handle is NULL" );
331- return UMF_RESULT_ERROR_INVALID_ARGUMENT ;
332- }
356+ UMF_CHECK ((hPool != NULL ), UMF_RESULT_ERROR_INVALID_ARGUMENT );
333357
334358 if (umf_ba_global_is_destroyed ()) {
335359 return UMF_RESULT_ERROR_UNKNOWN ;
@@ -509,6 +533,13 @@ umf_result_t umfPoolGetTag(umf_memory_pool_handle_t hPool, void **tag) {
509533 return UMF_RESULT_SUCCESS ;
510534}
511535
536+ umf_result_t umfPoolTrimMemory (umf_memory_pool_handle_t hPool ,
537+ size_t minBytesToKeep ) {
538+ UMF_CHECK ((hPool != NULL ), UMF_RESULT_ERROR_INVALID_ARGUMENT );
539+
540+ return hPool -> ops .ext_trim_memory (hPool -> pool_priv , minBytesToKeep );
541+ }
542+
512543void umfPoolCtlDefaultsDestroy (void ) {
513544 utils_init_once (& mem_pool_ctl_initialized , pool_ctl_init );
514545
0 commit comments