| 195 | |
| 196 | |
| 197 | static struct script_route_ref * __ref_script_route_by_name(char *name, int l, |
| 198 | struct script_route *sr, int size, int type, int in_shm) |
| 199 | { |
| 200 | struct script_route_ref *ref; |
| 201 | unsigned int i; |
| 202 | |
| 203 | if (!in_shm) { |
| 204 | /* first check if a reference to the route already exists */ |
| 205 | for( ref=sroute_refs ; ref ; ref=ref->next ) { |
| 206 | if (ref->type==type && ref->name.len==l |
| 207 | && strncmp(ref->name.s,name,l)==0) { |
| 208 | /* we found an already exists reference */ |
| 209 | ref->u.refcnt++; |
| 210 | LM_DBG("returning existing %p [%.*s] with idx %d, " |
| 211 | "ver/cnt %d\n", ref, |
| 212 | ref->name.len, ref->name.s, ref->idx, ref->u.refcnt); |
| 213 | /* note that the returned reference may point to |
| 214 | * no route, there is no guarantee to be a working one */ |
| 215 | return ref; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* no reference found, create a new one */ |
| 220 | ref = pkg_malloc( sizeof *ref + l + 1 ); |
| 221 | } else { |
| 222 | ref = shm_malloc( sizeof *ref + l + 1 ); |
| 223 | } |
| 224 | |
| 225 | if (ref==NULL) { |
| 226 | LM_ERR("failed to allocate new sroute reference\n"); |
| 227 | return NULL; |
| 228 | } |
| 229 | ref->name.s = (char*)(ref+1); |
| 230 | ref->name.len = l; |
| 231 | memcpy( ref->name.s, name, l); |
| 232 | ref->name.s[l] = 0; // make it NULL terminated also |
| 233 | ref->type = type; |
| 234 | ref->idx = -1; |
| 235 | |
| 236 | /* see if we find its idx */ |
| 237 | for( i=1 ; i<size && sr[i].name ; i++ ) { |
| 238 | if (strcmp(sr[i].name,ref->name.s)==0 ) |
| 239 | ref->idx = i; |
| 240 | } |
| 241 | |
| 242 | if (in_shm) { |
| 243 | ref->u.version = sroutes->version; |
| 244 | ref->next = NULL; |
| 245 | } else { |
| 246 | ref->u.refcnt = 1; |
| 247 | /* link it */ |
| 248 | ref->next = sroute_refs; |
| 249 | sroute_refs = ref; |
| 250 | } |
| 251 | |
| 252 | LM_DBG("returning new %p [%.*s] with idx %d, ver/xnt %d\n", ref, |
| 253 | ref->name.len, ref->name.s, ref->idx, ref->u.version); |
| 254 | return ref; |
no test coverage detected