@@ -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,24 @@ 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 ("Unsupported Memory Pool ops version: %d" , ops -> version );
270+             return  UMF_RESULT_ERROR_NOT_SUPPORTED ;
271+         }
272+         ops  =  & compatible_ops ;
252273    }
253274
254275    umf_memory_pool_handle_t  pool  = 
@@ -278,6 +299,10 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
278299        pool -> ops .ext_ctl  =  umfDefaultCtlPoolHandle ;
279300    }
280301
302+     if  (NULL  ==  pool -> ops .ext_trim_memory ) {
303+         pool -> ops .ext_trim_memory  =  umfDefaultTrimMemory ;
304+     }
305+ 
281306    if  (NULL  ==  utils_mutex_init (& pool -> lock )) {
282307        LOG_ERR ("Failed to initialize mutex for pool" );
283308        ret  =  UMF_RESULT_ERROR_UNKNOWN ;
@@ -326,10 +351,7 @@ static umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
326351}
327352
328353umf_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-     }
354+     UMF_CHECK ((hPool  !=  NULL ), UMF_RESULT_ERROR_INVALID_ARGUMENT );
333355
334356    if  (umf_ba_global_is_destroyed ()) {
335357        return  UMF_RESULT_ERROR_UNKNOWN ;
@@ -509,6 +531,13 @@ umf_result_t umfPoolGetTag(umf_memory_pool_handle_t hPool, void **tag) {
509531    return  UMF_RESULT_SUCCESS ;
510532}
511533
534+ umf_result_t  umfPoolTrimMemory (umf_memory_pool_handle_t  hPool ,
535+                                size_t  minBytesToKeep ) {
536+     UMF_CHECK ((hPool  !=  NULL ), UMF_RESULT_ERROR_INVALID_ARGUMENT );
537+ 
538+     return  hPool -> ops .ext_trim_memory (hPool -> pool_priv , minBytesToKeep );
539+ }
540+ 
512541void  umfPoolCtlDefaultsDestroy (void ) {
513542    utils_init_once (& mem_pool_ctl_initialized , pool_ctl_init );
514543
0 commit comments