-
Notifications
You must be signed in to change notification settings - Fork 1
/
mem.h
48 lines (42 loc) · 1.4 KB
/
mem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* Copyright (c) 2023-2024 Gilad Odinak */
/* Memory data structures and functions */
#ifndef MEM_H
#define MEM_H
#include <stdlib.h> /* declares free() */
void* allocmem_int(size_t M, size_t N, int S, char* typ,
const char* func, char* file, int line);
/* Allocates contiguous memory for an array of MxN elements of type T,
* and initializes all elements to zero.
*
* Parameters:
* M : Number of rows in the array.
* N : Number of columns in the array.
* T : Type of the elements in the array (e.g., int, float, double, char).
*
* Returns:
* A pointer to the allocated memory block. If memory allocation fails, the
* function prints an error message and terminates the program.
*
* Examples:
* allocmem(128, 64, float)
* allocmem(1, 1000, char)
*
* Notice:
* If the representation of float zero (or double) is not binary zero,
* the memory will be correctly initialized to all zeros only if T is
* float (or double).
*
*/
#define allocmem(M,N,T) \
allocmem_int(M,N,sizeof(T),#T,__FUNCTION__,__FILE__,__LINE__)
/* Frees the memory allocated by the allocmem function.
*
* Parameters:
* p : Pointer to the memory block to be freed.
*
* Notice:
* It is okay to pass a NULL pointer to this function; it that case, it
* does nothing.
*/
inline static void freemem(void *p) { if (p != NULL) free(p); }
#endif