add functions fc_floor_prime and fc_ceil_prime
parent
74bcd17360
commit
3e3bcda2df
3
HISTORY
3
HISTORY
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
Version 1.44 2020-02-25
|
||||
Version 1.44 2020-02-27
|
||||
* add test file src/tests/test_pthread_lock.c
|
||||
* add uniq_skiplist.[hc]
|
||||
* add function split_string_ex
|
||||
|
|
@ -10,6 +10,7 @@ Version 1.44 2020-02-25
|
|||
* add function fast_mpool_log_stats
|
||||
* add files: server_id_func.[hc]
|
||||
* common_blocked_queue support pop all nodes
|
||||
* shared_func.[hc]: add functions fc_floor_prime and fc_ceil_prime
|
||||
|
||||
Version 1.43 2019-12-25
|
||||
* replace function call system to getExecResult,
|
||||
|
|
|
|||
|
|
@ -119,6 +119,8 @@ int conn_pool_connect_server_ex(ConnectionInfo *pConnection,
|
|||
{
|
||||
if ((result=socketBind2(domain, pConnection->sock, bind_ipaddr, 0)) != 0)
|
||||
{
|
||||
close(pConnection->sock);
|
||||
pConnection->sock = -1;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,17 +42,17 @@ void fast_buffer_destroy(FastBuffer *buffer)
|
|||
}
|
||||
}
|
||||
|
||||
int fast_buffer_check(FastBuffer *buffer, const int inc_len)
|
||||
int fast_buffer_check_capacity(FastBuffer *buffer, const int capacity)
|
||||
{
|
||||
int alloc_size;
|
||||
char *buff;
|
||||
|
||||
if (buffer->alloc_size >= buffer->length + inc_len)
|
||||
if (buffer->alloc_size >= capacity)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
alloc_size = buffer->alloc_size * 2;
|
||||
while (alloc_size <= buffer->length + inc_len)
|
||||
while (alloc_size <= capacity)
|
||||
{
|
||||
alloc_size *= 2;
|
||||
}
|
||||
|
|
@ -97,7 +97,7 @@ int fast_buffer_append(FastBuffer *buffer, const char *format, ...)
|
|||
}
|
||||
else //maybe full, realloc and try again
|
||||
{
|
||||
if ((result=fast_buffer_check(buffer, len)) == 0)
|
||||
if ((result=fast_buffer_check(buffer, len + 1)) == 0)
|
||||
{
|
||||
va_start(ap, format);
|
||||
buffer->length += vsnprintf(buffer->data + buffer->length,
|
||||
|
|
@ -120,7 +120,7 @@ int fast_buffer_append_buff(FastBuffer *buffer, const char *data, const int len)
|
|||
{
|
||||
return 0;
|
||||
}
|
||||
if ((result=fast_buffer_check(buffer, len)) != 0)
|
||||
if ((result=fast_buffer_check(buffer, len + 1)) != 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,16 @@ static inline void fast_buffer_reset(FastBuffer *buffer)
|
|||
|
||||
void fast_buffer_destroy(FastBuffer *buffer);
|
||||
|
||||
int fast_buffer_check(FastBuffer *buffer, const int inc_len);
|
||||
#define fast_buffer_check(buffer, inc_len) \
|
||||
fast_buffer_check_inc_size(buffer, inc_len)
|
||||
|
||||
int fast_buffer_check_capacity(FastBuffer *buffer, const int capacity);
|
||||
|
||||
static inline int fast_buffer_check_inc_size(FastBuffer *buffer,
|
||||
const int inc_size)
|
||||
{
|
||||
return fast_buffer_check_capacity(buffer, buffer->length + inc_size);
|
||||
}
|
||||
|
||||
int fast_buffer_append(FastBuffer *buffer, const char *format, ...);
|
||||
|
||||
|
|
|
|||
|
|
@ -1476,7 +1476,7 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array,
|
|||
return &(*current)->conn;
|
||||
}
|
||||
|
||||
if ((*err_no= conn_pool_connect_server_ex(&(*current)->conn,
|
||||
if ((*err_no=conn_pool_connect_server_ex(&(*current)->conn,
|
||||
connect_timeout, bind_ipaddr, log_connect_error)) == 0)
|
||||
{
|
||||
return &(*current)->conn;
|
||||
|
|
@ -1491,7 +1491,7 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array,
|
|||
if (addr == current) {
|
||||
continue;
|
||||
}
|
||||
if ((*err_no= conn_pool_connect_server_ex(&(*addr)->conn,
|
||||
if ((*err_no=conn_pool_connect_server_ex(&(*addr)->conn,
|
||||
connect_timeout, bind_ipaddr,
|
||||
log_connect_error)) == 0)
|
||||
{
|
||||
|
|
@ -1513,3 +1513,48 @@ void fc_server_disconnect(FCAddressPtrArray *addr_array)
|
|||
(*current)->conn.sock = -1;
|
||||
}
|
||||
}
|
||||
|
||||
int fc_server_make_connection_ex(FCAddressPtrArray *addr_array,
|
||||
ConnectionInfo *conn, const int connect_timeout,
|
||||
const char *bind_ipaddr, const bool log_connect_error)
|
||||
{
|
||||
FCAddressInfo **current;
|
||||
FCAddressInfo **addr;
|
||||
FCAddressInfo **end;
|
||||
int result;
|
||||
|
||||
if (addr_array->count <= 0) {
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
current = addr_array->addrs + addr_array->index;
|
||||
*conn = (*current)->conn;
|
||||
conn->sock = -1;
|
||||
if ((result=conn_pool_connect_server_ex(conn, connect_timeout,
|
||||
bind_ipaddr, log_connect_error)) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (addr_array->count == 1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
end = addr_array->addrs + addr_array->count;
|
||||
for (addr=addr_array->addrs; addr<end; addr++) {
|
||||
if (addr == current) {
|
||||
continue;
|
||||
}
|
||||
|
||||
*conn = (*addr)->conn;
|
||||
conn->sock = -1;
|
||||
if ((result=conn_pool_connect_server_ex(conn, connect_timeout,
|
||||
bind_ipaddr, log_connect_error)) == 0)
|
||||
{
|
||||
addr_array->index = addr - addr_array->addrs;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -180,9 +180,16 @@ ConnectionInfo *fc_server_check_connect_ex(FCAddressPtrArray *addr_array,
|
|||
#define fc_server_check_connect(addr_array, connect_timeout, err_no) \
|
||||
fc_server_check_connect_ex(addr_array, connect_timeout, NULL, true, err_no)
|
||||
|
||||
|
||||
void fc_server_disconnect(FCAddressPtrArray *addr_array);
|
||||
|
||||
|
||||
int fc_server_make_connection_ex(FCAddressPtrArray *addr_array,
|
||||
ConnectionInfo *conn, const int connect_timeout,
|
||||
const char *bind_ipaddr, const bool log_connect_error);
|
||||
|
||||
#define fc_server_make_connection(addr_array, conn, connect_timeout) \
|
||||
fc_server_make_connection_ex(addr_array, conn, connect_timeout, NULL, true)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -2947,3 +2947,60 @@ int fc_delete_file_ex(const char *filename, const char *caption)
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool fc_is_prime(const int n)
|
||||
{
|
||||
int loop;
|
||||
int i;
|
||||
|
||||
if (n <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
loop = lround(sqrt((double)n));
|
||||
for (i=2; i<=loop; i++)
|
||||
{
|
||||
if (n % i == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int fc_floor_prime(const int n)
|
||||
{
|
||||
int start;
|
||||
int i;
|
||||
|
||||
start = (n % 2 == 0 ? n - 1 : n);
|
||||
for (i = start; i > 0; i -= 2)
|
||||
{
|
||||
if (fc_is_prime(i))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int fc_ceil_prime(const int n)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (n <= 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
i = (n % 2 == 0 ? n + 1 : n);
|
||||
while (!fc_is_prime(i))
|
||||
{
|
||||
i += 2;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -889,6 +889,28 @@ static inline int fc_delete_file(const char *filename)
|
|||
return fc_delete_file_ex(filename, "");
|
||||
}
|
||||
|
||||
/** if prime number
|
||||
* parameters:
|
||||
* n: the number to detect
|
||||
* return: true for prime number, otherwise false
|
||||
*/
|
||||
bool fc_is_prime(const int n);
|
||||
|
||||
|
||||
/** find the largest prime number not greater than n
|
||||
* parameters:
|
||||
* n: the number to detect
|
||||
* return: the largest prime number near n
|
||||
*/
|
||||
int fc_floor_prime(const int n);
|
||||
|
||||
/** find the smallest prime number not less than n
|
||||
* parameters:
|
||||
* n: the number to detect
|
||||
* return: the smallest prime number near n
|
||||
*/
|
||||
int fc_ceil_prime(const int n);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -26,7 +26,6 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
log_init();
|
||||
|
||||
if ((result=fc_server_load_from_file_ex(&ctx, config_filename,
|
||||
default_port, min_hosts_each_group,
|
||||
share_between_groups)) != 0)
|
||||
|
|
|
|||
Loading…
Reference in New Issue