c-template
safe_mem.c
Go to the documentation of this file.
1
#include <stdlib.h>
2
#include <stdbool.h>
3
#include "../../include/utils/safe_mem.h"
4
5
/*
6
free_memory_object_data can be used to safely free up allocated memory.
7
the first time you call free_memory_object the memory is freed and
8
the void pointer is set to a null pointer
9
the second time you call free_memory_object a -1 is returned
10
*/
11
int
free_memory_object_data
(
memory_object
*obj) {
12
// check to see if we have freed the memory before
13
if
(obj->
freed
) {
14
// return -1 indicating error
15
return
-1;
16
}
17
// set freed to true
18
obj->
freed
=
true
;
19
// deallocate the memory returning it to os
20
free(obj->
data
);
21
// set the data to a null pointer
22
obj->
data
= NULL;
23
return
0;
24
}
25
26
/*
27
new_memory_object is used to return an initialized memory_object
28
with the data member set to the void pointer
29
*/
30
memory_object
new_memory_object
(
void
*input) {
31
memory_object
mobj;
32
mobj.
freed
=
false
;
33
mobj.
data
= input;
34
return
mobj;
35
}
memory_object
Definition:
safe_mem.h:9
memory_object::freed
bool freed
Definition:
safe_mem.h:11
free_memory_object_data
int free_memory_object_data(memory_object *obj)
Definition:
safe_mem.c:11
memory_object::data
void * data
Definition:
safe_mem.h:10
new_memory_object
memory_object new_memory_object(void *input)
Definition:
safe_mem.c:30
src
utils
safe_mem.c
Generated by
1.8.17