Memory allocation


Detailed Description

Memory allocation and size_t arithmetics.


Defines

#define MYMALLOC(target, type, count)
 Allocate count elements of type type and store the pointer to target.
#define MYMALLOCZ(target, type, count, init)
 Allocate count elements of type type, initialise each element to init, and store the pointer to target.
#define MYMALLOC2Z(target, type, count1, count2, init)   MYMALLOCZ(target, type, size_multiply(count1, count2), init)
 Allocate count1 times count2 elements of type type, initialise each element to init, and store the pointer to target.

Functions

static size_t size_multiply (size_t a, size_t b)
 Multiply a by b; exit with failure if an overflow occurs.
static void * mymalloc (size_t s)
 Allocate s bytes; exit with failure if out of memory.


Define Documentation

#define MYMALLOC ( target,
type,
count   ) 

Value:

do { \
        type *my_result = mymalloc(size_multiply(sizeof(type), count)); \
        target = my_result; \
    } while (0)
Allocate count elements of type type and store the pointer to target.

Definition at line 145 of file types.c.

Referenced by prepare_slots(), and process_input().

#define MYMALLOC2Z ( target,
type,
count1,
count2,
init   )     MYMALLOCZ(target, type, size_multiply(count1, count2), init)

Allocate count1 times count2 elements of type type, initialise each element to init, and store the pointer to target.

Definition at line 163 of file types.c.

Referenced by calculate_statistics().

#define MYMALLOCZ ( target,
type,
count,
init   ) 

Value:

do { \
        size_t my_count = count; \
        type *my_result = mymalloc(size_multiply(sizeof(type), my_count)); \
        for (size_t my_i = 0; my_i < my_count; my_i++) { \
            my_result[my_i] = init; \
        } \
        target = my_result; \
    } while (0)
Allocate count elements of type type, initialise each element to init, and store the pointer to target.

Definition at line 152 of file types.c.


Function Documentation

static void* mymalloc ( size_t  s  )  [static]

Allocate s bytes; exit with failure if out of memory.

Definition at line 181 of file types.c.

static size_t size_multiply ( size_t  a,
size_t  b 
) [static]

Multiply a by b; exit with failure if an overflow occurs.

Definition at line 168 of file types.c.