GATE
memalloc.h File Reference

Memory allocation and type construction/destruction functions. More...

#include "gate/gate_core_api.h"
#include "gate/atomics.h"

Classes

union  gate_c_maxalign_t
 

Macros

#define GATE_INVALID_SIZE   ((gate_size_t)-1)
 

Typedefs

typedef gate_result_t(* gate_mem_ctor_t) (void *dest)
 
typedef gate_result_t(* gate_mem_copyctor_t) (void *dest, void const *src)
 
typedef void(* gate_mem_dtor_t) (void *dest)
 

Functions

GATE_CORE_API void * gate_mem_alloc (gate_size_t sz)
 Allocates a block of memory on the process heap.
 
GATE_CORE_API void * gate_mem_realloc (void *ptr, gate_size_t newsize)
 Re-allocates a block of memory.
 
GATE_CORE_API void gate_mem_dealloc (void *ptr)
 Deallocates a block of memory.
 
GATE_CORE_API void * gate_mem_copy (void *dst, void const *src, gate_size_t sz)
 Copies the requested amount of bytes from a source buffer to a destination buffer.
 
GATE_CORE_API void * gate_mem_move (void *dst, void const *src, gate_size_t sz)
 Like gate_mem_copy() but handles overlapping areas of source and destination buffer.
 
GATE_CORE_API void gate_mem_clear (void *dst, gate_size_t sz)
 Clears all bits in a given buffer (fills with zero)
 
GATE_CORE_API void gate_mem_fill (void *dst, char chr, gate_size_t sz)
 Fills all bytes in a buffer with a given character.
 
GATE_CORE_API int gate_mem_compare (void const *ptr1, void const *ptr2, gate_size_t sz)
 Compares two memory blocks byte by byte.
 
GATE_CORE_API void gate_mem_reversebyteorder (void *ptr, gate_size_t sz)
 Reverts the byte order in a given buffer.
 
GATE_CORE_API void * gate_mem_copy_reverse (void *dst, void const *src, gate_size_t sz)
 Copies bytes from a source to a destination buffer in reverse order.
 
GATE_CORE_API gate_size_t gate_mem_align_size (gate_size_t sz)
 Returns a memory size that fits exactly in a aligned memory block.
 
GATE_CORE_API gate_result_t gate_mem_copy_construct (void *dest_item, void const *src_item, gate_size_t item_size, gate_mem_copyctor_t ctor)
 Copy constructs a data type using memcpy or a given constructor function.
 
GATE_CORE_API void gate_mem_destruct (void *dest_item, gate_mem_dtor_t dtor)
 Destructs a data type.
 
GATE_CORE_API gate_result_t gate_mem_move_construct (void *dest_item, void *src_item, gate_size_t item_size, gate_mem_copyctor_t ctor, gate_mem_dtor_t dtor)
 Move-constructs a data type, copies the data from source to destination and destroys source afterwards.
 
GATE_CORE_API gate_bool_t gate_mem_swap (void *item1, void *item2, gate_size_t item_size, gate_mem_copyctor_t ctor, gate_mem_dtor_t dtor)
 Swaps the contents of 2 type instances.
 

Detailed Description

Memory allocation and type construction/destruction functions.

Typedef Documentation

◆ gate_mem_copyctor_t

typedef gate_result_t(* gate_mem_copyctor_t) (void *dest, void const *src)

object copy constructor function, initializes a type on a preallocated memory block

◆ gate_mem_ctor_t

typedef gate_result_t(* gate_mem_ctor_t) (void *dest)

object constructor function, initializes a type on a preallocated memory block

◆ gate_mem_dtor_t

typedef void(* gate_mem_dtor_t) (void *dest)

object destructor function, releases object resources, no deallocation of the pointer itself

Function Documentation

◆ gate_mem_align_size()

GATE_CORE_API gate_size_t gate_mem_align_size ( gate_size_t sz)

Returns a memory size that fits exactly in a aligned memory block.

Parameters
[in]szMemory size to be aligned
Returns
Aligned memory size, will be equal or greater than sz

◆ gate_mem_alloc()

GATE_CORE_API void * gate_mem_alloc ( gate_size_t sz)

Allocates a block of memory on the process heap.

Parameters
[in]szAmount of bytes to allocate (must be greater than zero)
Returns
Pointer to begin of allocated block, or NULL in case of error

◆ gate_mem_clear()

GATE_CORE_API void gate_mem_clear ( void * dst,
gate_size_t sz )

Clears all bits in a given buffer (fills with zero)

Parameters
[in]dstPointer to buffer to be cleared
[in]szAmount of bytes in buffer to be cleared

◆ gate_mem_compare()

GATE_CORE_API int gate_mem_compare ( void const * ptr1,
void const * ptr2,
gate_size_t sz )

Compares two memory blocks byte by byte.

Parameters
[in]ptr1Pointer to first memory block
[in]ptr2Pointer to second memory block
[in]szAmount of bytes to compare
Returns
Returns 0 if both buffers are equal, -1 if ptr1 has a less value than ptr2, or 1 if ptr1 has a greater value than ptr2

◆ gate_mem_copy()

GATE_CORE_API void * gate_mem_copy ( void * dst,
void const * src,
gate_size_t sz )

Copies the requested amount of bytes from a source buffer to a destination buffer.

Parameters
[in]dstPointer to copy destination buffer
[in]srcPointer to copy source buffer
[in]szAmount of bytes to copy
Returns
Pointer to dst on success, or NULL in case of an error

◆ gate_mem_copy_construct()

GATE_CORE_API gate_result_t gate_mem_copy_construct ( void * dest_item,
void const * src_item,
gate_size_t item_size,
gate_mem_copyctor_t ctor )

Copy constructs a data type using memcpy or a given constructor function.

Parameters
[out]dest_itemPointer to destination were type is constructed
[in]src_itemPointer to source used to copy data from
[in]item_sizeSize of data type (used for memcpy when ctor is NULL)
[in]ctorOptional constructor function
Returns
GATE_RESULT_* result code

◆ gate_mem_copy_reverse()

GATE_CORE_API void * gate_mem_copy_reverse ( void * dst,
void const * src,
gate_size_t sz )

Copies bytes from a source to a destination buffer in reverse order.

Parameters
[in]dstPointer to destination buffer
[in]srcPointer to source buffer
[in]szAmount of bytes to copy in reverse order
Returns
Pointer to dst or NULL in case of an error

◆ gate_mem_dealloc()

GATE_CORE_API void gate_mem_dealloc ( void * ptr)

Deallocates a block of memory.

Parameters
[in]ptrPointer to allocated memory block

◆ gate_mem_destruct()

GATE_CORE_API void gate_mem_destruct ( void * dest_item,
gate_mem_dtor_t dtor )

Destructs a data type.

Parameters
[in]dest_itemPointer to type instance to be destroyed
[in]dtorOptional destructor function used to release internals of dest_item

◆ gate_mem_fill()

GATE_CORE_API void gate_mem_fill ( void * dst,
char chr,
gate_size_t sz )

Fills all bytes in a buffer with a given character.

Parameters
[in]dstPointer to buffer to be filled
[in]chrcharacter value used to fill the buffer
[in]szAmount of bytes in buffer to be filled

◆ gate_mem_move()

GATE_CORE_API void * gate_mem_move ( void * dst,
void const * src,
gate_size_t sz )

Like gate_mem_copy() but handles overlapping areas of source and destination buffer.

Parameters
[in]dstPointer to copy destination buffer
[in]srcPointer to copy source buffer
[in]szAmount of bytes to copy
Returns
Pointer to dst on success, or NULL in case of an error

◆ gate_mem_move_construct()

GATE_CORE_API gate_result_t gate_mem_move_construct ( void * dest_item,
void * src_item,
gate_size_t item_size,
gate_mem_copyctor_t ctor,
gate_mem_dtor_t dtor )

Move-constructs a data type, copies the data from source to destination and destroys source afterwards.

Parameters
[out]dest_itemPointer to destination were type is constructed
[in]src_itemPointer to source used to copy data from, will be destroyed if copying succeeds
[in]item_sizeSize of data type (used for memcpy when ctor is NULL)
[in]ctorOptional constructor function
[in]dtorOptional destructor function
Returns
GATE_RESULT_* result code

◆ gate_mem_realloc()

GATE_CORE_API void * gate_mem_realloc ( void * ptr,
gate_size_t newsize )

Re-allocates a block of memory.

Parameters
[in]ptrPointer to already allocated block
[in]newsizeNew requested size (must be greater than zero)
Returns
Pointer to new allocated block, or NULL in case of error

◆ gate_mem_reversebyteorder()

GATE_CORE_API void gate_mem_reversebyteorder ( void * ptr,
gate_size_t sz )

Reverts the byte order in a given buffer.

e.g. [1, 2, 3, 4, 5] will become [5, 4, 3, 2, 1]

Parameters
[in]ptrPointer to memory buffer
[in]szAmount of bytes in buffer

◆ gate_mem_swap()

GATE_CORE_API gate_bool_t gate_mem_swap ( void * item1,
void * item2,
gate_size_t item_size,
gate_mem_copyctor_t ctor,
gate_mem_dtor_t dtor )

Swaps the contents of 2 type instances.

Parameters
[out]item1Pointer to type that shall hold the content of item2
[in]item2Pointer to type that shall hold the content of item1
[in]item_sizeSize of data type (used for memcpy when ctor is NULL)
[in]ctorOptional constructor function
[in]dtorOptional destructor function
Returns
GATE_RESULT_* result code