forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scratch-buffers: Support any number of stacks
Instead of being tied to GStrings only, support registering new kinds of stacks, which will be automatiaclly free'd on thread shutdown. For convenience, wrappers are provided for GString-based scratch buffer acquire and release, and that stack is automatically registered and free'd by the library itself. This fixes elastic#2. Signed-off-by: Gergely Nagy <[email protected]> Signed-off-by: Balazs Scheidler <[email protected]>
- Loading branch information
Showing
7 changed files
with
174 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2011-2012 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011-2012 Gergely Nagy <[email protected]> | ||
* Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011-2013 Gergely Nagy <[email protected]> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
|
@@ -28,42 +28,86 @@ | |
|
||
TLS_BLOCK_START | ||
{ | ||
GTrashStack *scratch_buffers; | ||
GTrashStack *sb_gstrings; | ||
GList *sb_registry; | ||
} | ||
TLS_BLOCK_END; | ||
|
||
#define local_scratch_buffers __tls_deref(scratch_buffers) | ||
/* GStrings */ | ||
|
||
ScratchBuffer * | ||
scratch_buffer_acquire(void) | ||
#define local_sb_gstrings __tls_deref(sb_gstrings) | ||
|
||
GTrashStack * | ||
sb_gstring_acquire_buffer(void) | ||
{ | ||
ScratchBuffer *sb; | ||
SBGString *sb; | ||
|
||
sb = g_trash_stack_pop(&local_scratch_buffers); | ||
sb = g_trash_stack_pop(&local_sb_gstrings); | ||
if (!sb) | ||
{ | ||
sb = g_new(ScratchBuffer, 1); | ||
g_string_steal(sb_string(sb)); | ||
sb = g_new(SBGString, 1); | ||
g_string_steal(sb_gstring_string(sb)); | ||
} | ||
else | ||
g_string_set_size(sb_string(sb), 0); | ||
return sb; | ||
g_string_set_size(sb_gstring_string(sb), 0); | ||
|
||
return (GTrashStack *) sb; | ||
} | ||
|
||
void | ||
scratch_buffer_release(ScratchBuffer *sb) | ||
sb_gstring_release_buffer(GTrashStack *s) | ||
{ | ||
g_trash_stack_push(&local_scratch_buffers, sb); | ||
SBGString *sb = (SBGString *) s; | ||
|
||
g_trash_stack_push(&local_sb_gstrings, sb); | ||
} | ||
|
||
void | ||
scratch_buffers_free(void) | ||
sb_gstring_free_stack(void) | ||
{ | ||
ScratchBuffer *sb; | ||
SBGString *sb; | ||
|
||
while ((sb = g_trash_stack_pop(&local_scratch_buffers)) != NULL) | ||
while ((sb = g_trash_stack_pop(&local_sb_gstrings)) != NULL) | ||
{ | ||
g_free(sb_string(sb)->str); | ||
g_free(sb_gstring_string(sb)->str); | ||
g_free(sb); | ||
} | ||
} | ||
|
||
ScratchBufferStack SBGStringStack = { | ||
.acquire_buffer = sb_gstring_acquire_buffer, | ||
.release_buffer = sb_gstring_release_buffer, | ||
.free_stack = sb_gstring_free_stack | ||
}; | ||
|
||
/* Global API */ | ||
|
||
#define local_sb_registry __tls_deref(sb_registry) | ||
|
||
void | ||
scratch_buffers_register(ScratchBufferStack *stack) | ||
{ | ||
local_sb_registry = g_list_append(local_sb_registry, stack); | ||
} | ||
|
||
void | ||
scratch_buffers_init(void) | ||
{ | ||
local_sb_registry = NULL; | ||
scratch_buffers_register(&SBGStringStack); | ||
} | ||
|
||
static void | ||
scratch_buffers_free_stack(gpointer data, gpointer user_data) | ||
{ | ||
ScratchBufferStack *s = (ScratchBufferStack *) data; | ||
|
||
s->free_stack(); | ||
} | ||
|
||
void | ||
scratch_buffers_free(void) | ||
{ | ||
g_list_foreach(local_sb_registry, scratch_buffers_free_stack, NULL); | ||
g_list_free(local_sb_registry); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2011 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011 Gergely Nagy <[email protected]> | ||
* Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011-2013 Gergely Nagy <[email protected]> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
|
@@ -27,17 +27,44 @@ | |
|
||
#include <glib.h> | ||
|
||
/* Global API */ | ||
|
||
typedef struct | ||
{ | ||
GTrashStack *(*acquire_buffer)(void); | ||
void (*release_buffer)(GTrashStack *stack); | ||
void (*free_stack)(void); | ||
} ScratchBufferStack; | ||
|
||
static inline GTrashStack * | ||
scratch_buffer_acquire(ScratchBufferStack *stack) | ||
{ | ||
return stack->acquire_buffer(); | ||
} | ||
|
||
static inline void | ||
scratch_buffer_release(ScratchBufferStack *stack, GTrashStack *buffer) | ||
{ | ||
stack->release_buffer(buffer); | ||
} | ||
|
||
void scratch_buffers_register(ScratchBufferStack *stack); | ||
void scratch_buffers_init(void); | ||
void scratch_buffers_free(void); | ||
|
||
/* GStrings */ | ||
|
||
typedef struct | ||
{ | ||
GTrashStack stackp; | ||
GString s; | ||
} ScratchBuffer; | ||
} SBGString; | ||
|
||
ScratchBuffer *scratch_buffer_acquire(void); | ||
void scratch_buffer_release(ScratchBuffer *sb); | ||
extern ScratchBufferStack SBGStringStack; | ||
|
||
#define sb_string(buffer) (&buffer->s) | ||
#define sb_gstring_acquire() ((SBGString *)scratch_buffer_acquire(&SBGStringStack)) | ||
#define sb_gstring_release(b) (scratch_buffer_release(&SBGStringStack, (GTrashStack *)b)) | ||
|
||
void scratch_buffers_free(void); | ||
#define sb_gstring_string(buffer) (&buffer->s) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2011-2012 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011-2012 Gergely Nagy <[email protected]> | ||
* Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011-2013 Gergely Nagy <[email protected]> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
|
@@ -193,22 +193,22 @@ vp_pairs_foreach(gpointer data, gpointer user_data) | |
LogMessage *msg = ((gpointer *)user_data)[2]; | ||
gint32 seq_num = GPOINTER_TO_INT (((gpointer *)user_data)[3]); | ||
GTree *scope_set = ((gpointer *)user_data)[5]; | ||
ScratchBuffer *sb = scratch_buffer_acquire(); | ||
SBGString *sb = sb_gstring_acquire(); | ||
VPPairConf *vpc = (VPPairConf *)data; | ||
|
||
log_template_format((LogTemplate *)vpc->template, msg, NULL, LTZ_LOCAL, | ||
seq_num, NULL, sb_string(sb)); | ||
seq_num, NULL, sb_gstring_string(sb)); | ||
|
||
if (!sb_string(sb)->str[0]) | ||
if (!sb_gstring_string(sb)->str[0]) | ||
{ | ||
scratch_buffer_release(sb); | ||
sb_gstring_release(sb); | ||
return; | ||
} | ||
|
||
g_tree_insert(scope_set, vp_transform_apply(vp, vpc->name), | ||
sb_string(sb)->str); | ||
g_string_steal(sb_string(sb)); | ||
scratch_buffer_release(sb); | ||
sb_gstring_string(sb)->str); | ||
g_string_steal(sb_gstring_string(sb)); | ||
sb_gstring_release(sb); | ||
} | ||
|
||
/* runs over the LogMessage nv-pairs, and inserts them unless excluded */ | ||
|
@@ -246,7 +246,7 @@ static void | |
vp_merge_set(ValuePairs *vp, LogMessage *msg, gint32 seq_num, ValuePairSpec *set, GTree *dest) | ||
{ | ||
gint i; | ||
ScratchBuffer *sb = scratch_buffer_acquire(); | ||
SBGString *sb = sb_gstring_acquire(); | ||
|
||
for (i = 0; set[i].name; i++) | ||
{ | ||
|
@@ -265,28 +265,30 @@ vp_merge_set(ValuePairs *vp, LogMessage *msg, gint32 seq_num, ValuePairSpec *set | |
switch (set[i].type) | ||
{ | ||
case VPT_MACRO: | ||
log_macro_expand(sb_string(sb), set[i].id, FALSE, NULL, LTZ_LOCAL, seq_num, NULL, msg); | ||
log_macro_expand(sb_gstring_string(sb), set[i].id, FALSE, | ||
NULL, LTZ_LOCAL, seq_num, NULL, msg); | ||
break; | ||
case VPT_NVPAIR: | ||
{ | ||
const gchar *nv; | ||
gssize len; | ||
|
||
nv = log_msg_get_value(msg, (NVHandle) set[i].id, &len); | ||
g_string_append_len(sb_string(sb), nv, len); | ||
g_string_append_len(sb_gstring_string(sb), nv, len); | ||
break; | ||
} | ||
default: | ||
g_assert_not_reached(); | ||
} | ||
|
||
if (!sb_string(sb)->str[0]) | ||
if (!sb_gstring_string(sb)->str[0]) | ||
continue; | ||
|
||
g_tree_insert(dest, vp_transform_apply(vp, set[i].name), sb_string(sb)->str); | ||
g_string_steal(sb_string(sb)); | ||
g_tree_insert(dest, vp_transform_apply(vp, set[i].name), | ||
sb_gstring_string(sb)->str); | ||
g_string_steal(sb_gstring_string(sb)); | ||
} | ||
scratch_buffer_release(sb); | ||
sb_gstring_release(sb); | ||
} | ||
|
||
void | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* Copyright (c) 2011-2012 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011-2012 Gergely Nagy <[email protected]> | ||
* Copyright (c) 2011-2013 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011-2013 Gergely Nagy <[email protected]> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
|
@@ -31,7 +31,7 @@ | |
|
||
#include <string.h> | ||
|
||
typedef void (*VPTransFunc)(ValuePairsTransform *t, ScratchBuffer *name); | ||
typedef void (*VPTransFunc)(ValuePairsTransform *t, SBGString *name); | ||
typedef void (*VPTransDestroyFunc)(ValuePairsTransform *t); | ||
|
||
struct _ValuePairsTransformSet | ||
|
@@ -92,19 +92,19 @@ value_pairs_transform_free(ValuePairsTransform *t) | |
} | ||
|
||
static inline void | ||
value_pairs_transform_apply(ValuePairsTransform *t, ScratchBuffer *key) | ||
value_pairs_transform_apply(ValuePairsTransform *t, SBGString *key) | ||
{ | ||
t->transform(t, key); | ||
} | ||
|
||
/* add_prefix() */ | ||
|
||
static void | ||
vp_trans_add_prefix(ValuePairsTransform *t, ScratchBuffer *key) | ||
vp_trans_add_prefix(ValuePairsTransform *t, SBGString *key) | ||
{ | ||
VPTransAddPrefix *self = (VPTransAddPrefix *)t; | ||
|
||
g_string_prepend(sb_string(key), self->prefix); | ||
g_string_prepend(sb_gstring_string(key), self->prefix); | ||
} | ||
|
||
static void | ||
|
@@ -132,11 +132,11 @@ value_pairs_new_transform_add_prefix (const gchar *prefix) | |
/* shift() */ | ||
|
||
static void | ||
vp_trans_shift(ValuePairsTransform *t, ScratchBuffer* key) | ||
vp_trans_shift(ValuePairsTransform *t, SBGString* key) | ||
{ | ||
VPTransShift *self = (VPTransShift *)t; | ||
|
||
g_string_erase(sb_string(key), 0, self->amount); | ||
g_string_erase(sb_gstring_string(key), 0, self->amount); | ||
} | ||
|
||
ValuePairsTransform * | ||
|
@@ -156,15 +156,17 @@ value_pairs_new_transform_shift (gint amount) | |
/* replace() */ | ||
|
||
static void | ||
vp_trans_replace(ValuePairsTransform *t, ScratchBuffer *key) | ||
vp_trans_replace(ValuePairsTransform *t, SBGString *key) | ||
{ | ||
VPTransReplace *self = (VPTransReplace *)t; | ||
|
||
if (strncmp(self->old_prefix, sb_string(key)->str, self->old_prefix_len) != 0) | ||
if (strncmp(self->old_prefix, sb_gstring_string(key)->str, | ||
self->old_prefix_len) != 0) | ||
return; | ||
|
||
g_string_erase(sb_string(key), 0, self->old_prefix_len); | ||
g_string_prepend_len(sb_string(key), self->new_prefix, self->new_prefix_len); | ||
g_string_erase(sb_gstring_string(key), 0, self->old_prefix_len); | ||
g_string_prepend_len(sb_gstring_string(key), | ||
self->new_prefix, self->new_prefix_len); | ||
} | ||
|
||
static void | ||
|
@@ -238,11 +240,11 @@ value_pairs_transform_set_apply(ValuePairsTransformSet *vpts, gchar *key) | |
if (g_pattern_match_string(vpts->pattern, key)) | ||
{ | ||
GList *l; | ||
ScratchBuffer *sb; | ||
SBGString *sb; | ||
gchar *new_key; | ||
|
||
sb = scratch_buffer_acquire (); | ||
g_string_assign(sb_string(sb), key); | ||
sb = sb_gstring_acquire (); | ||
g_string_assign(sb_gstring_string(sb), key); | ||
|
||
l = vpts->transforms; | ||
while (l) | ||
|
@@ -251,9 +253,9 @@ value_pairs_transform_set_apply(ValuePairsTransformSet *vpts, gchar *key) | |
l = l->next; | ||
} | ||
|
||
new_key = sb_string(sb)->str; | ||
g_string_steal(sb_string(sb)); | ||
scratch_buffer_release (sb); | ||
new_key = sb_gstring_string(sb)->str; | ||
g_string_steal(sb_gstring_string(sb)); | ||
sb_gstring_release (sb); | ||
|
||
return new_key; | ||
} | ||
|
Oops, something went wrong.