Browse code

core: use generic PKG_MEM_ERROR, SHM_ERROR and _CRITICAL helper defines in core

- refactoring of the core to use generic PKG_MEM_ERROR, SHM_ERROR,
PKG_MEM_CRITICAL, SHM_MEM_CRITICAL and SYS_MEM_ERROR helper defines
- unify many different error messages in different spellings
- add a few missing error handler for allocation errors after (found
with a complete review of all memory allocation functions in core)
- no other functional changes, change has been reviewed two times

Henning Westerholt authored on 23/12/2018 21:31:03
Showing 86 changed files
... ...
@@ -752,7 +752,7 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
752 752
 					len=strlen(a->val[0].u.string);
753 753
 					msg->new_uri.s=pkg_malloc(len+1);
754 754
 					if (msg->new_uri.s==0){
755
-						LM_ERR("memory allocation failure\n");
755
+						PKG_MEM_ERROR;
756 756
 						ret=E_OUT_OF_MEM;
757 757
 						goto error;
758 758
 					}
... ...
@@ -802,7 +802,7 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
802 802
 
803 803
 				new_uri=pkg_malloc(MAX_URI_SIZE);
804 804
 				if (new_uri==0){
805
-					LM_ERR("memory allocation failure\n");
805
+					PKG_MEM_ERROR;
806 806
 					ret=E_OUT_OF_MEM;
807 807
 					goto error;
808 808
 				}
... ...
@@ -1433,8 +1433,7 @@ static char* addstr(struct str_buf* dst_b, char* src, int len)
1433 1433
 
1434 1434
 	return dst_b->s;
1435 1435
 error:
1436
-	LM_CRIT("lex: memory allocation error\n");
1437
-	LM_CRIT("lex: try to increase pkg size with -M parameter\n");
1436
+	PKG_MEM_CRITICAL;
1438 1437
 	exit(-1);
1439 1438
 }
1440 1439
 
... ...
@@ -1602,7 +1601,7 @@ static int sr_push_yy_state(char *fin, int mode)
1602 1601
 		newf = (char*)pkg_malloc(x-tmpfiname+strlen(fbuf)+2);
1603 1602
 		if(newf==0)
1604 1603
 		{
1605
-			LM_CRIT("no more pkg\n");
1604
+			PKG_MEM_CRITICAL;
1606 1605
 			return -1;
1607 1606
 		}
1608 1607
 		newf[0] = '\0';
... ...
@@ -1664,7 +1663,7 @@ static int sr_push_yy_state(char *fin, int mode)
1664 1663
 		{
1665 1664
 			if(newf!=fbuf)
1666 1665
 				pkg_free(newf);
1667
-			LM_CRIT("no more pkg\n");
1666
+			PKG_MEM_CRITICAL;
1668 1667
 			return -1;
1669 1668
 		}
1670 1669
 		if(newf==fbuf)
... ...
@@ -1673,7 +1672,7 @@ static int sr_push_yy_state(char *fin, int mode)
1673 1672
 			if(fn->fname==0)
1674 1673
 			{
1675 1674
 				pkg_free(fn);
1676
-				LM_CRIT("no more pkg!\n");
1675
+				PKG_MEM_CRITICAL;
1677 1676
 				return -1;
1678 1677
 			}
1679 1678
 			strcpy(fn->fname, fbuf);
... ...
@@ -1793,7 +1792,7 @@ int pp_define(int len, const char * text)
1793 1792
 	pp_defines[pp_num_defines].name.len = len;
1794 1793
 	pp_defines[pp_num_defines].name.s = (char*)pkg_malloc(len+1);
1795 1794
 	if(pp_defines[pp_num_defines].name.s==NULL) {
1796
-		LM_CRIT("no more memory to define: %.*s\n", len, text);
1795
+		PKG_MEM_CRITICAL;
1797 1796
 		return -1;
1798 1797
 	}
1799 1798
 	memcpy(pp_defines[pp_num_defines].name.s, text, len);
... ...
@@ -649,7 +649,7 @@ listen_id:
649 649
 			} else {
650 650
 				$$=pkg_malloc(strlen(tmp)+1);
651 651
 				if ($$==0) {
652
-					LM_CRIT("cfg. parser: out of memory.\n");
652
+					PKG_MEM_CRITICAL;
653 653
 				} else {
654 654
 					strncpy($$, tmp, strlen(tmp)+1);
655 655
 				}
... ...
@@ -659,7 +659,7 @@ listen_id:
659 659
 	| STRING {
660 660
 		$$=pkg_malloc(strlen($1)+1);
661 661
 		if ($$==0) {
662
-				LM_CRIT("cfg. parser: out of memory.\n");
662
+				PKG_MEM_CRITICAL;
663 663
 		} else {
664 664
 				strncpy($$, $1, strlen($1)+1);
665 665
 		}
... ...
@@ -668,7 +668,7 @@ listen_id:
668 668
 		if ($1){
669 669
 			$$=pkg_malloc(strlen($1)+1);
670 670
 			if ($$==0) {
671
-					LM_CRIT("cfg. parser: out of memory.\n");
671
+					PKG_MEM_CRITICAL;
672 672
 			} else {
673 673
 					strncpy($$, $1, strlen($1)+1);
674 674
 			}
... ...
@@ -1470,7 +1470,7 @@ assign_stm:
1470 1470
 	| ADVERTISED_PORT EQUAL NUMBER {
1471 1471
 		tmp=int2str($3, &i_tmp);
1472 1472
 		if ((default_global_port.s=pkg_malloc(i_tmp))==0) {
1473
-			LM_CRIT("cfg. parser: out of memory.\n");
1473
+			PKG_MEM_CRITICAL;
1474 1474
 			default_global_port.len=0;
1475 1475
 		} else {
1476 1476
 			default_global_port.len=i_tmp;
... ...
@@ -1789,7 +1789,7 @@ ipv4:
1789 1789
 	NUMBER DOT NUMBER DOT NUMBER DOT NUMBER {
1790 1790
 		$$=pkg_malloc(sizeof(struct ip_addr));
1791 1791
 		if ($$==0) {
1792
-			LM_CRIT("cfg. parser: out of memory.\n");
1792
+			PKG_MEM_CRITICAL;
1793 1793
 		} else {
1794 1794
 			memset($$, 0, sizeof(struct ip_addr));
1795 1795
 			$$->af=AF_INET;
... ...
@@ -1818,7 +1818,7 @@ ipv6addr:
1818 1818
 	IPV6ADDR {
1819 1819
 		$$=pkg_malloc(sizeof(struct ip_addr));
1820 1820
 		if ($$==0) {
1821
-			LM_CRIT("cfg. parser: out of memory.\n");
1821
+			PKG_MEM_CRITICAL;
1822 1822
 		} else {
1823 1823
 			memset($$, 0, sizeof(struct ip_addr));
1824 1824
 			$$->af=AF_INET6;
... ...
@@ -2271,8 +2271,7 @@ host:
2271 2271
 		if ($1){
2272 2272
 			$$=(char*)pkg_malloc(strlen($1)+1+strlen($3)+1);
2273 2273
 			if ($$==0) {
2274
-				LM_CRIT("cfg. parser: memory allocation"
2275
-							" failure while parsing host\n");
2274
+				PKG_MEM_CRITICAL;
2276 2275
 			} else {
2277 2276
 				memcpy($$, $1, strlen($1));
2278 2277
 				$$[strlen($1)]='.';
... ...
@@ -2287,8 +2286,7 @@ host:
2287 2286
 		if ($1){
2288 2287
 			$$=(char*)pkg_malloc(strlen($1)+1+strlen($3)+1);
2289 2288
 			if ($$==0) {
2290
-				LM_CRIT("cfg. parser: memory allocation"
2291
-							" failure while parsing host\n");
2289
+				PKG_MEM_CRITICAL;
2292 2290
 			} else {
2293 2291
 				memcpy($$, $1, strlen($1));
2294 2292
 				$$[strlen($1)]='-';
... ...
@@ -2308,8 +2306,11 @@ host_if_id: ID
2308 2306
 		| NUMBER {
2309 2307
 			/* get string version */
2310 2308
 			$$=pkg_malloc(strlen(yy_number_str)+1);
2311
-			if ($$)
2309
+			if ($$==0) {
2310
+				PKG_MEM_ERROR;
2311
+			} else {
2312 2312
 				strcpy($$, yy_number_str);
2313
+			}
2313 2314
 		}
2314 2315
 		;
2315 2316
 
... ...
@@ -2319,8 +2320,7 @@ host_or_if:
2319 2320
 		if ($1){
2320 2321
 			$$=(char*)pkg_malloc(strlen($1)+1+strlen($3)+1);
2321 2322
 			if ($$==0) {
2322
-				LM_CRIT("cfg. parser: memory allocation"
2323
-							" failure while parsing host/interface name\n");
2323
+				PKG_MEM_CRITICAL;
2324 2324
 			} else {
2325 2325
 				memcpy($$, $1, strlen($1));
2326 2326
 				$$[strlen($1)]='.';
... ...
@@ -2335,8 +2335,7 @@ host_or_if:
2335 2335
 		if ($1){
2336 2336
 			$$=(char*)pkg_malloc(strlen($1)+1+strlen($3)+1);
2337 2337
 			if ($$==0) {
2338
-				LM_CRIT("cfg. parser: memory allocation"
2339
-							" failure while parsing host/interface name\n");
2338
+				PKG_MEM_CRITICAL;
2340 2339
 			} else {
2341 2340
 				memcpy($$, $1, strlen($1));
2342 2341
 				$$[strlen($1)]='-';
... ...
@@ -3294,7 +3293,7 @@ cmd:
3294 3293
 	| SET_ADV_ADDRESS LPAREN listen_id RPAREN {
3295 3294
 		$$=0;
3296 3295
 		if ((str_tmp=pkg_malloc(sizeof(str)))==0) {
3297
-			LM_CRIT("cfg. parser: out of memory.\n");
3296
+			PKG_MEM_CRITICAL;
3298 3297
 		} else {
3299 3298
 			str_tmp->s=$3;
3300 3299
 			str_tmp->len=$3?strlen($3):0;
... ...
@@ -3308,10 +3307,10 @@ cmd:
3308 3307
 		$$=0;
3309 3308
 		tmp=int2str($3, &i_tmp);
3310 3309
 		if ((str_tmp=pkg_malloc(sizeof(str)))==0) {
3311
-			LM_CRIT("cfg. parser: out of memory.\n");
3310
+			PKG_MEM_CRITICAL;
3312 3311
 		} else {
3313 3312
 			if ((str_tmp->s=pkg_malloc(i_tmp))==0) {
3314
-				LM_CRIT("cfg. parser: out of memory.\n");
3313
+				PKG_MEM_CRITICAL;
3315 3314
 			} else {
3316 3315
 				memcpy(str_tmp->s, tmp, i_tmp);
3317 3316
 				str_tmp->len=i_tmp;
... ...
@@ -3680,7 +3679,7 @@ static struct name_lst* mk_name_lst(char* host, int flags)
3680 3679
 	if (host==0) return 0;
3681 3680
 	l=pkg_malloc(sizeof(struct name_lst));
3682 3681
 	if (l==0) {
3683
-		LM_CRIT("cfg. parser: out of memory.\n");
3682
+		PKG_MEM_CRITICAL;
3684 3683
 	} else {
3685 3684
 		l->name=host;
3686 3685
 		l->flags=flags;
... ...
@@ -3696,7 +3695,7 @@ static struct socket_id* mk_listen_id(char* host, int proto, int port)
3696 3695
 	if (host==0) return 0;
3697 3696
 	l=pkg_malloc(sizeof(struct socket_id));
3698 3697
 	if (l==0) {
3699
-		LM_CRIT("cfg. parser: out of memory.\n");
3698
+		PKG_MEM_CRITICAL;
3700 3699
 	} else {
3701 3700
 		l->addr_lst=mk_name_lst(host, 0);
3702 3701
 		if (l->addr_lst==0){
... ...
@@ -3731,7 +3730,7 @@ static struct socket_id* mk_listen_id2(struct name_lst* addr_l, int proto,
3731 3730
 	if (addr_l==0) return 0;
3732 3731
 	l=pkg_malloc(sizeof(struct socket_id));
3733 3732
 	if (l==0) {
3734
-		LM_CRIT("cfg. parser: out of memory.\n");
3733
+		PKG_MEM_CRITICAL;
3735 3734
 	} else {
3736 3735
 		l->flags=addr_l->flags;
3737 3736
 		l->port=port;
... ...
@@ -49,7 +49,7 @@ int cfg_declare(char *group_name, cfg_def_t *def, void *values, int def_size,
49 49
 
50 50
 	mapping = (cfg_mapping_t *)pkg_malloc(sizeof(cfg_mapping_t)*num);
51 51
 	if (!mapping) {
52
-		LM_ERR("not enough memory\n");
52
+		PKG_MEM_ERROR;
53 53
 		goto error;
54 54
 	}
55 55
 	memset(mapping, 0, sizeof(cfg_mapping_t)*num);
... ...
@@ -212,7 +212,7 @@ int cfg_declare_str(char *group_name, char *var_name, char *val, char *descr)
212 212
 		len = strlen(val);
213 213
 		var->val.s.s = (char *)pkg_malloc(sizeof(char) * (len + 1));
214 214
 		if (!var->val.s.s) {
215
-			LM_ERR("not enough memory\n");
215
+			PKG_MEM_ERROR;
216 216
 			return -1;
217 217
 		}
218 218
 		memcpy(var->val.s.s, val, len + 1);
... ...
@@ -46,7 +46,7 @@ int cfg_register_ctx(cfg_ctx_t **handle, cfg_on_declare on_declare_cb)
46 46
 	 * must be in shm mem anyway */
47 47
 	ctx = (cfg_ctx_t *)shm_malloc(sizeof(cfg_ctx_t));
48 48
 	if (!ctx) {
49
-		LM_ERR("not enough shm memory\n");
49
+		SHM_MEM_ERROR;
50 50
 		return -1;
51 51
 	}
52 52
 	memset(ctx, 0, sizeof(cfg_ctx_t));
... ...
@@ -183,7 +183,7 @@ int convert_val(unsigned int val_type, void *val,
183 183
 				if (temp_string) pkg_free(temp_string);
184 184
 				temp_string = (char *)pkg_malloc(sizeof(char) * (((str *)val)->len + 1));
185 185
 				if (!temp_string) {
186
-					LM_ERR("not enough memory\n");
186
+					PKG_MEM_ERROR;
187 187
 					return -1;
188 188
 				}
189 189
 				memcpy(temp_string, ((str *)val)->s, ((str *)val)->len);
... ...
@@ -427,7 +427,7 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *va
427 427
 				var->def->on_set_child_cb,
428 428
 				var->def->type);
429 429
 		if (!child_cb) {
430
-			LM_ERR("not enough shm memory\n");
430
+			SHM_MEM_ERROR;
431 431
 			goto error0;
432 432
 		}
433 433
 	}
... ...
@@ -558,7 +558,7 @@ int cfg_set_now(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id, str *va
558 558
 			replaced = (void **)shm_malloc(sizeof(void *)
559 559
 					* ((old_string?1:0) + (new_array?1:0) + 1));
560 560
 			if (!replaced) {
561
-				LM_ERR("not enough shm memory\n");
561
+				SHM_MEM_ERROR;
562 562
 				goto error;
563 563
 			}
564 564
 			i = 0;
... ...
@@ -782,7 +782,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id,
782 782
 		if (ctx->changed_first) {
783 783
 			temp_handle = (unsigned char *)pkg_malloc(group->size);
784 784
 			if (!temp_handle) {
785
-				LM_ERR("not enough memory\n");
785
+				PKG_MEM_ERROR;
786 786
 				goto error;
787 787
 			}
788 788
 			temp_handle_created = 1;
... ...
@@ -831,7 +831,7 @@ int cfg_set_delayed(cfg_ctx_t *ctx, str *group_name, unsigned int *group_id,
831 831
 		+ ((val_type != CFG_VAR_UNSET) ? cfg_var_size(var) : 0);
832 832
 	changed = (cfg_changed_var_t *)shm_malloc(size);
833 833
 	if (!changed) {
834
-		LM_ERR("not enough shm memory\n");
834
+		SHM_MEM_ERROR;
835 835
 		goto error;
836 836
 	}
837 837
 	memset(changed, 0, size);
... ...
@@ -1063,7 +1063,7 @@ int cfg_commit(cfg_ctx_t *ctx)
1063 1063
 		size = sizeof(void *)*(replaced_num + 1);
1064 1064
 		replaced = (void **)shm_malloc(size);
1065 1065
 		if (!replaced) {
1066
-			LM_ERR("not enough shm memory\n");
1066
+			SHM_MEM_ERROR;
1067 1067
 			goto error0;
1068 1068
 		}
1069 1069
 		memset(replaced, 0 , size);
... ...
@@ -1635,7 +1635,7 @@ int cfg_add_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id)
1635 1635
 		 * they will be freed when the old block is freed */
1636 1636
 		replaced = (void **)shm_malloc(sizeof(void *) * 2);
1637 1637
 		if (!replaced) {
1638
-			LM_ERR("not enough shm memory\n");
1638
+			SHM_MEM_ERROR;
1639 1639
 			goto error;
1640 1640
 		}
1641 1641
 		replaced[0] = CFG_GROUP_META(*cfg_global, group)->array;
... ...
@@ -1740,7 +1740,7 @@ int cfg_del_group_inst(cfg_ctx_t *ctx, str *group_name, unsigned int group_id)
1740 1740
 
1741 1741
 		replaced = (void **)shm_malloc(sizeof(void *) * (num + 2));
1742 1742
 		if (!replaced) {
1743
-			LM_ERR("not enough shm memory\n");
1743
+			SHM_MEM_ERROR;
1744 1744
 			goto error;
1745 1745
 		}
1746 1746
 
... ...
@@ -139,7 +139,7 @@ cfg_script_var_t *new_cfg_script_var(char *gname, char *vname, unsigned int type
139 139
 	return var;
140 140
 
141 141
 error:
142
-	LM_ERR("not enough memory\n");
142
+	PKG_MEM_ERROR;
143 143
 	return NULL;
144 144
 }
145 145
 
... ...
@@ -188,7 +188,7 @@ int cfg_set_script_var(cfg_group_t *group, str *var_name,
188 188
 					s.len = ((str *)v)->len;
189 189
 					s.s = pkg_malloc(sizeof(char) * (s.len + 1));
190 190
 					if (!s.s) {
191
-						LM_ERR("not enough memory\n");
191
+						PKG_MEM_ERROR;
192 192
 						goto error;
193 193
 					}
194 194
 					memcpy(s.s, ((str *)v)->s, s.len);
... ...
@@ -325,7 +325,7 @@ error:
325 325
 	if (def) pkg_free(def);
326 326
 	if (handle) pkg_free(handle);
327 327
 
328
-	LM_ERR("not enough memory\n");
328
+	PKG_MEM_ERROR;
329 329
 	return -1;
330 330
 }
331 331
 
... ...
@@ -73,7 +73,7 @@ static int cfg_new_select(str *gname, str *vname, void **group_p, void **var_p)
73 73
 	return 0;
74 74
 
75 75
 error:
76
-	LM_ERR("not enough memory\n");
76
+	PKG_MEM_ERROR;
77 77
 	if (sel) {
78 78
 		if (sel->gname.s) pkg_free(sel->gname.s);
79 79
 		if (sel->vname.s) pkg_free(sel->vname.s);
... ...
@@ -78,7 +78,7 @@ cfg_group_t *cfg_new_group(char *name, int name_len,
78 78
 
79 79
 	group = (cfg_group_t *)pkg_malloc(sizeof(cfg_group_t)+name_len-1);
80 80
 	if (!group) {
81
-		LM_ERR("not enough memory\n");
81
+		PKG_MEM_ERROR;
82 82
 		return NULL;
83 83
 	}
84 84
 	memset(group, 0, sizeof(cfg_group_t)+name_len-1);
... ...
@@ -129,7 +129,7 @@ int cfg_clone_str(str *src, str *dst)
129 129
 
130 130
 	c = (char *)shm_malloc(sizeof(char)*(src->len+1));
131 131
 	if (!c) {
132
-		LM_ERR("not enough shm memory\n");
132
+		SHM_MEM_ERROR;
133 133
 		return -1;
134 134
 	}
135 135
 	memcpy(c, src->s, src->len);
... ...
@@ -216,7 +216,7 @@ int cfg_shmize(void)
216 216
 
217 217
 	block = (cfg_block_t*)shm_malloc(sizeof(cfg_block_t)+size-1);
218 218
 	if (!block) {
219
-		LM_ERR("not enough shm memory\n");
219
+		SHM_MEM_ERROR;
220 220
 		goto error;
221 221
 	}
222 222
 	memset(block, 0, sizeof(cfg_block_t)+size-1);
... ...
@@ -319,9 +319,10 @@ static void cfg_destory_groups(unsigned char *block)
319 319
 /* initiate the cfg framework */
320 320
 int sr_cfg_init(void)
321 321
 {
322
+	/* lock_alloc() is a define for shm_malloc */
322 323
 	cfg_global_lock = lock_alloc();
323 324
 	if (!cfg_global_lock) {
324
-		LM_ERR("not enough shm memory\n");
325
+		SHM_MEM_ERROR;
325 326
 		goto error;
326 327
 	}
327 328
 	if (lock_init(cfg_global_lock) == 0) {
... ...
@@ -333,7 +334,7 @@ int sr_cfg_init(void)
333 334
 
334 335
 	cfg_writer_lock = lock_alloc();
335 336
 	if (!cfg_writer_lock) {
336
-		LM_ERR("not enough shm memory\n");
337
+		SHM_MEM_ERROR;
337 338
 		goto error;
338 339
 	}
339 340
 	if (lock_init(cfg_writer_lock) == 0) {
... ...
@@ -345,21 +346,21 @@ int sr_cfg_init(void)
345 346
 
346 347
 	cfg_global = (cfg_block_t **)shm_malloc(sizeof(cfg_block_t *));
347 348
 	if (!cfg_global) {
348
-		LM_ERR("not enough shm memory\n");
349
+		SHM_MEM_ERROR;
349 350
 		goto error;
350 351
 	}
351 352
 	*cfg_global = NULL;
352 353
 
353 354
 	cfg_child_cb_first = (cfg_child_cb_t **)shm_malloc(sizeof(cfg_child_cb_t *));
354 355
 	if (!cfg_child_cb_first) {
355
-		LM_ERR("not enough shm memory\n");
356
+		SHM_MEM_ERROR;
356 357
 		goto error;
357 358
 	}
358 359
 	*cfg_child_cb_first = NULL;
359 360
 
360 361
 	cfg_child_cb_last = (cfg_child_cb_t **)shm_malloc(sizeof(cfg_child_cb_t *));
361 362
 	if (!cfg_child_cb_last) {
362
-		LM_ERR("not enough shm memory\n");
363
+		SHM_MEM_ERROR;
363 364
 		goto error;
364 365
 	}
365 366
 	*cfg_child_cb_last = NULL;
... ...
@@ -624,7 +625,7 @@ cfg_block_t *cfg_clone_global(void)
624 625
 
625 626
 	block = (cfg_block_t*)shm_malloc(sizeof(cfg_block_t)+cfg_block_size-1);
626 627
 	if (!block) {
627
-		LM_ERR("not enough shm memory\n");
628
+		SHM_MEM_ERROR;
628 629
 		return NULL;
629 630
 	}
630 631
 	memcpy(block, *cfg_global, sizeof(cfg_block_t)+cfg_block_size-1);
... ...
@@ -647,7 +648,7 @@ cfg_group_inst_t *cfg_clone_array(cfg_group_meta_t *meta, cfg_group_t *group)
647 648
 	size = (sizeof(cfg_group_inst_t) + group->size - 1) * meta->num;
648 649
 	new_array = (cfg_group_inst_t *)shm_malloc(size);
649 650
 	if (!new_array) {
650
-		LM_ERR("not enough shm memory\n");
651
+		SHM_MEM_ERROR;
651 652
 		return NULL;
652 653
 	}
653 654
 	memcpy(new_array, meta->array, size);
... ...
@@ -668,7 +669,7 @@ cfg_group_inst_t *cfg_extend_array(cfg_group_meta_t *meta, cfg_group_t *group,
668 669
 	inst_size = sizeof(cfg_group_inst_t) + group->size - 1;
669 670
 	new_array = (cfg_group_inst_t *)shm_malloc(inst_size * (meta->num + 1));
670 671
 	if (!new_array) {
671
-		LM_ERR("not enough shm memory\n");
672
+		SHM_MEM_ERROR;
672 673
 		return NULL;
673 674
 	}
674 675
 	/* Find the position of the new group in the array. The array is ordered
... ...
@@ -717,7 +718,7 @@ int cfg_collapse_array(cfg_group_meta_t *meta, cfg_group_t *group,
717 718
 	inst_size = sizeof(cfg_group_inst_t) + group->size - 1;
718 719
 	new_array = (cfg_group_inst_t *)shm_malloc(inst_size * (meta->num - 1));
719 720
 	if (!new_array) {
720
-		LM_ERR("not enough shm memory\n");
721
+		SHM_MEM_ERROR;
721 722
 		return -1;
722 723
 	}
723 724
 
... ...
@@ -796,7 +797,7 @@ void cfg_install_global(cfg_block_t *block, void **replaced,
796 797
 				cb_last = cb_first;
797 798
 				cb_first->replaced = replaced;
798 799
 			} else {
799
-				LM_ERR("not enough shm memory\n");
800
+				SHM_MEM_ERROR;
800 801
 				/* Nothing more can be done here, the replaced strings are still needed,
801 802
 				 * they cannot be freed at this moment.
802 803
 				 */
... ...
@@ -827,7 +828,7 @@ cfg_child_cb_t *cfg_child_cb_new(str *gname, str *name,
827 828
 
828 829
 	cb_struct = (cfg_child_cb_t *)shm_malloc(sizeof(cfg_child_cb_t));
829 830
 	if (!cb_struct) {
830
-		LM_ERR("not enough shm memory\n");
831
+		SHM_MEM_ERROR;
831 832
 		return NULL;
832 833
 	}
833 834
 	memset(cb_struct, 0, sizeof(cfg_child_cb_t));
... ...
@@ -923,7 +924,7 @@ int new_add_var(str *group_name, unsigned int group_id, str *var_name,
923 924
 	add_var = (cfg_add_var_t *)pkg_malloc(sizeof(cfg_add_var_t) +
924 925
 			(type ? (var_name->len - 1) : 0));
925 926
 	if (!add_var) {
926
-		LM_ERR("Not enough memory\n");
927
+		PKG_MEM_ERROR;
927 928
 		goto error;
928 929
 	}
929 930
 	memset(add_var, 0, sizeof(cfg_add_var_t) +
... ...
@@ -944,7 +945,7 @@ int new_add_var(str *group_name, unsigned int group_id, str *var_name,
944 945
 				if (len) {
945 946
 					add_var->val.s.s = (char *)pkg_malloc(sizeof(char) * len);
946 947
 					if (!add_var->val.s.s) {
947
-						LM_ERR("Not enough memory\n");
948
+						PKG_MEM_ERROR;
948 949
 						goto error;
949 950
 					}
950 951
 					memcpy(add_var->val.s.s, ((str *)val)->s, len);
... ...
@@ -959,7 +960,7 @@ int new_add_var(str *group_name, unsigned int group_id, str *var_name,
959 960
 					len = strlen((char *)val);
960 961
 					add_var->val.ch = (char *)pkg_malloc(sizeof(char) * (len + 1));
961 962
 					if (!add_var->val.ch) {
962
-						LM_ERR("Not enough memory\n");
963
+						PKG_MEM_ERROR;
963 964
 						goto error;
964 965
 					}
965 966
 					memcpy(add_var->val.ch, (char *)val, len);
... ...
@@ -1049,7 +1050,7 @@ static int apply_add_var_list(cfg_block_t *block, cfg_group_t *group)
1049 1050
 	size = (sizeof(cfg_group_inst_t) + group->size - 1) * num;
1050 1051
 	new_array = (cfg_group_inst_t *)shm_malloc(size);
1051 1052
 	if (!new_array) {
1052
-		LM_ERR("not enough shm memory\n");
1053
+		SHM_MEM_ERROR;
1053 1054
 		return -1;
1054 1055
 	}
1055 1056
 	memset(new_array, 0, size);
... ...
@@ -287,6 +287,7 @@ static void print_token(cfg_token_t* token)
287 287
 			token->type, STR_FMT(&token->val),
288 288
 			token->start.line, token->start.col, 
289 289
 			token->end.line, token->end.col);
290
+			PKG_MEM_ERROR;
290 291
 	} else {
291 292
 		for(i = 0, j = 0; i < token->val.len; i++) {
292 293
 			switch(token->val.s[i]) {
... ...
@@ -599,7 +600,7 @@ static char* get_base_name(str* filename)
599 600
 	len = strlen(tmp2);
600 601
 
601 602
 	if ((res = pkg_malloc(len + 1)) == NULL) {
602
-		ERR("cfg_parser: No memory left");
603
+	        PKG_MEM_ERROR;
603 604
 		goto error;
604 605
 	}
605 606
 	memcpy(res, tmp2, len + 1);
... ...
@@ -644,7 +645,7 @@ cfg_parser_t* cfg_parser_init(str* basedir, str* filename)
644 645
 	if ((base = get_base_name(filename)) == NULL) goto error;
645 646
 
646 647
 	if ((st = (cfg_parser_t*)pkg_malloc(sizeof(*st))) == NULL) {
647
-		ERR("cfg_parser: No memory left\n");
648
+	        PKG_MEM_ERROR;
648 649
 		goto error;
649 650
 	}
650 651
 	memset(st, '\0', sizeof(*st));
... ...
@@ -904,22 +905,19 @@ int cfg_parse_str(void* param, cfg_parser_t* st, unsigned int flags)
904 905
 		buf = val->s;
905 906
 	} else if (flags & CFG_STR_SHMMEM) {
906 907
 		if ((buf = shm_malloc(t.val.len + 1)) == NULL) {
907
-			ERR("%s:%d:%d: Out of shared memory\n", st->file,
908
-				t.start.line, t.start.col);
908
+		        SHM_MEM_ERROR;
909 909
 			return -1;
910 910
 		}
911 911
 		if (val->s) shm_free(val->s);
912 912
 	} else if (flags & CFG_STR_MALLOC) {
913 913
 		if ((buf = malloc(t.val.len + 1)) == NULL) {
914
-			ERR("%s:%d:%d: Out of malloc memory\n", st->file,
915
-				t.start.line, t.start.col);
914
+		        SYS_MEM_ERROR;
916 915
 			return -1;
917 916
 		}
918 917
 		if (val->s) free(val->s);
919 918
 	} else if (flags & CFG_STR_PKGMEM) {
920 919
 		if ((buf = pkg_malloc(t.val.len + 1)) == NULL) {
921
-			ERR("%s:%d:%d: Out of private memory\n", st->file,
922
-				t.start.line, t.start.col);
920
+		        PKG_MEM_ERROR;
923 921
 			return -1;
924 922
 		}
925 923
 		if (val->s) pkg_free(val->s);
... ...
@@ -453,7 +453,7 @@ static void core_pwd(rpc_t* rpc, void* c)
453 453
 	max_len = pathmax();
454 454
 	cwd_buf = pkg_malloc(max_len);
455 455
 	if (!cwd_buf) {
456
-		ERR("core_pwd: No memory left\n");
456
+		PKG_MEM_ERROR;
457 457
 		rpc->fault(c, 500, "Server Ran Out of Memory");
458 458
 		return;
459 459
 	}
... ...
@@ -114,14 +114,18 @@ int init_counters()
114 114
 	grp_no = 0;
115 115
 	cnt_id2record_size = CNT_ID2RECORD_SIZE;
116 116
 	cnt_id2record = pkg_malloc(sizeof(*cnt_id2record) * cnt_id2record_size);
117
-	if (cnt_id2record == 0)
117
+	if (cnt_id2record == 0) {
118
+		PKG_MEM_ERROR;
118 119
 		goto error;
120
+	}
119 121
 	memset(cnt_id2record, 0, sizeof(*cnt_id2record) * cnt_id2record_size);
120 122
 	grp_sorted_max_size = GRP_SORTED_SIZE;
121 123
 	grp_sorted_crt_size = 0;
122 124
 	grp_sorted = pkg_malloc(sizeof(*grp_sorted) * grp_sorted_max_size);
123
-	if (grp_sorted == 0)
125
+	if (grp_sorted == 0) {
126
+		PKG_MEM_ERROR;
124 127
 		goto error;
128
+	}
125 129
 	memset(grp_sorted, 0, sizeof(*grp_sorted) * grp_sorted_max_size);
126 130
 	return 0;
127 131
 error:
... ...
@@ -205,8 +209,10 @@ int counters_prefork_init(int max_process_no)
205 209
 	   the final shm version (with max_process_no rows) */
206 210
 	old = _cnts_vals;
207 211
 	_cnts_vals = shm_malloc(size);
208
-	if (_cnts_vals == 0)
212
+	if (_cnts_vals == 0) {
213
+		SHM_MEM_ERROR;
209 214
 		return -1;
215
+	}
210 216
 	memset(_cnts_vals, 0, size);
211 217
 	cnts_max_rows = max_process_no;
212 218
 	/* copy prefork values into the newly shm array */
... ...
@@ -232,8 +238,10 @@ static struct grp_record* grp_hash_add(str* group)
232 238
 	/* grp_rec copied at &g->u.data */
233 239
 	g = pkg_malloc(sizeof(struct str_hash_entry) - sizeof(g->u.data) +
234 240
 					sizeof(*grp_rec) + group->len + 1);
235
-	if (g == 0)
241
+	if (g == 0) {
242
+		PKG_MEM_ERROR;
236 243
 		goto error;
244
+	}
237 245
 	grp_rec = (struct grp_record*)&g->u.data[0];
238 246
 	grp_rec->group.s = (char*)(grp_rec + 1);
239 247
 	grp_rec->group.len = group->len;
... ...
@@ -330,8 +338,10 @@ static struct counter_record* cnt_hash_add(
330 338
 	e = pkg_malloc(sizeof(struct str_hash_entry) - sizeof(e->u.data) +
331 339
 					sizeof(*cnt_rec) + name->len + 1 + group->len + 1 +
332 340
 					doc_len + 1);
333
-	if (e == 0)
341
+	if (e == 0) {
342
+		PKG_MEM_ERROR;
334 343
 		goto error;
344
+	}
335 345
 	cnt_rec = (struct counter_record*)&e->u.data[0];
336 346
 	cnt_rec->group.s = (char*)(cnt_rec + 1);
337 347
 	cnt_rec->group.len = group->len;
... ...
@@ -57,7 +57,7 @@ struct lump* append_new_lump(struct lump** list, char* new_hdr,
57 57
 
58 58
 	tmp=pkg_malloc(sizeof(struct lump));
59 59
 	if (tmp==0){
60
-		LM_ERR("out of memory\n");
60
+		PKG_MEM_ERROR;
61 61
 		return 0;
62 62
 	}
63 63
 		
... ...
@@ -83,7 +83,7 @@ struct lump* add_new_lump(struct lump** list, char* new_hdr,
83 83
 
84 84
 	tmp=pkg_malloc(sizeof(struct lump));
85 85
 	if (tmp==0){
86
-		LM_ERR("out of memory\n");
86
+		PKG_MEM_ERROR;
87 87
 		return 0;
88 88
 	}
89 89
 		
... ...
@@ -108,7 +108,7 @@ struct lump* insert_new_lump(struct lump** list, char* new_hdr,
108 108
 
109 109
 	tmp=pkg_malloc(sizeof(struct lump));
110 110
 	if (tmp==0){
111
-		LM_ERR("out of memory\n");
111
+		PKG_MEM_ERROR;
112 112
 		return 0;
113 113
 	}
114 114
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -133,7 +133,7 @@ struct lump* insert_new_lump_after( struct lump* after, char* new_hdr,
133 133
 	tmp=pkg_malloc(sizeof(struct lump));
134 134
 	if (tmp==0){
135 135
 		ser_error=E_OUT_OF_MEM;
136
-		LM_ERR("out of memory\n");
136
+		PKG_MEM_ERROR;
137 137
 		return 0;
138 138
 	}
139 139
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -158,7 +158,7 @@ struct lump* insert_new_lump_before( struct lump* before, char* new_hdr,
158 158
 	tmp=pkg_malloc(sizeof(struct lump));
159 159
 	if (tmp==0){
160 160
 		ser_error=E_OUT_OF_MEM;
161
-		LM_ERR("out of memory\n");
161
+		PKG_MEM_ERROR;
162 162
 		return 0;
163 163
 	}
164 164
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -183,7 +183,7 @@ struct lump* insert_subst_lump_after( struct lump* after, enum lump_subst subst,
183 183
 	tmp=pkg_malloc(sizeof(struct lump));
184 184
 	if (tmp==0){
185 185
 		ser_error=E_OUT_OF_MEM;
186
-		LM_ERR("out of memory\n");
186
+		PKG_MEM_ERROR;
187 187
 		return 0;
188 188
 	}
189 189
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -209,7 +209,7 @@ struct lump* insert_subst_lump_before(	struct lump* before,
209 209
 	tmp=pkg_malloc(sizeof(struct lump));
210 210
 	if (tmp==0){
211 211
 		ser_error=E_OUT_OF_MEM;
212
-		LM_ERR("out of memory\n");
212
+		PKG_MEM_ERROR;
213 213
 		return 0;
214 214
 	}
215 215
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -234,7 +234,7 @@ struct lump* insert_cond_lump_after( struct lump* after, enum lump_conditions c,
234 234
 	tmp=pkg_malloc(sizeof(struct lump));
235 235
 	if (tmp==0){
236 236
 		ser_error=E_OUT_OF_MEM;
237
-		LM_ERR("out of memory\n");
237
+		PKG_MEM_ERROR;
238 238
 		return 0;
239 239
 	}
240 240
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -260,7 +260,7 @@ struct lump* insert_cond_lump_before(	struct lump* before,
260 260
 	tmp=pkg_malloc(sizeof(struct lump));
261 261
 	if (tmp==0){
262 262
 		ser_error=E_OUT_OF_MEM;
263
-		LM_ERR("out of memory\n");
263
+		PKG_MEM_ERROR;
264 264
 		return 0;
265 265
 	}
266 266
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -303,7 +303,7 @@ struct lump* del_lump(struct sip_msg* msg, int offset, int len, enum _hdr_types_
303 303
 	
304 304
 	tmp=pkg_malloc(sizeof(struct lump));
305 305
 	if (tmp==0){
306
-		LM_ERR("out of memory\n");
306
+		PKG_MEM_ERROR;
307 307
 		return 0;
308 308
 	}
309 309
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -358,7 +358,7 @@ struct lump* anchor_lump(struct sip_msg* msg, int offset, int len, enum _hdr_typ
358 358
 	tmp=pkg_malloc(sizeof(struct lump));
359 359
 	if (tmp==0){
360 360
 		ser_error=E_OUT_OF_MEM;
361
-		LM_ERR("out of memory\n");
361
+		PKG_MEM_ERROR;
362 362
 		return 0;
363 363
 	}
364 364
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -436,7 +436,7 @@ struct lump* anchor_lump2(struct sip_msg* msg, int offset, int len, enum _hdr_ty
436 436
 	tmp=pkg_malloc(sizeof(struct lump));
437 437
 	if (tmp==0){
438 438
 		ser_error=E_OUT_OF_MEM;
439
-		LM_ERR("out of memory\n");
439
+		PKG_MEM_ERROR;
440 440
 		return 0;
441 441
 	}
442 442
 	memset(tmp,0,sizeof(struct lump));
... ...
@@ -535,7 +535,11 @@ static struct lump *dup_lump_list_r( struct lump *l,
535 535
 	if (!l) { *error=0; return 0; }
536 536
 	/* otherwise duplicate current element */
537 537
 	new_lump=pkg_malloc(sizeof(struct lump));
538
-	if (!new_lump) { *error=1; return 0; }
538
+	if (!new_lump) {
539
+		PKG_MEM_ERROR;
540
+		*error=1;
541
+		return 0;
542
+	}
539 543
 
540 544
 	memcpy(new_lump, l, sizeof(struct lump));
541 545
 	new_lump->flags=LUMPFLAG_DUPED;
... ...
@@ -750,7 +754,7 @@ int sr_hdr_add(sip_msg_t *msg, str *sname, str *sbody)
750 754
 	h.len = sname->len + 2 + sbody->len + CRLF_LEN;
751 755
 	h.s = (char*)pkg_malloc(h.len+1);
752 756
 	if(h.s == 0) {
753
-		LM_ERR("no more pkg\n");
757
+		PKG_MEM_ERROR;
754 758
 		return -1;
755 759
 	}
756 760
 	anchor = anchor_lump(msg, msg->last_header->name.s + msg->last_header->len
... ...
@@ -56,7 +56,7 @@ struct lump_rpl** add_lump_rpl2(struct sip_msg *msg, char *s,
56 56
 	lump = (struct lump_rpl*) pkg_malloc
57 57
 		( sizeof(struct lump_rpl) + ((flags&LUMP_RPL_NODUP)?0:len) );
58 58
 	if (!lump) {
59
-		LM_ERR("no free pkg memory !\n");
59
+		PKG_MEM_ERROR;
60 60
 		goto error;
61 61
 	}
62 62
 
... ...
@@ -335,6 +335,7 @@ int init_dns_cache()
335 335
 	}
336 336
 	dns_cache_mem_used=shm_malloc(sizeof(*dns_cache_mem_used));
337 337
 	if (dns_cache_mem_used==0){
338
+		SHM_MEM_ERROR;
338 339
 		ret=E_OUT_OF_MEM;
339 340
 		goto error;
340 341
 	}
... ...
@@ -342,6 +343,7 @@ int init_dns_cache()
342 343
 
343 344
 	dns_last_used_lst=shm_malloc(sizeof(*dns_last_used_lst));
344 345
 	if (dns_last_used_lst==0){
346
+		SHM_MEM_ERROR;
345 347
 		ret=E_OUT_OF_MEM;
346 348
 		goto error;
347 349
 	}
... ...
@@ -349,6 +351,7 @@ int init_dns_cache()
349 351
 
350 352
 	dns_hash=shm_malloc(sizeof(struct dns_hash_head)*DNS_HASH_SIZE);
351 353
 	if (dns_hash==0){
354
+		SHM_MEM_ERROR;
352 355
 		ret=E_OUT_OF_MEM;
353 356
 		goto error;
354 357
 	}
... ...
@@ -370,6 +373,7 @@ int init_dns_cache()
370 373
 #ifdef DNS_WATCHDOG_SUPPORT
371 374
 	dns_servers_up=shm_malloc(sizeof(atomic_t));
372 375
 	if (dns_servers_up==0){
376
+		SHM_MEM_ERROR;
373 377
 		ret=E_OUT_OF_MEM;
374 378
 		goto error;
375 379
 	}
... ...
@@ -416,6 +420,7 @@ int init_dns_cache_stats(int iproc_num)
416 420
 
417 421
 	dns_cache_stats=shm_malloc(sizeof(*dns_cache_stats) * iproc_num);
418 422
 	if (dns_cache_stats==0){
423
+		SHM_MEM_ERROR;
419 424
 		return E_OUT_OF_MEM;
420 425
 	}
421 426
 	memset(dns_cache_stats, 0, sizeof(*dns_cache_stats) * iproc_num);
... ...
@@ -757,7 +762,7 @@ inline static struct dns_hash_entry* dns_cache_mk_bad_entry(str* name,
757 762
 	size=sizeof(struct dns_hash_entry)+name->len-1+1;
758 763
 	e=shm_malloc(size);
759 764
 	if (e==0){
760
-		LM_ERR("out of memory\n");
765
+		SHM_MEM_ERROR;
761 766
 		return 0;
762 767
 	}
763 768
 	memset(e, 0, size); /* init with 0*/
... ...
@@ -798,7 +803,7 @@ inline static struct dns_hash_entry* dns_cache_mk_ip_entry(str* name,
798 803
 			sizeof(struct dns_rr)+ ip->len;
799 804
 	e=shm_malloc(size);
800 805
 	if (e==0){
801
-		LM_ERR("out of memory\n");
806
+		SHM_MEM_ERROR;
802 807
 		return 0;
803 808
 	}
804 809
 	memset(e, 0, size); /* init with 0*/
... ...
@@ -851,7 +856,7 @@ static struct dns_hash_entry* dns_cache_mk_srv_entry(str* name,
851 856
 
852 857
 	e=shm_malloc(size);
853 858
 	if (e==0){
854
-		LM_ERR("out of memory\n");
859
+		SHM_MEM_ERROR;
855 860
 		return 0;
856 861
 	}
857 862
 	memset(e, 0, size); /* init with 0*/
... ...
@@ -1104,7 +1109,7 @@ inline static struct dns_hash_entry* dns_cache_mk_rd_entry(str* name, int type,
1104 1109
 	size+=ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1);
1105 1110
 	e=shm_malloc(size);
1106 1111
 	if (e==0){
1107
-		LM_ERR("out of memory\n");
1112
+		SHM_MEM_ERROR;
1108 1113
 		return 0;
1109 1114
 	}
1110 1115
 	memset(e, 0, size); /* init with 0 */
... ...
@@ -1411,7 +1416,7 @@ found:
1411 1416
 	for (r=0; r<no_records; r++){
1412 1417
 		rec[r].e=shm_malloc(rec[r].size);
1413 1418
 		if (rec[r].e==0){
1414
-			LM_ERR("out of memory\n");
1419
+			SHM_MEM_ERROR;
1415 1420
 			goto error;
1416 1421
 		}
1417 1422
 		memset(rec[r].e, 0, rec[r].size); /* init with 0*/
... ...
@@ -3988,7 +3993,7 @@ static struct dns_hash_entry *dns_cache_clone_entry(struct dns_hash_entry *e,
3988 3993
 
3989 3994
 	new=shm_malloc(rounded_size+rr_size+rdata_size);
3990 3995
 	if (!new) {
3991
-		LM_ERR("out of memory\n");
3996
+		SHM_MEM_ERROR;
3992 3997
 		return NULL;
3993 3998
 	}
3994 3999
 	memset(new, 0, rounded_size+rr_size+rdata_size);
... ...
@@ -82,7 +82,7 @@ int init_dst_set(void)
82 82
 	/* sr_dst_max_branches - 1 : because of the default branch for r-uri, #0 in tm */
83 83
 	branches = (branch_t*)pkg_malloc((sr_dst_max_branches-1)*sizeof(branch_t));
84 84
 	if(branches==NULL) {
85
-		LM_ERR("not enough memory to initialize destination branches\n");
85
+		PKG_MEM_ERROR;
86 86
 		return -1;
87 87
 	}
88 88
 	memset(branches, 0, (sr_dst_max_branches-1)*sizeof(branch_t));
... ...
@@ -602,7 +602,7 @@ int rewrite_uri(struct sip_msg* _m, str* _s)
602 602
 	if(_m->new_uri.s==NULL || _m->new_uri.len<_s->len) {
603 603
 		buf = (char*)pkg_malloc(_s->len + 1);
604 604
 		if (!buf) {
605
-			LM_ERR("No memory left to rewrite r-uri\n");
605
+			PKG_MEM_ERROR;
606 606
 			return -1;
607 607
 		}
608 608
 	}
... ...
@@ -165,6 +165,7 @@ static int init_blst_callback_lst(struct blst_callbacks_lst*  cb_lst, int max)
165 165
 	cb_lst->last_idx=0;
166 166
 	cb_lst->hooks=pkg_malloc(cb_lst->max_hooks*sizeof(struct blacklist_hook));
167 167
 	if (cb_lst->hooks==0)
168
+	        PKG_MEM_ERROR;
168 169
 		goto error;
169 170
 	memset(cb_lst->hooks, 0, cb_lst->max_hooks*sizeof(struct blacklist_hook));
170 171
 	return 0;
... ...
@@ -429,6 +430,7 @@ int init_dst_blacklist()
429 430
 #endif
430 431
 	blst_mem_used=shm_malloc(sizeof(*blst_mem_used));
431 432
 	if (blst_mem_used==0){
433
+	        SHM_MEM_ERROR;
432 434
 		ret=E_OUT_OF_MEM;
433 435
 		goto error;
434 436
 	}
... ...
@@ -436,6 +438,7 @@ int init_dst_blacklist()
436 438
 	dst_blst_hash=shm_malloc(sizeof(struct dst_blst_lst_head) *
437 439
 											DST_BLST_HASH_SIZE);
438 440
 	if (dst_blst_hash==0){
441
+	        SHM_MEM_ERROR;
439 442
 		ret=E_OUT_OF_MEM;
440 443
 		goto error;
441 444
 	}
... ...
@@ -511,6 +514,7 @@ int init_dst_blacklist_stats(int iproc_num)
511 514
 
512 515
 	dst_blacklist_stats=shm_malloc(sizeof(*dst_blacklist_stats) * iproc_num);
513 516
 	if (dst_blacklist_stats==0){
517
+	        SHM_MEM_ERROR;
514 518
 		return E_OUT_OF_MEM;
515 519
 	}
516 520
 	memset(dst_blacklist_stats, 0, sizeof(*dst_blacklist_stats) * iproc_num);
... ...
@@ -744,6 +748,7 @@ inline static int dst_blacklist_add_ip(unsigned char err_flags,
744 748
 			}
745 749
 			e=shm_malloc(size);
746 750
 			if (e==0){
751
+			        SHM_MEM_ERROR;
747 752
 				UNLOCK_BLST(hash);
748 753
 				ret=E_OUT_OF_MEM;
749 754
 				goto error;
... ...
@@ -218,7 +218,7 @@ void get_reply_status( str *status, struct sip_msg *reply, int code )
218 218
 	status->len=phrase.len+3/*code*/+1/*space*/; 
219 219
 	status->s=pkg_malloc(status->len+1/*ZT */);
220 220
 	if (!status->s) {
221
-		LM_ERR("no mem\n");
221
+		PKG_MEM_ERROR;
222 222
 		return;
223 223
 	}
224 224
 	status->s[3]=' ';
... ...
@@ -242,7 +242,7 @@ int register_flag(char* name, int pos)
242 242
 	
243 243
 	e=pkg_malloc(sizeof(struct flag_entry));
244 244
 	if (e==0){
245
-		LM_ERR("memory allocation failure\n");
245
+		PKG_MEM_ERROR;
246 246
 		return -3;
247 247
 	}
248 248
 	e->name.s=name;
... ...
@@ -372,7 +372,7 @@ int register_check_self_func(check_self_f f)
372 372
 	nf=(struct _check_self_func*)pkg_malloc(sizeof(struct _check_self_func));
373 373
 	if(nf==0)
374 374
 	{
375
-		LM_ERR("no more pkg\n");
375
+		PKG_MEM_ERROR;
376 376
 		return -1;
377 377
 	}
378 378
 	nf->fself = f;
... ...
@@ -474,8 +474,7 @@ int init_io_wait(io_wait_h* h, int max_fd, enum poll_types poll_method)
474 474
 	/* common stuff, everybody has fd_hash */
475 475
 	h->fd_hash=local_malloc(sizeof(*(h->fd_hash))*h->max_fd_no);
476 476
 	if (h->fd_hash==0){
477
-		LM_CRIT("could not alloc fd hashtable (%ld bytes)\n",
478
-					(long)sizeof(*(h->fd_hash))*h->max_fd_no );
477
+		PKG_MEM_CRITICAL;
479 478
 		goto error;
480 479
 	}
481 480
 	memset((void*)h->fd_hash, 0, sizeof(*(h->fd_hash))*h->max_fd_no);
... ...
@@ -493,8 +492,7 @@ int init_io_wait(io_wait_h* h, int max_fd, enum poll_types poll_method)
493 492
 #endif
494 493
 			h->fd_array=local_malloc(sizeof(*(h->fd_array))*h->max_fd_no);
495 494
 			if (h->fd_array==0){
496
-				LM_CRIT("could not alloc fd array (%ld bytes)\n",
497
-						(long)sizeof(*(h->fd_hash))*h->max_fd_no);
495
+				PKG_MEM_CRITICAL;
498 496
 				goto error;
499 497
 			}
500 498
 			memset((void*)h->fd_array, 0, sizeof(*(h->fd_array))*h->max_fd_no);
... ...
@@ -523,7 +521,7 @@ int init_io_wait(io_wait_h* h, int max_fd, enum poll_types poll_method)
523 521
 		case POLL_EPOLL_ET:
524 522
 			h->ep_array=local_malloc(sizeof(*(h->ep_array))*h->max_fd_no);
525 523
 			if (h->ep_array==0){
526
-				LM_CRIT("could not alloc epoll array\n");
524
+				PKG_MEM_CRITICAL;
527 525
 				goto error;
528 526
 			}
529 527
 			memset((void*)h->ep_array, 0, sizeof(*(h->ep_array))*h->max_fd_no);
... ...
@@ -546,13 +544,13 @@ int init_io_wait(io_wait_h* h, int max_fd, enum poll_types poll_method)
546 544
 			h->kq_array_size=2 * h->max_fd_no + h->kq_changes_size;
547 545
 			h->kq_array=local_malloc(sizeof(*(h->kq_array))*h->kq_array_size);
548 546
 			if (h->kq_array==0){
549
-				LM_CRIT("could not alloc kqueue event array\n");
547
+				PKG_MEM_CRITICAL;
550 548
 				goto error;
551 549
 			}
552 550
 			h->kq_changes=local_malloc(sizeof(*(h->kq_changes))*
553 551
 										h->kq_changes_size);
554 552
 			if (h->kq_changes==0){
555
-				LM_CRIT("could not alloc kqueue changes array\n");
553
+				PKG_MEM_CRITICAL;
556 554
 				goto error;
557 555
 			}
558 556
 			h->kq_nchanges=0;
... ...
@@ -51,7 +51,7 @@ struct net* mk_new_net(struct ip_addr* ip, struct ip_addr* mask)
51 51
 	}
52 52
 	n=(struct net*)pkg_malloc(sizeof(struct net));
53 53
 	if (n==0){ 
54
-		LM_CRIT("memory allocation failure\n");
54
+		PKG_MEM_CRITICAL;
55 55
 		goto error;
56 56
 	}
57 57
 	n->ip=*ip;
... ...
@@ -1533,7 +1533,7 @@ static int sr_kemi_hdr_append(sip_msg_t *msg, str *txt)
1533 1533
 
1534 1534
 	hdr = (char*)pkg_malloc(txt->len);
1535 1535
 	if(hdr==NULL) {
1536
-		LM_ERR("no pkg memory left\n");
1536
+		PKG_MEM_ERROR;
1537 1537
 		return -1;
1538 1538
 	}
1539 1539
 	memcpy(hdr, txt->s, txt->len);
... ...
@@ -1593,7 +1593,7 @@ static int sr_kemi_hdr_append_after(sip_msg_t *msg, str *txt, str *hname)
1593 1593
 
1594 1594
 	hdr = (char*)pkg_malloc(txt->len);
1595 1595
 	if(hdr==NULL) {
1596
-		LM_ERR("no pkg memory left\n");
1596
+		PKG_MEM_ERROR;
1597 1597
 		return -1;
1598 1598
 	}
1599 1599
 	memcpy(hdr, txt->s, txt->len);
... ...
@@ -1725,7 +1725,7 @@ static int sr_kemi_hdr_insert(sip_msg_t *msg, str *txt)
1725 1725
 	LM_DBG("insert hf: %.*s\n", txt->len, txt->s);
1726 1726
 	hdr = (char*)pkg_malloc(txt->len);
1727 1727
 	if(hdr==NULL) {
1728
-		LM_ERR("no pkg memory left\n");
1728
+		PKG_MEM_ERROR;
1729 1729
 		return -1;
1730 1730
 	}
1731 1731
 	memcpy(hdr, txt->s, txt->len);
... ...
@@ -1785,7 +1785,7 @@ static int sr_kemi_hdr_insert_before(sip_msg_t *msg, str *txt, str *hname)
1785 1785
 
1786 1786
 	hdr = (char*)pkg_malloc(txt->len);
1787 1787
 	if(hdr==NULL) {
1788
-		LM_ERR("no pkg memory left\n");
1788
+		PKG_MEM_ERROR;
1789 1789
 		return -1;
1790 1790
 	}
1791 1791
 	memcpy(hdr, txt->s, txt->len);
... ...
@@ -2094,14 +2094,14 @@ int sr_kemi_cbname_list_init(void)
2094 2094
 	if(_sr_kemi_cbname_list_size==NULL) {
2095 2095
 		lock_destroy(_sr_kemi_cbname_lock);
2096 2096
 		lock_dealloc(_sr_kemi_cbname_lock);
2097
-		LM_ERR("no more shared memory\n");
2097
+		SHM_MEM_ERROR;
2098 2098
 		return -1;
2099 2099
 	}
2100 2100
 	*_sr_kemi_cbname_list_size = 0;
2101 2101
 	_sr_kemi_cbname_list
2102 2102
 			= shm_malloc(KEMI_CBNAME_LIST_SIZE*sizeof(sr_kemi_cbname_t));
2103 2103
 	if(_sr_kemi_cbname_list==NULL) {
2104
-		LM_ERR("no more shared memory\n");
2104
+		SHM_MEM_ERROR;
2105 2105
 		shm_free(_sr_kemi_cbname_list_size);
2106 2106
 		_sr_kemi_cbname_list_size = NULL;
2107 2107
 		lock_destroy(_sr_kemi_cbname_lock);
... ...
@@ -69,7 +69,7 @@ inline static gen_lock_set_t* lock_set_alloc(int n)
69 69
 	gen_lock_set_t* ls;
70 70
 	ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t)+n*sizeof(gen_lock_t));
71 71
 	if (ls==0){
72
-		LM_CRIT("could not allocate lock_set\n");
72
+		SHM_MEM_CRITICAL;
73 73
 	}else{
74 74
 		ls->locks=(gen_lock_t*)((char*)ls+sizeof(gen_lock_set_t));
75 75
 		ls->size=n;
... ...
@@ -90,10 +90,12 @@ inline static gen_lock_set_t* lock_set_alloc(int n)
90 90
 {
91 91
 	gen_lock_set_t* ls;
92 92
 	ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t));
93
-	if (ls){
93
+	if (ls==0){
94
+		SHM_MEM_CRITICAL;
95
+	}else{
94 96
 		ls->size=n;
95 97
 		ls->semid=-1;
96
-	};
98
+	}
97 99
 	return ls;
98 100
 }
99 101
 
... ...
@@ -101,7 +101,7 @@ rec_lock_set_t* rec_lock_set_alloc(int n)
101 101
 	rec_lock_set_t* ls;
102 102
 	ls=(rec_lock_set_t*)shm_malloc(sizeof(rec_lock_set_t)+n*sizeof(rec_lock_t));
103 103
 	if (ls==0){
104
-		LM_CRIT("could not allocate rec_lock_set\n");
104
+		SHM_MEM_CRITICAL;
105 105
 	}else{
106 106
 		ls->locks=(rec_lock_t*)((char*)ls+sizeof(rec_lock_set_t));
107 107
 		ls->size=n;
... ...
@@ -211,7 +211,7 @@ int fixup_regexp_null(void** param, int param_no)
211 211
 	if (param_no != 1)
212 212
 		return E_UNSPEC;
213 213
 	if ((re=pkg_malloc(sizeof(*re))) ==0) {
214
-		ERR("No memory left\n");
214
+		PKG_MEM_ERROR;
215 215
 		goto error;
216 216
 	}
217 217
 	if (regcomp(&re->regex, *param,
... ...
@@ -277,7 +277,7 @@ int fixup_pvar_all(void** param, int param_no)
277 277
 		/* not a pvs id */
278 278
 		goto error;
279 279
 	if ((pvs_f=pkg_malloc(sizeof(*pvs_f))) == 0) {
280
-		ERR("No memory left\n");
280
+		PKG_MEM_ERROR;
281 281
 		goto error;
282 282
 	}
283 283
 	if (pv_parse_spec2(&name, &pvs_f->pvs, 1) == 0)
... ...
@@ -61,7 +61,7 @@ int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
61 61
 	len = strlen(regex);
62 62
 	reg = pkg_malloc(len + 4 + 1);
63 63
 	if (reg == 0) {
64
-		LM_ERR("No memory left\n");
64
+		PKG_MEM_ERROR;
65 65
 		return -1;
66 66
 	}
67 67
 	reg[0] = '^';
... ...
@@ -109,7 +109,7 @@ int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
109 109
 						case PARAM_STRING:
110 110
 							*((char**)ptr) = pkg_malloc(strlen((char*)val2)+1);
111 111
 							if (!*((char**)ptr)) {
112
-								LM_ERR("No memory left\n");
112
+								PKG_MEM_ERROR;
113 113
 								regfree(&preg);
114 114
 								pkg_free(reg);
115 115
 								return -1;
... ...
@@ -120,7 +120,7 @@ int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
120 120
 						case PARAM_STR:
121 121
 							((str*)ptr)->s = pkg_malloc(((str*)val2)->len+1);
122 122
 							if (!((str*)ptr)->s) {
123
-								LM_ERR("No memory left\n");
123
+								PKG_MEM_ERROR;
124 124
 								regfree(&preg);
125 125
 								pkg_free(reg);
126 126
 								return -1;
... ...
@@ -332,7 +332,7 @@ char* received_builder(struct sip_msg *msg, unsigned int *received_len)
332 332
 	buf=pkg_malloc(sizeof(char)*MAX_RECEIVED_SIZE);
333 333
 	if (buf==0){
334 334
 		ser_error=E_OUT_OF_MEM;
335
-		LM_ERR("out of memory\n");
335
+		PKG_MEM_ERROR;
336 336
 		return 0;
337 337
 	}
338 338
 	memcpy(buf, RECEIVED, RECEIVED_LEN);
... ...
@@ -365,7 +365,7 @@ char* rport_builder(struct sip_msg *msg, unsigned int *rport_len)
365 365
 	buf=pkg_malloc(sizeof(char)*(len+1));/* space for null term */
366 366
 	if (buf==0){
367 367
 		ser_error=E_OUT_OF_MEM;
368
-		LM_ERR("out of memory\n");
368
+		PKG_MEM_ERROR;
369 369
 		return 0;
370 370
 	}
371 371
 	memcpy(buf, RPORT, RPORT_LEN);
... ...
@@ -397,7 +397,7 @@ char* id_builder(struct sip_msg* msg, unsigned int *id_len)
397 397
 	buf=pkg_malloc(sizeof(char)*(len+1));/* place for ending \0 */
398 398
 	if (buf==0){
399 399
 		ser_error=E_OUT_OF_MEM;
400
-		LM_ERR("out of memory\n");
400
+		PKG_MEM_ERROR;
401 401
 		return 0;
402 402
 	}
403 403
 	memcpy(buf, ID_PARAM, ID_PARAM_LEN);
... ...
@@ -440,7 +440,7 @@ char* clen_builder(	struct sip_msg* msg, int *clen_len, int diff,
440 440
 	buf=pkg_malloc(sizeof(char)*(len+1));
441 441
 	if (buf==0){
442 442
 		ser_error=E_OUT_OF_MEM;
443
-		LM_ERR("out of memory\n");
443
+		PKG_MEM_ERROR;
444 444
 		return 0;
445 445
 	}
446 446
 	if (body_only) {
... ...
@@ -1734,7 +1734,7 @@ int get_boundary(struct sip_msg* msg, str* boundary)
1734 1734
 			if (boundary->s == NULL)
1735 1735
 			{
1736 1736
 				free_params(list);
1737
-				LM_ERR("no memory for boundary string\n");
1737
+				PKG_MEM_ERROR;
1738 1738
 				return -1;
1739 1739
 			}
1740 1740
 			*(boundary->s) = '-';
... ...
@@ -2087,7 +2087,7 @@ after_update_via1:
2087 2087
 		path_buf.len=ROUTE_PREFIX_LEN+msg->path_vec.len+CRLF_LEN;
2088 2088
 		path_buf.s=pkg_malloc(path_buf.len+1);
2089 2089
 		if (unlikely(path_buf.s==0)){
2090
-			LM_ERR("out of memory\n");
2090
+		        PKG_MEM_ERROR;
2091 2091
 			ser_error=E_OUT_OF_MEM;
2092 2092
 			goto error00;
2093 2093
 		}
... ...
@@ -2187,7 +2187,10 @@ after_update_via1:
2187 2187
 		new_buf=(char*)pkg_malloc(new_len+1);
2188 2188
 	if (new_buf==0){
2189 2189
 		ser_error=E_OUT_OF_MEM;
2190
-		LM_ERR("out of memory\n");
2190
+		if(unlikely(mode&BUILD_IN_SHM))
2191
+                        SHM_MEM_ERROR;
2192
+                else
2193
+                        PKG_MEM_ERROR;
2191 2194
 		goto error00;
2192 2195
 	}
2193 2196
 
... ...
@@ -2289,7 +2292,7 @@ char * generate_res_buf_from_sip_res( struct sip_msg* msg,
2289 2292
 	new_buf=(char*)pkg_malloc(new_len+1); /* +1 is for debugging
2290 2293
 											 (\0 to print it )*/
2291 2294
 	if (new_buf==0){
2292
-		LM_ERR("out of mem\n");
2295
+	        PKG_MEM_ERROR;
2293 2296
 		goto error;
2294 2297
 	}
2295 2298
 	new_buf[new_len]=0; /* debug: print the message */
... ...
@@ -2446,7 +2449,7 @@ char * build_res_buf_from_sip_req( unsigned int code, str *text ,str *new_tag,
2446 2449
 	buf = (char*) pkg_malloc( len+1 );
2447 2450
 	if (!buf)
2448 2451
 	{
2449
-		LM_ERR("out of memory; needs %d\n",len);
2452
+	        PKG_MEM_ERROR;
2450 2453
 		goto error01;
2451 2454
 	}
2452 2455
 
... ...
@@ -2770,7 +2773,7 @@ char* via_builder(unsigned int *len, sip_msg_t *msg,
2770 2773
 	line_buf=pkg_malloc( max_len );
2771 2774
 	if (line_buf==0){
2772 2775
 		ser_error=E_OUT_OF_MEM;
2773
-		LM_ERR("out of memory\n");
2776
+		PKG_MEM_ERROR;
2774 2777
 		return 0;
2775 2778
 	}
2776 2779
 
... ...
@@ -2942,7 +2945,7 @@ char* create_via_hf(unsigned int *len,
2942 2945
 		/* params so far + ';rport' + '\0' */
2943 2946
 		via = (char*)pkg_malloc(extra_params.len+RPORT_LEN);
2944 2947
 		if(via==0) {
2945
-			LM_ERR("building local rport via param failed\n");
2948
+		        PKG_MEM_ERROR;
2946 2949
 			if (extra_params.s) pkg_free(extra_params.s);
2947 2950
 			return 0;
2948 2951
 		}
... ...
@@ -2964,7 +2967,7 @@ char* create_via_hf(unsigned int *len,
2964 2967
 		} else {
2965 2968
 			via = (char*)pkg_malloc(extra_params.len+slen+1);
2966 2969
 			if(via==0) {
2967
-				LM_ERR("building srvid param failed\n");
2970
+			        PKG_MEM_ERROR;
2968 2971
 				if (extra_params.s) pkg_free(extra_params.s);
2969 2972
 				return 0;
2970 2973
 			}
... ...
@@ -2988,7 +2991,7 @@ char* create_via_hf(unsigned int *len,
2988 2991
 		if(xparams.len>0) {
2989 2992
 			via = (char*)pkg_malloc(extra_params.len+xparams.len+2);
2990 2993
 			if(via==0) {
2991
-				LM_ERR("building xavps params failed\n");
2994
+			        PKG_MEM_ERROR;
2992 2995
 				if (extra_params.s) pkg_free(extra_params.s);
2993 2996
 				return 0;
2994 2997
 			}
... ...
@@ -3048,7 +3051,7 @@ char * build_only_headers( struct sip_msg* msg, int skip_first_line,
3048 3051
 
3049 3052
 	new_buf = (char *)pkg_malloc(new_len+1);
3050 3053
 	if (!new_buf) {
3051
-		LM_ERR("Not enough memory\n");
3054
+	        PKG_MEM_ERROR;
3052 3055
 		*error = -1;
3053 3056
 		return 0;
3054 3057
 	}
... ...
@@ -3098,7 +3101,7 @@ char * build_body( struct sip_msg* msg,
3098 3101
 
3099 3102
 	new_buf = (char *)pkg_malloc(new_len+1);
3100 3103
 	if (!new_buf) {
3101
-		LM_ERR("Not enough memory\n");
3104
+	        PKG_MEM_ERROR;
3102 3105
 		*error = -1;
3103 3106
 		return 0;
3104 3107
 	}
... ...
@@ -3158,7 +3161,7 @@ char * build_all( struct sip_msg* msg, int touch_clen,
3158 3161
 
3159 3162
 	new_buf = (char *)pkg_malloc(new_len+1);
3160 3163
 	if (!new_buf) {
3161
-		LM_ERR("Not enough memory\n");
3164
+	        PKG_MEM_ERROR;
3162 3165
 		*error = -1;
3163 3166
 		return 0;
3164 3167
 	}
... ...
@@ -101,7 +101,7 @@ static inline int add_alias(char* name, int len, unsigned short port,
101 101
 	aliases=a;
102 102
 	return 1;
103 103
 error:
104
-	LM_ERR("memory allocation error\n");
104
+	PKG_MEM_ERROR;
105 105
 	if (a) pkg_free(a);
106 106
 	return -1;
107 107
 }
... ...
@@ -42,12 +42,13 @@ int init_nonsip_hooks()
42 42
 	nonsip_hooks=pkg_malloc(nonsip_max_hooks*
43 43
 									sizeof(struct nonsip_hook));
44 44
 	if (nonsip_hooks==0){
45
+		PKG_MEM_ERROR;
45 46
 		goto error;
46 47
 	}
47 48
 	memset(nonsip_hooks, 0, nonsip_max_hooks*sizeof(struct nonsip_hook));
48 49
 	return 0;
49 50
 error:
50
-	LM_ERR("memory allocation failure\n");
51
+	PKG_MEM_ERROR;
51 52
 	return -1;
52 53
 }
53 54
 
... ...
@@ -187,7 +187,7 @@ int parse_contacts(str* _s, contact_t** _c)
187 187
 		/* Allocate and clear contact structure */
188 188
 		c = (contact_t*)pkg_malloc(sizeof(contact_t));
189 189
 		if (c == 0) {
190
-			LM_ERR("no memory left\n");
190
+			PKG_MEM_ERROR;
191 191
 			goto error;
192 192
 		}
193 193
 		memset(c, 0, sizeof(contact_t));
... ...
@@ -75,7 +75,7 @@ int parse_contact(struct hdr_field* _h)
75 75
 
76 76
 	b = (contact_body_t*)pkg_malloc(sizeof(contact_body_t));
77 77
 	if (b == 0) {
78
-		LM_ERR("no memory left\n");
78
+		PKG_MEM_ERROR;
79 79
 		return -1;
80 80
 	}
81 81
 
... ...
@@ -42,7 +42,7 @@ static inline int new_credentials(struct hdr_field* _h)
42 42
 
43 43
 	b = (auth_body_t*)pkg_malloc(sizeof(auth_body_t));
44 44
 	if (b == 0) {
45
-		LM_ERR("no memory left\n");
45
+		PKG_MEM_ERROR;
46 46
 		return -1;
47 47
 	}
48 48
 
... ...
@@ -115,7 +115,7 @@ char* get_hdr_field(char* const buf, char* const end, struct hdr_field* const hd
115 115
 			via_cnt++;
116 116
 			vb=pkg_malloc(sizeof(struct via_body));
117 117
 			if (vb==0){
118
-				ERR("out of memory\n");
118
+				PKG_MEM_ERROR;
119 119
 				goto error;
120 120
 			}
121 121
 			memset(vb,0,sizeof(struct via_body));
... ...
@@ -134,7 +134,7 @@ char* get_hdr_field(char* const buf, char* const end, struct hdr_field* const hd
134 134
 		case HDR_CSEQ_T:
135 135
 			cseq_b=pkg_malloc(sizeof(struct cseq_body));
136 136
 			if (cseq_b==0){
137
-				ERR("out of memory\n");
137
+				PKG_MEM_ERROR;
138 138
 				goto error;
139 139
 			}
140 140
 			memset(cseq_b, 0, sizeof(struct cseq_body));
... ...
@@ -154,7 +154,7 @@ char* get_hdr_field(char* const buf, char* const end, struct hdr_field* const hd
154 154
 		case HDR_TO_T:
155 155
 			to_b=pkg_malloc(sizeof(struct to_body));
156 156
 			if (to_b==0){
157
-				ERR("out of memory\n");
157
+				PKG_MEM_ERROR;
158 158
 				goto error;
159 159
 			}
160 160
 			memset(to_b, 0, sizeof(struct to_body));
... ...
@@ -324,8 +324,8 @@ int parse_headers(struct sip_msg* const msg, const hdr_flags_t flags, const int
324 324
 		prefetch_loc_r(tmp+64, 1);
325 325
 		hf=pkg_malloc(sizeof(struct hdr_field));
326 326
 		if (unlikely(hf==0)){
327
+			PKG_MEM_ERROR;
327 328
 			ser_error=E_OUT_OF_MEM;
328
-			ERR("memory allocation error\n");
329 329
 			goto error;
330 330
 		}
331 331
 		memset(hf,0, sizeof(struct hdr_field));
... ...
@@ -751,7 +751,7 @@ int set_dst_uri(struct sip_msg* const msg, const str* const uri)
751 751
 	} else {
752 752
 		ptr = (char*)pkg_malloc(uri->len);
753 753
 		if (!ptr) {
754
-			ERR("Not enough memory\n");
754
+			PKG_MEM_ERROR;
755 755
 			return -1;
756 756
 		}
757 757
 
... ...
@@ -790,7 +790,7 @@ int set_path_vector(struct sip_msg* msg, str* path)
790 790
 	} else {
791 791
 		ptr = (char*)pkg_malloc(path->len);
792 792
 		if (!ptr) {
793
-			ERR("not enough pkg memory\n");
793
+			PKG_MEM_ERROR;
794 794
 			return -1;
795 795
 		}
796 796
 
... ...
@@ -830,7 +830,7 @@ int set_instance(struct sip_msg* msg, str* instance)
830 830
 	} else {
831 831
 		ptr = (char*)pkg_malloc(instance->len);
832 832
 		if (!ptr) {
833
-			ERR("not enough pkg memory for instance\n");
833
+			PKG_MEM_ERROR;
834 834
 			return -1;
835 835
 		}
836 836
 		memcpy(ptr, instance->s, instance->len);
... ...
@@ -869,7 +869,7 @@ int set_ruid(struct sip_msg* msg, str* ruid)
869 869
 	} else {
870 870
 		ptr = (char*)pkg_malloc(ruid->len);
871 871
 		if (!ptr) {
872
-			ERR("not enough pkg memory for ruid\n");
872
+			PKG_MEM_ERROR;
873 873
 			return -1;
874 874
 		}
875 875
 		memcpy(ptr, ruid->s, ruid->len);
... ...
@@ -908,7 +908,7 @@ int set_ua(struct sip_msg* msg, str* location_ua)
908 908
 	} else {
909 909
 		ptr = (char*)pkg_malloc(location_ua->len);
910 910
 		if (!ptr) {
911
-			ERR("not enough pkg memory for location_ua\n");
911
+			PKG_MEM_ERROR;
912 912
 			return -1;
913 913
 		}
914 914
 		memcpy(ptr, location_ua->s, location_ua->len);
... ...
@@ -284,7 +284,7 @@ static char *parse_to_param(char *const buffer, const char *const end,
284 284
 						param = (struct to_param *)pkg_malloc(
285 285
 								sizeof(struct to_param));
286 286
 						if(!param) {
287
-							LM_ERR("out of memory\n");
287
+							PKG_MEM_ERROR;
288 288
 							goto error;
289 289
 						}
290 290
 						memset(param, 0, sizeof(struct to_param));
... ...
@@ -56,7 +56,7 @@ int parse_allow_header(struct hdr_field* _hf)
56