Просмотр исходного кода

core: mem - qm memory manager updated to align to 16

- same as malloc() on linux 64b
- https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html
- some libraries (e.g., libwolfssl) expect such alignement for specific optimizations
- done even for 32b, expected rather low overhead in average, not expected many
  chunks under 16 bytes versus those over
Daniel-Constantin Mierla 1 год назад
Родитель
Сommit
a5bc1c0396
2 измененных файлов с 32 добавлено и 28 удалено
  1. 15 4
      src/core/mem/q_malloc.c
  2. 17 24
      src/core/mem/q_malloc.h

+ 15 - 4
src/core/mem/q_malloc.c

@@ -196,10 +196,20 @@ struct qm_block *qm_malloc_init(char *address, unsigned long size, int type)
 	unsigned long init_overhead;
 	int h;
 
-	/* make address and size multiple of 8*/
+	if((sizeof(struct qm_frag) % ROUNDTO != 0)
+			|| (sizeof(struct qm_frag_end) % ROUNDTO != 0)) {
+		LM_ERR("memory align constraints failure (%lu %% %lu = %lu ::: %lu %% "
+			   "%lu = %lu)\n",
+				sizeof(struct qm_frag), ROUNDTO,
+				sizeof(struct qm_frag) % ROUNDTO, sizeof(struct qm_frag_end),
+				ROUNDTO, sizeof(struct qm_frag_end) % ROUNDTO);
+		return 0;
+	}
+
+	/* make address and size multiple of ROUNDTO */
 	start = (char *)ROUNDUP((unsigned long)address);
-	LM_DBG("QM_OPTIMIZE=%lu, /ROUNDTO=%lu\n", QM_MALLOC_OPTIMIZE,
-			QM_MALLOC_OPTIMIZE / ROUNDTO);
+	LM_DBG("QM_OPTIMIZE=%lu, QM_OPTIMIZE/ROUNDTO=%lu, ROUNDTO=%lu\n",
+			QM_MALLOC_OPTIMIZE, QM_MALLOC_OPTIMIZE / ROUNDTO, ROUNDTO);
 	LM_DBG("QM_HASH_SIZE=%lu, qm_block size=%lu\n", QM_HASH_SIZE,
 			(unsigned long)sizeof(struct qm_block));
 	LM_DBG("qm_malloc_init(%p, %lu), start=%p\n", address, (unsigned long)size,
@@ -1061,7 +1071,8 @@ void qm_status_filter(void *qmp, str *fmatch, FILE *fp)
 										!= NULL))) {
 					fprintf(fp,
 							"unused fragm.: hash = %3d, fragment %p,"
-							" address %p size %lu, created from %s:%lu / %s()\n",
+							" address %p size %lu, created from %s:%lu / "
+							"%s()\n",
 							h, f, (char *)f + sizeof(struct qm_frag), f->size,
 							f->file, f->line, f->func);
 				}

+ 17 - 24
src/core/mem/q_malloc.h

@@ -33,33 +33,22 @@
 #define DBG_QM_MALLOC
 #endif
 
-/* defs*/
-#ifdef DBG_QM_MALLOC
-#if defined(__CPU_sparc64) || defined(__CPU_sparc)
-/* tricky, on sun in 32 bits mode long long must be 64 bits aligned
- * but long can be 32 bits aligned => malloc should return long long
- * aligned memory */
-#define ROUNDTO sizeof(long long)
-#else
-#define ROUNDTO \
-	sizeof(void *) /* minimum possible ROUNDTO
-										* ->heavy debugging*/
-#endif
-#else /* DBG_QM_MALLOC */
-#define ROUNDTO \
-	16UL /* size we round to, must be = 2^n  and also
-							* sizeof(qm_frag)+sizeof(qm_frag_end)
-							* must be multiple of ROUNDTO!
-							*/
-#endif
-#define MIN_FRAG_SIZE ROUNDTO
+/**
+ * ROUNDTO: size to round to, must be = 2^n
+ * - on sun in 32 bits mode long long must be 64 bits aligned but long
+ * can be 32 bits aligned => malloc should return multiple of long long
+ * aligned memory
+ * - malloc() on gnu/linux: multiple of 8 or 16 on 64-bit systems
+ * - for simplicity settle for 16 always
+ * - sizeof(qm_frag) and sizeof(qm_frag_end) must be multiple of ROUNDTO!
+ */
+#define ROUNDTO 16UL
 
+#define MIN_FRAG_SIZE ROUNDTO
 
 #define QM_MALLOC_OPTIMIZE_FACTOR 14UL /*used below */
+/* size to optimize for, (most allocs <= this size), must be 2^k */
 #define QM_MALLOC_OPTIMIZE ((unsigned long)(1UL << QM_MALLOC_OPTIMIZE_FACTOR))
-/* size to optimize for,
-									(most allocs <= this size),
-									must be 2^k */
 
 #define QM_HASH_SIZE                              \
 	((unsigned long)(QM_MALLOC_OPTIMIZE / ROUNDTO \
@@ -70,6 +59,7 @@
  *                            ROUNDTO from bucket to bucket
  * +1 .... end -  size = 2^k, big buckets */
 
+/* start of QM fragment - its size has to be multiple of ROUNDTO */
 struct qm_frag
 {
 	unsigned long size;
@@ -84,9 +74,11 @@ struct qm_frag
 	const char *mname;
 	unsigned long line;
 	unsigned long check;
+	unsigned long reserved1;
 #endif
 };
 
+/* end of QM fragment - its size has to be multiple of ROUNDTO */
 struct qm_frag_end
 {
 #ifdef DBG_QM_MALLOC
@@ -105,6 +97,7 @@ struct qm_frag_lnk
 	struct qm_frag head;
 	struct qm_frag_end tail;
 	unsigned long no;
+	unsigned long reserved1;
 };
 
 
@@ -115,7 +108,7 @@ struct qm_frag_lnk
 
 struct qm_block
 {
-	int type;				 /* type of memory */
+	long type;				 /* type of memory */
 	unsigned long size;		 /* total size */
 	unsigned long used;		 /* alloc'ed size*/
 	unsigned long real_used; /* used+malloc overhead*/