Skip to content

Buffer.c overhaul #1085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Migrate CHEROKEE_BUF_INIT to cherokee_buffer_init. Fix #1081
  • Loading branch information
skinkie committed Jan 4, 2014
commit 83ac72fa3cc1c5385cb5b26c29575f2ff901c2ef
68 changes: 48 additions & 20 deletions cget/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ do_download__has_headers (cherokee_downloader_t *downloader, void *param)
/* Check the response
*/
if (quiet == false) {
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;
cherokee_buffer_init (&tmp);

cherokee_http_code_copy (HDR_RESPONSE(hdr), &tmp);
print_tuple_str ("Response", tmp.buf);
Expand Down Expand Up @@ -226,7 +227,7 @@ do_download__read_body (cherokee_downloader_t *downloader, void *param)
{
ret_t ret;
ssize_t len;
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;

UNUSED(param);

Expand All @@ -240,6 +241,7 @@ do_download__read_body (cherokee_downloader_t *downloader, void *param)

/* Print info
*/
cherokee_buffer_init (&tmp);
cherokee_buffer_add_fsize (&tmp, downloader->content_length);
cherokee_buffer_add_str (&tmp, " of ");
cherokee_buffer_add_fsize (&tmp, downloader->info.body_recv);
Expand Down Expand Up @@ -348,7 +350,7 @@ main (int argc, char **argv)
cint_t param_num;
cint_t long_index;
cherokee_downloader_t *downloader;
cherokee_buffer_t proxy = CHEROKEE_BUF_INIT;
cherokee_buffer_t proxy;
cuint_t proxy_port;

struct option long_options[] = {
Expand Down Expand Up @@ -419,49 +421,70 @@ main (int argc, char **argv)
return EXIT_OK;
}

/* Initialise the buffers
*/
cherokee_buffer_init (&proxy);

/* Tracing and proxy discovering..
*/
cherokee_init();
cget_find_proxy (&proxy, &proxy_port);

for (val=optind; val<optind+param_num; val++) {
cherokee_buffer_t url = CHEROKEE_BUF_INIT;
cherokee_buffer_t url;

/* Initialise the buffers
*/
cherokee_buffer_init (&url);

/* Build the url buffer
*/
ret = cherokee_buffer_add_va (&url, "%s", argv[val]);
if (ret != ret_ok)
exit (EXIT_ERROR);
if (ret != ret_ok) {
re = EXIT_ERROR;
goto out;
}

/* Create the downloader object..
*/
ret = cherokee_downloader_new (&downloader);
if (ret != ret_ok)
exit (EXIT_ERROR);
if (ret != ret_ok) {
re = EXIT_ERROR;
goto out;
}

ret = cherokee_downloader_init(downloader);
if (ret != ret_ok)
exit (EXIT_ERROR);
if (ret != ret_ok) {
re = EXIT_ERROR;
goto out;
}

if (! cherokee_buffer_is_empty (&proxy)) {
ret = cherokee_downloader_set_proxy (downloader, &proxy, proxy_port);
if (ret != ret_ok)
exit (EXIT_ERROR);
if (ret != ret_ok) {
re = EXIT_ERROR;
goto out;
}
}

ret = cherokee_downloader_set_url (downloader, &url);
if (ret != ret_ok)
exit (EXIT_ERROR);
if (ret != ret_ok) {
re = EXIT_ERROR;
goto out;
}

ret = cherokee_downloader_connect (downloader);
if (ret != ret_ok)
exit (EXIT_ERROR);
if (ret != ret_ok) {
re = EXIT_ERROR;
goto out;
}

/* Download it!
*/
ret = do_download (downloader);
if ((ret != ret_ok) && (ret != ret_eof)) {
exit (EXIT_ERROR);
re = EXIT_ERROR;
goto out;
}

/* Free the objects..
Expand All @@ -473,9 +496,14 @@ main (int argc, char **argv)
/* Close the output file
*/
re = close (output_fd);
if (re != 0)
exit (EXIT_ERROR);
if (re != 0) {
re = EXIT_ERROR;
goto out;
}

out:
re = EXIT_OK;
cherokee_buffer_mrproper (&proxy);
cherokee_mrproper();
return EXIT_OK;
return re;
}
5 changes: 3 additions & 2 deletions cherokee/access.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,21 @@ cherokee_access_add_subnet (cherokee_access_t *entry, char *subnet)
char *slash;
char *mask;
subnet_item_t *n;
cherokee_buffer_t ip = CHEROKEE_BUF_INIT;
cherokee_buffer_t ip;

/* Split the string
*/
slash = strpbrk (subnet, "/\\");
if (slash == NULL) return ret_error;

mask = slash +1;
cherokee_buffer_init (&ip);
cherokee_buffer_add (&ip, subnet, mask-subnet-1);

/* Create the new list object
*/
n = new_subnet();
if (n == NULL) return ret_error;
if (n == NULL) goto error;

cherokee_list_add (LIST(n), &entry->list_subnets);

Expand Down
10 changes: 7 additions & 3 deletions cherokee/balancer_failover.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ reactivate_entry (cherokee_balancer_failover_t *balancer,
/* balancer->mutex is LOCKED
*/
ret_t ret;
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;

/* Reactivate
*/
ret = reactivate_entry_guts (balancer, entry);

/* Notify
*/
cherokee_buffer_init (&tmp);
cherokee_source_copy_name (entry->source, &tmp);
LOG_WARNING (CHEROKEE_ERROR_BALANCER_FAILOVER_REACTIVE, tmp.buf);
cherokee_buffer_mrproper (&tmp);
Expand Down Expand Up @@ -124,10 +125,12 @@ report_fail (cherokee_balancer_failover_t *balancer,
ret_t ret;
cherokee_list_t *i;
cherokee_balancer_entry_t *entry;
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;

UNUSED(conn);

cherokee_buffer_init (&tmp);

CHEROKEE_MUTEX_LOCK (&balancer->mutex);

list_for_each (i, &BAL(balancer)->entries) {
Expand All @@ -152,9 +155,9 @@ report_fail (cherokee_balancer_failover_t *balancer,
*/
cherokee_source_copy_name (entry->source, &tmp);
LOG_WARNING (CHEROKEE_ERROR_BALANCER_FAILOVER_DISABLE, tmp.buf);
cherokee_buffer_mrproper (&tmp);

CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
cherokee_buffer_mrproper (&tmp);
return ret_ok;
}

Expand All @@ -163,6 +166,7 @@ report_fail (cherokee_balancer_failover_t *balancer,

out:
CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
cherokee_buffer_mrproper (&tmp);
return ret;
}

Expand Down
10 changes: 7 additions & 3 deletions cherokee/balancer_ip_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ reactivate_entry (cherokee_balancer_ip_hash_t *balancer,
{
/* balancer->mutex is LOCKED
*/
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;

/* Disable
*/
Expand All @@ -94,6 +94,7 @@ reactivate_entry (cherokee_balancer_ip_hash_t *balancer,

/* Notify
*/
cherokee_buffer_init (&tmp);
cherokee_source_copy_name (entry->source, &tmp);
LOG_WARNING (CHEROKEE_ERROR_BALANCER_IP_REACTIVE, tmp.buf, balancer->n_active);
cherokee_buffer_mrproper (&tmp);
Expand All @@ -110,10 +111,12 @@ report_fail (cherokee_balancer_ip_hash_t *balancer,
ret_t ret;
cherokee_list_t *i;
cherokee_balancer_entry_t *entry;
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;

UNUSED(conn);

cherokee_buffer_init (&tmp);

CHEROKEE_MUTEX_LOCK (&balancer->mutex);

list_for_each (i, &BAL(balancer)->entries) {
Expand All @@ -140,9 +143,9 @@ report_fail (cherokee_balancer_ip_hash_t *balancer,
*/
cherokee_source_copy_name (entry->source, &tmp);
LOG_WARNING (CHEROKEE_ERROR_BALANCER_IP_DISABLE, tmp.buf, balancer->n_active);
cherokee_buffer_mrproper (&tmp);

CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
cherokee_buffer_mrproper (&tmp);
return ret_ok;
}

Expand All @@ -151,6 +154,7 @@ report_fail (cherokee_balancer_ip_hash_t *balancer,

out:
CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
cherokee_buffer_mrproper (&tmp);
return ret;
}

Expand Down
11 changes: 8 additions & 3 deletions cherokee/balancer_round_robin.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ reactivate_entry (cherokee_balancer_entry_t *entry)
{
/* balancer->mutex is LOCKED
*/
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;

/* Disable
*/
Expand All @@ -78,6 +78,7 @@ reactivate_entry (cherokee_balancer_entry_t *entry)

/* Notify
*/
cherokee_buffer_init (&tmp);
cherokee_source_copy_name (entry->source, &tmp);
LOG_WARNING (CHEROKEE_ERROR_BALANCER_ONLINE_SOURCE, tmp.buf);
cherokee_buffer_mrproper (&tmp);
Expand Down Expand Up @@ -141,9 +142,12 @@ report_fail (cherokee_balancer_round_robin_t *balancer,
ret_t ret;
cherokee_list_t *i;
cherokee_balancer_entry_t *entry;
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;

UNUSED(conn);

cherokee_buffer_init (&tmp);

CHEROKEE_MUTEX_LOCK (&balancer->mutex);

list_for_each (i, &BAL(balancer)->entries) {
Expand All @@ -168,9 +172,9 @@ report_fail (cherokee_balancer_round_robin_t *balancer,
*/
cherokee_source_copy_name (entry->source, &tmp);
LOG_WARNING (CHEROKEE_ERROR_BALANCER_OFFLINE_SOURCE, tmp.buf);
cherokee_buffer_mrproper (&tmp);

CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
cherokee_buffer_mrproper (&tmp);
return ret_ok;
}

Expand All @@ -179,6 +183,7 @@ report_fail (cherokee_balancer_round_robin_t *balancer,

out:
CHEROKEE_MUTEX_UNLOCK (&balancer->mutex);
cherokee_buffer_mrproper (&tmp);
return ret;
}

Expand Down
10 changes: 0 additions & 10 deletions cherokee/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@
CHEROKEE_ADD_FUNC_NEW (buffer);
CHEROKEE_ADD_FUNC_FREE (buffer);

ret_t
cherokee_buffer_init (cherokee_buffer_t *buf)
{
buf->buf = NULL;
buf->len = 0;
buf->size = 0;

return ret_ok;
}

void
cherokee_buffer_fake (cherokee_buffer_t *buf, const char *str, cuint_t len)
{
Expand Down
14 changes: 13 additions & 1 deletion cherokee/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ typedef struct {
cuint_t len; /**< Length of the string */
} cherokee_buffer_t;

/* Placing the function here allows the compiler to inline
* the code without the use of link-time optimisation #1081
*/
static inline ret_t
cherokee_buffer_init (cherokee_buffer_t *buf)
{
buf->buf = NULL;
buf->len = 0;
buf->size = 0;

return ret_ok;
}

#define BUF(x) ((cherokee_buffer_t *)(x))
#define CHEROKEE_BUF_INIT {NULL, 0, 0}
#define CHEROKEE_BUF_SLIDE_NONE INT_MIN

#define cherokee_buffer_is_empty(b) (BUF(b)->len == 0)
Expand Down
3 changes: 2 additions & 1 deletion cherokee/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,9 @@ cherokee_cache_get (cherokee_cache_t *cache,

#ifdef TRACE_ENABLED
if (cache->count % 100 == 0) {
cherokee_buffer_t tmp = CHEROKEE_BUF_INIT;
cherokee_buffer_t tmp;

cherokee_buffer_init (&tmp);
cherokee_cache_get_stats (cache, &tmp);
TRACE(ENTRIES, "Cache stats:\n%s", tmp.buf);
cherokee_buffer_mrproper (&tmp);
Expand Down
Loading