github.com/axw/llgo@v0.0.0-20160805011314-95b5fe4dca20/third_party/gofrontend/libbacktrace/alloc.c (about)

     1  /* alloc.c -- Memory allocation without mmap.
     2     Copyright (C) 2012-2015 Free Software Foundation, Inc.
     3     Written by Ian Lance Taylor, Google.
     4  
     5  Redistribution and use in source and binary forms, with or without
     6  modification, are permitted provided that the following conditions are
     7  met:
     8  
     9      (1) Redistributions of source code must retain the above copyright
    10      notice, this list of conditions and the following disclaimer. 
    11  
    12      (2) Redistributions in binary form must reproduce the above copyright
    13      notice, this list of conditions and the following disclaimer in
    14      the documentation and/or other materials provided with the
    15      distribution.  
    16      
    17      (3) The name of the author may not be used to
    18      endorse or promote products derived from this software without
    19      specific prior written permission.
    20  
    21  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    22  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    24  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
    25  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    26  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    27  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    28  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    29  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
    30  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    31  POSSIBILITY OF SUCH DAMAGE.  */
    32  
    33  #include "config.h"
    34  
    35  #include <errno.h>
    36  #include <stdlib.h>
    37  #include <sys/types.h>
    38  
    39  #include "backtrace.h"
    40  #include "internal.h"
    41  
    42  /* Allocation routines to use on systems that do not support anonymous
    43     mmap.  This implementation just uses malloc, which means that the
    44     backtrace functions may not be safely invoked from a signal
    45     handler.  */
    46  
    47  /* Allocate memory like malloc.  */
    48  
    49  void *
    50  backtrace_alloc (struct backtrace_state *state ATTRIBUTE_UNUSED,
    51  		 size_t size, backtrace_error_callback error_callback,
    52  		 void *data)
    53  {
    54    void *ret;
    55  
    56    ret = malloc (size);
    57    if (ret == NULL)
    58      error_callback (data, "malloc", errno);
    59    return ret;
    60  }
    61  
    62  /* Free memory.  */
    63  
    64  void
    65  backtrace_free (struct backtrace_state *state ATTRIBUTE_UNUSED,
    66  		void *p, size_t size ATTRIBUTE_UNUSED,
    67  		backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
    68  		void *data ATTRIBUTE_UNUSED)
    69  {
    70    free (p);
    71  }
    72  
    73  /* Grow VEC by SIZE bytes.  */
    74  
    75  void *
    76  backtrace_vector_grow (struct backtrace_state *state ATTRIBUTE_UNUSED,
    77  		       size_t size, backtrace_error_callback error_callback,
    78  		       void *data, struct backtrace_vector *vec)
    79  {
    80    void *ret;
    81  
    82    if (size > vec->alc)
    83      {
    84        size_t alc;
    85        void *base;
    86  
    87        if (vec->size == 0)
    88  	alc = 32 * size;
    89        else if (vec->size >= 4096)
    90  	alc = vec->size + 4096;
    91        else
    92  	alc = 2 * vec->size;
    93  
    94        if (alc < vec->size + size)
    95  	alc = vec->size + size;
    96  
    97        base = realloc (vec->base, alc);
    98        if (base == NULL)
    99  	{
   100  	  error_callback (data, "realloc", errno);
   101  	  return NULL;
   102  	}
   103  
   104        vec->base = base;
   105        vec->alc = alc - vec->size;
   106      }
   107  
   108    ret = (char *) vec->base + vec->size;
   109    vec->size += size;
   110    vec->alc -= size;
   111    return ret;
   112  }
   113  
   114  /* Finish the current allocation on VEC.  */
   115  
   116  void *
   117  backtrace_vector_finish (struct backtrace_state *state,
   118  			 struct backtrace_vector *vec,
   119  			 backtrace_error_callback error_callback,
   120  			 void *data)
   121  {
   122    void *ret;
   123  
   124    /* With this allocator we call realloc in backtrace_vector_grow,
   125       which means we can't easily reuse the memory here.  So just
   126       release it.  */
   127    if (!backtrace_vector_release (state, vec, error_callback, data))
   128      return NULL;
   129    ret = vec->base;
   130    vec->base = NULL;
   131    vec->size = 0;
   132    vec->alc = 0;
   133    return ret;
   134  }
   135  
   136  /* Release any extra space allocated for VEC.  */
   137  
   138  int
   139  backtrace_vector_release (struct backtrace_state *state ATTRIBUTE_UNUSED,
   140  			  struct backtrace_vector *vec,
   141  			  backtrace_error_callback error_callback,
   142  			  void *data)
   143  {
   144    vec->base = realloc (vec->base, vec->size);
   145    if (vec->base == NULL)
   146      {
   147        error_callback (data, "realloc", errno);
   148        return 0;
   149      }
   150    vec->alc = 0;
   151    return 1;
   152  }