EzC
Macros
ezc_mem.h File Reference

Memory-related macros, such as allocation and array length getters. More...

#include "ezc/ezc_macro.h"
#include <stdlib.h>

Go to the source code of this file.

Macros

#define EZC_LENGTH(array)   (sizeof(array)/sizeof(array[0]))
 Get the length of an array. More...
 
#define EZC_NEW(ptr)   ((ptr) = malloc(sizeof *(ptr)))
 Allocate memory based on the size of the given pointer. More...
 
#define EZC_NEW0(ptr)   ((ptr) = calloc(1, sizeof *(ptr)))
 Allocate memory based on the size of the given pointer while also zero-initializing it. More...
 
#define EZC_NEWN(ptr, n)   ((ptr) = calloc((n), sizeof *(ptr)))
 Allocate array and zero-initialize it. More...
 
#define EZC_FREE(ptr, ...)
 Free memory and avoid dangling pointers. More...
 

Detailed Description

Memory-related macros, such as allocation and array length getters.

Macro Definition Documentation

◆ EZC_FREE

#define EZC_FREE (   ptr,
  ... 
)
Value:
(SST_MAP(EZC_DO_FREE, ptr, ##__VA_ARGS__), \
SST_MAP(EZC_TO_ZERO, ptr, ##__VA_ARGS__))
#define SST_MAP(f,...)
Map function to specified variables.
Definition: ezc_macro.h:103

Free memory and avoid dangling pointers.

This macro does both the free and ptr = 0 for you! Simply use EZC_FREE(ptr);.

Parameters
ptrPointer that you want set to zero and whose memory it was pointing to you want freed.
...Optional additional pointers you want to be freed.

◆ EZC_LENGTH

#define EZC_LENGTH (   array)    (sizeof(array)/sizeof(array[0]))

Get the length of an array.

This works via the sizeof(array)/sizeof(array[0]) technique.

Parameters
arrayPointer to an array.

◆ EZC_NEW

#define EZC_NEW (   ptr)    ((ptr) = malloc(sizeof *(ptr)))

Allocate memory based on the size of the given pointer.

Does the work of calling sizeof for you! This macro does the ptr = ... for you, all you need to do is EZC_NEW(ptr);.

Parameters
ptrPointer to which you want memory allocated.

◆ EZC_NEW0

#define EZC_NEW0 (   ptr)    ((ptr) = calloc(1, sizeof *(ptr)))

Allocate memory based on the size of the given pointer while also zero-initializing it.

EZC_NEW details documentation also applies to EZC_NEW0.

Parameters
ptrPointer to which you want memory allocated.

◆ EZC_NEWN

#define EZC_NEWN (   ptr,
 
)    ((ptr) = calloc((n), sizeof *(ptr)))

Allocate array and zero-initialize it.

EZC_NEW details documentation also applies to EZC_NEWN.

Parameters
ptrPointer to which you want memory allocated.