Skip to content
Snippets Groups Projects
dlmalloc.src 102 KiB
Newer Older
  • Learn to ignore specific revisions
  • Wolfgang Denk's avatar
    Wolfgang Denk committed
            return 1;
          }
        }
      }
    }
    
    
    
    /*
      malloc_usable_size:
    
        This routine tells you how many bytes you can actually use in an
        allocated chunk, which may be more than you requested (although
        often not). You can use this many bytes without worrying about
        overwriting other allocated objects. Not a particularly great
        programming practice, but still sometimes useful.
    
    */
    
    #if __STD_C
    size_t malloc_usable_size(Void_t* mem)
    #else
    size_t malloc_usable_size(mem) Void_t* mem;
    #endif
    {
      mchunkptr p;
      if (mem == 0)
        return 0;
      else
      {
        p = mem2chunk(mem);
        if(!chunk_is_mmapped(p))
        {
          if (!inuse(p)) return 0;
          check_inuse_chunk(p);
          return chunksize(p) - SIZE_SZ;
        }
        return chunksize(p) - 2*SIZE_SZ;
      }
    }
    
    
    
    
    /* Utility to update current_mallinfo for malloc_stats and mallinfo() */
    
    static void malloc_update_mallinfo()
    {
      int i;
      mbinptr b;
      mchunkptr p;
    #if DEBUG
      mchunkptr q;
    #endif
    
      INTERNAL_SIZE_T avail = chunksize(top);
      int   navail = ((long)(avail) >= (long)MINSIZE)? 1 : 0;
    
      for (i = 1; i < NAV; ++i)
      {
        b = bin_at(i);
        for (p = last(b); p != b; p = p->bk)
        {
    #if DEBUG
          check_free_chunk(p);
          for (q = next_chunk(p);
               q < top && inuse(q) && (long)(chunksize(q)) >= (long)MINSIZE;
               q = next_chunk(q))
            check_inuse_chunk(q);
    #endif
          avail += chunksize(p);
          navail++;
        }
      }
    
      current_mallinfo.ordblks = navail;
      current_mallinfo.uordblks = sbrked_mem - avail;
      current_mallinfo.fordblks = avail;
      current_mallinfo.hblks = n_mmaps;
      current_mallinfo.hblkhd = mmapped_mem;
      current_mallinfo.keepcost = chunksize(top);
    
    }
    
    
    
    /*
    
      malloc_stats:
    
        Prints on stderr the amount of space obtain from the system (both
        via sbrk and mmap), the maximum amount (which may be more than
        current if malloc_trim and/or munmap got called), the maximum
        number of simultaneous mmap regions used, and the current number
        of bytes allocated via malloc (or realloc, etc) but not yet
        freed. (Note that this is the number of bytes allocated, not the
        number requested. It will be larger than the number requested
        because of alignment and bookkeeping overhead.)
    
    */
    
    void malloc_stats()
    {
      malloc_update_mallinfo();
      fprintf(stderr, "max system bytes = %10u\n",
              (unsigned int)(max_total_mem));
      fprintf(stderr, "system bytes     = %10u\n",
              (unsigned int)(sbrked_mem + mmapped_mem));
      fprintf(stderr, "in use bytes     = %10u\n",
              (unsigned int)(current_mallinfo.uordblks + mmapped_mem));
    #if HAVE_MMAP
      fprintf(stderr, "max mmap regions = %10u\n",
              (unsigned int)max_n_mmaps);
    #endif
    }
    
    /*
      mallinfo returns a copy of updated current mallinfo.
    */
    
    struct mallinfo mALLINFo()
    {
      malloc_update_mallinfo();
      return current_mallinfo;
    }
    
    
    
    
    /*
      mallopt:
    
        mallopt is the general SVID/XPG interface to tunable parameters.
        The format is to provide a (parameter-number, parameter-value) pair.
        mallopt then sets the corresponding parameter to the argument
        value if it can (i.e., so long as the value is meaningful),
        and returns 1 if successful else 0.
    
        See descriptions of tunable parameters above.
    
    */
    
    #if __STD_C
    int mALLOPt(int param_number, int value)
    #else
    int mALLOPt(param_number, value) int param_number; int value;
    #endif
    {
      switch(param_number)
      {
        case M_TRIM_THRESHOLD:
          trim_threshold = value; return 1;
        case M_TOP_PAD:
          top_pad = value; return 1;
        case M_MMAP_THRESHOLD:
          mmap_threshold = value; return 1;
        case M_MMAP_MAX:
    #if HAVE_MMAP
          n_mmaps_max = value; return 1;
    #else
          if (value != 0) return 0; else  n_mmaps_max = value; return 1;
    #endif
    
        default:
          return 0;
      }
    }
    
    /*
    
    History:
    
        V2.6.6 Sun Dec  5 07:42:19 1999  Doug Lea  (dl at gee)
          * return null for negative arguments
          * Added Several WIN32 cleanups from Martin C. Fong <mcfong@yahoo.com>
             * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h'
              (e.g. WIN32 platforms)
             * Cleanup up header file inclusion for WIN32 platforms
             * Cleanup code to avoid Microsoft Visual C++ compiler complaints
             * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing
               memory allocation routines
             * Set 'malloc_getpagesize' for WIN32 platforms (needs more work)
             * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to
    	   usage of 'assert' in non-WIN32 code
             * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to
               avoid infinite loop
          * Always call 'fREe()' rather than 'free()'
    
        V2.6.5 Wed Jun 17 15:57:31 1998  Doug Lea  (dl at gee)
          * Fixed ordering problem with boundary-stamping
    
        V2.6.3 Sun May 19 08:17:58 1996  Doug Lea  (dl at gee)
          * Added pvalloc, as recommended by H.J. Liu
          * Added 64bit pointer support mainly from Wolfram Gloger
          * Added anonymously donated WIN32 sbrk emulation
          * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen
          * malloc_extend_top: fix mask error that caused wastage after
            foreign sbrks
          * Add linux mremap support code from HJ Liu
    
        V2.6.2 Tue Dec  5 06:52:55 1995  Doug Lea  (dl at gee)
          * Integrated most documentation with the code.
          * Add support for mmap, with help from
            Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
          * Use last_remainder in more cases.
          * Pack bins using idea from  colin@nyx10.cs.du.edu
          * Use ordered bins instead of best-fit threshhold
          * Eliminate block-local decls to simplify tracing and debugging.
          * Support another case of realloc via move into top
          * Fix error occuring when initial sbrk_base not word-aligned.
          * Rely on page size for units instead of SBRK_UNIT to
            avoid surprises about sbrk alignment conventions.
          * Add mallinfo, mallopt. Thanks to Raymond Nijssen
            (raymond@es.ele.tue.nl) for the suggestion.
          * Add `pad' argument to malloc_trim and top_pad mallopt parameter.
          * More precautions for cases where other routines call sbrk,
            courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de).
          * Added macros etc., allowing use in linux libc from
            H.J. Lu (hjl@gnu.ai.mit.edu)
          * Inverted this history list
    
        V2.6.1 Sat Dec  2 14:10:57 1995  Doug Lea  (dl at gee)
          * Re-tuned and fixed to behave more nicely with V2.6.0 changes.
          * Removed all preallocation code since under current scheme
            the work required to undo bad preallocations exceeds
            the work saved in good cases for most test programs.
          * No longer use return list or unconsolidated bins since
            no scheme using them consistently outperforms those that don't
            given above changes.
          * Use best fit for very large chunks to prevent some worst-cases.
          * Added some support for debugging
    
        V2.6.0 Sat Nov  4 07:05:23 1995  Doug Lea  (dl at gee)
          * Removed footers when chunks are in use. Thanks to
            Paul Wilson (wilson@cs.texas.edu) for the suggestion.
    
        V2.5.4 Wed Nov  1 07:54:51 1995  Doug Lea  (dl at gee)
          * Added malloc_trim, with help from Wolfram Gloger
            (wmglo@Dent.MED.Uni-Muenchen.DE).
    
        V2.5.3 Tue Apr 26 10:16:01 1994  Doug Lea  (dl at g)
    
        V2.5.2 Tue Apr  5 16:20:40 1994  Doug Lea  (dl at g)
          * realloc: try to expand in both directions
          * malloc: swap order of clean-bin strategy;
          * realloc: only conditionally expand backwards
          * Try not to scavenge used bins
          * Use bin counts as a guide to preallocation
          * Occasionally bin return list chunks in first scan
          * Add a few optimizations from colin@nyx10.cs.du.edu
    
        V2.5.1 Sat Aug 14 15:40:43 1993  Doug Lea  (dl at g)
          * faster bin computation & slightly different binning
          * merged all consolidations to one part of malloc proper
             (eliminating old malloc_find_space & malloc_clean_bin)
          * Scan 2 returns chunks (not just 1)
          * Propagate failure in realloc if malloc returns 0
          * Add stuff to allow compilation on non-ANSI compilers
              from kpv@research.att.com
    
        V2.5 Sat Aug  7 07:41:59 1993  Doug Lea  (dl at g.oswego.edu)
          * removed potential for odd address access in prev_chunk
          * removed dependency on getpagesize.h
          * misc cosmetics and a bit more internal documentation
          * anticosmetics: mangled names in macros to evade debugger strangeness
          * tested on sparc, hp-700, dec-mips, rs6000
              with gcc & native cc (hp, dec only) allowing
              Detlefs & Zorn comparison study (in SIGPLAN Notices.)
    
        Trial version Fri Aug 28 13:14:29 1992  Doug Lea  (dl at g.oswego.edu)
          * Based loosely on libg++-1.2X malloc. (It retains some of the overall
             structure of old version,  but most details differ.)
    
    */