Skip to content

Commit

Permalink
Merge pull request #717 from ionel-cerghit/shmem_rework
Browse files Browse the repository at this point in the history
Shmem rework
  • Loading branch information
bogdan-iancu committed Dec 22, 2015
2 parents 49efaa9 + d182f9d commit 420b663
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 268 deletions.
111 changes: 17 additions & 94 deletions blacklists.c
Expand Up @@ -45,7 +45,6 @@ static unsigned int bl_default_marker = 0;

static unsigned int max_heads = 8*sizeof(bl_marker);
static unsigned int used_heads = 0;
static unsigned int no_shm = 1;


static void delete_expired_routine(unsigned int ticks, void* param);
Expand All @@ -58,81 +57,8 @@ static mi_export_t mi_bl_cmds[] = {
{ 0, 0, 0, 0, 0, 0}
};



int preinit_black_lists(void)
{
blst_heads = (struct bl_head*)pkg_malloc(max_heads*sizeof(struct bl_head));
if (blst_heads==NULL) {
LM_ERR("no more pkg memory!\n");
return -1;
}
memset( blst_heads, 0, max_heads*sizeof(struct bl_head));

used_heads = 0;

/* black lists were successfully allocated */
return 0;
}



int init_black_lists(void)
{
struct bl_head *old_blst_heads;
struct bl_rule *head;
struct bl_rule *tail;
struct bl_rule *it, *it1;
unsigned int old_used_heads;
unsigned int i;

if (!no_shm) {
LM_CRIT("called twice\n");
return -1;
}
no_shm = 0;

old_blst_heads = blst_heads;
blst_heads = (struct bl_head*)shm_malloc(max_heads*sizeof(struct bl_head));
if (blst_heads==NULL) {
LM_ERR("no more shm memory!\n");
return -1;
}
memset( blst_heads, 0, max_heads * sizeof(struct bl_head));
old_used_heads = used_heads;

used_heads = 0;
bl_default_marker = 0;

/*for lists already created, init locks and move them into shm */
for( i=0 ; i<old_used_heads ; i++ ) {

/* duplicate in shm */
it = old_blst_heads[i].first;
head = tail = 0;

for( it1=it ; it ; it=it1 ) {
if (add_rule_to_list( &head, &tail, &it->ip_net,
&it->body, it->port, it->proto, it->flags)!=0) {
LM_ERR("failed to clone rule!\n");
return -1;
}

it1 = it->next;
pkg_free(it);
}

if (create_bl_head( old_blst_heads[i].owner, old_blst_heads[i].flags,
head, tail, &old_blst_heads[i].name )==NULL ) {
LM_ERR("failed to clone head!\n");
return -1;
}

pkg_free(old_blst_heads[i].name.s);
}

pkg_free(old_blst_heads);

/* register timer routine */
if ( register_timer( "blcore-expire", delete_expired_routine, 0, 1,
TIMER_FLAG_SKIP_ON_DELAY)<0 ) {
Expand All @@ -156,6 +82,14 @@ struct bl_head *create_bl_head(int owner, int flags, struct bl_rule *head,
{
unsigned int i;

if(!blst_heads) {
blst_heads = (struct bl_head*)shm_malloc(max_heads*sizeof(struct bl_head));
if (blst_heads==NULL) {
LM_ERR("no more shared memory!\n");
return NULL;
}
memset( blst_heads, 0, max_heads*sizeof(struct bl_head));
}
i = used_heads;
if (i==max_heads) {
LM_ERR("too many lists\n");
Expand All @@ -173,20 +107,17 @@ struct bl_head *create_bl_head(int owner, int flags, struct bl_rule *head,
}

/* copy list name */
if (no_shm)
blst_heads[i].name.s = (char*)pkg_malloc(name->len + 1);
else
blst_heads[i].name.s = (char*)shm_malloc(name->len + 1);
blst_heads[i].name.s = (char*)shm_malloc(name->len + 1);
if (blst_heads[i].name.s==NULL) {
LM_ERR("no more pkg memory!\n");
LM_ERR("no more shm memory!\n");
return NULL;
}
memcpy( blst_heads[i].name.s, name->s, name->len);
blst_heads[i].name.s[name->len] = '\0';
blst_heads[i].name.len = name->len;

/* build lock? */
if (!no_shm && !(flags&BL_READONLY_LIST)) {
if (!(flags&BL_READONLY_LIST)) {
if ( (blst_heads[i].lock=lock_alloc())==NULL ) {
LM_ERR("failed to create lock!\n");
shm_free(blst_heads[i].name.s);
Expand Down Expand Up @@ -220,9 +151,6 @@ void destroy_black_lists(void)
unsigned int i;
struct bl_rule *p, *q;

if (no_shm)
return;

for(i = 0 ; i < used_heads ; i++){

if (blst_heads[i].lock) {
Expand All @@ -242,7 +170,8 @@ void destroy_black_lists(void)
blst_heads[i].first = blst_heads[i].last = NULL;
}

shm_free(blst_heads);
if(blst_heads)
shm_free(blst_heads);
}


Expand Down Expand Up @@ -366,14 +295,10 @@ int add_rule_to_list(struct bl_rule **first, struct bl_rule **last,


/* alloc memory */
if (no_shm)
p = (struct bl_rule*)pkg_malloc
(sizeof(struct bl_rule) + (body?(body->len + 1):0));
else
p = (struct bl_rule*)shm_malloc
p = (struct bl_rule*)shm_malloc
(sizeof(struct bl_rule) + (body?(body->len + 1):0));
if(!p){
LM_ERR("no more %s memory!\n", no_shm?"pkg":"shm");
LM_ERR("no more shm memory!\n");
return -1;
}

Expand Down Expand Up @@ -429,13 +354,11 @@ static inline void rm_dups(struct bl_head *head,
if (q->next==NULL) *last=p;
if (p) {
p->next = q->next;
if (no_shm) pkg_free(q);
else shm_free(q);
shm_free(q);
q = p->next;
} else {
*first = q->next;
if (no_shm) pkg_free(q);
else shm_free(q);
shm_free(q);
q = *first;
}
} else {
Expand Down
3 changes: 0 additions & 3 deletions blacklists.h
Expand Up @@ -62,9 +62,6 @@ struct bl_head{

#define BL_CORE_ID 13


int preinit_black_lists();

int init_black_lists();

void destroy_black_lists();
Expand Down
5 changes: 0 additions & 5 deletions main.c
Expand Up @@ -993,11 +993,6 @@ int main(int argc, char** argv)
/*register builtin modules*/
register_builtin_modules();

if (preinit_black_lists()!=0) {
LM_CRIT("failed to alloc black list's anchor\n");
goto error00;
}

/* init avps */
if (init_global_avps() != 0) {
LM_ERR("error while initializing avps\n");
Expand Down
6 changes: 0 additions & 6 deletions modules/cfgutils/cfgutils.c
Expand Up @@ -707,12 +707,6 @@ static int mod_init(void)
return -1;
}

if(init_shvars()<0)
{
LM_ERR("init shvars failed\n");
shm_free(probability);
return -1;
}
LM_INFO("module initialized, pid [%d]\n", getpid());

return 0;
Expand Down

0 comments on commit 420b663

Please sign in to comment.