求大仙帮忙读程序,可以一句一句解释解释最好,实在看不懂它,有个面试关于它,求求了
1 #d e f i n e ARRAY SIZE EXP 7
2 #d e f i n e ARRAY SIZE ( 1 << ARRAY SIZE EXP )
3
4 s t a t i c p t h r e a d m u t e x t mutex = PTHREAD MUTEX INITIALIZER ; 5
6 s t r u c t m y s t r u c t 7 {
8 i n t a ;
9 unsigned b ;
10 short c ;
11 char ∗d ; 12 } ;
13
14 union my union 15 {
16 i n t a ;
17 unsigned b ;
18 short c ;
19 char ∗d ; 20 } ;
21
22 s t a t i c void ∗∗ a r r a y ; 23
24 i n t
25 m y f u n c t i o n ( const s t r u c t m y s t r u c t ∗ const ms , union my union mu) 26 {
27 s t a t i c unsigned s l o t = ARRAY SIZE / 2 ;
28 i n t e r r ; 29
30 e r r = p t h r e a d m u t e x l o c k (&mutex ) ; 31 i f ( e r r != 0 ) {
32 return −1;
33 }
34
35 i f ( a r r a y == NULL) {
36 a r r a y = m a l l o c ( s i z e o f ( void ∗ ) ∗ ARRAY SIZE) ;
37 i f ( a r r a y == NULL) {
38 e r r = p t h r e a d m u t e x u n l o c k (&mutex ) ;
39 a s s e r t ( e r r == 0 ) ;
40 return −2;
41 }
42 memset ( a r r a y , 0 , s i z e o f ( char ∗ ) ∗ ARRAY SIZE) ;
43 }
44
45 const unsigned m y s l o t = s l o t ;
46 s l o t = ( s l o t + 1 ) & (ARRAY SIZE − 1 ) ;
47
48 e r r = p t h r e a d m u t e x u n l o c k (&mutex ) ;
49 a s s e r t ( e r r == 0 ) ; 50
51 i f ( a r r a y [ m y s l o t ] != NULL) {
52 return 1 ;
53 }
54
55 i f ( m y s l o t & 1 ) {
56 a r r a y [ m y s l o t ] = ( void ∗ ) s t r t o u l ( ms−>d , NULL, 0 ) ;
57 } e l s e i f ( m y s l o t && 1 ) {
58 a r r a y [ m y s l o t ] = m a l l o c ( s i z e o f (mu) ) ;
59 i f ( a r r a y [ m y s l o t ] == NULL) {
60 return −2;
61 }
62 memcpy ( a r r a y [ m y s l o t ] , &mu, s i z e o f (mu) ) ;
63 } e l s e {
64 a r r a y [ m y s l o t ] = ( void ∗ ) 1 2 3 4 ;
65 }
66
67 return 0 ;
68 }
1