Skip to content

Commit

Permalink
dialog: Mitigate 'dlg_list' PKG fragmentation
Browse files Browse the repository at this point in the history
By using pkg_str_extend() instead of pkg_realloc(), we avoid a constant
creation of PKG fragments (the buffer will eventually stop growing),
which seems to gradually fragment the private memory pool, as the MI
'dlg_list' is continuously polled by various monitoring software.

Related to #3235

(cherry picked from commit d2037f6)
  • Loading branch information
liviuchircu committed Nov 15, 2023
1 parent e4ec93f commit 7f334f4
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions modules/dialog/dlg_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ void next_state_dlg(struct dlg_cell *dlg, int event, int dir, int *old_state,


/**************************** MI functions ******************************/
static char *dlg_val_buf;
static str dlg_val_buf;
static inline int internal_mi_print_dlg(mi_item_t *dialog_obj,
struct dlg_cell *dlg, int with_context)
{
Expand Down Expand Up @@ -1477,11 +1477,12 @@ static inline int internal_mi_print_dlg(mi_item_t *dialog_obj,
/* print dlg values -> iterate the list */
for( dv=dlg->vals ; dv ; dv=dv->next) {
/* escape non-printable chars */
p = pkg_realloc(dlg_val_buf, 4 * dv->val.len + 1);
if (!p) {
LM_ERR("not enough mem to allocate: %d\n", dv->val.len);
if (!pkg_str_extend(&dlg_val_buf, 4 * dv->val.len + 1)) {
LM_ERR("not enough mem to allocate: %d\n", 4 * dv->val.len + 1);
continue;
}

p = dlg_val_buf.s;
for (i = 0, j = 0; i < dv->val.len; i++) {
if (dv->val.s[i] < 0x20 || dv->val.s[i] >= 0x7F) {
p[j++] = '\\';
Expand All @@ -1507,8 +1508,6 @@ static inline int internal_mi_print_dlg(mi_item_t *dialog_obj,
goto error;
if (add_mi_string(values_item,dv->name.s,dv->name.len,p,j) < 0)
goto error;

dlg_val_buf = p;
}
}

Expand Down

0 comments on commit 7f334f4

Please sign in to comment.