1212#include "osh_log.h"
1313
1414#include "shmem.h"
15+ #include "shmemx.h"
1516
1617#include "osh_basic_tests.h"
1718
@@ -32,6 +33,7 @@ static int test_shmem_ptr();
3233static int test_allocation_size (void );
3334static int test_global_vars (void );
3435static int test_max_size (void );
36+ static int test_alloc_with_hint (void );
3537
3638#ifdef QUICK_TEST
3739#define LOOP_COUNT 100
@@ -153,6 +155,12 @@ int osh_basic_tc3(const TE_NODE *node, int argc, const char *argv[])
153155 log_item (node , 12 , rc );
154156 }
155157
158+ if (rc == TC_PASS )
159+ {
160+ rc = test_alloc_with_hint ();
161+ log_item (node , 12 , rc );
162+ }
163+
156164 return rc ;
157165}
158166
@@ -759,3 +767,80 @@ static int test_global_vars()
759767 return TC_PASS ;
760768}
761769
770+ static int test_alloc_with_hint ()
771+ {
772+ #if HAVE_DECL_SHMEMX_MALLOC_WITH_HINT
773+ const size_t alloc_base = 32 ;
774+ const size_t alloc_growed = 64 ;
775+ const size_t alloc_reduced = 16 ;
776+ char * p1 , * p2 ;
777+
778+ log_debug (OSH_TC , "testting malloc_with_hint\n" );
779+
780+ p1 = shmemx_malloc_with_hint (alloc_base , SHMEM_HINT_DEVICE_NIC_MEM );
781+ if (!p1 )
782+ {
783+ log_error (OSH_TC , "Failed to allocate hinted memory\n" );
784+ return TC_FAIL ;
785+ }
786+
787+ memset (p1 , 0xEF , alloc_base );
788+
789+ p2 = shmem_realloc (p1 , alloc_reduced );
790+ if (!p2 )
791+ {
792+ log_error (OSH_TC , "Failed to realloc hinted memory\n" );
793+ return TC_FAIL ;
794+ }
795+
796+ if (__verify (p2 , alloc_reduced , 0xEF ) == TC_FAIL )
797+ {
798+ log_error (OSH_TC , "Failed to verify from %zu to %zu\n" , alloc_base , alloc_reduced );
799+ return TC_FAIL ;
800+ }
801+
802+ p1 = shmem_realloc (p2 , alloc_growed );
803+ if (!p1 )
804+ {
805+ log_error (OSH_TC , "Failed to realloc from %zu to %zu\n" , alloc_reduced , alloc_growed );
806+ return TC_FAIL ;
807+ }
808+ if (__verify (p1 , alloc_reduced , 0xEF ) == TC_FAIL )
809+ {
810+ log_error (OSH_TC , "Failed to verify from %zu to %zu\n" , alloc_reduced , alloc_growed );
811+ return TC_FAIL ;
812+ }
813+
814+ /* allocate one more buffer to block in-place realloc */
815+ p2 = shmemx_malloc_with_hint (alloc_base , SHMEM_HINT_DEVICE_NIC_MEM );
816+ if (!p2 )
817+ {
818+ log_error (OSH_TC , "Failed to allocate hinted memory\n" );
819+ return TC_FAIL ;
820+ }
821+ p1 = shmem_realloc (p1 , alloc_growed * 2 );
822+ if (!p1 )
823+ {
824+ log_error (OSH_TC , "Failed to realloc from %zu to %zu\n" , alloc_growed , alloc_growed * 2 );
825+ return TC_FAIL ;
826+ }
827+ if (__verify (p1 , alloc_reduced , 0xEF ) == TC_FAIL )
828+ {
829+ log_error (OSH_TC , "Failed to verify from %zu to %zu (non-implace)\n" , alloc_growed , alloc_growed * 2 );
830+ return TC_FAIL ;
831+ }
832+ shmem_free (p2 );
833+
834+ /* corner cases */
835+ p2 = shmem_realloc (p1 , 0 ); /* works as shfree() */
836+ if (!p2 ) /* returned pointer should NOT be NULL */
837+ {
838+ log_error (OSH_TC , "failed shrealloc as shfree()\n" );
839+ return TC_FAIL ;
840+ }
841+
842+ shmem_free (p2 );
843+ #endif /* HAVE_DECL_SHMEMX_MALLOC_WITH_HINT */
844+ return TC_PASS ;
845+ }
846+
0 commit comments