Browse code

mem: convert TLSF memory manager to new memory API

- run kamailio with `-X tlsf` to manage pkg memory with TLSF
- run kamailio with `-x tlsf` to manage shm memory with TLSF

Camille Oudot authored on 18/09/2015 13:16:13
Showing 7 changed files
... ...
@@ -507,8 +507,8 @@ data_target = $(prefix)/$(data_dir)
507 507
 #		custom quick malloc, recommended for debugging
508 508
 # -DF_MALLOC
509 509
 #		an even faster malloc, not recommended for debugging
510
-# -DTLSF_MALLOC=1
511
-#       an implemetation of the "two levels segregation fit" malloc algorithm
510
+# -DTLSF_MALLOC
511
+#       an implemetation of the "two levels segregated fit" malloc algorithm
512 512
 # -DDL_MALLOC
513 513
 #		a malloc implementation based on Doug Lea's dl_malloc
514 514
 # -DSF_MALLOC 
... ...
@@ -668,6 +668,8 @@ C_DEFS= $(extra_defs) \
668 668
 C_DEFS+= -DF_MALLOC
669 669
 # enable q_malloc
670 670
 C_DEFS+= -DQ_MALLOC
671
+# enable TLSF malloc
672
+C_DEFS+= -DTLSF_MALLOC
671 673
 ifeq 	($(MEMDBG), 1)
672 674
 	C_DEFS+= -DDBG_SR_MEMORY
673 675
 endif
... ...
@@ -34,4 +34,10 @@ int qm_malloc_init_pkg_manager(void);
34 34
 int qm_malloc_init_shm_manager(void);
35 35
 #endif
36 36
 
37
+#ifdef TLSF_MALLOC
38
+/* two levels segregated fit - implemented in tlsf_malloc.c */
39
+int tlsf_malloc_init_pkg_manager(void);
40
+int tlsf_malloc_init_shm_manager(void);
41
+#endif
42
+
37 43
 #endif
... ...
@@ -64,6 +64,10 @@ int pkg_init_manager(char *name)
64 64
 			|| strcmp(name, "qmalloc")==0) {
65 65
 		/*quick malloc*/
66 66
 		return qm_malloc_init_pkg_manager();
67
+	} else if(strcmp(name, "tlsf")==0
68
+			|| strcmp(name, "tlsf_malloc")==0) {
69
+		/*tlsf malloc*/
70
+		return tlsf_malloc_init_pkg_manager();
67 71
 	} else if(strcmp(name, "sm")==0) {
68 72
 		/*system malloc*/
69 73
 	} else {
... ...
@@ -240,6 +240,10 @@ int shm_init_manager(char *name)
240 240
 			|| strcmp(name, "qmalloc")==0) {
241 241
 		/*quick malloc*/
242 242
 		return qm_malloc_init_shm_manager();
243
+	} else if(strcmp(name, "tlsf")==0
244
+			|| strcmp(name, "tlsf_malloc")==0) {
245
+		/*tlsf malloc*/
246
+		return tlsf_malloc_init_shm_manager();
243 247
 	} else if(strcmp(name, "sm")==0) {
244 248
 		/*system malloc*/
245 249
 	} else {
246 250
similarity index 85%
247 251
rename from mem/tlsf.c
248 252
rename to mem/tlsf_malloc.c
... ...
@@ -1,3 +1,5 @@
1
+#if defined(TLSF_MALLOC)
2
+
1 3
 #include <assert.h>
2 4
 #include <limits.h>
3 5
 #include <stddef.h>
... ...
@@ -5,12 +7,14 @@
5 7
 #include <stdlib.h>
6 8
 #include <string.h>
7 9
 
8
-#include "tlsf.h"
9
-#include "tlsfbits.h"
10
+#include "tlsf_malloc.h"
11
+#include "tlsf_malloc_bits.h"
10 12
 #include "src_loc.h"
13
+#include "memdbg.h"
14
+#include "memapi.h"
11 15
 #include "../dprint.h"
12 16
 #include "../cfg/cfg.h"
13
-#include "memdbg.h"
17
+#include "../globals.h"
14 18
 
15 19
 /*
16 20
 ** Constants.
... ...
@@ -104,7 +108,7 @@ tlsf_static_assert(ALIGN_SIZE == SMALL_BLOCK_SIZE / SL_INDEX_COUNT);
104 108
 typedef struct {
105 109
 	const char* file;
106 110
 	const char* func;
107
-	unsigned long line;
111
+	unsigned int line;
108 112
 } alloc_info_t;
109 113
 #endif
110 114
 /*
... ...
@@ -166,24 +170,20 @@ static const size_t block_size_min =
166 170
 	sizeof(block_header_t) - sizeof(block_header_t*);
167 171
 static const size_t block_size_max = tlsf_cast(size_t, 1) << FL_INDEX_MAX;
168 172
 
169
-#ifdef TLSF_STATS
170
-	#define TLSF_INCREASE_REAL_USED(control, increment) do {control->real_used += (increment) ; control->max_used = tlsf_max(control->real_used, control->max_used);}while(0)
171
-	#define TLSF_INCREASE_FRAGMENTS(control) do {control->fragments++ ; control->max_fragments = tlsf_max(control->fragments, control->max_fragments);}while(0)
172
-#endif
173
+#define TLSF_INCREASE_REAL_USED(control, increment) do {control->real_used += (increment) ; control->max_used = tlsf_max(control->real_used, control->max_used);}while(0)
174
+#define TLSF_INCREASE_FRAGMENTS(control) do {control->fragments++ ; control->max_fragments = tlsf_max(control->fragments, control->max_fragments);}while(0)
173 175
 
174 176
 /* The TLSF control structure. */
175 177
 typedef struct control_t
176 178
 {
177 179
 	/* Empty lists point at this block to indicate they are free. */
178 180
 	block_header_t block_null;
179
-#ifdef TLSF_STATS
180 181
 	size_t total_size;
181 182
 	size_t allocated;
182 183
 	size_t real_used;
183 184
 	size_t max_used;
184 185
 	size_t fragments;
185 186
 	size_t max_fragments;
186
-#endif
187 187
 	/* Bitmaps for free lists. */
188 188
 	unsigned int fl_bitmap;
189 189
 	unsigned int sl_bitmap[FL_INDEX_COUNT];
... ...
@@ -430,9 +430,7 @@ static void remove_free_block(control_t* control, block_header_t* block, int fl,
430 430
 			}
431 431
 		}
432 432
 	}
433
-#if defined TLSF_STATS
434 433
 	control->fragments--;
435
-#endif
436 434
 }
437 435
 
438 436
 /* Insert a free block into the free block list. */
... ...
@@ -454,9 +452,7 @@ static void insert_free_block(control_t* control, block_header_t* block, int fl,
454 452
 	control->blocks[fl][sl] = block;
455 453
 	control->fl_bitmap |= (1 << fl);
456 454
 	control->sl_bitmap[fl] |= (1 << sl);
457
-#if defined TLSF_STATS
458 455
 	TLSF_INCREASE_FRAGMENTS(control);
459
-#endif
460 456
 }
461 457
 
462 458
 /* Remove a given block from the free list. */
... ...
@@ -613,12 +609,10 @@ static void* block_prepare_used(control_t* control, block_header_t* block, size_
613 609
 		block_trim_free(control, block, size);
614 610
 		block_mark_as_used(block);
615 611
 		p = block_to_ptr(block);
616
-#ifdef TLSF_STATS
617 612
 		TLSF_INCREASE_REAL_USED(control, block_size(block) + (p - (void *)block
618 613
 				/* prev_phys_block is melted in the previous block when the current block is used */
619 614
 				+ sizeof(block->prev_phys_block)));
620 615
 		control->allocated += block_size(block);
621
-#endif
622 616
 #ifdef DBG_TLSF_MALLOC
623 617
 		block->alloc_info.file = file;
624 618
 		block->alloc_info.func = function;
... ...
@@ -849,9 +843,7 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
849 843
 	block_set_free(block);
850 844
 	block_set_prev_used(block);
851 845
 	block_insert(tlsf_cast(control_t*, tlsf), block);
852
-#ifdef TLSF_STATS
853 846
 	tlsf_cast(control_t*, tlsf)->total_size += block_size(block);
854
-#endif
855 847
 #ifdef DBG_TLSF_MALLOC
856 848
 	block->alloc_info.file = _SRC_LOC_;
857 849
 	block->alloc_info.func = _SRC_FUNCTION_;
... ...
@@ -879,9 +871,7 @@ void tlsf_remove_pool(tlsf_t tlsf, pool_t pool)
879 871
 
880 872
 	mapping_insert(block_size(block), &fl, &sl);
881 873
 	remove_free_block(control, block, fl, sl);
882
-#ifdef TLSF_STATS
883 874
 	tlsf_cast(control_t*, tlsf)->total_size -= block_size(block);
884
-#endif
885 875
 }
886 876
 
887 877
 /*
... ...
@@ -933,14 +923,12 @@ tlsf_t tlsf_create(void* mem)
933 923
 	}
934 924
 
935 925
 	control_construct(tlsf_cast(control_t*, mem));
936
-#ifdef TLSF_STATS
937 926
 	tlsf_cast(control_t*, mem)->real_used = tlsf_size();
938 927
 	tlsf_cast(control_t*, mem)->max_used = tlsf_size();
939 928
 	tlsf_cast(control_t*, mem)->allocated = 0;
940 929
 	tlsf_cast(control_t*, mem)->total_size = tlsf_size();
941 930
 	tlsf_cast(control_t*, mem)->fragments = 0;
942 931
 	tlsf_cast(control_t*, mem)->max_fragments = 0;
943
-#endif
944 932
 	return tlsf_cast(tlsf_t, mem);
945 933
 }
946 934
 
... ...
@@ -964,7 +952,7 @@ pool_t tlsf_get_pool(tlsf_t tlsf)
964 952
 
965 953
 #ifdef DBG_TLSF_MALLOC
966 954
 void* tlsf_malloc(tlsf_t tlsf, size_t size,
967
-		const char *file, const char *function, unsigned long line)
955
+		const char *file, const char *function, unsigned int line)
968 956
 #else
969 957
 void* tlsf_malloc(tlsf_t tlsf, size_t size)
970 958
 #endif
... ...
@@ -975,7 +963,7 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size)
975 963
 #ifdef DBG_TLSF_MALLOC
976 964
 	void *ptr;
977 965
 
978
-	MDBG("tlsf_malloc(%p, %zu) called from %s: %s(%lu)\n", tlsf, size, file, function, line);
966
+	MDBG("tlsf_malloc(%p, %zu) called from %s: %s(%u)\n", tlsf, size, file, function, line);
979 967
 	ptr = block_prepare_used(control, block, adjust, file, function, line);
980 968
 	MDBG("tlsf_malloc(%p, %zu) returns address %p \n", tlsf, size,
981 969
 		ptr);
... ...
@@ -987,13 +975,13 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size)
987 975
 
988 976
 #ifdef DBG_TLSF_MALLOC
989 977
 void tlsf_free(tlsf_t tlsf, void* ptr,
990
-		const char *file, const char *function, unsigned long line)
978
+		const char *file, const char *function, unsigned int line)
991 979
 #else
992 980
 void tlsf_free(tlsf_t tlsf, void* ptr)
993 981
 #endif
994 982
 {
995 983
 #ifdef DBG_TLSF_MALLOC
996
-	MDBG("tlsf_free(%p, %p), called from %s: %s(%lu)\n", tlsf, ptr, file, function, line);
984
+	MDBG("tlsf_free(%p, %p), called from %s: %s(%u)\n", tlsf, ptr, file, function, line);
997 985
 #endif
998 986
 	/* Don't attempt to free a NULL pointer. */
999 987
 	if (ptr)
... ...
@@ -1003,8 +991,8 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
1003 991
 		if (block_is_free(block)) {
1004 992
 			LOG(L_CRIT, "BUG: tlsf_free: freeing already freed pointer (%p)"
1005 993
 #ifdef DBG_TLSF_MALLOC
1006
-					", called from %s: %s(%lu)"
1007
-					", first free %s: %s(%lu)\n",
994
+					", called from %s: %s(%u)"
995
+					", first free %s: %s(%u)\n",
1008 996
 					ptr, file, function, line,
1009 997
 					block->alloc_info.file, block->alloc_info.func, block->alloc_info.line);
1010 998
 #else
... ...
@@ -1017,12 +1005,10 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
1017 1005
 			}
1018 1006
 
1019 1007
 		}
1020
-#if defined TLSF_STATS
1021 1008
 		control->allocated -= block_size(block);
1022 1009
 		control->real_used -= (block_size(block) + (ptr - (void *)block
1023 1010
 				/* prev_phys_block is melted in the previous block when the current block is used */
1024 1011
 				+ sizeof(block->prev_phys_block)));
1025
-#endif
1026 1012
 #ifdef DBG_TLSF_MALLOC
1027 1013
 		block->alloc_info.file = file;
1028 1014
 		block->alloc_info.func = function;
... ...
@@ -1034,7 +1020,7 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
1034 1020
 		block_insert(control, block);
1035 1021
 	} else {
1036 1022
 #ifdef DBG_TLSF_MALLOC
1037
-		LOG(L_WARN, "tlsf_free: free(0) called from %s: %s(%lu)\n", file, function, line);
1023
+		LOG(L_WARN, "tlsf_free: free(0) called from %s: %s(%u)\n", file, function, line);
1038 1024
 #else
1039 1025
 		LOG(L_WARN, "tlsf_free: free(0) called\n");
1040 1026
 #endif
... ...
@@ -1056,7 +1042,7 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
1056 1042
 */
1057 1043
 #ifdef DBG_TLSF_MALLOC
1058 1044
 void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size,
1059
-		const char *file, const char *function, unsigned long line)
1045
+		const char *file, const char *function, unsigned int line)
1060 1046
 #else
1061 1047
 void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
1062 1048
 #endif
... ...
@@ -1117,10 +1103,8 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
1117 1103
 		}
1118 1104
 		else
1119 1105
 		{
1120
-#ifdef TLSF_STATS
1121 1106
 			control->allocated -= block_size(block);
1122 1107
 			control->real_used -= block_size(block);
1123
-#endif
1124 1108
 			/* Do we need to expand to the next block? */
1125 1109
 			if (adjust > cursize)
1126 1110
 			{
... ...
@@ -1131,18 +1115,14 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
1131 1115
 			/* Trim the resulting block and return the original pointer. */
1132 1116
 			block_trim_used(control, block, adjust);
1133 1117
 			p = ptr;
1134
-#ifdef TLSF_STATS
1135 1118
 			control->allocated +=block_size(block);
1136 1119
 			TLSF_INCREASE_REAL_USED(control, block_size(block));
1137
-#endif
1138 1120
 		}
1139 1121
 	}
1140 1122
 
1141 1123
 	return p;
1142 1124
 }
1143 1125
 
1144
-#ifdef TLSF_STATS
1145
-
1146 1126
 void tlsf_meminfo(tlsf_t pool, struct mem_info *info)
1147 1127
 {
1148 1128
 	control_t* control = tlsf_cast(control_t*, pool);
... ...
@@ -1171,7 +1151,6 @@ void tlsf_status(tlsf_t pool)
1171 1151
 
1172 1152
 	memlog=cfg_get(core, core_cfg, memlog);
1173 1153
 	LOG_(DEFAULT_FACILITY, memlog, "tlsf_status: ", "status of pool (%p):\n", pool);
1174
-#ifdef TLSF_STATS
1175 1154
 	LOG_(DEFAULT_FACILITY, memlog, "tlsf_status: ", "heap size= %zu\n",
1176 1155
 			control->total_size);
1177 1156
 	LOG_(DEFAULT_FACILITY, memlog, "tlsf_status: ",
... ...
@@ -1179,11 +1158,10 @@ void tlsf_status(tlsf_t pool)
1179 1158
 			control->allocated, control->real_used, control->total_size - control->real_used, control->fragments);
1180 1159
 	LOG_(DEFAULT_FACILITY, memlog, "tlsf_status: ",
1181 1160
 			"max used (+overhead)=%zu, max fragments=%zu\n", control->max_used, control->max_fragments);
1182
-#endif
1183 1161
 
1184 1162
 	/* print a summary of the 2 levels bucket list */
1185 1163
 	LOG_(DEFAULT_FACILITY, memlog, "tlsf_status: ",
1186
-				"Used blocks matrix ('.': unused, 'X': between 2^X and (2^(X+1)-1) used blocks, X=A..Z, A=0, B=1, ...)\n");
1164
+				"Free blocks matrix ('.': none, 'X': between 2^X and (2^(X+1)-1) free blocks, X=A..Z, A=0, B=1, ...)\n");
1187 1165
 	LOG_(DEFAULT_FACILITY, memlog, "tlsf_status: ",
1188 1166
 				"> first-level: %d block list arrays between 2^fl and 2^(fl+1) bytes (fl=%d..%d)\n",
1189 1167
 				FL_INDEX_COUNT, FL_INDEX_SHIFT, FL_INDEX_MAX);
... ...
@@ -1284,5 +1262,211 @@ void tlsf_sums(tlsf_t pool)
1284 1262
 {}
1285 1263
 #endif /* defined DBG_TLSF_MALLOC */
1286 1264
 
1265
+/*memory manager core api*/
1266
+static char *_tlsf_mem_name = "f_malloc";
1267
+
1268
+/* PKG - private memory API*/
1269
+static void *_tlsf_pkg_pool = 0;
1270
+static tlsf_t _tlsf_pkg_block = 0;
1271
+
1272
+/**
1273
+ * \brief Destroy memory pool
1274
+ */
1275
+void tlsf_malloc_destroy_pkg_manager(void)
1276
+{
1277
+	if (_tlsf_pkg_pool) {
1278
+		free(_tlsf_pkg_pool);
1279
+		_tlsf_pkg_pool = 0;
1280
+	}
1281
+	_tlsf_pkg_block = 0;
1282
+}
1283
+
1284
+/**
1285
+ * \brief Init memory pool
1286
+ */
1287
+int tlsf_malloc_init_pkg_manager(void)
1288
+{
1289
+	sr_pkg_api_t ma;
1290
+	_tlsf_pkg_pool = malloc(pkg_mem_size);
1291
+	if (_tlsf_pkg_pool) {
1292
+		_tlsf_pkg_block = tlsf_create_with_pool(_tlsf_pkg_pool, pkg_mem_size);
1293
+	} else {
1294
+		LOG(L_CRIT, "could not initialize tlsf pkg memory pool\n");
1295
+		fprintf(stderr, "Too much tlsf pkg memory demanded: %ld bytes\n",
1296
+						pkg_mem_size);
1297
+		return -1;
1298
+	}
1299
+
1300
+	memset(&ma, 0, sizeof(sr_pkg_api_t));
1301
+	ma.mname      = _tlsf_mem_name;
1302
+	ma.mem_pool   = _tlsf_pkg_pool;
1303
+	ma.mem_block  = _tlsf_pkg_block;
1304
+	ma.xmalloc    = tlsf_malloc;
1305
+	ma.xfree      = tlsf_free;
1306
+	ma.xrealloc   = tlsf_realloc;
1307
+	ma.xstatus    = tlsf_status;
1308
+	ma.xinfo      = tlsf_meminfo;
1309
+	ma.xavailable = tlsf_available;
1310
+	ma.xsums      = tlsf_sums;
1311
+	ma.xdestroy   = tlsf_malloc_destroy_pkg_manager;
1312
+
1313
+	return pkg_init_api(&ma);
1314
+}
1315
+
1316
+/* SHM - shared memory API*/
1317
+static void *_tlsf_shm_pool = 0;
1318
+static tlsf_t _tlsf_shm_block = 0;
1319
+
1320
+/*SHM wrappers to sync the access to memory block*/
1321
+#ifdef DBG_TLSF_MALLOC
1322
+void* tlsf_shm_malloc(void* tlsfmp, unsigned long size,
1323
+					const char* file, const char* func, unsigned int line)
1324
+{
1325
+	void *r;
1326
+	shm_lock();
1327
+	r = tlsf_malloc(tlsfmp, size, file, func, line);
1328
+	shm_unlock();
1329
+	return r;
1330
+}
1331
+void* tlsf_shm_realloc(void* tlsfmp, void* p, unsigned long size,
1332
+					const char* file, const char* func, unsigned int line)
1333
+{
1334
+	void *r;
1335
+	shm_lock();
1336
+	r = tlsf_realloc(tlsfmp, p, size, file, func, line);
1337
+	shm_unlock();
1338
+	return r;
1339
+}
1340
+void* tlsf_shm_resize(void* tlsfmp, void* p, unsigned long size,
1341
+					const char* file, const char* func, unsigned int line)
1342
+{
1343
+	void *r;
1344
+	shm_lock();
1345
+	if(p) tlsf_free(tlsfmp, p, file, func, line);
1346
+	r = tlsf_malloc(tlsfmp, size, file, func, line);
1347
+	shm_unlock();
1348
+	return r;
1349
+}
1350
+void tlsf_shm_free(void* tlsfmp, void* p, const char* file, const char* func,
1351
+				unsigned int line)
1352
+{
1353
+	shm_lock();
1354
+	tlsf_free(tlsfmp, p, file, func, line);
1355
+	shm_unlock();
1356
+}
1357
+#else
1358
+void* tlsf_shm_malloc(void* tlsfmp, unsigned long size)
1359
+{
1360
+	void *r;
1361
+	shm_lock();
1362
+	r = tlsf_malloc(tlsfmp, size);
1363
+	shm_unlock();
1364
+	return r;
1365
+}
1366
+void* tlsf_shm_realloc(void* tlsfmp, void* p, unsigned long size)
1367
+{
1368
+	void *r;
1369
+	shm_lock();
1370
+	r = tlsf_realloc(tlsfmp, p, size);
1371
+	shm_unlock();
1372
+	return r;
1373
+}
1374
+void* tlsf_shm_resize(void* tlsfmp, void* p, unsigned long size)
1375
+{
1376
+	void *r;
1377
+	shm_lock();
1378
+	if(p) tlsf_free(tlsfmp, p);
1379
+	r = tlsf_malloc(tlsfmp, size);
1380
+	shm_unlock();
1381
+	return r;
1382
+}
1383
+void tlsf_shm_free(void* tlsfmp, void* p)
1384
+{
1385
+	shm_lock();
1386
+	tlsf_free(tlsfmp, p);
1387
+	shm_unlock();
1388
+}
1287 1389
 #endif
1390
+void tlsf_shm_status(void* tlsfmp)
1391
+{
1392
+	shm_lock();
1393
+	tlsf_status(tlsfmp);
1394
+	shm_unlock();
1395
+}
1396
+void tlsf_shm_info(void* tlsfmp, struct mem_info* info)
1397
+{
1398
+	shm_lock();
1399
+	tlsf_meminfo(tlsfmp, info);
1400
+	shm_unlock();
1401
+
1402
+}
1403
+unsigned long tlsf_shm_available(void* tlsfmp)
1404
+{
1405
+	unsigned long r;
1406
+	shm_lock();
1407
+	r = tlsf_available(tlsfmp);
1408
+	shm_unlock();
1409
+	return r;
1410
+}
1411
+void tlsf_shm_sums(void* tlsfmp)
1412
+{
1413
+	shm_lock();
1414
+	tlsf_sums(tlsfmp);
1415
+	shm_unlock();
1416
+}
1417
+
1418
+
1419
+/**
1420
+ * \brief Destroy memory pool
1421
+ */
1422
+void tlsf_malloc_destroy_shm_manager(void)
1423
+{
1424
+	/*shm pool from core - nothing to do*/
1425
+	_tlsf_shm_pool = 0;
1426
+	_tlsf_shm_block = 0;
1427
+}
1428
+
1429
+/**
1430
+ * \brief Init memory pool
1431
+ */
1432
+int tlsf_malloc_init_shm_manager(void)
1433
+{
1434
+	sr_shm_api_t ma;
1435
+	_tlsf_shm_pool = shm_core_get_pool();
1436
+	if (_tlsf_shm_pool) {
1437
+		_tlsf_shm_block = tlsf_create_with_pool(_tlsf_shm_pool, shm_mem_size);
1438
+	} else {
1439
+		LOG(L_CRIT, "could not initialize tlsf shm memory pool\n");
1440
+		fprintf(stderr, "Too much tlsf shm memory demanded: %ld bytes\n",
1441
+						pkg_mem_size);
1442
+		return -1;
1443
+	}
1444
+
1445
+	memset(&ma, 0, sizeof(sr_shm_api_t));
1446
+	ma.mname          = _tlsf_mem_name;
1447
+	ma.mem_pool       = _tlsf_shm_pool;
1448
+	ma.mem_block      = _tlsf_shm_block;
1449
+	ma.xmalloc        = tlsf_shm_malloc;
1450
+	ma.xmalloc_unsafe = tlsf_malloc;
1451
+	ma.xfree          = tlsf_shm_free;
1452
+	ma.xfree_unsafe   = tlsf_free;
1453
+	ma.xrealloc       = tlsf_shm_realloc;
1454
+	ma.xresize        = tlsf_shm_resize;
1455
+	ma.xstatus        = tlsf_shm_status;
1456
+	ma.xinfo          = tlsf_shm_info;
1457
+	ma.xavailable     = tlsf_shm_available;
1458
+	ma.xsums          = tlsf_shm_sums;
1459
+	ma.xdestroy       = tlsf_malloc_destroy_shm_manager;
1460
+
1461
+	if(shm_init_api(&ma)<0) {
1462
+		LM_ERR("cannot initialize the core shm api\n");
1463
+		return -1;
1464
+	}
1465
+	if(shm_core_lock_init()<0) {
1466
+		LM_ERR("cannot initialize the core shm lock\n");
1467
+		return -1;
1468
+	}
1469
+	return 0;
1470
+}
1288 1471
 
1472
+#endif /* TLSF_MALLOC */
1289 1473
similarity index 88%
1290 1474
rename from mem/tlsf.h
1291 1475
rename to mem/tlsf_malloc.h
... ...
@@ -1,6 +1,8 @@
1 1
 #ifndef INCLUDED_tlsf
2 2
 #define INCLUDED_tlsf
3 3
 
4
+#if defined(TLSF_MALLOC)
5
+
4 6
 /*
5 7
 ** Two Level Segregated Fit memory allocator, version 3.0.
6 8
 ** Written by Matthew Conte, and placed in the Public Domain.
... ...
@@ -18,7 +20,10 @@
18 20
 
19 21
 #include <stddef.h>
20 22
 #include "meminfo.h"
21
-#define TLSF_STATS
23
+
24
+#ifdef DBG_SR_MEMORY
25
+#define DBG_TLSF_MALLOC
26
+#endif
22 27
 
23 28
 #if defined(__cplusplus)
24 29
 extern "C" {
... ...
@@ -42,11 +47,11 @@ void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
42 47
 /* malloc/memalign/realloc/free replacements. */
43 48
 #ifdef DBG_TLSF_MALLOC
44 49
 void* tlsf_malloc(tlsf_t tlsf, size_t size,
45
-		const char *file, const char *function, unsigned long line);
50
+		const char *file, const char *function, unsigned int line);
46 51
 void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size,
47
-		const char *file, const char *function, unsigned long line);
52
+		const char *file, const char *function, unsigned int line);
48 53
 void tlsf_free(tlsf_t tlsf, void* ptr,
49
-		const char *file, const char *function, unsigned long line);
54
+		const char *file, const char *function, unsigned int line);
50 55
 #else
51 56
 void* tlsf_malloc(tlsf_t tlsf, size_t bytes);
52 57
 void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
... ...
@@ -80,4 +85,6 @@ size_t tlsf_available(tlsf_t pool);
80 85
 };
81 86
 #endif
82 87
 
88
+#endif /* TLSF_MALLOC */
89
+
83 90
 #endif
84 91
similarity index 100%
85 92
rename from mem/tlsfbits.h
86 93
rename to mem/tlsf_malloc_bits.h