Skip to content

Commit

Permalink
msg/body: reuse osips_{malloc,free}_f functions
Browse files Browse the repository at this point in the history
  • Loading branch information
razvancrainea committed Feb 4, 2020
1 parent ff7ba6e commit 9a4a96f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 40 deletions.
12 changes: 6 additions & 6 deletions modules/sip_i/sip_i.c
Expand Up @@ -294,22 +294,22 @@ int pv_parse_isup_param_index(pv_spec_p sp, str* in)
return 0;
}

void free_isup_parsed(void *parsed, pb_free free_f)
void free_isup_parsed(void *parsed, osips_free_f free_f)
{
struct opt_param *it, *tmp;

it = ((struct isup_parsed_struct *)parsed)->opt_params_list;
while (it) {
tmp = it;
it = it->next;
free_f(tmp);
func_free(free_f, tmp);
}

free_f(parsed);
func_free(free_f, parsed);
}

void *clone_isup_parsed(struct body_part *old_part, struct body_part *new_part,
struct sip_msg *src_msg, struct sip_msg *dst_msg, pb_malloc malloc_f)
struct sip_msg *src_msg, struct sip_msg *dst_msg, osips_malloc_f malloc_f)
{
struct isup_parsed_struct *new_ps, *old_ps;
struct opt_param *optp_it, *optp_new = NULL, *optp_prev = NULL;
Expand All @@ -325,7 +325,7 @@ void *clone_isup_parsed(struct body_part *old_part, struct body_part *new_part,
return NULL;
}

new_ps = malloc_f(sizeof(struct isup_parsed_struct));
new_ps = func_malloc(malloc_f, sizeof(struct isup_parsed_struct));
if (!new_ps) {
LM_ERR("No more pkg mem for cloned data\n");
return NULL;
Expand All @@ -336,7 +336,7 @@ void *clone_isup_parsed(struct body_part *old_part, struct body_part *new_part,

/* clone list of optional params */
for (optp_it = old_ps->opt_params_list; optp_it; optp_it = optp_it->next) {
optp_new = malloc_f(sizeof(struct opt_param));
optp_new = func_malloc(malloc_f, sizeof(struct opt_param));
if (!optp_new) {
LM_ERR("No more pkg mem\n");
return NULL;
Expand Down
37 changes: 8 additions & 29 deletions parser/parse_body.c
Expand Up @@ -406,34 +406,13 @@ int delete_body_part(struct sip_msg *msg, struct body_part *part)
}


static void *pb_pkg_malloc(unsigned long size)
{
return pkg_malloc(size);
}

static void pb_pkg_free(void *p)
{
pkg_free(p);
}

static void *pb_shm_malloc(unsigned long size)
{
return shm_malloc(size);
}

static void pb_shm_free(void *p)
{
shm_free(p);
}


void free_sip_body(struct sip_msg_body *body)
{
struct body_part * p, *tmp;
pb_free my_free;
osips_free_f my_free;

if (body) {
my_free = (body->flags&SIP_BODY_FLAG_SHM) ? pb_shm_free : pb_pkg_free;
my_free = (body->flags&SIP_BODY_FLAG_SHM) ? shm_free_func : pkg_free_func;
/* the first part does not need to be freed */
p = &body->first;
if (p->parsed && p->free_parsed_f)
Expand All @@ -446,9 +425,9 @@ void free_sip_body(struct sip_msg_body *body)
/* any need to free some parsed format of the part ? */
if (tmp->parsed && tmp->free_parsed_f)
tmp->free_parsed_f( tmp->parsed, my_free );
my_free(tmp);
func_free(my_free, tmp);
}
my_free(body);
func_free(my_free, body);
}
}

Expand All @@ -470,21 +449,21 @@ int clone_sip_msg_body(struct sip_msg *src_msg, struct sip_msg *dst_msg,
{
struct sip_msg_body *dst, *src;
struct body_part *p, *np;
pb_malloc my_malloc;
osips_malloc_f my_malloc;
int extra_len;

if (src_msg==NULL || src_msg->body==NULL) {
*p_dst = NULL;
return 0;
}

my_malloc = shared ? pb_shm_malloc : pb_pkg_malloc;
my_malloc = shared ? shm_malloc_func : pkg_malloc_func;
src = src_msg->body;

/* clone the SIP MSG BODY */
extra_len = (src->flags&SIP_BODY_FLAG_NEW) ?
src->first.mime_s.len+src->first.body.len : 0 ;
if ( (dst=my_malloc(sizeof(struct sip_msg_body)+extra_len))==NULL ) {
if ( (dst=func_malloc(my_malloc, sizeof(struct sip_msg_body)+extra_len))==NULL ) {
LM_ERR("failed to allocate new sip_msg_body clone (shared=%d)\n",
shared);
goto err;
Expand All @@ -510,7 +489,7 @@ int clone_sip_msg_body(struct sip_msg *src_msg, struct sip_msg *dst_msg,
} else {
extra_len = (p->flags&SIP_BODY_PART_FLAG_NEW) ?
p->mime_s.len+p->body.len : 0 ;
if((np->next=my_malloc(sizeof(struct body_part)+extra_len))==NULL){
if((np->next=func_malloc(my_malloc, sizeof(struct body_part)+extra_len))==NULL){
LM_ERR("failed to allocate new body_part clone (shared=%d)\n",
shared);
goto err;
Expand Down
9 changes: 4 additions & 5 deletions parser/parse_body.h
Expand Up @@ -24,14 +24,13 @@
#ifndef _PARSE_SIPBODY
#define _PARSE_SIPBODY

struct body_part;
#include "../mem/mem.h"

typedef void* (*pb_malloc)(unsigned long);
typedef void (*pb_free)(void *);
struct body_part;

typedef void (*free_parsed_part_function)(void *, pb_free);
typedef void (*free_parsed_part_function)(void *, osips_free_f);
typedef void* (*clone_parsed_part_function)(struct body_part*,
struct body_part*, struct sip_msg *, struct sip_msg* , pb_malloc);
struct body_part*, struct sip_msg *, struct sip_msg* , osips_malloc_f);
typedef int (*dump_part_function)(void *, struct sip_msg *, str *buf);


Expand Down

0 comments on commit 9a4a96f

Please sign in to comment.