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

📄 vxmalloc.c

📁 This version of malloc for VxWorks contains two different algorithms. One is the BSD based Kingsley
💻 C
📖 第 1 页 / 共 5 页
字号:
      * 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.)*/intdl_assert(char *msg){        logMsg(*msg,0,0,0,0,0,0);        taskSuspend(0);}intdl_malloc_init(char *cp, int sz){        mchunkptr p;         if (dl_malloc_initialized > 0) {		dbg_printf("dl_malloc_init: already initialized\n");		return -1;        }                dl_malloc_bottom = cp;        dl_malloc_brk = cp;		/* brk pointer currently at bottom */        dl_malloc_top = cp + sz;                dl_malloc_initialized ++;        /*p = (mchunkptr ) cp;        p ->size = sz | PREV_INUSE;        */        semMInit(dl_mem_sid, SEM_Q_PRIORITY);                dbg_printf("dl malloc initialized!\n");                return 1;}char *sbrk(int sz){        char *cp;                if (dl_malloc_initialized == 0) {		dbg_printf("sbrk: bsd_malloc package not yet intialized\n");		return (char *)-1;        }        cp = dl_malloc_brk;        if (sz == 0)		return cp;                if (dl_malloc_brk + sz >= dl_malloc_top) {		dbg_printf("sbrk: can't	alloc %d bytes\n", sz);		return (char *)-1;        }                sz = (sz + 3) & ~3;		/* align sz to 4 bytes */        dl_malloc_brk += sz;        return cp;}/*   malloc_chunk details:    (The following includes lightly edited explanations by Colin Plumb.)    Chunks of memory are maintained using a `boundary tag' method as    described in e.g., Knuth or Standish.  (See the paper by Paul    Wilson ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a    survey of such techniques.)	 Sizes of free chunks are stored both    in the front of each chunk and at the end.  This makes    consolidating fragmented chunks into bigger chunks very fast.  The    size fields also hold bits representing whether chunks are free or    in use.    An allocated chunk looks like this:      chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+	    |		  Size of previous chunk, if allocated		  | |	    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+	    |		  Size of chunk, in bytes			  |P|      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+	    |		  User data starts here...			    .	    .								    .	    .		  (malloc_usable_space() bytes)			    .	    .								    |nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+	    |		  Size of chunk					    |	    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    Where "chunk" is the front of the chunk for the purpose of most of    the malloc code, but "mem" is the pointer that is returned to the    user.  "Nextchunk" is the beginning of the next contiguous chunk.    Chunks always begin on even word boundries, so the mem portion    (which is returned to the user) is also on an even word boundary, and    thus double-word aligned.    Free chunks are stored in circular doubly-linked lists, and look like this:    chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+	    |		  Size of previous chunk			    |	    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    `head:' |		  Size of chunk, in bytes			  |P|      mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+	    |		  Forward pointer to next chunk	in list		    |	    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+	    |		  Back pointer to previous chunk in list	    |	    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+	    |		  Unused space (may be 0 bytes long)		    .	    .								    .	    .								    |nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    `foot:' |		  Size of chunk, in bytes			    |	    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+    The P (PREV_INUSE) bit, stored in the unused low-order bit of the    chunk size (which is always a multiple of two words), is an in-use    bit for the *previous* chunk.  If that bit is *clear*, then the    word before the current chunk size contains the previous chunk    size, and can be used to find the front of the previous chunk.    (The very first chunk allocated always has this bit set,    preventing access to non-existent (or non-owned) memory.)    Note that the `foot' of the current chunk is actually represented    as the prev_size of the NEXT chunk. (This makes it easier to    deal with alignments etc).    The two exceptions to all this are      1. The special chunk `top', which doesn't bother using the         trailing size field since there is no        next contiguous chunk that would have to index off it. (After        initialization, `top' is forced to always exist.  If it would        become less than MINSIZE bytes long, it is replenished via        malloc_extend_top.)     2. Chunks allocated via mmap, which have the second-lowest-order        bit (IS_MMAPPED) set in their size fields.  Because they are        never merged or traversed from any other chunk, they have no        foot size or inuse information.    Available chunks are kept in any of several places (all declared below):    * `av': An array of chunks serving as bin headers for consolidated       chunks. Each bin is doubly linked.  The bins are approximately       proportionally (log) spaced.  There are a lot of these bins       (128). This may look excessive, but works very well in       practice.  All procedures maintain the invariant that no       consolidated chunk physically borders another one. Chunks in       bins are kept in size order, with ties going to the       approximately least recently used chunk.       The chunks in each bin are maintained in decreasing sorted order by       size.  This is irrelevant for the small bins, which all contain       the same-sized chunks, but facilitates best-fit allocation for       larger chunks. (These lists are just sequential. Keeping them in       order almost never requires enough traversal to warrant using       fancier ordered data structures.)  Chunks of the same size are       linked with the most recently freed at the front, and allocations       are taken from the back.	 This results in LRU or FIFO allocation       order, which tends to give each chunk an equal opportunity to be       consolidated with adjacent freed chunks, resulting in larger free       chunks and less fragmentation.     * `top': The top-most available chunk (i.e., the one bordering the       end of available memory) is treated specially. It is never       included in any bin, is used only if no other chunk is       available, and is released back to the system if it is very       large (see M_TRIM_THRESHOLD).    * `last_remainder': A bin holding only the remainder of the       most recently split (non-top) chunk. This bin is checked       before other non-fitting chunks, so as to provide better       locality for runs of sequentially allocated chunks.     *  Implicitly, through the host system's memory mapping tables.       If supported, requests greater than a threshold are usually        serviced via calls to mmap, and then later released via munmap.*//*  sizes, alignments */#define SIZE_SZ		       (sizeof(INTERNAL_SIZE_T))#define MALLOC_ALIGNMENT       (16)#define MALLOC_ALIGN_MASK      (MALLOC_ALIGNMENT - 1)/*#define MINSIZE		 (sizeof(struct	malloc_chunk))*/#define MINSIZE		       32/* conversion from malloc headers to user pointers, and back */#define chunk2mem(p)   ((Void_t*)((char*)(p) + 2*SIZE_SZ))#define mem2chunk(mem) ((mchunkptr)((char*)(mem) - 2*SIZE_SZ))/* pad request bytes into a usable size */#define request2size(req) \ (((long)((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) < \  (long)(MINSIZE + MALLOC_ALIGN_MASK)) ? MINSIZE : \   (((req) + (SIZE_SZ + MALLOC_ALIGN_MASK)) & ~(MALLOC_ALIGN_MASK)))/* Check if m has acceptable alignment */#define aligned_OK(m)	 (((unsigned long)((m))	& (MALLOC_ALIGN_MASK)) == 0)/*   Physical chunk operations  *//* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */#define PREV_INUSE 0x1 /* Bits to mask off when extracting size */#define SIZE_BITS (PREV_INUSE)/* Ptr to next physical malloc_chunk. */#define next_chunk(p) ((mchunkptr)( ((char*)(p)) + ((p)->size & ~PREV_INUSE) ))/* Ptr to previous physical malloc_chunk */#define prev_chunk(p)\   ((mchunkptr)( ((char*)(p)) - ((p)->prev_size) ))/* Treat space at ptr + offset as a chunk */#define chunk_at_offset(p, s)  ((mchunkptr)(((char*)(p)) + (s)))/*   Dealing with use bits *//* extract p's inuse bit */#define inuse(p)\((((mchunkptr)(((char*)(p))+((p)->size & ~PREV_INUSE)))->size) & PREV_INUSE)/* extract inuse bit of previous chunk */#define prev_inuse(p)  ((p)->size & PREV_INUSE)/* set/clear chunk as in use without otherwise disturbing */#define set_inuse(p)\((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size |= PREV_INUSE#define clear_inuse(p)\((mchunkptr)(((char*)(p)) + ((p)->size & ~PREV_INUSE)))->size &= ~(PREV_INUSE)/* check/set/clear inuse bits in known places */#define inuse_bit_at_offset(p, s)\ (((mchunkptr)(((char*)(p)) + (s)))->size & PREV_INUSE)#define set_inuse_bit_at_offset(p, s)\ (((mchunkptr)(((char*)(p)) + (s)))->size |= PREV_INUSE)#define clear_inuse_bit_at_offset(p, s)\ (((mchunkptr)(((char*)(p)) + (s)))->size &= ~(PREV_INUSE))/*   Dealing with size fields *//* Get size, ignoring use bits */#define chunksize(p)	      ((p)->size & ~(SIZE_BITS))/* Set size at head, without disturbing its use bit */#define set_head_size(p, s)   ((p)->size = (((p)->size & PREV_INUSE) | (s)))/* Set size/use ignoring previous bits in header */#define set_head(p, s)	      ((p)->size = (s))/* Set size at footer (only when chunk is not in use) */#define set_foot(p, s)	 (((mchunkptr)((char*)(p) + (s)))->prev_size = (s))/*   Bins    The bins, `av_' are an array of pairs of pointers serving as the    heads of (initially empty) doubly-linked lists of chunks, laid out    in a way so that each pair can be treated as if it were in a    malloc_chunk. (This way, the fd/bk offsets for linking bin heads    and chunks are the same).    Bins for sizes < 512 bytes contain chunks of all the same size, spaced    8 bytes apart. Larger bins are approximately logarithmically    spaced. (See the table below.) The `av_' array is never mentioned    directly in the code, but instead via bin access macros.    Bin layout:    64 bins of size	  8    32 bins of size	 64    16 bins of size     512     8 bins of size    4096     4 bins of size   32768     2 bins of size  262144     1 bin  of size what's left    There is actually a little bit of slop in the numbers in bin_index    for the sake of speed. This makes no difference elsewhere.    The special chunks `top' and `last_remainder' get their own bins,    (this is implemented via yet more trickery with the av_ array),    although `top' is never properly linked to its bin since it is    always handled specially.*/#define NAV		128   /* number	of bins */typedef struct malloc_chunk* mbinptr;/* access macros */

⌨️ 快捷键说明

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