TinyMUSH 3.3
TinyMUSH Server
|
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>
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... | |
memory management subsystem
#define XLOGALLOC | ( | x, | |
y, | |||
z, | |||
s, | |||
... | |||
) |
Macros and utilities.
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.
file | Filename where the allocation is done. |
line | Line number where the allocation is done. |
function | Function where the allocation is done. |
var | Name of the variable that will receive the pointer (not the variable itself). |
format | Format string. |
... | Variables arguments for the format string. |
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.
file | Filename where the allocation is done. |
line | Line number where the allocation is done. |
function | Function where the allocation is done. |
var | Name of the variable that will receive the pointer (not the variable itself). |
format | Format string. |
... | Variables arguments for the format string. |
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.
nmemb | Number of elements. |
size | Size of each elements. |
file | Filename where the allocation is done. |
line | Line number where the allocation is done. |
function | Function where the allocation is done. |
var | Name of the variable that will receive the pointer (not the variable itself). |
int __xcheck | ( | void * | ptr | ) |
Check if a buffer wasn't overrun.
ptr | Pointer to check. |
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.
ptr | Pointer to or somewhere in the allocated memory block. |
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.
ptr | Pointer to the allocation to be freed. |
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.
size | Size of memory to allocate. |
file | Filename where the allocation is done. |
line | Line number where the allocation is done. |
function | Function where the allocation is done. |
var | Name of the variable that will receive the pointer (not the variable itself). |
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.
dest | Pointer to destination buffer. |
src | Pointer to source buffer. |
n | Number of bytes to copy. |
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.
dest | Pointer to destination buffer. |
src | Pointer to source buffer. |
n | Number of bytes to copy. |
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.
dest | Pointer to destination buffer. |
src | Pointer to source buffer. |
n | Number of bytes to copy. |
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.
s | Pointer to the memory area to fill. |
c | Character to fill with. |
n | Size of the memory area to fill. |
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.
ptr | Pointer to the allocation to be resize. |
size | Size of memory to allocate. |
file | Filename where the allocation is done. |
line | Line number where the allocation is done. |
function | Function where the allocation is done. |
var | Name of the variable that will receive the pointer (not the variable itself). |
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.
size | Size of the string to build. |
c | Character to fill the string with. |
void __xsafeltos | ( | char * | dest, |
char ** | destp, | ||
long | num, | ||
size_t | size | ||
) |
Convert a long signed number into string.
dest | Pointer to destination buffer. |
destp | Pointer tracking the desstination buffer. |
num | Number to convert. |
size | Maximum size of the destination buffer. |
char * __xsafesprintf | ( | char * | buff, |
char ** | bufp, | ||
const char * | format, | ||
... | |||
) |
tracked safe_printf replacement.
buff | Buffer that will receive the result |
bufp | Pointer to the location in the buffer |
format | Format string. |
... | Variables argument list for the format string. |
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.
c | Char to copy |
dest | Pointer to destination buffer. |
destp | Pointer tracking the desstination buffer. |
size | Maximum size of the destination buffer. |
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.
dest | Pointer to destination buffer. |
destp | Pointer tracking the desstination buffer. |
src | Pointer to the string to concatenate. |
n | Maximum number of characters to concatenate. |
size | Maximum size of dest buffer if untracked. |
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.
dest | Pointer to destination buffer. |
destp | Pointer tracking the desstination buffer. |
src | Pointer to the string to concatenate. |
n | Maximum number of characters to concatenate. |
size | Maximum size of dest buffer if untracked. |
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.
str | Buffer where to write the resulting string. |
size | Maximum size to write to the buffer. |
format | Format string. |
... | Variables arguments for the format string. |
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()
p1 | Pointer to the first MEMTRACK struct to compare. |
p2 | Pointer to the second MEMTRACK struct to compare. |
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.
str | Buffer where to write the resulting string. |
format | Format string. |
... | Variables argument list for the format string. |
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.
str | Buffer where to write the resulting string. |
format | Format string. |
... | Variables argument list for the format string. |
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.
dest | Pointer to destination buffer. |
src | Pointer to source buffer. |
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.
dest | Pointer to destination buffer. |
src | Pointer to source buffer. |
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.
dest | Pointer to destination buffer. |
src | Source character. |
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.
dest | Pointer to destination buffer. |
src | Pointer to source buffer. |
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.
s | String to duplicate. |
file | Filename where the allocation is done. |
line | Line number where the allocation is done. |
function | Function where the allocation is done. |
var | Name of the variable that will receive the pointer (not the variable itself). |
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.
dest | Pointer to destination buffer. |
src | Pointer to source buffer. |
n | Maximum numbers of characters to copy. |
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.
dest | Pointer to destination buffer. |
src | Source character. |
n | Maximum length of dest. |
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.
dest | Pointer to destination buffer. |
src | Pointer to source buffer. |
n | Maximum numbers of characters to copy. |
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.
s | String to duplicate. |
n | Number of bytes to replicates. |
file | Filename where the allocation is done. |
line | Line number where the allocation is done. |
function | Function where the allocation is done. |
var | Name of the variable that will receive the pointer (not the variable itself). |
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.
str | Buffer where to write the resulting string. |
size | Maximum size to write to the buffer. |
format | Format string. |
ap | Variables argument list for the format string. |
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.
str | Buffer where to write the resulting string. |
format | Format string. |
ap | Variables argument list for the format string. |
void list_bufstats | ( | dbref | player | ) |
Function placeholder. Not used for now.
player | dbref of the player who did the command |
void list_buftrace | ( | dbref | player | ) |
Function placeholder. Not used for now.
player | dbref of the player who did the command |
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.
player | Dbref of the player making the request. |
size_t total_rawmemory | ( | void | ) |
Calculate the total size of all allocations.