浏览代码

Add macros to abstract the malloc header format a bit.

Miles Bader 23 年之前
父节点
当前提交
ae96781efd
共有 1 个文件被更改,包括 22 次插入1 次删除
  1. 22 1
      libc/stdlib/malloc/malloc.h

+ 22 - 1
libc/stdlib/malloc/malloc.h

@@ -7,7 +7,7 @@
  * This file is subject to the terms and conditions of the GNU Lesser
  * General Public License.  See the file COPYING.LIB in the main
  * directory of this archive for more details.
- * 
+ *
  * Written by Miles Bader <miles@gnu.org>
  */
 
@@ -42,6 +42,27 @@
 #endif
 
 
+/* The size of a malloc allocation is stored in a size_t word
+   MALLOC_ALIGNMENT bytes prior to the start address of the allocation:
+
+     +--------+---------+-------------------+
+     | SIZE   |(unused) | allocation  ...   |
+     +--------+---------+-------------------+
+     ^ BASE             ^ ADDR
+     ^ ADDR - MALLOC_ALIGN
+*/
+
+/* Return base-address of a malloc allocation, given the user address.  */
+#define MALLOC_BASE(addr)	((void *)((char *)addr - MALLOC_ALIGNMENT))
+/* Return the size of a malloc allocation, given the user address.  */
+#define MALLOC_SIZE(addr)	(*(size_t *)MALLOC_BASE(addr))
+
+/* Return the user address of a malloc allocation, given the base address.  */
+#define MALLOC_ADDR(base)	((void *)((char *)base + MALLOC_ALIGNMENT))
+/* Sets the size of a malloc allocation, given the base address.  */
+#define MALLOC_SET_SIZE(base, size)	(*(size_t *)(base) = (size))
+
+
 #ifdef __UCLIBC_HAS_THREADS__
 
 # include <pthread.h>