⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aix_5l_notes

📁 减少内存碎片的malloc分配函数
💻
字号:
[ From: Freddie M ]Here is a howto for dmalloc using AIX 5L and xlcFor xlc compiler:1. CC='xlc_r' ./configure --enable-threads2. settings.hifndef LOCK_THREADS#define LOCK_THREADS 1       <<<<<<<  should be 1#endif3. malloc.c changes , diff changes also includedNOTE :  all "malloc" entry points need to be changed to        have 2 "__" pre and post fix.        Example:        malloc     __malloc__        free       __free__malloc.c changes:#ifdef AIXextern int __multi_threaded;#include <malloc.h>#endif#if LOCK_THREADS/* * mutex lock the malloc library */static  void    lock_thread(void){  /* we only lock if the lock-on counter has reached 0 */#ifdef AIX  /* In AIX __mutli_threaded variable is 0 until a thread is created.   * It will never reset to 0 after that.   */  if (__multi_threaded) {#else  if (thread_lock_c == 0) {#endif#if HAVE_PTHREAD_MUTEX_LOCK    pthread_mutex_lock(&dmalloc_mutex);#endif  }}/* * mutex unlock the malloc library */static  void    unlock_thread(void){#ifdef AIX  /* In AIX __mutli_threaded variable is 0 until a thread is created.   * It will never reset to 0 after that.   */  if (__multi_threaded) {#if HAVE_PTHREAD_MUTEX_UNLOCK    pthread_mutex_unlock(&dmalloc_mutex);#endif /* ifdef AIX */  } ...#ifdef AIXvoid __malloc_init__(){        pthread_mutex_init(&dmalloc_mutex, THREAD_LOCK_INIT_VAL);}void __malloc_prefork_lock__(){    pthread_mutex_lock(&dmalloc_mutex);}void __malloc_postfork_unlock__(){    pthread_mutex_unlock(&dmalloc_mutex);}/* legacy function not needed */int __mallopt__ (int x, int y){}/* legacy function not needed */struct mallinfo __mallinfo__(){}/* optional - not really needed */void __malloc_once__(){}#endif---- here is the diff -----*** ../1/dmalloc-4.8.2/malloc.c Mon Feb 26 13:31:16 2001--- malloc.c    Thu Sep  5 11:26:24 2002****************** 61,66 ****--- 61,71 ----  #include <signal.h>  #endif+ #ifdef AIX+ extern int __multi_threaded;+ #include <malloc.h>+ #endif+  #define DMALLOC_DISABLE  #include "dmalloc.h"****************** 163,169 ****--- 168,181 ----  static        void    lock_thread(void)  {    /* we only lock if the lock-on counter has reached 0 */+ #ifdef AIX+   /* In AIX __mutli_threaded variable is 0 until a thread is created.+    * It will never reset to 0 after that.+    */+   if (__multi_threaded) {+ #else    if (thread_lock_c == 0) {+ #endif  #if HAVE_PTHREAD_MUTEX_LOCK      pthread_mutex_lock(&dmalloc_mutex);  #endif****************** 175,180 ****--- 187,202 ----   */  static        void    unlock_thread(void)  {+ #ifdef AIX+   /* In AIX __mutli_threaded variable is 0 until a thread is created.+    * It will never reset to 0 after that.+    */+   if (__multi_threaded) {+ #if HAVE_PTHREAD_MUTEX_UNLOCK+     pthread_mutex_unlock(&dmalloc_mutex);+ #endif+   }+ #else    /* if the lock-on counter has not reached 0 then count it down */    if (thread_lock_c > 0) {      thread_lock_c--;****************** 205,210 ****--- 227,233 ----      pthread_mutex_unlock(&dmalloc_mutex);  #endif    }+ #endif  }  #endif****************** 770,776 ****   * Returns 0L on error.   */  #undef malloc! DMALLOC_PNT   malloc(DMALLOC_SIZE size)  {    char        *file;--- 793,799 ----   * Returns 0L on error.   */  #undef malloc! DMALLOC_PNT   __malloc__(DMALLOC_SIZE size)  {    char        *file;****************** 785,791 ****   * Returns 0L on error.   */  #undef calloc! DMALLOC_PNT   calloc(DMALLOC_SIZE num_elements, DMALLOC_SIZE size)  {    DMALLOC_SIZE        len = num_elements * size;    char                *file;--- 808,814 ----   * Returns 0L on error.   */  #undef calloc! DMALLOC_PNT   __calloc__(DMALLOC_SIZE num_elements, DMALLOC_SIZE size)  {    DMALLOC_SIZE        len = num_elements * size;    char                *file;****************** 804,810 ****   * Returns 0L on error.   */  #undef realloc! DMALLOC_PNT   realloc(DMALLOC_PNT old_pnt, DMALLOC_SIZE new_size)  {    char        *file;--- 827,833 ----   * Returns 0L on error.   */  #undef realloc! DMALLOC_PNT   __realloc__(DMALLOC_PNT old_pnt, DMALLOC_SIZE new_size)  {    char        *file;****************** 901,907 ****   * is defined by your compiler.   */  #undef free! DMALLOC_FREE_RET      free(DMALLOC_PNT pnt)  {    char        *file;    int ret;--- 924,930 ----   * is defined by your compiler.   */  #undef free! DMALLOC_FREE_RET      __free__(DMALLOC_PNT pnt)  {    char        *file;    int ret;****************** 932,937 ****--- 955,994 ----    return ret;  #endif  }++ #ifdef AIX+ void __malloc_init__()+ {+       pthread_mutex_init(&dmalloc_mutex, THREAD_LOCK_INIT_VAL);+ }++ void __malloc_prefork_lock__()+ {+     pthread_mutex_lock(&dmalloc_mutex);+ }++ void __malloc_postfork_unlock__()+ {+     pthread_mutex_unlock(&dmalloc_mutex);+ }+++ /* legacy function not needed */+ int __mallopt__ (int x, int y)+ {+ }++ /* legacy function not needed */+ struct mallinfo __mallinfo__()+ {+ }++ /* optional - not really needed */+ void __malloc_once__()+ {+ }++ #endif  /******************************* utility calls *******************************/create the file  mem.exp with the following entries__malloc____free____realloc____calloc____mallopt____mallinfo____malloc_init____malloc_prefork_lock____malloc_postfork_unlock____malloc_once__Building a shared library for 32 and 64 bit use.Makefile  - 32 bit  .1. add -DAIX to DEFS2. change LIB_TH  . This just actually renames the module   built by the ld command.        LIB_TH  = mem32.o3. Add to CCFLAGS        CCFLAGS = -g -D_LARGE_THREADS4. change the $(LIB_TH) build stanza$(LIB_TH) : $(OBJS) $(THREAD_OBJS)        ld -b32 -m -o $@ $? -bE:mem.exp -bM:SRE -lpthreads -lcMakefile - 64 bit1. add -DAIX to DEFS2. change LIB_TH  . This just actually renames the module   built by the ld command.        LIB_TH  = mem64.o3. Add to CCFLAGS        CCFLAGS = -g  -q64 -D_LARGE_THREADS4. change the $(LIB_TH) build stanza$(LIB_TH) : $(OBJS) $(THREAD_OBJS)        ld -b64 -m -o $@ $? -bE:mem.exp -bM:SRE -lpthreads -lc5. If you care about making a library archive   Add -X64 to the ar command$(LIBRARY) : $(OBJS) $(NORMAL_OBJS)        ar -X64 cr $@ $?        ranlib $@Save mem32.o and mem64.o somewhere safe so that a make clean does notclean them out.The last step is to make an AIX shared lirary.	ar -X32_64 -r libmymem.a mem32.o mem64.oOn the AIX machine	export MALLOCTYPE=user:libmymem.a	export LIBPATH=<path to where libmymem.a lives>Define your dmalloc envTest the library outOther notes:In return.h , there is a macro called  #define GET_RET_ADDR(file)   file = DMALLOC_DEFAULT_FILEfor AIX when the calling address is not know, it will return an "unknown" in the log forunfreed memoryFor AIX you can do the following:typedef struct _frame{        struct _frame  *fr_next;        /* frame list */        int             fr_unused1;        long            fr_lr;          /* link register */}_frame_t;/* _frame_t *get_stkp(void); */#pragma mc_func get_stkp \{ \        "7c230b78" /* mr 3,1 */ \}#pragma reg_killed_by get_stkp gr3#define CALLER (((_frame_t *)get_stkp())->fr_next->fr_lr)#define CALLER3 (                                                             \                 ( ((_frame_t *)get_stkp())->fr_next == NULL )                \                 ? DMALLOC_DEFAULT_FILE :                                     \                   ( ((_frame_t *)get_stkp())->fr_next->fr_next == NULL )     \                   ? DMALLOC_DEFAULT_FILE : ((_frame_t *)get_stkp())->fr_next->fr_next->fr_next->fr_lr \                )#define CALLER2 (                                                             \                 ( ((_frame_t *)get_stkp())->fr_next == NULL )                \                 ? DMALLOC_DEFAULT_FILE :                                     \                   ( ((_frame_t *)get_stkp())->fr_next->fr_next == NULL )     \                   ? DMALLOC_DEFAULT_FILE : ((_frame_t *)get_stkp())->fr_next->fr_next->fr_lr \                )You will want to define CALLER2 because if you define CALLER1 you willjust get "malloc" as the return address. You really want to get thecaller that called malloc instead of __malloc__#define GET_RET_ADDR(file)  file = (char *) CALLER2or#define GET_RET_ADDR(file)  file = (char *) CALLER1Other notes (con't)1. If you are debugging a system command, say telnet,   this command may do an exec and not copy over the   LIBPATH where the damlloc library resides.   This results in the following error:USER DEFINED MALLOC ERROR:Unable to load user supplied object: "libmymem.a(mem32.o)", load() errno == 2   The solution is to place the library in /usr/lib   chmod 555   chown bin.bin

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -