Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

switch_core_port_allocator.c File Reference


Data Structures

struct  switch_core_port_allocator

Functions

switch_status_t switch_core_port_allocator_new (switch_port_t start, switch_port_t end, switch_port_flag_t flags, switch_core_port_allocator_t **new_allocator)
switch_status_t switch_core_port_allocator_request_port (switch_core_port_allocator_t *alloc, switch_port_t *port_ptr)
switch_status_t switch_core_port_allocator_free_port (switch_core_port_allocator_t *alloc, switch_port_t port)
void switch_core_port_allocator_destroy (switch_core_port_allocator_t **alloc)


Function Documentation

void switch_core_port_allocator_destroy switch_core_port_allocator_t **  alloc  ) 
 

00194 {
00195         switch_memory_pool_t *pool = (*alloc)->pool;
00196         switch_core_destroy_memory_pool(&pool);
00197         *alloc = NULL;
00198 }

switch_status_t switch_core_port_allocator_free_port switch_core_port_allocator_t alloc,
switch_port_t  port
 

00172 {
00173         switch_status_t status = SWITCH_STATUS_FALSE;
00174         int even = switch_test_flag(alloc, SPF_EVEN);
00175         int odd = switch_test_flag(alloc, SPF_ODD);
00176         int index = port - alloc->start;
00177         
00178         if (!(even && odd)) {
00179                 index /= 2;
00180         }
00181 
00182         switch_mutex_lock(alloc->mutex);
00183         if (alloc->track[index]) {
00184                 alloc->track[index] = 0;
00185                 alloc->track_used--;
00186                 status = SWITCH_STATUS_SUCCESS;
00187         }
00188         switch_mutex_unlock(alloc->mutex);
00189 
00190         return status;
00191 }

Here is the call graph for this function:

switch_status_t switch_core_port_allocator_new switch_port_t  start,
switch_port_t  end,
switch_port_flag_t  flags,
switch_core_port_allocator_t **  new_allocator
 

00052 {
00053         switch_status_t status;
00054         switch_memory_pool_t *pool;
00055         switch_core_port_allocator_t *alloc;
00056         int even, odd;
00057         
00058         if ((status = switch_core_new_memory_pool(&pool)) != SWITCH_STATUS_SUCCESS) {
00059                 return status;
00060         }
00061 
00062         if (!(alloc = switch_core_alloc(pool, sizeof(*alloc)))) {
00063                 switch_core_destroy_memory_pool(&pool);
00064                 return SWITCH_STATUS_MEMERR;
00065         }
00066         
00067         alloc->flags = flags;
00068         even = switch_test_flag(alloc, SPF_EVEN);
00069         odd = switch_test_flag(alloc, SPF_ODD);
00070 
00071         if (!(even && odd)) {
00072                 if (even) {
00073                         if ((start % 2) != 0) {
00074                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Rounding odd start port %d to %d\n", start, start + 1);
00075                                 start++;
00076                         }
00077                         if ((end % 2) != 0) {
00078                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Rounding odd end port %d to %d\n", end, end - 1);
00079                                 end--;
00080                         }
00081                 } else if (odd) {
00082                         if ((start % 2) == 0) {
00083                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Rounding even start port %d to %d\n", start, start + 1);
00084                                 start++;
00085                         }
00086                         if ((end % 2) == 0) {
00087                                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Rounding even end port %d to %d\n", end, end - 1);
00088                                 end--;
00089                         }
00090                 }
00091         }
00092 
00093         alloc->track_len = (end - start) + 2;   
00094 
00095         if (!(even && odd)) {
00096                 alloc->track_len /= 2;
00097         }
00098         
00099         alloc->track = switch_core_alloc(pool, (alloc->track_len + 2) * sizeof(switch_byte_t));
00100         
00101         alloc->start = start;
00102         alloc->next = start;
00103         alloc->end = end;
00104 
00105 
00106         switch_mutex_init(&alloc->mutex, SWITCH_MUTEX_NESTED, pool);
00107         alloc->pool = pool;
00108         *new_allocator = alloc;
00109 
00110         return SWITCH_STATUS_SUCCESS;
00111 }

Here is the call graph for this function:

switch_status_t switch_core_port_allocator_request_port switch_core_port_allocator_t alloc,
switch_port_t port_ptr
 

00114 {
00115         switch_port_t port = 0;
00116         switch_status_t status = SWITCH_STATUS_FALSE;
00117         int even = switch_test_flag(alloc, SPF_EVEN);
00118         int odd = switch_test_flag(alloc, SPF_ODD);
00119 
00120         switch_mutex_lock(alloc->mutex);
00121         srand(getpid() + (unsigned)switch_timestamp(NULL));
00122         
00123         while(alloc->track_used < alloc->track_len) {
00124                 double r;
00125                 uint32_t index;
00126                 int tries = 0;
00127 
00128                 do {
00129                         r = ((double)rand() / ((double)(RAND_MAX)+(double)(1)));
00130                         index = (int) (r * alloc->track_len);
00131                         tries++;
00132                 } while((alloc->track[index] || index >= alloc->track_len) && tries < 10000);
00133                 
00134                 while(alloc->track[index]) {
00135                         if (++index >= alloc->track_len) {
00136                                 index = 0;
00137                         }
00138                 }
00139 
00140                 if (index < alloc->track_len) {
00141                         alloc->track[index] = 1;
00142                         alloc->track_used++;
00143                         status = SWITCH_STATUS_SUCCESS;
00144 
00145                         if ((even && odd)) {
00146                                 port = (switch_port_t)(index + alloc->start);
00147                         } else {
00148                                 port = (switch_port_t)(index + (alloc->start / 2));
00149                                 port *= 2;
00150                         } 
00151                         goto end;
00152                 }
00153         }
00154         
00155         
00156  end:
00157 
00158         switch_mutex_unlock(alloc->mutex);
00159         
00160         if (status == SWITCH_STATUS_SUCCESS) {
00161                 *port_ptr = port;
00162         } else {
00163                 *port_ptr = 0;
00164         }
00165 
00166         
00167         return status;
00168         
00169 }

Here is the call graph for this function:


Generated on Mon May 26 22:06:52 2008 for FreeSWITCH by  doxygen 1.3.9.1