wanghao_branch
jiehaoxuan 1 year ago
parent 396fd2b00c
commit b5d2e83695

@ -1,3 +1,4 @@
//2023/12/3 wh
/* NetHack 3.7 alloc.c $NHDT-Date: 1687343500 2023/06/21 10:31:40 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.31 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
@ -122,16 +123,17 @@ static int ptrbufidx = 0;
char *
fmt_ptr(const genericptr ptr)
{
char *buf;
char *buf; // Declare a character pointer variable buf
buf = ptrbuf[ptrbufidx];
if (++ptrbufidx >= PTRBUFCNT)
ptrbufidx = 0;
buf = ptrbuf[ptrbufidx]; // Point buf to the element at index ptrbufidx in the ptrbuf array
if (++ptrbufidx >= PTRBUFCNT) // If ptrbufidx exceeds the range of PTRBUFCNT after incrementing
ptrbufidx = 0; // Reset ptrbufidx to 0
Sprintf(buf, PTR_FMT, (PTR_TYP) ptr);
return buf;
Sprintf(buf, PTR_FMT, (PTR_TYP) ptr); // Use the Sprintf function to format the pointer value into a string and store it in buf
return buf; // Return the formatted string
}
#ifdef MONITOR_HEAP
/* If ${NH_HEAPLOG} is defined and we can create a file by that name,
@ -231,42 +233,42 @@ nhdupstr(const char *string, const char *file, int line)
char *
dupstr(const char *string)
{
unsigned len = FITSuint_(strlen(string), __func__, (int) __LINE__);
unsigned len = FITSuint_(strlen(string), __func__, (int) __LINE__); // Get the length of the input string using strlen() function, and store it in the variable len
return strcpy((char *) alloc(len + 1), string);
return strcpy((char *) alloc(len + 1), string); // Allocate memory for a new string of length len+1, and copy the input string into the newly allocated memory
}
/* similar for reasonable size strings, but return length of input as well */
char *
dupstr_n(const char *string, unsigned int *lenout)
{
size_t len = strlen(string);
size_t len = strlen(string); // Get the length of the input string using strlen() function
if (len >= LARGEST_INT)
panic("string too long");
*lenout = (unsigned int) len;
return strcpy((char *) alloc(len + 1), string);
if (len >= LARGEST_INT) // If the length exceeds a predefined constant LARGEST_INT, which represents the maximum length allowed
panic("string too long"); // Raise an error or handle the situation when the string is too long
*lenout = (unsigned int) len; // Store the length of the input string in the variable lenout
return strcpy((char *) alloc(len + 1), string); // Allocate memory for a new string of length len+1, copy the input string into the newly allocated memory, and return the new string
}
/* cast to int or panic on overflow; use via macro */
int
FITSint_(LUA_INTEGER i, const char *file, int line)
{
int iret = (int) i;
int iret = (int) i; // Cast the input parameter i to int type and store it in the variable iret
if (iret != i)
panic("Overflow at %s:%d", file, line);
return iret;
if (iret != i) // If the casted value is not equal to the original input value
panic("Overflow at %s:%d", file, line); // Raise an error or handle the situation when overflow occurs
return iret; // Return the casted int value
}
unsigned
FITSuint_(unsigned long long ull, const char *file, int line)
{
unsigned uret = (unsigned) ull;
unsigned uret = (unsigned) ull; // Cast the input parameter ull to unsigned type and store it in the variable uret
if (uret != ull)
panic("Overflow at %s:%d", file, line);
return uret;
if (uret != ull) // If the casted value is not equal to the original input value
panic("Overflow at %s:%d", file, line); // Raise an error or handle the situation when overflow occurs
return uret; // Return the casted unsigned value
}
/*alloc.c*/

Loading…
Cancel
Save