Rev 4505 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4505 | Rev 4582 | ||
---|---|---|---|
Line 36... | Line 36... | ||
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <errno.h> |
38 | #include <errno.h> |
39 | #include <malloc.h> |
39 | #include <malloc.h> |
40 | #include <mem.h> |
40 | #include <mem.h> |
41 | #include <rwlock.h> |
41 | #include <fibril_sync.h> |
42 | //#include <stdio.h> |
42 | //#include <stdio.h> |
43 | #include <unistd.h> |
43 | #include <unistd.h> |
44 | 44 | ||
45 | #include <sys/mman.h> |
45 | #include <sys/mman.h> |
46 | 46 | ||
Line 88... | Line 88... | ||
88 | /** Packet map global data. |
88 | /** Packet map global data. |
89 | */ |
89 | */ |
90 | static struct{ |
90 | static struct{ |
91 | /** Safety lock. |
91 | /** Safety lock. |
92 | */ |
92 | */ |
93 | rwlock_t lock; |
93 | fibril_rwlock_t lock; |
94 | /** Packet map. |
94 | /** Packet map. |
95 | */ |
95 | */ |
96 | gpm_t packet_map; |
96 | gpm_t packet_map; |
97 | } pm_globals; |
97 | } pm_globals; |
98 | 98 | ||
Line 104... | Line 104... | ||
104 | } |
104 | } |
105 | 105 | ||
106 | int pm_init( void ){ |
106 | int pm_init( void ){ |
107 | ERROR_DECLARE; |
107 | ERROR_DECLARE; |
108 | 108 | ||
109 | rwlock_initialize( & pm_globals.lock ); |
109 | fibril_rwlock_initialize( & pm_globals.lock ); |
110 | rwlock_write_lock( & pm_globals.lock ); |
110 | fibril_rwlock_write_lock( & pm_globals.lock ); |
111 | ERROR_PROPAGATE( gpm_initialize( & pm_globals.packet_map )); |
111 | ERROR_PROPAGATE( gpm_initialize( & pm_globals.packet_map )); |
112 | rwlock_write_unlock( & pm_globals.lock ); |
112 | fibril_rwlock_write_unlock( & pm_globals.lock ); |
113 | return EOK; |
113 | return EOK; |
114 | } |
114 | } |
115 | 115 | ||
116 | packet_t pm_find( packet_id_t packet_id ){ |
116 | packet_t pm_find( packet_id_t packet_id ){ |
117 | packet_map_ref map; |
117 | packet_map_ref map; |
118 | packet_t packet; |
118 | packet_t packet; |
119 | 119 | ||
120 | if( ! packet_id ) return NULL; |
120 | if( ! packet_id ) return NULL; |
121 | rwlock_read_lock( & pm_globals.lock ); |
121 | fibril_rwlock_read_lock( & pm_globals.lock ); |
122 | if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )){ |
122 | if( packet_id > PACKET_MAP_SIZE * gpm_count( & pm_globals.packet_map )){ |
123 | rwlock_read_unlock( & pm_globals.lock ); |
123 | fibril_rwlock_read_unlock( & pm_globals.lock ); |
124 | return NULL; |
124 | return NULL; |
125 | } |
125 | } |
126 | map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id )); |
126 | map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet_id )); |
127 | if( ! map ){ |
127 | if( ! map ){ |
128 | rwlock_read_unlock( & pm_globals.lock ); |
128 | fibril_rwlock_read_unlock( & pm_globals.lock ); |
129 | return NULL; |
129 | return NULL; |
130 | } |
130 | } |
131 | packet = ( * map )[ PACKET_MAP_INDEX( packet_id ) ]; |
131 | packet = ( * map )[ PACKET_MAP_INDEX( packet_id ) ]; |
132 | rwlock_read_unlock( & pm_globals.lock ); |
132 | fibril_rwlock_read_unlock( & pm_globals.lock ); |
133 | return packet; |
133 | return packet; |
134 | } |
134 | } |
135 | 135 | ||
136 | int pm_add( packet_t packet ){ |
136 | int pm_add( packet_t packet ){ |
137 | ERROR_DECLARE; |
137 | ERROR_DECLARE; |
138 | 138 | ||
139 | packet_map_ref map; |
139 | packet_map_ref map; |
140 | 140 | ||
141 | if( ! packet_is_valid( packet )) return EINVAL; |
141 | if( ! packet_is_valid( packet )) return EINVAL; |
142 | rwlock_write_lock( & pm_globals.lock ); |
142 | fibril_rwlock_write_lock( & pm_globals.lock ); |
143 | if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){ |
143 | if( PACKET_MAP_PAGE( packet->packet_id ) < gpm_count( & pm_globals.packet_map )){ |
144 | map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id )); |
144 | map = gpm_get_index( & pm_globals.packet_map, PACKET_MAP_PAGE( packet->packet_id )); |
145 | }else{ |
145 | }else{ |
146 | do{ |
146 | do{ |
147 | map = ( packet_map_ref ) malloc( sizeof( packet_map_t )); |
147 | map = ( packet_map_ref ) malloc( sizeof( packet_map_t )); |
148 | if( ! map ){ |
148 | if( ! map ){ |
149 | rwlock_write_unlock( & pm_globals.lock ); |
149 | fibril_rwlock_write_unlock( & pm_globals.lock ); |
150 | return ENOMEM; |
150 | return ENOMEM; |
151 | } |
151 | } |
152 | bzero( map, sizeof( packet_map_t )); |
152 | bzero( map, sizeof( packet_map_t )); |
153 | if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0 ){ |
153 | if(( ERROR_CODE = gpm_add( & pm_globals.packet_map, map )) < 0 ){ |
154 | rwlock_write_unlock( & pm_globals.lock ); |
154 | fibril_rwlock_write_unlock( & pm_globals.lock ); |
155 | free( map ); |
155 | free( map ); |
156 | return ERROR_CODE; |
156 | return ERROR_CODE; |
157 | } |
157 | } |
158 | }while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map )); |
158 | }while( PACKET_MAP_PAGE( packet->packet_id ) >= gpm_count( & pm_globals.packet_map )); |
159 | } |
159 | } |
160 | ( * map )[ PACKET_MAP_INDEX( packet->packet_id ) ] = packet; |
160 | ( * map )[ PACKET_MAP_INDEX( packet->packet_id ) ] = packet; |
161 | rwlock_write_unlock( & pm_globals.lock ); |
161 | fibril_rwlock_write_unlock( & pm_globals.lock ); |
162 | return EOK; |
162 | return EOK; |
163 | } |
163 | } |
164 | 164 | ||
165 | void pm_destroy( void ){ |
165 | void pm_destroy( void ){ |
166 | int count; |
166 | int count; |
167 | int index; |
167 | int index; |
168 | packet_map_ref map; |
168 | packet_map_ref map; |
169 | packet_t packet; |
169 | packet_t packet; |
170 | 170 | ||
171 | rwlock_write_lock( & pm_globals.lock ); |
171 | fibril_rwlock_write_lock( & pm_globals.lock ); |
172 | count = gpm_count( & pm_globals.packet_map ); |
172 | count = gpm_count( & pm_globals.packet_map ); |
173 | while( count > 0 ){ |
173 | while( count > 0 ){ |
174 | map = gpm_get_index( & pm_globals.packet_map, count - 1 ); |
174 | map = gpm_get_index( & pm_globals.packet_map, count - 1 ); |
175 | for( index = PACKET_MAP_SIZE - 1; index >= 0; -- index ){ |
175 | for( index = PACKET_MAP_SIZE - 1; index >= 0; -- index ){ |
176 | packet = ( * map )[ index ]; |
176 | packet = ( * map )[ index ]; |