Browse code

mem: f_malloc updated to implement new pkg/shm management api

Daniel-Constantin Mierla authored on 14/09/2015 07:24:50
Showing 2 changed files
... ...
@@ -24,7 +24,7 @@
24 24
  */
25 25
 
26 26
 
27
-#if !defined(q_malloc)  && (defined F_MALLOC)
27
+#if defined(F_MALLOC)
28 28
 
29 29
 #include <string.h>
30 30
 #include <stdlib.h>
... ...
@@ -415,15 +415,18 @@ struct fm_frag* fm_search_defrag(struct fm_block* qm, unsigned long size)
415 415
  * \return address of allocated memory
416 416
  */
417 417
 #ifdef DBG_F_MALLOC
418
-void* fm_malloc(struct fm_block* qm, unsigned long size,
418
+void* fm_malloc(void* qmp, unsigned long size,
419 419
 					const char* file, const char* func, unsigned int line)
420 420
 #else
421
-void* fm_malloc(struct fm_block* qm, unsigned long size)
421
+void* fm_malloc(void* qmp, unsigned long size)
422 422
 #endif
423 423
 {
424
+	struct fm_block* qm;
424 425
 	struct fm_frag** f;
425 426
 	struct fm_frag* frag;
426 427
 	int hash;
428
+
429
+	qm = (struct fm_block*)qmp;
427 430
 	
428 431
 #ifdef DBG_F_MALLOC
429 432
 	MDBG("fm_malloc(%p, %lu) called from %s: %s(%d)\n", qm, size, file, func,
... ...
@@ -550,14 +553,17 @@ static void fm_join_frag(struct fm_block* qm, struct fm_frag* f)
550 553
  * \param p freed memory
551 554
  */
552 555
 #ifdef DBG_F_MALLOC
553
-void fm_free(struct fm_block* qm, void* p, const char* file, const char* func, 
556
+void fm_free(void* qmp, void* p, const char* file, const char* func, 
554 557
 				unsigned int line)
555 558
 #else
556
-void fm_free(struct fm_block* qm, void* p)
559
+void fm_free(void* qmp, void* p)
557 560
 #endif
558 561
 {
562
+	struct fm_block* qm;
559 563
 	struct fm_frag* f;
560 564
 
565
+	qm = (struct fm_block*)qmp;
566
+
561 567
 #ifdef DBG_F_MALLOC
562 568
 	MDBG("fm_free(%p, %p), called from %s: %s(%d)\n", qm, p, file, func, line);
563 569
 #endif
... ...
@@ -611,18 +617,21 @@ void fm_free(struct fm_block* qm, void* p)
611 617
  * \return reallocated memory block
612 618
  */
613 619
 #ifdef DBG_F_MALLOC
614
-void* fm_realloc(struct fm_block* qm, void* p, unsigned long size,
620
+void* fm_realloc(void* qmp, void* p, unsigned long size,
615 621
 					const char* file, const char* func, unsigned int line)
616 622
 #else
617
-void* fm_realloc(struct fm_block* qm, void* p, unsigned long size)
623
+void* fm_realloc(void* qmp, void* p, unsigned long size)
618 624
 #endif
619 625
 {
626
+	struct fm_block* qm;
620 627
 	struct fm_frag *f;
621 628
 	unsigned long diff;
622 629
 	unsigned long orig_size;
623 630
 	struct fm_frag *n;
624 631
 	void *ptr;
625
-	
632
+
633
+	qm = (struct fm_block*)qmp;
634
+
626 635
 #ifdef DBG_F_MALLOC
627 636
 	MDBG("fm_realloc(%p, %p, %lu) called from %s: %s(%d)\n", qm, p, size,
628 637
 			file, func, line);
... ...
@@ -726,8 +735,9 @@ void* fm_realloc(struct fm_block* qm, void* p, unsigned long size)
726 735
  * \brief Report internal memory manager status
727 736
  * \param qm memory block
728 737
  */
729
-void fm_status(struct fm_block* qm)
738
+void fm_status(void* qmp)
730 739
 {
740
+	struct fm_block* qm;
731 741
 	struct fm_frag* f;
732 742
 	int i,j;
733 743
 	int h;
... ...
@@ -736,6 +746,8 @@ void fm_status(struct fm_block* qm)
736 746
 	int memlog;
737 747
 	int mem_summary;
738 748
 
749
+	qm = (struct fm_block*)qmp;
750
+
739 751
 	memlog=cfg_get(core, core_cfg, memlog);
740 752
 	mem_summary=cfg_get(core, core_cfg, mem_summary);
741 753
 	LOG_(DEFAULT_FACILITY, memlog, "fm_status: ", "fm_status (%p):\n", qm);
... ...
@@ -824,8 +836,11 @@ void fm_status(struct fm_block* qm)
824 836
  * \param qm memory block
825 837
  * \param info memory information
826 838
  */
827
-void fm_info(struct fm_block* qm, struct mem_info* info)
839
+void fm_info(void* qmp, struct mem_info* info)
828 840
 {
841
+	struct fm_block* qm;
842
+
843
+	qm = (struct fm_block*)qmp;
829 844
 	memset(info,0, sizeof(*info));
830 845
 	info->total_size=qm->size;
831 846
 	info->min_frag=MIN_FRAG_SIZE;
... ...
@@ -843,8 +858,11 @@ void fm_info(struct fm_block* qm, struct mem_info* info)
843 858
  * \return Returns how much free memory is available, on error (not compiled
844 859
  * with bookkeeping code) returns (unsigned long)(-1)
845 860
  */
846
-unsigned long fm_available(struct fm_block* qm)
861
+unsigned long fm_available(void* qmp)
847 862
 {
863
+	struct fm_block* qm;
864
+
865
+	qm = (struct fm_block*)qmp;
848 866
 	return qm->size-qm->real_used;
849 867
 }
850 868
 
... ...
@@ -887,8 +905,11 @@ make_new:
887 905
  * \brief Debugging helper, summary and logs all allocated memory blocks
888 906
  * \param qm memory block
889 907
  */
890
-void fm_sums(struct fm_block* qm)
908
+void fm_sums(void* qmp)
891 909
 {
910
+	struct fm_block* qm;
911
+
912
+	qm = (struct fm_block*)qmp;
892 913
 	struct fm_frag* f;
893 914
 	int i;
894 915
 	int memlog;
... ...
@@ -923,8 +944,224 @@ void fm_sums(struct fm_block* qm)
923 944
 	LOG_(DEFAULT_FACILITY, memlog, "fm_status: ",
924 945
 			"-----------------------------\n");
925 946
 }
947
+#else
948
+void fm_sums(void* qmp)
949
+{
950
+	struct fm_block* qm;
951
+
952
+	qm = (struct fm_block*)qmp;
953
+	LOG_(DEFAULT_FACILITY, memlog, "fm_sums not available (%p)\n", qm);
954
+	return;
955
+}
926 956
 #endif /* DBG_F_MALLOC */
927 957
 
928 958
 
959
+/*memory manager core api*/
960
+static char *_fm_mem_name = "f_malloc";
961
+
962
+/* PKG - private memory API*/
963
+static char *_fm_pkg_pool = 0;
964
+static struct fm_block *_fm_pkg_block = 0;
965
+
966
+/**
967
+ * \brief Destroy memory pool
968
+ */
969
+void fm_malloc_destroy_pkg_manager(void)
970
+{
971
+	if (_fm_pkg_pool) {
972
+		free(_fm_pkg_pool);
973
+		_fm_pkg_pool = 0;
974
+	}
975
+	_fm_pkg_block = 0;
976
+}
977
+
978
+/**
979
+ * \brief Init memory pool
980
+ */
981
+int fm_malloc_init_pkg_manager(void)
982
+{
983
+	sr_pkg_api_t ma;
984
+	_fm_pkg_pool = malloc(pkg_mem_size);
985
+	if (_fm_pkg_pool)
986
+		_fm_pkg_block=fm_malloc_init(_fm_pkg_pool, pkg_mem_size, MEM_TYPE_PKG);
987
+	if (_fm_pkg_block==0){
988
+		LOG(L_CRIT, "could not initialize fm pkg memory pool\n");
989
+		fprintf(stderr, "Too much fm pkg memory demanded: %ld bytes\n",
990
+						pkg_mem_size);
991
+		return -1;
992
+	}
993
+
994
+	memset(&ma, 0, sizeof(sr_pkg_api_t));
995
+	ma.mname      = _fm_mem_name;
996
+	ma.mem_pool   = _fm_pkg_pool;
997
+	ma.mem_block  = _fm_pkg_block;
998
+	ma.xmalloc    = fm_malloc;
999
+	ma.xfree      = fm_free;
1000
+	ma.xrealloc   = fm_realloc;
1001
+	ma.xstatus    = fm_status;
1002
+	ma.xinfo      = fm_info;
1003
+	ma.xavailable = fm_available;
1004
+	ma.xsums      = fm_sums;
1005
+	ma.xdestroy   = fm_malloc_destroy_pkg_manager;
1006
+
1007
+	return pkg_init_api(&ma);
1008
+}
1009
+
1010
+
1011
+/* SHM - shared memory API*/
1012
+static void *_fm_shm_pool = 0;
1013
+static struct fm_block *_fm_shm_block = 0;
1014
+
1015
+/*SHM wrappers to sync the access to memory block*/
1016
+#ifdef DBG_F_MALLOC
1017
+void* fm_shm_malloc(void* qmp, unsigned long size,
1018
+					const char* file, const char* func, unsigned int line)
1019
+{
1020
+	void *r;
1021
+	shm_lock();
1022
+	r = fm_malloc(qmp, size, file, func, line);
1023
+	shm_unlock();
1024
+	return r;
1025
+}
1026
+void* fm_shm_realloc(void* qmp, void* p, unsigned long size,
1027
+					const char* file, const char* func, unsigned int line)
1028
+{
1029
+	void *r;
1030
+	shm_lock();
1031
+	r = fm_realloc(qmp, p, size, file, func, line);
1032
+	shm_unlock();
1033
+	return r;
1034
+}
1035
+void* fm_shm_resize(void* qmp, void* p, unsigned long size,
1036
+					const char* file, const char* func, unsigned int line)
1037
+{
1038
+	void *r;
1039
+	shm_lock();
1040
+	if(p) fm_free(qmp, p, file, func, line);
1041
+	r = fm_malloc(qmp, size, file, func, line);
1042
+	shm_unlock();
1043
+	return r;
1044
+}
1045
+void fm_shm_free(void* qmp, void* p, const char* file, const char* func,
1046
+				unsigned int line)
1047
+{
1048
+	shm_lock();
1049
+	fm_free(qmp, p, file, func, line);
1050
+	shm_unlock();
1051
+}
1052
+#else
1053
+void* fm_shm_malloc(void* qmp, unsigned long size)
1054
+{
1055
+	void *r;
1056
+	shm_lock();
1057
+	r = fm_malloc(qmp, size);
1058
+	shm_unlock();
1059
+	return r;
1060
+}
1061
+void* fm_shm_realloc(void* qmp, void* p, unsigned long size)
1062
+{
1063
+	void *r;
1064
+	shm_lock();
1065
+	r = fm_realloc(qmp, p, size);
1066
+	shm_unlock();
1067
+	return r;
1068
+}
1069
+void* fm_shm_resize(void* qmp, void* p, unsigned long size)
1070
+{
1071
+	void *r;
1072
+	shm_lock();
1073
+	if(p) fm_free(qmp, p);
1074
+	r = fm_malloc(qmp, size);
1075
+	shm_unlock();
1076
+	return r;
1077
+}
1078
+void fm_shm_free(void* qmp, void* p)
1079
+{
1080
+	shm_lock();
1081
+	fm_free(qmp, p);
1082
+	shm_unlock();
1083
+}
1084
+#endif
1085
+void fm_shm_status(void* qmp)
1086
+{
1087
+	shm_lock();
1088
+	fm_status(qmp);
1089
+	shm_unlock();
1090
+}
1091
+void fm_shm_info(void* qmp, struct mem_info* info)
1092
+{
1093
+	shm_lock();
1094
+	fm_info(qmp, info);
1095
+	shm_unlock();
1096
+
1097
+}
1098
+unsigned long fm_shm_available(void* qmp)
1099
+{
1100
+	unsigned long r;
1101
+	shm_lock();
1102
+	r = fm_available(qmp);
1103
+	shm_unlock();
1104
+	return r;
1105
+}
1106
+void fm_shm_sums(void* qmp)
1107
+{
1108
+	shm_lock();
1109
+	fm_sums(qmp);
1110
+	shm_unlock();
1111
+}
1112
+
1113
+
1114
+/**
1115
+ * \brief Destroy memory pool
1116
+ */
1117
+void fm_malloc_destroy_shm_manager(void)
1118
+{
1119
+	/*shm pool from core - nothing to do*/
1120
+	_fm_shm_pool = 0;
1121
+	_fm_shm_block = 0;
1122
+}
1123
+
1124
+/**
1125
+ * \brief Init memory pool
1126
+ */
1127
+int fm_malloc_init_shm_manager(void)
1128
+{
1129
+	sr_shm_api_t ma;
1130
+	_fm_shm_pool = shm_core_get_pool();
1131
+	if (_fm_shm_pool)
1132
+		_fm_shm_block=fm_malloc_init(_fm_shm_pool, shm_mem_size, MEM_TYPE_SHM);
1133
+	if (_fm_shm_block==0){
1134
+		LOG(L_CRIT, "could not initialize fm shm memory pool\n");
1135
+		fprintf(stderr, "Too much fm shm memory demanded: %ld bytes\n",
1136
+						pkg_mem_size);
1137
+		return -1;
1138
+	}
1139
+
1140
+	memset(&ma, 0, sizeof(sr_shm_api_t));
1141
+	ma.mname          = _fm_mem_name;
1142
+	ma.mem_pool       = _fm_shm_pool;
1143
+	ma.mem_block      = _fm_shm_block;
1144
+	ma.xmalloc        = fm_shm_malloc;
1145
+	ma.xmalloc_unsafe = fm_malloc;
1146
+	ma.xfree          = fm_shm_free;
1147
+	ma.xfree_unsafe   = fm_free;
1148
+	ma.xrealloc       = fm_shm_realloc;
1149
+	ma.xresize        = fm_shm_resize;
1150
+	ma.xstatus        = fm_shm_status;
1151
+	ma.xinfo          = fm_shm_info;
1152
+	ma.xavailable     = fm_shm_available;
1153
+	ma.xsums          = fm_shm_sums;
1154
+	ma.xdestroy       = fm_malloc_destroy_shm_manager;
1155
+
1156
+	if(shm_init_api(&ma)<0) {
1157
+		LM_ERR("cannot initialize the core shm api\n");
1158
+		return -1;
1159
+	}
1160
+	if(shm_core_lock_init()<0) {
1161
+		LM_ERR("cannot initialize the core shm lock\n");
1162
+		return -1;
1163
+	}
1164
+	return 0;
1165
+}
929 1166
 
930 1167
 #endif
... ...
@@ -23,17 +23,16 @@
23 23
  */
24 24
 
25 25
 
26
+#if defined(F_MALLOC)
27
+
26 28
 #if !defined(f_malloc_h)
27 29
 #define f_malloc_h
28 30
 
29
-#ifdef DBG_QM_MALLOC
30
-#ifndef DBG_F_MALLOC
31
-	#define DBG_F_MALLOC
32
-#endif /* DBG_F_MALLOC */
33
-#endif /* DBG_QM_MALLOC */
34
-
35 31
 #include "meminfo.h"
36 32
 
33
+#ifdef DBG_SR_MEMORY
34
+#define DBG_F_MALLOC
35
+#endif
37 36
 
38 37
 /**
39 38
  * Use a bitmap to quickly find free fragments, should speed up
... ...
@@ -137,10 +136,10 @@ struct fm_block* fm_malloc_init(char* address, unsigned long size, int type);
137 136
  * \return address of allocated memory
138 137
  */
139 138
 #ifdef DBG_F_MALLOC
140
-void* fm_malloc(struct fm_block* qm, unsigned long size,
139
+void* fm_malloc(void* qmp, unsigned long size,
141 140
 					const char* file, const char* func, unsigned int line);
142 141
 #else
143
-void* fm_malloc(struct fm_block* qm, unsigned long size);
142
+void* fm_malloc(void* qmp, unsigned long size);
144 143
 #endif
145 144
 
146 145
 
... ...
@@ -152,10 +151,10 @@ void* fm_malloc(struct fm_block* qm, unsigned long size);
152 151
  * \param p freed memory
153 152
  */
154 153
 #ifdef DBG_F_MALLOC
155
-void  fm_free(struct fm_block* qm, void* p, const char* file, const char* func, 
154
+void  fm_free(void* qmp, void* p, const char* file, const char* func, 
156 155
 				unsigned int line);
157 156
 #else
158
-void  fm_free(struct fm_block* qm, void* p);
157
+void  fm_free(void* qmp, void* p);
159 158
 #endif
160 159
 
161 160
 
... ...
@@ -169,10 +168,10 @@ void  fm_free(struct fm_block* qm, void* p);
169 168
  * \return reallocated memory block
170 169
  */
171 170
 #ifdef DBG_F_MALLOC
172
-void*  fm_realloc(struct fm_block* qm, void* p, unsigned long size, 
171
+void*  fm_realloc(void* qmp, void* p, unsigned long size, 
173 172
 					const char* file, const char* func, unsigned int line);
174 173
 #else
175
-void*  fm_realloc(struct fm_block* qm, void* p, unsigned long size);
174
+void*  fm_realloc(void* qmp, void* p, unsigned long size);
176 175
 #endif
177 176
 
178 177
 
... ...
@@ -180,7 +179,7 @@ void*  fm_realloc(struct fm_block* qm, void* p, unsigned long size);
180 179
  * \brief Report internal memory manager status
181 180
  * \param qm memory block
182 181
  */
183
-void fm_status(struct fm_block* qm);
182
+void fm_status(void* qmp);
184 183
 
185 184
 
186 185
 /**
... ...
@@ -191,7 +190,7 @@ void fm_status(struct fm_block* qm);
191 190
  * \param qm memory block
192 191
  * \param info memory information
193 192
  */
194
-void fm_info(struct fm_block* qm, struct mem_info* info);
193
+void fm_info(void* qmp, struct mem_info* info);
195 194
 
196 195
 
197 196
 /**
... ...
@@ -200,17 +199,14 @@ void fm_info(struct fm_block* qm, struct mem_info* info);
200 199
  * \return Returns how much free memory is available, on error (not compiled
201 200
  * with bookkeeping code) returns (unsigned long)(-1)
202 201
  */
203
-unsigned long fm_available(struct fm_block* qm);
202
+unsigned long fm_available(void* qmp);
204 203
 
205 204
 
206 205
 /**
207 206
  * \brief Debugging helper, summary and logs all allocated memory blocks
208 207
  * \param qm memory block
209 208
  */
210
-#ifdef DBG_F_MALLOC
211
-void fm_sums(struct fm_block* qm);
212
-#else
213
-#define fm_sums(qm) do{}while(0)
214
-#endif /* DBG_F_MALLOC */
209
+void fm_sums(void* qmp);
215 210
 
216 211
 #endif
212
+#endif