Skip to content

Commit

Permalink
core: use new memory handler wrappers
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Oct 19, 2016
1 parent 9f59555 commit 377ae93
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 58 deletions.
18 changes: 12 additions & 6 deletions include/fluent-bit/flb_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_log.h>

#include <stdlib.h>

/*
* The following memory handling wrappers, aims to simplify the way to use
* the default memory allocator from the libc or an alternative one as Jemalloc.
*
* Here there is no error logging in case of failures, we defer that task to the
* caller.
*/

#if ((__GNUC__ * 100 + __GNUC__MINOR__) > 430) /* gcc version > 4.3 */
# define ALLOCSZ_ATTR(x,...) __attribute__ ((alloc_size(x, ##__VA_ARGS__)))
Expand All @@ -36,7 +45,6 @@ void *flb_malloc(const size_t size) {

aux = malloc(size);
if (flb_unlikely(!aux && size)) {
flb_errno();
return NULL;
}

Expand All @@ -48,8 +56,7 @@ void *flb_calloc(size_t n, const size_t size) {
void *buf;

buf = calloc(n, size);
if (mk_unlikely(!buf)) {
flb_errno();
if (flb_unlikely(!buf)) {
return NULL;
}

Expand All @@ -62,8 +69,7 @@ void *flb_realloc(void *ptr, const size_t size)
void *aux;

aux = realloc(ptr, size);
if (mk_unlikely(!aux && size)) {
flb_errno();
if (flb_unlikely(!aux && size)) {
return NULL;
}

Expand Down
9 changes: 5 additions & 4 deletions src/flb_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <inttypes.h>

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>

#ifdef FLB_HAVE_BUFFERING

Expand Down Expand Up @@ -362,7 +363,7 @@ struct flb_buffer *flb_buffer_create(char *path, int workers,
}

/* Main buffer context */
ctx = malloc(sizeof(struct flb_buffer));
ctx = flb_malloc(sizeof(struct flb_buffer));
if (!ctx) {
return NULL;
}
Expand All @@ -371,7 +372,7 @@ struct flb_buffer *flb_buffer_create(char *path, int workers,

path_len = strlen(path);
if (path[path_len - 1] != '/') {
ctx->path = malloc(path_len + 2);
ctx->path = flb_malloc(path_len + 2);
memcpy(ctx->path, path, path_len);
ctx->path[path_len++] = '/';
ctx->path[path_len++] = '\0';
Expand All @@ -391,7 +392,7 @@ struct flb_buffer *flb_buffer_create(char *path, int workers,

for (i = 0; i < ctx->workers_n; i++) {
/* Allocate worker context */
worker = calloc(1, sizeof(struct flb_buffer_worker));
worker = flb_calloc(1, sizeof(struct flb_buffer_worker));
if (!worker) {
flb_buffer_destroy(ctx);
return NULL;
Expand Down Expand Up @@ -450,7 +451,7 @@ struct flb_buffer *flb_buffer_create(char *path, int workers,
ctx->workers_n = i;

/* Generate pseudo input plugin and instance */
ctx->i_ins = calloc(1, sizeof(struct flb_input_instance));
ctx->i_ins = flb_calloc(1, sizeof(struct flb_input_instance));
if (!ctx->i_ins) {
flb_errno();
flb_buffer_destroy(ctx);
Expand Down
7 changes: 4 additions & 3 deletions src/flb_buffer_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>

#ifdef FLB_HAVE_BUFFERING

Expand Down Expand Up @@ -218,7 +219,7 @@ static int chunk_find(char *root_path, char *hash,
return -1;
}

target = malloc(PATH_MAX);
target = flb_malloc(PATH_MAX);
if (!target) {
free(file);
return -1;
Expand Down Expand Up @@ -267,7 +268,7 @@ static int chunk_remove_route(char *root_path, char *abs_path,
return -1;
}

to = malloc(PATH_MAX);
to = flb_malloc(PATH_MAX);
if (!to) {
flb_errno();
return -1;
Expand Down Expand Up @@ -385,7 +386,7 @@ int flb_buffer_chunk_add(struct flb_buffer_worker *worker,
*
* SHA1(chunk.data).routes_id.wID.tag
*/
fchunk = malloc(PATH_MAX);
fchunk = flb_malloc(PATH_MAX);
if (!fchunk) {
flb_errno();
return -1;
Expand Down
5 changes: 3 additions & 2 deletions src/flb_buffer_qchunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_buffer.h>
#include <fluent-bit/flb_buffer_qchunk.h>
#include <fluent-bit/flb_engine_dispatch.h>
Expand Down Expand Up @@ -57,7 +58,7 @@ struct flb_buffer_qchunk *flb_buffer_qchunk_add(struct flb_buffer_qworker *qw,
int len;
struct flb_buffer_qchunk *qchunk;

qchunk = malloc(sizeof(struct flb_buffer_qchunk));
qchunk = flb_malloc(sizeof(struct flb_buffer_qchunk));
if (!qchunk) {
perror("malloc");
return NULL;
Expand Down Expand Up @@ -343,7 +344,7 @@ int flb_buffer_qchunk_create(struct flb_buffer *ctx)
struct flb_buffer_qworker *qw;

/* Allocate context */
qw = malloc(sizeof(struct flb_buffer_qworker));
qw = flb_malloc(sizeof(struct flb_buffer_qworker));
if (!qw) {
perror("malloc");
return -1;
Expand Down
3 changes: 2 additions & 1 deletion src/flb_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stddef.h>

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_plugins.h>
Expand Down Expand Up @@ -77,7 +78,7 @@ struct flb_config *flb_config_init()
{
struct flb_config *config;

config = calloc(1, sizeof(struct flb_config));
config = flb_calloc(1, sizeof(struct flb_config));
if (!config) {
perror("malloc");
return NULL;
Expand Down
10 changes: 6 additions & 4 deletions src/flb_http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* - Get return Status, Headers and Body content if found.
*/

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_http_client.h>

/* check if there is enough space in the client header buffer */
Expand Down Expand Up @@ -156,7 +158,7 @@ struct flb_http_client *flb_http_client(struct flb_upstream_conn *u_conn,
break;
};

buf = calloc(1, FLB_HTTP_BUF_SIZE);
buf = flb_calloc(1, FLB_HTTP_BUF_SIZE);
if (!buf) {
perror("malloc");
return NULL;
Expand Down Expand Up @@ -190,7 +192,7 @@ struct flb_http_client *flb_http_client(struct flb_upstream_conn *u_conn,
return NULL;
}

c = calloc(1, sizeof(struct flb_http_client));
c = flb_calloc(1, sizeof(struct flb_http_client));
if (!c) {
free(buf);
return NULL;
Expand Down Expand Up @@ -246,7 +248,7 @@ int flb_http_add_header(struct flb_http_client *c,
else {
new_size = c->header_size + required;
}
tmp = realloc(c->header_buf, new_size);
tmp = flb_realloc(c->header_buf, new_size);
if (!tmp) {
perror("realloc");
return -1;
Expand Down Expand Up @@ -290,7 +292,7 @@ int flb_http_do(struct flb_http_client *c, size_t *bytes)
/* check enough space for the ending CRLF */
if (header_available(c, crlf) != 0) {
new_size = c->header_size + 2;
tmp = realloc(c->header_buf, new_size);
tmp = flb_realloc(c->header_buf, new_size);
if (!tmp) {
return -1;
}
Expand Down
16 changes: 9 additions & 7 deletions src/flb_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <string.h>
#include <unistd.h>

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_error.h>
Expand Down Expand Up @@ -99,7 +101,7 @@ struct flb_input_instance *flb_input_new(struct flb_config *config,
}

/* Create plugin instance */
instance = malloc(sizeof(struct flb_input_instance));
instance = flb_malloc(sizeof(struct flb_input_instance));
if (!instance) {
perror("malloc");
return NULL;
Expand Down Expand Up @@ -179,7 +181,7 @@ int flb_input_set_property(struct flb_input_instance *in, char *k, char *v)
}
else {
/* Append any remaining configuration key to prop list */
prop = malloc(sizeof(struct flb_config_prop));
prop = flb_malloc(sizeof(struct flb_config_prop));
if (!prop) {
return -1;
}
Expand Down Expand Up @@ -377,7 +379,7 @@ int flb_input_set_collector_time(struct flb_input_instance *in,
{
struct flb_input_collector *collector;

collector = malloc(sizeof(struct flb_input_collector));
collector = flb_malloc(sizeof(struct flb_input_collector));
collector->type = FLB_COLLECT_TIME;
collector->cb_collect = cb_collect;
collector->fd_event = -1;
Expand All @@ -397,7 +399,7 @@ int flb_input_set_collector_event(struct flb_input_instance *in,
{
struct flb_input_collector *collector;

collector = malloc(sizeof(struct flb_input_collector));
collector = flb_malloc(sizeof(struct flb_input_collector));
collector->type = FLB_COLLECT_FD_EVENT;
collector->cb_collect = cb_collect;
collector->fd_event = fd;
Expand All @@ -417,7 +419,7 @@ int flb_input_set_collector_socket(struct flb_input_instance *in,
{
struct flb_input_collector *collector;

collector = malloc(sizeof(struct flb_input_collector));
collector = flb_malloc(sizeof(struct flb_input_collector));
collector->type = FLB_COLLECT_FD_SERVER;
collector->cb_collect = cb_new_connection;
collector->fd_event = fd;
Expand All @@ -441,14 +443,14 @@ struct flb_input_dyntag *flb_input_dyntag_create(struct flb_input_instance *in,
}

/* Allocate node and reset fields */
dt = malloc(sizeof(struct flb_input_dyntag));
dt = flb_malloc(sizeof(struct flb_input_dyntag));
if (!dt) {
return NULL;
}
dt->busy = FLB_FALSE;
dt->lock = FLB_FALSE;
dt->in = in;
dt->tag = malloc(tag_len + 1);
dt->tag = flb_malloc(tag_len + 1);
memcpy(dt->tag, tag, tag_len);
dt->tag[tag_len] = '\0';
dt->tag_len = tag_len;
Expand Down
6 changes: 4 additions & 2 deletions src/flb_io_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <mbedtls/debug.h>
#include <mbedtls/error.h>

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_io.h>
#include <fluent-bit/flb_tls.h>
#include <fluent-bit/flb_io_tls.h>
Expand Down Expand Up @@ -75,7 +77,7 @@ struct flb_tls_context *flb_tls_context_new(int verify,
int ret;
struct flb_tls_context *ctx;

ctx = malloc(sizeof(struct flb_tls_context));
ctx = flb_malloc(sizeof(struct flb_tls_context));
if (!ctx) {
perror("malloc");
return NULL;
Expand Down Expand Up @@ -157,7 +159,7 @@ struct flb_tls_session *flb_tls_session_new(struct flb_tls_context *ctx)
int ret;
struct flb_tls_session *session;

session = malloc(sizeof(struct flb_tls_session));
session = flb_malloc(sizeof(struct flb_tls_session));
if (!session) {
return NULL;
}
Expand Down
6 changes: 4 additions & 2 deletions src/flb_kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include <ctype.h>
#include <sys/utsname.h>

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_kernel.h>
#include <fluent-bit/flb_utils.h>

Expand Down Expand Up @@ -77,14 +79,14 @@ struct flb_kernel *flb_kernel_info()
c = atoi(tmp);
free(tmp);

kernel = malloc(sizeof(struct flb_kernel));
kernel = flb_malloc(sizeof(struct flb_kernel));
if (!kernel) {
return NULL;
}
kernel->minor = a;
kernel->major = b;
kernel->patch = c;
kernel->s_version.data = malloc(16);
kernel->s_version.data = flb_malloc(16);

len = snprintf(kernel->s_version.data, 16, "%i.%i.%i", a, b, c);
if (len == -1) {
Expand Down
5 changes: 3 additions & 2 deletions src/flb_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdarg.h>

#include <fluent-bit/flb_lib.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_engine.h>
#include <fluent-bit/flb_input.h>
#include <fluent-bit/flb_output.h>
Expand Down Expand Up @@ -82,7 +83,7 @@ flb_ctx_t *flb_create()
mtrace();
#endif

ctx = calloc(1, sizeof(flb_ctx_t));
ctx = flb_calloc(1, sizeof(flb_ctx_t));
if (!ctx) {
perror("malloc");
return NULL;
Expand Down Expand Up @@ -115,7 +116,7 @@ flb_ctx_t *flb_create()
config->ch_evl = ctx->event_loop;

/* Prepare the notification channels */
ctx->event_channel = calloc(1, sizeof(struct mk_event));
ctx->event_channel = flb_calloc(1, sizeof(struct mk_event));
ret = mk_event_channel_create(config->ch_evl,
&config->ch_notif[0],
&config->ch_notif[1],
Expand Down
5 changes: 3 additions & 2 deletions src/flb_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_worker.h>
#include <fluent-bit/flb_mem.h>

FLB_TLS_DEFINE(struct flb_log, flb_log_ctx)

Expand Down Expand Up @@ -170,7 +171,7 @@ struct flb_log *flb_log_init(struct flb_config *config, int type,
struct flb_worker *worker;
struct mk_event_loop *evl;

log = malloc(sizeof(struct flb_log));
log = flb_malloc(sizeof(struct flb_log));
if (!log) {
perror("malloc");
return NULL;
Expand Down Expand Up @@ -216,7 +217,7 @@ struct flb_log *flb_log_init(struct flb_config *config, int type,
* it will need a 'worker-like' context, here we create a fake worker
* context just for messaging purposes.
*/
worker = malloc(sizeof(struct flb_worker));
worker = flb_malloc(sizeof(struct flb_worker));
if (!worker) {
flb_errno();
free(log);
Expand Down
Loading

0 comments on commit 377ae93

Please sign in to comment.