TinyMUSH 3.3
TinyMUSH Server
Loading...
Searching...
No Matches
Macros | Functions
alloc.c File Reference

memory management subsystem More...

#include "config.h"
#include "constants.h"
#include "typedefs.h"
#include "macros.h"
#include "externs.h"
#include "prototypes.h"
#include <string.h>
Include dependency graph for alloc.c:

Macros

#define XLOGALLOC(x, y, z, s, ...)
 

Functions

void * __xmalloc (size_t size, const char *file, int line, const char *function, const char *var)
 Tracked version of malloc(). More...
 
void * __xcalloc (size_t nmemb, size_t size, const char *file, int line, const char *function, const char *var)
 Tracked version of calloc(). More...
 
void * __xrealloc (void *ptr, size_t size, const char *file, int line, const char *function, const char *var)
 Tracked version of realloc(). More...
 
MEMTRACK__xfind (void *ptr)
 Search the tracking list for a pointer and return it's information. More...
 
int __xcheck (void *ptr)
 Check if a buffer wasn't overrun. More...
 
int __xfree (void *ptr)
 Tracked version of free(). More...
 
char * __xasprintf (const char *file, int line, const char *function, const char *var, const char *format,...)
 Tracked sprintf replacement that create a new formatted string buffer. More...
 
char * __xavsprintf (const char *file, int line, const char *function, const char *var, const char *format, va_list va)
 Tracked vsprintf replacement that create a new formatted string buffer. More...
 
int __xsprintf (char *str, const char *format,...)
 Tracked sprintf replacement. More...
 
int __xvsprintf (char *str, const char *format, va_list ap)
 Tracked vsprintf replacement. More...
 
int __xsnprintf (char *str, size_t size, const char *format,...)
 Tracked snprintf replacement. More...
 
int __xvsnprintf (char *str, size_t size, const char *format, va_list ap)
 Tracked vsnprintf replacement. More...
 
int __xsprintfcat (char *str, const char *format,...)
 Tracked sprintfcat. More...
 
char * __xsafesprintf (char *buff, char **bufp, const char *format,...)
 tracked safe_printf replacement. More...
 
char * __xstrdup (const char *s, const char *file, int line, const char *function, const char *var)
 Tracked version of strdup. More...
 
char * __xstrndup (const char *s, size_t n, const char *file, int line, const char *function, const char *var)
 Tracked version of strndup. More...
 
char * __xstpcpy (char *dest, const char *src)
 Tracked stpcpy replacement. More...
 
char * __xstrcat (char *dest, const char *src)
 Tracked strcat replacement. More...
 
char * __xstrncat (char *dest, const char *src, size_t n)
 Tracked strncat replacement. More...
 
char * __xstrccat (char *dest, const char src)
 Tracked strccat. More...
 
char * __xstrnccat (char *dest, const char src, size_t n)
 Tracked strnccat. More...
 
char * __xstrcpy (char *dest, const char *src)
 Tracked strcpy replacement. More...
 
char * __xstrncpy (char *dest, const char *src, size_t n)
 Tracked strncpy replacement. More...
 
void * __xmemmove (void *dest, const void *src, size_t n)
 Tracked memmove replacement. More...
 
void * __xmempcpy (void *dest, const void *src, size_t n)
 Tracked mempcpy replacement. More...
 
void * __xmemccpy (void *dest, const void *src, int c, size_t n)
 Tracked memccpy replacement. More...
 
void * __xmemset (void *s, int c, size_t n)
 Tracked memset replacement. More...
 
void list_bufstats (dbref player)
 Function placeholder. Not used for now. More...
 
void list_buftrace (dbref player)
 Function placeholder. Not used for now. More...
 
int __xsorttrace (const void *p1, const void *p2)
 Helper function to sort the trace table. More...
 
void list_rawmemory (dbref player)
 Show to the player a summary of all allocations. More...
 
size_t total_rawmemory (void)
 Calculate the total size of all allocations. More...
 
size_t __xsafestrncpy (char *dest, char **destp, const char *src, size_t n, size_t size)
 Copy a string with at most n character from src and update the position pointer to the end of the destination buffer. More...
 
int __xsafestrcatchr (char *dest, char **destp, char c, size_t size)
 Copy char 'c' to dest and update the position pointer to the end of the destination buffer. More...
 
size_t __xsafestrncat (char *dest, char **destp, const char *src, size_t n, size_t size)
 Concatenate two string with at most n character from src and update the position pointer to the end of the destination buffer. More...
 
void __xsafeltos (char *dest, char **destp, long num, size_t size)
 Convert a long signed number into string. More...
 
char * __xrepeatchar (size_t size, char c)
 Return a string with 'count' number of 'ch' characters. More...
 

Detailed Description

memory management subsystem

Author
TinyMUSH development team (https://github.com/TinyMUSH)
Version
3.3
Date
2020-11-14

Macro Definition Documentation

◆ XLOGALLOC

#define XLOGALLOC (   x,
  y,
  z,
  s,
  ... 
)
Value:
if (mushconf.malloc_logger) \
{ \
log_write(x, y, z, s, ##__VA_ARGS__); \
}
int malloc_logger
Definition: typedefs.h:996

Macros and utilities.

Function Documentation

◆ __xasprintf()

char * __xasprintf ( const char *  file,
int  line,
const char *  function,
const char *  var,
const char *  format,
  ... 
)

Tracked sprintf replacement that create a new formatted string buffer.

String functions

Much like sprintf, but create a new buffer sized for the resulting string. Memory for the new string is tracked, and must be freed with __xfree().

Do not call this directly, use the wrapper macro XSPRINTF(const char *var, const char *format, ...) that will fill most of the banks for you.

Parameters
fileFilename where the allocation is done.
lineLine number where the allocation is done.
functionFunction where the allocation is done.
varName of the variable that will receive the pointer (not the variable itself).
formatFormat string.
...Variables arguments for the format string.
Returns
char* Pointer to the new string buffer.

◆ __xavsprintf()

char * __xavsprintf ( const char *  file,
int  line,
const char *  function,
const char *  var,
const char *  format,
va_list  va 
)

Tracked vsprintf replacement that create a new formatted string buffer.

Much like vsprintf, but create a new buffer sized for the resulting string. Memory for the new string is tracked, and must be freed with __xfree().

Do not call this directly, use the wrapper macro XAVSPRINTF(const char *var, const char *format, ...) that will fill most of the banks for you.

Parameters
fileFilename where the allocation is done.
lineLine number where the allocation is done.
functionFunction where the allocation is done.
varName of the variable that will receive the pointer (not the variable itself).
formatFormat string.
...Variables arguments for the format string.
Returns
char* Pointer to the new string buffer.

◆ __xcalloc()

void * __xcalloc ( size_t  nmemb,
size_t  size,
const char *  file,
int  line,
const char *  function,
const char *  var 
)

Tracked version of calloc().

The __xcalloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns NULL. If the multiplication of nmemb and size would result in integer overflow, then __xcalloc() returns an error.

This variant doesn't check if the multiplication of nmemb and size would result in integer overflow, so don't be silly and don't try to allocate more than 18 exabytes of memory (or 4 gigabytes if your still on 32 bits).

Do not call this directly, use the wrapper macro XCALLOC(size_t nmemb, size_t size, const char *var) that will fill most of the banks for you.

Parameters
nmembNumber of elements.
sizeSize of each elements.
fileFilename where the allocation is done.
lineLine number where the allocation is done.
functionFunction where the allocation is done.
varName of the variable that will receive the pointer (not the variable itself).
Returns
void* Pointer to the allocated buffer.

◆ __xcheck()

int __xcheck ( void *  ptr)

Check if a buffer wasn't overrun.

Parameters
ptrPointer to check.
Returns
int 1, if everything is allright. 0, if buffer overrun, 2 if the buffer isn't track.

◆ __xfind()

MEMTRACK * __xfind ( void *  ptr)

Search the tracking list for a pointer and return it's information.

__xfind will search thru all allocated blocks for the address pointed by ptr, if this address is within the start and end of the allocaed block, it will return the MEMTRACK information about that block.

It return NULL if nothing found.

Parameters
ptrPointer to or somewhere in the allocated memory block.
Returns
Pointer to a MEMTRACK structure with information on the memory block. or NULL if not found.

◆ __xfree()

int __xfree ( void *  ptr)

Tracked version of free().

The __xfree() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc()/__xmalloc(), calloc()/__xcalloc(), or realloc()/__xrealloc(). Otherwise, or if __xfree(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

The wrapper macro XFREE(void *ptr) is identical to calling this function directly. Just keep things more redeable.

Parameters
ptrPointer to the allocation to be freed.
Returns
1 if the buffer was tracked and it has been overrun, else 0.

◆ __xmalloc()

void * __xmalloc ( size_t  size,
const char *  file,
int  line,
const char *  function,
const char *  var 
)

Tracked version of malloc().

Allocation functions

The __xmalloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is set to zero. If size is 0, then malloc() returns NULL.

This variant doesn't check if the multiplication of nmemb and size would result in integer overflow, so don't be silly and don't try to allocate more than 18 exabytes of memory (or 4 gigabytes if your still on 32 bits).

Do not call this directly, use the wrapper macro XMALLOC(size_t size, const char *var) that will fill most of the banks for you.

Parameters
sizeSize of memory to allocate.
fileFilename where the allocation is done.
lineLine number where the allocation is done.
functionFunction where the allocation is done.
varName of the variable that will receive the pointer (not the variable itself).
Returns
void* Pointer to the allocated buffer.

◆ __xmemccpy()

void * __xmemccpy ( void *  dest,
const void *  src,
int  c,
size_t  n 
)

Tracked memccpy replacement.

The memccpy() function copies no more than n bytes from memory area src to memory area dest, stopping when the character c is found. The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.

Parameters
destPointer to destination buffer.
srcPointer to source buffer.
nNumber of bytes to copy.
Returns
Pointer to the destination buffer.

◆ __xmemmove()

void * __xmemmove ( void *  dest,
const void *  src,
size_t  n 
)

Tracked memmove replacement.

The __xmemmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.

Parameters
destPointer to destination buffer.
srcPointer to source buffer.
nNumber of bytes to copy.
Returns
Pointer to the destination buffer.

◆ __xmempcpy()

void * __xmempcpy ( void *  dest,
const void *  src,
size_t  n 
)

Tracked mempcpy replacement.

The __xmempcpy() function is nearly identical to the memcpy(3) function. It copies n bytes from the object beginning at src into the object pointed to by dest. But instead of returning the value of dest it returns a pointer to the byte following the last written byte. The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.

Parameters
destPointer to destination buffer.
srcPointer to source buffer.
nNumber of bytes to copy.
Returns
Pointer to the byte following the last written byte.

◆ __xmemset()

void * __xmemset ( void *  s,
int  c,
size_t  n 
)

Tracked memset replacement.

The __xmemset() function fills the first n bytes of the memory area pointed to by s with the constant byte c. For tracked allocation, make sure that the buffer does not overrun.

Parameters
sPointer to the memory area to fill.
cCharacter to fill with.
nSize of the memory area to fill.
Returns
Pointer to the memory area.

◆ __xrealloc()

void * __xrealloc ( void *  ptr,
size_t  size,
const char *  file,
int  line,
const char *  function,
const char *  var 
)

Tracked version of realloc().

The __xrealloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc(), or realloc(). If the area pointed to was moved, a free(ptr) is done.

This variant doesn't check if the multiplication of nmemb and size would result in integer overflow, so don't be silly and don't try to allocate more than 18 exabytes of memory (or 4 gigabytes if your still on 32 bits).

Do not call this directly, use the wrapper macro XREALLOC(void *ptr, size_t size, const char *var) that will fill most of the banks for you.

Parameters
ptrPointer to the allocation to be resize.
sizeSize of memory to allocate.
fileFilename where the allocation is done.
lineLine number where the allocation is done.
functionFunction where the allocation is done.
varName of the variable that will receive the pointer (not the variable itself).
Returns
void* Pointer to the allocated buffer.

◆ __xrepeatchar()

char * __xrepeatchar ( size_t  size,
char  c 
)

Return a string with 'count' number of 'ch' characters.

It is the responsibility of the caller to free the resulting buffer.

Parameters
sizeSize of the string to build.
cCharacter to fill the string with.
Returns
Pointer to the build string.

◆ __xsafeltos()

void __xsafeltos ( char *  dest,
char **  destp,
long  num,
size_t  size 
)

Convert a long signed number into string.

Parameters
destPointer to destination buffer.
destpPointer tracking the desstination buffer.
numNumber to convert.
sizeMaximum size of the destination buffer.

◆ __xsafesprintf()

char * __xsafesprintf ( char *  buff,
char **  bufp,
const char *  format,
  ... 
)

tracked safe_printf replacement.

Parameters
buffBuffer that will receive the result
bufpPointer to the location in the buffer
formatFormat string.
...Variables argument list for the format string.
Returns
char* Pointer to buffer

◆ __xsafestrcatchr()

int __xsafestrcatchr ( char *  dest,
char **  destp,
char  c,
size_t  size 
)

Copy char 'c' to dest and update the position pointer to the end of the destination buffer.

Parameters
cChar to copy
destPointer to destination buffer.
destpPointer tracking the desstination buffer.
sizeMaximum size of the destination buffer.

◆ __xsafestrncat()

size_t __xsafestrncat ( char *  dest,
char **  destp,
const char *  src,
size_t  n,
size_t  size 
)

Concatenate two string with at most n character from src and update the position pointer to the end of the destination buffer.

Parameters
destPointer to destination buffer.
destpPointer tracking the desstination buffer.
srcPointer to the string to concatenate.
nMaximum number of characters to concatenate.
sizeMaximum size of dest buffer if untracked.
Returns
Number of characters that where not copied if the buffer (whichever is smaller) isn't big enough to hold the result.

◆ __xsafestrncpy()

size_t __xsafestrncpy ( char *  dest,
char **  destp,
const char *  src,
size_t  n,
size_t  size 
)

Copy a string with at most n character from src and update the position pointer to the end of the destination buffer.

Replacement for the safe_* functions.

Parameters
destPointer to destination buffer.
destpPointer tracking the desstination buffer.
srcPointer to the string to concatenate.
nMaximum number of characters to concatenate.
sizeMaximum size of dest buffer if untracked.
Returns
Number of characters that where not copied if the buffer (whichever is smaller) isn't big enough to hold the result.

◆ __xsnprintf()

int __xsnprintf ( char *  str,
size_t  size,
const char *  format,
  ... 
)

Tracked snprintf replacement.

Like snprintf, but make sure that the buffer does not overflow if the buffer is tracked. Else, act exactly like snprintf.

Parameters
strBuffer where to write the resulting string.
sizeMaximum size to write to the buffer.
formatFormat string.
...Variables arguments for the format string.
Returns
int Number of byte written to the buffer.

◆ __xsorttrace()

int __xsorttrace ( const void *  p1,
const void *  p2 
)

Helper function to sort the trace table.

This function is used by list_rawmemory for sorting the trace table via qsort()

Parameters
p1Pointer to the first MEMTRACK struct to compare.
p2Pointer to the second MEMTRACK struct to compare.
Returns
int Result of the comparison (see man qsort()).

◆ __xsprintf()

int __xsprintf ( char *  str,
const char *  format,
  ... 
)

Tracked sprintf replacement.

Like sprintf, but make sure that the buffer does not overflow if the buffer is tracked. Else, act exactly like vsprintf.

Parameters
strBuffer where to write the resulting string.
formatFormat string.
...Variables argument list for the format string.
Returns
int Number of byte written to the buffer.

◆ __xsprintfcat()

int __xsprintfcat ( char *  str,
const char *  format,
  ... 
)

Tracked sprintfcat.

Like sprintf, but append to the buffer and make sure it does not overflow if the buffer is tracked.

Parameters
strBuffer where to write the resulting string.
formatFormat string.
...Variables argument list for the format string.
Returns
int Number of byte written to the buffer.

◆ __xstpcpy()

char * __xstpcpy ( char *  dest,
const char *  src 
)

Tracked stpcpy replacement.

Copy a string from src to dest, returning a pointer to the end of the resulting string at dest. For tracked allocation, make sure that the destination buffer does not overrun.

Parameters
destPointer to destination buffer.
srcPointer to source buffer.
Returns
Pointer to the end of the string dest (that is, the address of the terminating null byte) rather than the beginning.

◆ __xstrcat()

char * __xstrcat ( char *  dest,
const char *  src 
)

Tracked strcat replacement.

Append the string src to string dest, returning a pointer to dest. For tracked allocation, make sure that the destination buffer does not overrun.

Parameters
destPointer to destination buffer.
srcPointer to source buffer.
Returns
Pointer to the destination string dest.

◆ __xstrccat()

char * __xstrccat ( char *  dest,
const char  src 
)

Tracked strccat.

Append the character src to string dest, returning a pointer to dest. For tracked allocation, make sure that the destination buffer does not overrun.

Parameters
destPointer to destination buffer.
srcSource character.
Returns
Pointer to the end of destination string dest.

◆ __xstrcpy()

char * __xstrcpy ( char *  dest,
const char *  src 
)

Tracked strcpy replacement.

Copy the string src to dest, returning a pointer to dest. For tracked allocation, make sure that the destination buffer does not overrun.

Parameters
destPointer to destination buffer.
srcPointer to source buffer.
Returns
Pointer to the destination string dest.

◆ __xstrdup()

char * __xstrdup ( const char *  s,
const char *  file,
int  line,
const char *  function,
const char *  var 
)

Tracked version of strdup.

The __xstrdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is tracked, and must be freed with __xfree().

Do not call this directly, use the wrapper macro XSTRDUP(const char *str, size_t size, const char *var) that will fill most of the banks for you.

Parameters
sString to duplicate.
fileFilename where the allocation is done.
lineLine number where the allocation is done.
functionFunction where the allocation is done.
varName of the variable that will receive the pointer (not the variable itself).
Returns
char* Pointer to the new string buffer.

◆ __xstrncat()

char * __xstrncat ( char *  dest,
const char *  src,
size_t  n 
)

Tracked strncat replacement.

Append at most n bytes from string src to string dest, returning a pointer to dest. For tracked allocation, make sure that the destination buffer does not overrun.

Parameters
destPointer to destination buffer.
srcPointer to source buffer.
nMaximum numbers of characters to copy.
Returns
Pointer to the destination string dest.

◆ __xstrnccat()

char * __xstrnccat ( char *  dest,
const char  src,
size_t  n 
)

Tracked strnccat.

Append the character src to string dest and limit dest lengt to n, returning a pointer to dest. For tracked allocation, make sure that the destination buffer does not overrun.

Parameters
destPointer to destination buffer.
srcSource character.
nMaximum length of dest.
Returns
Pointer to the end of destination string dest.

◆ __xstrncpy()

char * __xstrncpy ( char *  dest,
const char *  src,
size_t  n 
)

Tracked strncpy replacement.

Copy at most n bytes from string src to dest, returning a pointer to dest. For tracked allocation, make sure that the destination buffer does not overrun.

Parameters
destPointer to destination buffer.
srcPointer to source buffer.
nMaximum numbers of characters to copy.
Returns
Pointer to the destination string dest.

◆ __xstrndup()

char * __xstrndup ( const char *  s,
size_t  n,
const char *  file,
int  line,
const char *  function,
const char *  var 
)

Tracked version of strndup.

The __xstrdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is tracked, and must be freed with __xfree().

Do not call this directly, use the wrapper macro XSTRDUP(const char *str, size_t size, const char *var) that will fill most of the banks for you.

Parameters
sString to duplicate.
nNumber of bytes to replicates.
fileFilename where the allocation is done.
lineLine number where the allocation is done.
functionFunction where the allocation is done.
varName of the variable that will receive the pointer (not the variable itself).
Returns
char* Pointer to the new string buffer.

◆ __xvsnprintf()

int __xvsnprintf ( char *  str,
size_t  size,
const char *  format,
va_list  ap 
)

Tracked vsnprintf replacement.

Like vsnprintf, but make sure that the buffer does not overflow if the buffer is tracked. Else, act exactly like snprintf.

Parameters
strBuffer where to write the resulting string.
sizeMaximum size to write to the buffer.
formatFormat string.
apVariables argument list for the format string.
Returns
int Number of byte written to the buffer.

◆ __xvsprintf()

int __xvsprintf ( char *  str,
const char *  format,
va_list  ap 
)

Tracked vsprintf replacement.

Like vsprintf, but make sure that the buffer does not overflow if the buffer is tracked. Else, act exactly like vsprintf.

Parameters
strBuffer where to write the resulting string.
formatFormat string.
apVariables argument list for the format string.
Returns
int Number of byte written to the buffer.

◆ list_bufstats()

void list_bufstats ( dbref  player)

Function placeholder. Not used for now.

Parameters
playerdbref of the player who did the command

◆ list_buftrace()

void list_buftrace ( dbref  player)

Function placeholder. Not used for now.

Parameters
playerdbref of the player who did the command

◆ list_rawmemory()

void list_rawmemory ( dbref  player)

Show to the player a summary of all allocations.

Called by @list raw_memory. This is primary a debug function and may cause lag on a very large game. Use with caution.

Parameters
playerDbref of the player making the request.

◆ total_rawmemory()

size_t total_rawmemory ( void  )

Calculate the total size of all allocations.

Returns
size_t Memory usage.