Memory Management¶
Functions for allocating, reallocating, and freeing memory.
Defines
-
rs_new(type, n)¶
Dynamic array allocator.
This function uses rs_malloc() to create a dynamic array with the given type and length, and automatically casts it to the correct pointer type. You should use this instead of rs_malloc(), most of the time.
See also
rs_free, rs_new0
- Parameters:
type – the type to allocate for
n – the number of elements to allocate
- Returns:
the allocated memory
-
rs_new0(type, n)¶
Dynamic array allocator (zero-initialized).
This function uses rs_malloc0() to create a dynamic array with the given type and length, and automatically casts it to the correct pointer type. You should use this instead of rs_malloc0(), most of the time.
See also
rs_free, rs_new
- Parameters:
type – the type to allocate for
n – the number of elements to allocate
- Returns:
the allocated memory (filled with zeros)
-
rs_renew(type, mem, n)¶
Dynamic array reallocator.
This function uses rs_realloc() to resize a dynamic array returned by rs_new(). You should generally use this instead of rs_realloc().
See also
rs_free, rs_new
- Parameters:
type – the type to allocate for
mem – the old memory to reallocate
n – the number of elements to allocate
- Returns:
the allocated memory
-
rs_strdup(str)¶
Convenience string duplicator.
This function duplicates the given string (using rs_memdup()) and returns the duplicate as a freshly-allocated chunk of memory that must be freed with rs_free().
See also
rs_free, rs_memdup
- Parameters:
str – the string to duplicate
- Returns:
the duplicated string
Typedefs
-
typedef void *(*RSMalloc)(void*, size_t)¶
malloc function type
-
typedef void *(*RSRealloc)(void*, void*, size_t)¶
realloc function type
-
typedef void (*RSFree)(void*, void*)¶
free function type
Functions
-
void rs_set_memory_functions(RSMemoryFunctions *funcs)¶
Set the memory management vtable.
Use this to tell libredstone what memory functions to use. See RSMemoryFunctions for more information.
See also
- Parameters:
funcs – the vtable to use
-
void *rs_malloc(size_t size)¶
A safer malloc.
This function uses the user-provided memory functions, and will automatically check the return value for you. It will never return NULL.
You should generally use rs_new() instead.
See also
rs_malloc0, rs_free, rs_new
- Parameters:
size – the amount of memory to allocate
- Returns:
the allocated memory
-
void *rs_malloc0(size_t size)¶
A malloc that returns zero-filled buffers.
This function is exactly like rs_malloc(), but the buffer it returns is already filled with zeros.
You should generally use rs_new0() instead.
See also
rs_malloc, rs_free, rs_new0
- Parameters:
size – the amount of memory to allocate
- Returns:
the allocated memory, filled with zeros
-
void *rs_realloc(void *ptr, size_t size)¶
A safer realloc.
This function uses the user-provided memory functions, and will automatically check the return value for you. It will never return NULL.
If you pass a NULL pointer as the original memory, this will just act like a call to rs_malloc().
You should generally use rs_renew() instead.
See also
rs_malloc, rs_free, rs_renew
- Parameters:
ptr – the memory to reallocate
size – the amount of memory to allocate
-
void rs_free(void *ptr)¶
A safer free.
This function uses the user-provided memory functions, and will do nothing if the pointer provided is NULL.
See also
rs_malloc
- Parameters:
ptr – the memory to free
-
void *rs_memdup(const void *ptr, size_t size)¶
Convenience memory duplicator.
This function will duplicate the memory given, and return the duplicate as a freshly-allocated array that must be freed with rs_free().
See also
rs_free, rs_strdup
- Parameters:
ptr – the memory to duplicate
size – the number of bytes to duplicate
- Returns:
the duplicated memory
-
struct RSMemoryFunctions¶
- #include <memory.h>
Memory management vtable.
You can tell libredstone what memory management functions to use by populating a struct like this. All of the functions provided must accept a pointer to the struct they are listed in as the first argument; through this, you can pass in whatever data you want. Apart from this extra first argument, these functions should conform to the usual specs.
See also
rs_set_memory_functions