#include <switch.h>#include "private/switch_core_pvt.h"Include dependency graph for switch_core_port_allocator.c:
Go to the source code of this file.
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) |
| void switch_core_port_allocator_destroy | ( | switch_core_port_allocator_t ** | alloc | ) |
Definition at line 194 of file switch_core_port_allocator.c.
References pool, and switch_core_destroy_memory_pool.
00195 { 00196 switch_memory_pool_t *pool = (*alloc)->pool; 00197 switch_core_destroy_memory_pool(&pool); 00198 *alloc = NULL; 00199 }
| switch_status_t switch_core_port_allocator_free_port | ( | switch_core_port_allocator_t * | alloc, | |
| switch_port_t | port | |||
| ) |
Definition at line 172 of file switch_core_port_allocator.c.
References SPF_EVEN, SPF_ODD, switch_mutex_lock(), switch_mutex_unlock(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, and switch_test_flag.
00173 { 00174 switch_status_t status = SWITCH_STATUS_FALSE; 00175 int even = switch_test_flag(alloc, SPF_EVEN); 00176 int odd = switch_test_flag(alloc, SPF_ODD); 00177 int index = port - alloc->start; 00178 00179 if (!(even && odd)) { 00180 index /= 2; 00181 } 00182 00183 switch_mutex_lock(alloc->mutex); 00184 if (alloc->track[index] > 0) { 00185 alloc->track[index] = -4; 00186 alloc->track_used--; 00187 status = SWITCH_STATUS_SUCCESS; 00188 } 00189 switch_mutex_unlock(alloc->mutex); 00190 00191 return status; 00192 }
| 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 | |||
| ) |
Definition at line 50 of file switch_core_port_allocator.c.
References pool, SPF_EVEN, SPF_ODD, SWITCH_CHANNEL_LOG, switch_core_alloc, switch_core_destroy_memory_pool, switch_core_new_memory_pool, switch_log_printf(), SWITCH_LOG_WARNING, switch_mutex_init(), SWITCH_MUTEX_NESTED, SWITCH_STATUS_MEMERR, SWITCH_STATUS_SUCCESS, and switch_test_flag.
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 }
| switch_status_t switch_core_port_allocator_request_port | ( | switch_core_port_allocator_t * | alloc, | |
| switch_port_t * | port_ptr | |||
| ) |
Definition at line 113 of file switch_core_port_allocator.c.
References SPF_EVEN, SPF_ODD, switch_micro_time_now(), switch_mutex_lock(), switch_mutex_unlock(), SWITCH_STATUS_FALSE, SWITCH_STATUS_SUCCESS, switch_test_flag, and switch_thread_self().
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((unsigned) ((unsigned) (intptr_t) port_ptr + (unsigned) (intptr_t) switch_thread_self() + switch_micro_time_now())); 00122 00123 while (alloc->track_used < alloc->track_len) { 00124 uint32_t index; 00125 uint32_t tries = 0; 00126 00127 /* randomly pick a port */ 00128 index = rand() % alloc->track_len; 00129 00130 /* if it is used walk up the list to find a free one */ 00131 while (alloc->track[index] && tries < alloc->track_len) { 00132 tries++; 00133 if (alloc->track[index] < 0) { 00134 alloc->track[index]++; 00135 } 00136 if (++index >= alloc->track_len) { 00137 index = 0; 00138 } 00139 } 00140 00141 if (tries < alloc->track_len) { 00142 alloc->track[index] = 1; 00143 alloc->track_used++; 00144 status = SWITCH_STATUS_SUCCESS; 00145 00146 if ((even && odd)) { 00147 port = (switch_port_t) (index + alloc->start); 00148 } else { 00149 port = (switch_port_t) (index + (alloc->start / 2)); 00150 port *= 2; 00151 } 00152 goto end; 00153 } 00154 } 00155 00156 00157 end: 00158 00159 switch_mutex_unlock(alloc->mutex); 00160 00161 if (status == SWITCH_STATUS_SUCCESS) { 00162 *port_ptr = port; 00163 } else { 00164 *port_ptr = 0; 00165 } 00166 00167 00168 return status; 00169 00170 }
1.4.7