Source:NetHack 3.6.0/src/alloc.c

From NetHackWiki
Jump to: navigation, search

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

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.

Top of file

  1.  /* NetHack 3.6	alloc.c	$NHDT-Date: 1446975460 2015/11/08 09:37:40 $  $NHDT-Branch: master $:$NHDT-Revision: 1.14 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  /* to get the malloc() prototype from system.h */
  6.  #define ALLOC_C /* comment line for pre-compiled headers */
  7.  /* since this file is also used in auxiliary programs, don't include all the
  8.   * function declarations for all of nethack
  9.   */
  10.  #define EXTERN_H /* comment line for pre-compiled headers */
  11.  #include "config.h"
  12.  
  13.  char *FDECL(fmt_ptr, (const genericptr));
  14.  
  15.  #ifdef MONITOR_HEAP
  16.  #undef alloc
  17.  #undef free
  18.  extern void FDECL(free, (genericptr_t));
  19.  static void NDECL(heapmon_init);
  20.  
  21.  static FILE *heaplog = 0;
  22.  static boolean tried_heaplog = FALSE;
  23.  #endif
  24.  
  25.  long *FDECL(alloc, (unsigned int));
  26.  extern void
  27.  VDECL(panic, (const char *, ...))
  28.  PRINTF_F(1, 2);
  29.  

alloc

  1.  long *
  2.  alloc(lth)
  3.  register unsigned int lth;
  4.  {
  5.  #ifdef LINT
  6.      /*
  7.       * a ridiculous definition, suppressing
  8.       *  "possible pointer alignment problem" for (long *) malloc()
  9.       * from lint
  10.       */
  11.      long dummy = ftell(stderr);
  12.  
  13.      if (lth)
  14.          dummy = 0; /* make sure arg is used */
  15.      return &dummy;
  16.  #else
  17.      register genericptr_t ptr;
  18.  
  19.      ptr = malloc(lth);
  20.  #ifndef MONITOR_HEAP
  21.      if (!ptr)
  22.          panic("Memory allocation failure; cannot get %u bytes", lth);
  23.  #endif
  24.      return (long *) ptr;
  25.  #endif
  26.  }
  27.  

fmt_ptr

  1.  #ifdef HAS_PTR_FMT
  2.  #define PTR_FMT "%p"
  3.  #define PTR_TYP genericptr_t
  4.  #else
  5.  #define PTR_FMT "%06lx"
  6.  #define PTR_TYP unsigned long
  7.  #endif
  8.  
  9.  /* A small pool of static formatting buffers.
  10.   * PTRBUFSIZ:  We assume that pointers will be formatted as integers in
  11.   * hexadecimal, requiring at least 16+1 characters for each buffer to handle
  12.   * 64-bit systems, but the standard doesn't mandate that encoding and an
  13.   * implementation could do something different for %p, so we make some
  14.   * extra room.
  15.   * PTRBUFCNT:  Number of formatted values which can be in use at the same
  16.   * time.  To have more, callers need to make copies of them as they go.
  17.   */
  18.  #define PTRBUFCNT 4
  19.  #define PTRBUFSIZ 32
  20.  static char ptrbuf[PTRBUFCNT][PTRBUFSIZ];
  21.  static int ptrbufidx = 0;
  22.  
  23.  /* format a pointer for display purposes; returns a static buffer */
  24.  char *
  25.  fmt_ptr(ptr)
  26.  const genericptr ptr;
  27.  {
  28.      char *buf;
  29.  
  30.      buf = ptrbuf[ptrbufidx];
  31.      if (++ptrbufidx >= PTRBUFCNT)
  32.          ptrbufidx = 0;
  33.  
  34.      Sprintf(buf, PTR_FMT, (PTR_TYP) ptr);
  35.      return buf;
  36.  }
  37.  

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.  

nhdupstr

  1.  /* strdup() which uses our alloc() rather than libc's malloc(),
  2.     with caller tracking */
  3.  char *
  4.  nhdupstr(string, file, line)
  5.  const char *string;
  6.  const char *file;
  7.  int line;
  8.  {
  9.      return strcpy((char *) nhalloc(strlen(string) + 1, file, line), string);
  10.  }
  11.  #undef dupstr
  12.  
  13.  #endif /* MONITOR_HEAP */
  14.  

dupstr

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