Source:NetHack 3.6.1/src/alloc.c

From NetHackWiki
(Redirected from Source:Alloc.c)
Jump to: navigation, search

Below is the full text to alloc.c from the source code of NetHack 3.6.1. To link to a particular line, write [[Source:NetHack 3.6.1/src/alloc.c#line123]], for example.

Top of file

  1.  /* NetHack 3.6	alloc.c	$NHDT-Date: 1454376505 2016/02/02 01:28:25 $  $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.16 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /*-Copyright (c) Robert Patrick Rankin, 2012. */
  4.  /* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

  1.  
  2.  /* to get the malloc() prototype from system.h */
  3.  #define ALLOC_C /* comment line for pre-compiled headers */
  4.  /* since this file is also used in auxiliary programs, don't include all the
  5.     function declarations for all of nethack */
  6.  #define EXTERN_H /* comment line for pre-compiled headers */
  7.  #include "config.h"
  8.  
  9.  char *FDECL(fmt_ptr, (const genericptr));
  10.  
  11.  #ifdef MONITOR_HEAP
  12.  #undef alloc
  13.  #undef free
  14.  extern void FDECL(free, (genericptr_t));
  15.  static void NDECL(heapmon_init);
  16.  
  17.  static FILE *heaplog = 0;
  18.  static boolean tried_heaplog = FALSE;
  19.  #endif
  20.  
  21.  long *FDECL(alloc, (unsigned int));
  22.  extern void VDECL(panic, (const char *, ...)) PRINTF_F(1, 2);
  23.  
  24.  long *
  25.  alloc(lth)
  26.  register unsigned int lth;
  27.  {
  28.  #ifdef LINT
  29.      /*
  30.       * a ridiculous definition, suppressing
  31.       *  "possible pointer alignment problem" for (long *) malloc()
  32.       * from lint
  33.       */
  34.      long dummy = ftell(stderr);
  35.  
  36.      if (lth)
  37.          dummy = 0; /* make sure arg is used */
  38.      return &dummy;
  39.  #else
  40.      register genericptr_t ptr;
  41.  
  42.      ptr = malloc(lth);
  43.  #ifndef MONITOR_HEAP
  44.      if (!ptr)
  45.          panic("Memory allocation failure; cannot get %u bytes", lth);
  46.  #endif
  47.      return (long *) ptr;
  48.  #endif
  49.  }
  50.  
  51.  #ifdef HAS_PTR_FMT
  52.  #define PTR_FMT "%p"
  53.  #define PTR_TYP genericptr_t
  54.  #else
  55.  #define PTR_FMT "%06lx"
  56.  #define PTR_TYP unsigned long
  57.  #endif
  58.  
  59.  /* A small pool of static formatting buffers.
  60.   * PTRBUFSIZ:  We assume that pointers will be formatted as integers in
  61.   * hexadecimal, requiring at least 16+1 characters for each buffer to handle
  62.   * 64-bit systems, but the standard doesn't mandate that encoding and an
  63.   * implementation could do something different for %p, so we make some
  64.   * extra room.
  65.   * PTRBUFCNT:  Number of formatted values which can be in use at the same
  66.   * time.  To have more, callers need to make copies of them as they go.
  67.   */
  68.  #define PTRBUFCNT 4
  69.  #define PTRBUFSIZ 32
  70.  static char ptrbuf[PTRBUFCNT][PTRBUFSIZ];
  71.  static int ptrbufidx = 0;
  72.  

fmt_ptr

  1.  /* format a pointer for display purposes; returns a static buffer */
  2.  char *
  3.  fmt_ptr(ptr)
  4.  const genericptr ptr;
  5.  {
  6.      char *buf;
  7.  
  8.      buf = ptrbuf[ptrbufidx];
  9.      if (++ptrbufidx >= PTRBUFCNT)
  10.          ptrbufidx = 0;
  11.  
  12.      Sprintf(buf, PTR_FMT, (PTR_TYP) ptr);
  13.      return buf;
  14.  }
  15.  

heapmon_init

  1.  #ifdef MONITOR_HEAP
  2.  
  3.  /* If ${NH_HEAPLOG} is defined and we can create a file by that name,
  4.     then we'll log the allocation and release information to that file. */
  5.  static void
  6.  heapmon_init()
  7.  {
  8.      char *logname = getenv("NH_HEAPLOG");
  9.  
  10.      if (logname && *logname)
  11.          heaplog = fopen(logname, "w");
  12.      tried_heaplog = TRUE;
  13.  }
  14.  

nhalloc

  1.  long *
  2.  nhalloc(lth, file, line)
  3.  unsigned int lth;
  4.  const char *file;
  5.  int line;
  6.  {
  7.      long *ptr = alloc(lth);
  8.  
  9.      if (!tried_heaplog)
  10.          heapmon_init();
  11.      if (heaplog)
  12.          (void) fprintf(heaplog, "+%5u %s %4d %s\n", lth,
  13.                         fmt_ptr((genericptr_t) ptr), line, file);
  14.      /* potential panic in alloc() was deferred til here */
  15.      if (!ptr)
  16.          panic("Cannot get %u bytes, line %d of %s", lth, line, file);
  17.  
  18.      return ptr;
  19.  }
  20.  

nhfree

  1.  void
  2.  nhfree(ptr, file, line)
  3.  genericptr_t ptr;
  4.  const char *file;
  5.  int line;
  6.  {
  7.      if (!tried_heaplog)
  8.          heapmon_init();
  9.      if (heaplog)
  10.          (void) fprintf(heaplog, "-      %s %4d %s\n",
  11.                         fmt_ptr((genericptr_t) ptr), line, file);
  12.  
  13.      free(ptr);
  14.  }
  15.  
  16.  /* strdup() which uses our alloc() rather than libc's malloc(),
  17.     with caller tracking */

nhdupstr

  1.  char *
  2.  nhdupstr(string, file, line)
  3.  const char *string;
  4.  const char *file;
  5.  int line;
  6.  {
  7.      return strcpy((char *) nhalloc(strlen(string) + 1, file, line), string);
  8.  }
  9.  #undef dupstr
  10.  
  11.  #endif /* MONITOR_HEAP */
  12.  
  13.  /* strdup() which uses our alloc() rather than libc's malloc();
  14.     not used when MONITOR_HEAP is enabled, but included unconditionally
  15.     in case utility programs get built using a different setting for that */

dupstr

  1.  char *
  2.  dupstr(string)
  3.  const char *string;
  4.  {
  5.      return strcpy((char *) alloc(strlen(string) + 1), string);
  6.  }
  7.  
  8.  /*alloc.c*/