Source:NetHack 3.4.3/src/alloc.c

From NetHackWiki
(Redirected from Source:Ref/fmt ptr)
Jump to: navigation, search

Below is the full text to src/alloc.c from NetHack 3.4.3. To link to a particular line, write [[alloc.c#line123]], for example.

Top of file[edit]

  1. /*	SCCS Id: @(#)alloc.c	3.4	1995/10/04	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* 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. /* to get the malloc() prototype from system.h */
  2. #define ALLOC_C		/* comment line for pre-compiled headers */
  3. /* since this file is also used in auxiliary programs, don't include all the
  4. * function declarations for all of nethack
  5. */
  6. #define EXTERN_H	/* comment line for pre-compiled headers */
  7. #include "config.h"
  8.  
  9. #if defined(MONITOR_HEAP) || defined(WIZARD)
  10. char *FDECL(fmt_ptr, (const genericptr,char *));
  11. #endif
  12.  
  13. #ifdef MONITOR_HEAP
  14. #undef alloc
  15. #undef free
  16. extern void FDECL(free,(genericptr_t));
  17. static void NDECL(heapmon_init);
  18.  
  19. static FILE *heaplog = 0;
  20. static boolean tried_heaplog = FALSE;
  21. #endif
  22.  
  23. long *FDECL(alloc,(unsigned int));
  24. extern void VDECL(panic, (const char *,...)) PRINTF_F(1,2);

alloc[edit]

alloc() takes one parameter, the number of bytes to reserve, and returns a pointer to a block of memory which is that big.

  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) dummy = 0;	/* make sure arg is used */
  14. 	return(&dummy);
  15. #else
  16. 	register genericptr_t ptr;
  17.  
  18. 	ptr = malloc(lth);
  19. #ifndef MONITOR_HEAP
  20. 	if (!ptr) panic("Memory allocation failure; cannot get %u bytes", lth);
  21. #endif
  22. 	return((long *) ptr);
  23. #endif
  24. }

fmt_ptr[edit]

  1. #if defined(MONITOR_HEAP) || defined(WIZARD)
  2.  
  3. # if defined(MICRO) || defined(WIN32)
  4. /* we actually want to know which systems have an ANSI run-time library
  5. * to know which support the new %p format for printing pointers.
  6. * due to the presence of things like gcc, NHSTDC is not a good test.
  7. * so we assume microcomputers have all converted to ANSI and bigger
  8. * computers which may have older libraries give reasonable results with
  9. * the cast.
  10. */
  11. #  define MONITOR_PTR_FMT
  12. # endif
  13.  
  14. # ifdef MONITOR_PTR_FMT
  15. #  define PTR_FMT "%p"
  16. #  define PTR_TYP genericptr_t
  17. # else
  18. #  define PTR_FMT "%06lx"
  19. #  define PTR_TYP unsigned long
  20. # endif
  21.  
  22. /* format a pointer for display purposes; caller supplies the result buffer */
  23. char *
  24. fmt_ptr(ptr, buf)
  25. const genericptr ptr;
  26. char *buf;
  27. {
  28. 	Sprintf(buf, PTR_FMT, (PTR_TYP)ptr);
  29. 	return buf;
  30. }
  31.  
  32. #endif

heapmon_init[edit]

  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. }

nhalloc[edit]

  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. 	char ptr_address[20];
  9.  
  10. 	if (!tried_heaplog) heapmon_init();
  11. 	if (heaplog)
  12. 		(void) fprintf(heaplog, "+%5u %s %4d %s\n", lth,
  13. 				fmt_ptr((genericptr_t)ptr, ptr_address),
  14. 				line, file);
  15. 	/* potential panic in alloc() was deferred til here */
  16. 	if (!ptr) panic("Cannot get %u bytes, line %d of %s",
  17. 			lth, line, file);
  18.  
  19. 	return ptr;
  20. }

nhfree[edit]

  1. void
  2. nhfree(ptr, file, line)
  3. genericptr_t ptr;
  4. const char *file;
  5. int line;
  6. {
  7. 	char ptr_address[20];
  8.  
  9. 	if (!tried_heaplog) heapmon_init();
  10. 	if (heaplog)
  11. 		(void) fprintf(heaplog, "-      %s %4d %s\n",
  12. 				fmt_ptr((genericptr_t)ptr, ptr_address),
  13. 				line, file);
  14.  
  15. 	free(ptr);
  16. }
  17.  
  18. #endif /* MONITOR_HEAP */
  19.  
  20. /*alloc.c*/