(linenum→info "unix/slp.c:2238")

emacs/22.1/src/gmalloc.c

    1: /* This file is no longer automatically generated from libc.  */
    2: 
    3: #define _MALLOC_INTERNAL
    4: #ifdef HAVE_GTK_AND_PTHREAD
    5: #define USE_PTHREAD
    6: #endif
    7: 
    8: /* The malloc headers and source files from the C library follow here.  */
    9: 
   10: /* Declarations for `malloc' and friends.
   11:    Copyright (C) 1990, 1991, 1992, 1993, 1995, 1996, 1999, 2002, 2003, 2004,
   12:                  2005, 2006, 2007 Free Software Foundation, Inc.
   13:                   Written May 1989 by Mike Haertel.
   14: 
   15: This library is free software; you can redistribute it and/or
   16: modify it under the terms of the GNU General Public License as
   17: published by the Free Software Foundation; either version 2 of the
   18: License, or (at your option) any later version.
   19: 
   20: This library is distributed in the hope that it will be useful,
   21: but WITHOUT ANY WARRANTY; without even the implied warranty of
   22: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   23: General Public License for more details.
   24: 
   25: You should have received a copy of the GNU General Public
   26: License along with this library; see the file COPYING.  If
   27: not, write to the Free Software Foundation, Inc., 51 Franklin Street,
   28: Fifth Floor, Boston, MA 02110-1301, USA.
   29: 
   30:    The author may be reached (Email) at the address mike@ai.mit.edu,
   31:    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
   32: 
   33: #ifndef _MALLOC_H
   34: 
   35: #define _MALLOC_H       1
   36: 
   37: #ifdef _MALLOC_INTERNAL
   38: 
   39: #ifdef  HAVE_CONFIG_H
   40: #include <config.h>
   41: #endif
   42: 
   43: #if ((defined __cplusplus || (defined (__STDC__) && __STDC__) \
   44:       || defined STDC_HEADERS || defined PROTOTYPES) \
   45:      && ! defined (BROKEN_PROTOTYPES))
   46: #undef  PP
   47: #define PP(args)        args
   48: #undef  __ptr_t
   49: #define __ptr_t         void *
   50: #else /* Not C++ or ANSI C.  */
   51: #undef  PP
   52: #define PP(args)        ()
   53: #undef  __ptr_t
   54: #define __ptr_t         char *
   55: #endif /* C++ or ANSI C.  */
   56: 
   57: #if     defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
   58: #include <string.h>
   59: #else
   60: #ifndef memset
   61: #define memset(s, zero, n)      bzero ((s), (n))
   62: #endif
   63: #ifndef memcpy
   64: #define memcpy(d, s, n)         bcopy ((s), (d), (n))
   65: #endif
   66: #endif
   67: 
   68: #ifdef HAVE_LIMITS_H
   69: #include <limits.h>
   70: #endif
   71: #ifndef CHAR_BIT
   72: #define CHAR_BIT        8
   73: #endif
   74: 
   75: #ifdef  HAVE_UNISTD_H
   76: #include <unistd.h>
   77: #endif
   78: 
   79: #ifdef USE_PTHREAD
   80: #include <pthread.h>
   81: #endif
   82: 
   83: #endif  /* _MALLOC_INTERNAL.  */
   84: 
   85: 
   86: #ifdef  __cplusplus
   87: extern "C"
   88: {
   89: #endif
   90: 
   91: #ifdef STDC_HEADERS
   92: #include <stddef.h>
   93: #define __malloc_size_t         size_t
   94: #define __malloc_ptrdiff_t      ptrdiff_t
   95: #else
   96: #ifdef __GNUC__
   97: #include <stddef.h>
   98: #ifdef __SIZE_TYPE__
   99: #define __malloc_size_t         __SIZE_TYPE__
  100: #endif
  101: #endif
  102: #ifndef __malloc_size_t
  103: #define __malloc_size_t         unsigned int
  104: #endif
  105: #define __malloc_ptrdiff_t      int
  106: #endif
  107: 
  108: #ifndef NULL
  109: #define NULL    0
  110: #endif
  111: 
  112: #ifndef FREE_RETURN_TYPE
  113: #define FREE_RETURN_TYPE void
  114: #endif
  115: 
  116: 
  117: /* Allocate SIZE bytes of memory.  */
  118: extern __ptr_t malloc PP ((__malloc_size_t __size));
  119: /* Re-allocate the previously allocated block
  120:    in __ptr_t, making the new block SIZE bytes long.  */
  121: extern __ptr_t realloc PP ((__ptr_t __ptr, __malloc_size_t __size));
  122: /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
  123: extern __ptr_t calloc PP ((__malloc_size_t __nmemb, __malloc_size_t __size));
  124: /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
  125: extern FREE_RETURN_TYPE free PP ((__ptr_t __ptr));
  126: 
  127: /* Allocate SIZE bytes allocated to ALIGNMENT bytes.  */
  128: #if ! (defined (_MALLOC_INTERNAL) && __DJGPP__ - 0 == 1) /* Avoid conflict.  */
  129: extern __ptr_t memalign PP ((__malloc_size_t __alignment,
  130:                              __malloc_size_t __size));
  131: #endif
  132: 
  133: /* Allocate SIZE bytes on a page boundary.  */
  134: #if ! (defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC))
  135: extern __ptr_t valloc PP ((__malloc_size_t __size));
  136: #endif
  137: 
  138: 
  139: #ifdef _MALLOC_INTERNAL
  140: 
  141: /* The allocator divides the heap into blocks of fixed size; large
  142:    requests receive one or more whole blocks, and small requests
  143:    receive a fragment of a block.  Fragment sizes are powers of two,
  144:    and all fragments of a block are the same size.  When all the
  145:    fragments in a block have been freed, the block itself is freed.  */
  146: #define INT_BIT         (CHAR_BIT * sizeof(int))
  147: #define BLOCKLOG        (INT_BIT > 16 ? 12 : 9)
  148: #define BLOCKSIZE       (1 << BLOCKLOG)
  149: #define BLOCKIFY(SIZE)  (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  150: 
  151: /* Determine the amount of memory spanned by the initial heap table
  152:    (not an absolute limit).  */
  153: #define HEAP            (INT_BIT > 16 ? 4194304 : 65536)
  154: 
  155: /* Number of contiguous free blocks allowed to build up at the end of
  156:    memory before they will be returned to the system.  */
  157: #define FINAL_FREE_BLOCKS       8
  158: 
  159: /* Data structure giving per-block information.  */
  160: typedef union
  161:   {
  162:     /* Heap information for a busy block.  */
  163:     struct
  164:       {
  165:         /* Zero for a large (multiblock) object, or positive giving the
  166:            logarithm to the base two of the fragment size.  */
  167:         int type;
  168:         union
  169:           {
  170:             struct
  171:               {
  172:                 __malloc_size_t nfree; /* Free frags in a fragmented block.  */
  173:                 __malloc_size_t first; /* First free fragment of the block.  */
  174:               } frag;
  175:             /* For a large object, in its first block, this has the number
  176:                of blocks in the object.  In the other blocks, this has a
  177:                negative number which says how far back the first block is.  */
  178:             __malloc_ptrdiff_t size;
  179:           } info;
  180:       } busy;
  181:     /* Heap information for a free block
  182:        (that may be the first of a free cluster).  */
  183:     struct
  184:       {
  185:         __malloc_size_t size;  /* Size (in blocks) of a free cluster.  */
  186:         __malloc_size_t next;  /* Index of next free cluster.  */
  187:         __malloc_size_t prev;  /* Index of previous free cluster.  */
  188:       } free;
  189:   } malloc_info;
  190: 
  191: /* Pointer to first block of the heap.  */
  192: extern char *_heapbase;
  193: 
  194: /* Table indexed by block number giving per-block information.  */
  195: extern malloc_info *_heapinfo;
  196: 
  197: /* Address to block number and vice versa.  */
  198: #define BLOCK(A)        (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  199: #define ADDRESS(B)      ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
  200: 
  201: /* Current search index for the heap table.  */
  202: extern __malloc_size_t _heapindex;
  203: 
  204: /* Limit of valid info table indices.  */
  205: extern __malloc_size_t _heaplimit;
  206: 
  207: /* Doubly linked lists of free fragments.  */
  208: struct list
  209:   {
  210:     struct list *next;
  211:     struct list *prev;
  212:   };
  213: 
  214: /* Free list headers for each fragment size.  */
  215: extern struct list _fraghead[];
  216: 
  217: /* List of blocks allocated with `memalign' (or `valloc').  */
  218: struct alignlist
  219:   {
  220:     struct alignlist *next;
  221:     __ptr_t aligned;            /* The address that memaligned returned.  */
  222:     __ptr_t exact;              /* The address that malloc returned.  */
  223:   };
  224: extern struct alignlist *_aligned_blocks;
  225: 
  226: /* Instrumentation.  */
  227: extern __malloc_size_t _chunks_used;
  228: extern __malloc_size_t _bytes_used;
  229: extern __malloc_size_t _chunks_free;
  230: extern __malloc_size_t _bytes_free;
  231: 
  232: /* Internal versions of `malloc', `realloc', and `free'
  233:    used when these functions need to call each other.
  234:    They are the same but don't call the hooks.  */
  235: extern __ptr_t _malloc_internal PP ((__malloc_size_t __size));
  236: extern __ptr_t _realloc_internal PP ((__ptr_t __ptr, __malloc_size_t __size));
  237: extern void _free_internal PP ((__ptr_t __ptr));
  238: 
  239: #ifdef USE_PTHREAD
  240: extern pthread_mutex_t _malloc_mutex;
  241: #define LOCK()     pthread_mutex_lock (&_malloc_mutex)
  242: #define UNLOCK()   pthread_mutex_unlock (&_malloc_mutex)
  243: #else
  244: #define LOCK()
  245: #define UNLOCK()
  246: #endif
  247: 
  248: #endif /* _MALLOC_INTERNAL.  */
  249: 
  250: /* Given an address in the middle of a malloc'd object,
  251:    return the address of the beginning of the object.  */
  252: extern __ptr_t malloc_find_object_address PP ((__ptr_t __ptr));
  253: 
  254: /* Underlying allocation function; successive calls should
  255:    return contiguous pieces of memory.  */
  256: extern __ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size));
  257: 
  258: /* Default value of `__morecore'.  */
  259: extern __ptr_t __default_morecore PP ((__malloc_ptrdiff_t __size));
  260: 
  261: /* If not NULL, this function is called after each time
  262:    `__morecore' is called to increase the data size.  */
  263: extern void (*__after_morecore_hook) PP ((void));
  264: 
  265: /* Number of extra blocks to get each time we ask for more core.
  266:    This reduces the frequency of calling `(*__morecore)'.  */
  267: extern __malloc_size_t __malloc_extra_blocks;
  268: 
  269: /* Nonzero if `malloc' has been called and done its initialization.  */
  270: extern int __malloc_initialized;
  271: /* Function called to initialize malloc data structures.  */
  272: extern int __malloc_initialize PP ((void));
  273: 
  274: /* Hooks for debugging versions.  */
  275: extern void (*__malloc_initialize_hook) PP ((void));
  276: extern void (*__free_hook) PP ((__ptr_t __ptr));
  277: extern __ptr_t (*__malloc_hook) PP ((__malloc_size_t __size));
  278: extern __ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size));
  279: extern __ptr_t (*__memalign_hook) PP ((__malloc_size_t __size,
  280:                                        __malloc_size_t __alignment));
  281: 
  282: /* Return values for `mprobe': these are the kinds of inconsistencies that
  283:    `mcheck' enables detection of.  */
  284: enum mcheck_status
  285:   {
  286:     MCHECK_DISABLED = -1,       /* Consistency checking is not turned on.  */
  287:     MCHECK_OK,                  /* Block is fine.  */
  288:     MCHECK_FREE,                /* Block freed twice.  */
  289:     MCHECK_HEAD,                /* Memory before the block was clobbered.  */
  290:     MCHECK_TAIL                 /* Memory after the block was clobbered.  */
  291:   };
  292: 
  293: /* Activate a standard collection of debugging hooks.  This must be called
  294:    before `malloc' is ever called.  ABORTFUNC is called with an error code
  295:    (see enum above) when an inconsistency is detected.  If ABORTFUNC is
  296:    null, the standard function prints on stderr and then calls `abort'.  */
  297: extern int mcheck PP ((void (*__abortfunc) PP ((enum mcheck_status))));
  298: 
  299: /* Check for aberrations in a particular malloc'd block.  You must have
  300:    called `mcheck' already.  These are the same checks that `mcheck' does
  301:    when you free or reallocate a block.  */
  302: extern enum mcheck_status mprobe PP ((__ptr_t __ptr));
  303: 
  304: /* Activate a standard collection of tracing hooks.  */
  305: extern void mtrace PP ((void));
  306: extern void muntrace PP ((void));
  307: 
  308: /* Statistics available to the user.  */
  309: struct mstats
  310:   {
  311:     __malloc_size_t bytes_total; /* Total size of the heap. */
  312:     __malloc_size_t chunks_used; /* Chunks allocated by the user. */
  313:     __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */
  314:     __malloc_size_t chunks_free; /* Chunks in the free list. */
  315:     __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */
  316:   };
  317: 
  318: /* Pick up the current statistics. */
  319: extern struct mstats mstats PP ((void));
  320: 
  321: /* Call WARNFUN with a warning message when memory usage is high.  */
  322: extern void memory_warnings PP ((__ptr_t __start,
  323:                                  void (*__warnfun) PP ((const char *))));
  324: 
  325: 
  326: /* Relocating allocator.  */
  327: 
  328: /* Allocate SIZE bytes, and store the address in *HANDLEPTR.  */
  329: extern __ptr_t r_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size));
  330: 
  331: /* Free the storage allocated in HANDLEPTR.  */
  332: extern void r_alloc_free PP ((__ptr_t *__handleptr));
  333: 
  334: /* Adjust the block at HANDLEPTR to be SIZE bytes long.  */
  335: extern __ptr_t r_re_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size));
  336: 
  337: 
  338: #ifdef  __cplusplus
  339: }
  340: #endif
  341: 
  342: #endif /* malloc.h  */
  343: /* Memory allocator `malloc'.
  344:    Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  345:                   Written May 1989 by Mike Haertel.
  346: 
  347: This library is free software; you can redistribute it and/or
  348: modify it under the terms of the GNU General Public License as
  349: published by the Free Software Foundation; either version 2 of the
  350: License, or (at your option) any later version.
  351: 
  352: This library is distributed in the hope that it will be useful,
  353: but WITHOUT ANY WARRANTY; without even the implied warranty of
  354: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  355: General Public License for more details.
  356: 
  357: You should have received a copy of the GNU General Public
  358: License along with this library; see the file COPYING.  If
  359: not, write to the Free Software Foundation, Inc., 51 Franklin Street,
  360: Fifth Floor, Boston, MA 02110-1301, USA.
  361: 
  362:    The author may be reached (Email) at the address mike@ai.mit.edu,
  363:    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  364: 
  365: #ifndef _MALLOC_INTERNAL
  366: #define _MALLOC_INTERNAL
  367: #include <malloc.h>
  368: #endif
  369: #include <errno.h>
  370: 
  371: /* How to really get more memory.  */
  372: #if defined(CYGWIN)
  373: extern __ptr_t bss_sbrk PP ((ptrdiff_t __size));
  374: extern int bss_sbrk_did_unexec;
  375: #endif
  376: __ptr_t (*__morecore) PP ((ptrdiff_t __size)) = __default_morecore;
  377: 
  378: /* Debugging hook for `malloc'.  */
  379: __ptr_t (*__malloc_hook) PP ((__malloc_size_t __size));
  380: 
  381: /* Pointer to the base of the first block.  */
  382: char *_heapbase;
  383: 
  384: /* Block information table.  Allocated with align/__free (not malloc/free).  */
  385: malloc_info *_heapinfo;
  386: 
  387: /* Number of info entries.  */
  388: static __malloc_size_t heapsize;
  389: 
  390: /* Search index in the info table.  */
  391: __malloc_size_t _heapindex;
  392: 
  393: /* Limit of valid info table indices.  */
  394: __malloc_size_t _heaplimit;
  395: 
  396: /* Free lists for each fragment size.  */
  397: struct list _fraghead[BLOCKLOG];
  398: 
  399: /* Instrumentation.  */
  400: __malloc_size_t _chunks_used;
  401: __malloc_size_t _bytes_used;
  402: __malloc_size_t _chunks_free;
  403: __malloc_size_t _bytes_free;
  404: 
  405: /* Are you experienced?  */
  406: int __malloc_initialized;
  407: 
  408: __malloc_size_t __malloc_extra_blocks;
  409: 
  410: void (*__malloc_initialize_hook) PP ((void));
  411: void (*__after_morecore_hook) PP ((void));
  412: 
  413: #if defined GC_MALLOC_CHECK && defined GC_PROTECT_MALLOC_STATE
  414: 
  415: /* Some code for hunting a bug writing into _heapinfo.
  416: 
  417:    Call this macro with argument PROT non-zero to protect internal
  418:    malloc state against writing to it, call it with a zero argument to
  419:    make it readable and writable.
  420: 
  421:    Note that this only works if BLOCKSIZE == page size, which is
  422:    the case on the i386.  */
  423: 
  424: #include <sys/types.h>
  425: #include <sys/mman.h>
  426: 
  427: static int state_protected_p;
  428: static __malloc_size_t last_state_size;
  429: static malloc_info *last_heapinfo;
  430: 
  431: void
  432: protect_malloc_state (protect_p)
  433:      int protect_p;
  434: {
  435:   /* If _heapinfo has been relocated, make sure its old location
  436:      isn't left read-only; it will be reused by malloc.  */
  437:   if (_heapinfo != last_heapinfo
  438:       && last_heapinfo
  439:       && state_protected_p)
  440:     mprotect (last_heapinfo, last_state_size, PROT_READ | PROT_WRITE);
  441: 
  442:   last_state_size = _heaplimit * sizeof *_heapinfo;
  443:   last_heapinfo   = _heapinfo;
  444: 
  445:   if (protect_p != state_protected_p)
  446:     {
  447:       state_protected_p = protect_p;
  448:       if (mprotect (_heapinfo, last_state_size,
  449:                     protect_p ? PROT_READ : PROT_READ | PROT_WRITE) != 0)
  450:         abort ();
  451:     }
  452: }
  453: 
  454: #define PROTECT_MALLOC_STATE(PROT) protect_malloc_state(PROT)
  455: 
  456: #else
  457: #define PROTECT_MALLOC_STATE(PROT)      /* empty */
  458: #endif
  459: 
  460: 
  461: /* Aligned allocation.  */
  462: static __ptr_t align PP ((__malloc_size_t));
  463: static __ptr_t
  464: align (size)
  465:      __malloc_size_t size;
  466: {
  467:   __ptr_t result;
  468:   unsigned long int adj;
  469: 
  470:   /* align accepts an unsigned argument, but __morecore accepts a
  471:      signed one.  This could lead to trouble if SIZE overflows a
  472:      signed int type accepted by __morecore.  We just punt in that
  473:      case, since they are requesting a ludicrous amount anyway.  */
  474:   if ((__malloc_ptrdiff_t)size < 0)
  475:     result = 0;
  476:   else
  477:     result = (*__morecore) (size);
  478:   adj = (unsigned long int) ((unsigned long int) ((char *) result -
  479:                                                   (char *) NULL)) % BLOCKSIZE;
  480:   if (adj != 0)
  481:     {
  482:       __ptr_t new;
  483:       adj = BLOCKSIZE - adj;
  484:       new = (*__morecore) (adj);
  485:       result = (char *) result + adj;
  486:     }
  487: 
  488:   if (__after_morecore_hook)
  489:     (*__after_morecore_hook) ();
  490: 
  491:   return result;
  492: }
  493: 
  494: /* Get SIZE bytes, if we can get them starting at END.
  495:    Return the address of the space we got.
  496:    If we cannot get space at END, fail and return 0.  */
  497: static __ptr_t get_contiguous_space PP ((__malloc_ptrdiff_t, __ptr_t));
  498: static __ptr_t
  499: get_contiguous_space (size, position)
  500:      __malloc_ptrdiff_t size;
  501:      __ptr_t position;
  502: {
  503:   __ptr_t before;
  504:   __ptr_t after;
  505: 
  506:   before = (*__morecore) (0);
  507:   /* If we can tell in advance that the break is at the wrong place,
  508:      fail now.  */
  509:   if (before != position)
  510:     return 0;
  511: 
  512:   /* Allocate SIZE bytes and get the address of them.  */
  513:   after = (*__morecore) (size);
  514:   if (!after)
  515:     return 0;
  516: 
  517:   /* It was not contiguous--reject it.  */
  518:   if (after != position)
  519:     {
  520:       (*__morecore) (- size);
  521:       return 0;
  522:     }
  523: 
  524:   return after;
  525: }
  526: 
  527: 
  528: /* This is called when `_heapinfo' and `heapsize' have just
  529:    been set to describe a new info table.  Set up the table
  530:    to describe itself and account for it in the statistics.  */
  531: static void register_heapinfo PP ((void));
  532: #ifdef __GNUC__
  533: __inline__
  534: #endif
  535: static void
  536: register_heapinfo ()
  537: {
  538:   __malloc_size_t block, blocks;
  539: 
  540:   block = BLOCK (_heapinfo);
  541:   blocks = BLOCKIFY (heapsize * sizeof (malloc_info));
  542: 
  543:   /* Account for the _heapinfo block itself in the statistics.  */
  544:   _bytes_used += blocks * BLOCKSIZE;
  545:   ++_chunks_used;
  546: 
  547:   /* Describe the heapinfo block itself in the heapinfo.  */
  548:   _heapinfo[block].busy.type = 0;
  549:   _heapinfo[block].busy.info.size = blocks;
  550:   /* Leave back-pointers for malloc_find_address.  */
  551:   while (--blocks > 0)
  552:     _heapinfo[block + blocks].busy.info.size = -blocks;
  553: }
  554: 
  555: #ifdef USE_PTHREAD
  556: static pthread_once_t malloc_init_once_control = PTHREAD_ONCE_INIT;
  557: pthread_mutex_t _malloc_mutex;
  558: #endif
  559: 
  560: static void
  561: malloc_initialize_1 ()
  562: {
  563: #ifdef GC_MCHECK
  564:   mcheck (NULL);
  565: #endif
  566: 
  567:   if (__malloc_initialize_hook)
  568:     (*__malloc_initialize_hook) ();
  569: 
  570: #ifdef USE_PTHREAD
  571:   {
  572:     pthread_mutexattr_t attr;
  573: 
  574:     pthread_mutexattr_init (&attr);
  575:     pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
  576:     pthread_mutex_init (&_malloc_mutex, &attr);
  577:     pthread_mutexattr_destroy (&attr);
  578:   }
  579: #endif
  580: 
  581: