memory.c
Go to the documentation of this file.
1 /*!**********************************************************************
2 *
3 * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4 * All rights reserved.
5 *
6 *************************************************************************
7 *
8 * @file memory.c
9 * @date Jul 28, 2016
10 * @brief Memory module implementation file.
11 *
12 ************************************************************************/
13 
14 #include "memory.h"
15 #include "definitions.h"
16 #include "log_util.h"
17 
18 #if (1 == HAVE_STDLIB_H_)
19 #include <stdlib.h>
20 #endif
21 
22 void *memory_allocate(mcl_size_t bytes, const char *function, unsigned line)
23 {
24  VERBOSE_ENTRY("mcl_size_t bytes = <%u>, const char *function = <%s>, unsigned line = <%u>", bytes, function, line)
25 
26  void *p = MCL_NULL;
27 
28  ASSERT_MESSAGE(0 != bytes, "Requested bytes size is equal to 0!");
29 
30  p = memory_malloc(bytes);
31 
32  ASSERT_MESSAGE(MCL_NULL != p, "Memory couldn't be allocated!");
33 
34  MCL_VERBOSE("%d bytes allocated in memory successfully. Address = <%p>", bytes, p);
35  VERBOSE_LEAVE("retVal = <%p>", p);
36  return p;
37 }
38 
39 void *memory_allocate_with_zero(mcl_size_t count, mcl_size_t bytes, const char *function, unsigned line)
40 {
41  VERBOSE_ENTRY("mcl_size_t count = <%u>, mcl_size_t bytes = <%u>, const char *function = <%s>, unsigned line = <%u>", count, bytes, function, line)
42 
43  void *p;
44 
45  ASSERT_MESSAGE(0 != count, "Requested count size is equal to 0!");
46  ASSERT_MESSAGE(0 != bytes, "Requested bytes size is equal to 0!");
47 
48  p = memory_calloc(count, bytes);
49 
50  ASSERT_MESSAGE(p, "Memory couldn't be allocated!");
51 
52  MCL_VERBOSE("%d bytes allocated in memory successfully. Address = <%p>", count * bytes, p);
53  VERBOSE_LEAVE("retVal = <%p>", p);
54  return p;
55 }
56 
57 void *memory_reallocate(void *p, mcl_size_t bytes, const char *function, unsigned line)
58 {
59  VERBOSE_ENTRY("void *p = <%p>, mcl_size_t bytes = <%u>, const char *function = <%s>, unsigned line = <%u>", p, bytes, function, line)
60 
61  void *temp = MCL_NULL;
62 
63  ASSERT_MESSAGE(0 != bytes, "Requested bytes size is equal to 0!");
64 
65  temp = memory_realloc(p, bytes);
66  if (MCL_NULL != temp)
67  {
68  p = temp;
69  }
70  else
71  {
72  MCL_VERBOSE("Memory couldn't be re-allocated! Freeing the original one.");
73  MCL_FREE(p);
74  VERBOSE_LEAVE("retVal = <%p>", p);
75  return p;
76  }
77 
78  MCL_VERBOSE("%d bytes re-allocated in memory successfully. Address = <%p>", bytes, p);
79  VERBOSE_LEAVE("retVal = <%p>", p);
80  return p;
81 }
82 
83 void memory_release(void *p, const char *function, unsigned line)
84 {
85  VERBOSE_ENTRY("void *p = <%p>, const char *function = <%s>, unsigned line = <%u>", p, function, line)
86 
87  if (MCL_NULL != p)
88  {
89  memory_free(p);
90  }
91  else
92  {
93  MCL_VERBOSE("Pointer is already NULL!");
94  }
95 
96  VERBOSE_LEAVE("retVal = void");
97 }
98 
100 {
101  VERBOSE_ENTRY("mcl_size_t size = <%u>", size)
102 
103  void *p = malloc(size);
104 
105  VERBOSE_LEAVE("retVal = <%p>", p);
106  return p;
107 }
108 
110 {
111  VERBOSE_ENTRY("mcl_size_t count = <%u>, mcl_size_t bytes = <%u>", count, bytes)
112 
113  void *p = calloc(count, bytes);
114 
115  VERBOSE_LEAVE("retVal = <%p>", p);
116  return p;
117 }
118 
119 void *memory_realloc(void *p, mcl_size_t bytes)
120 {
121  VERBOSE_ENTRY("void *p = <%p>, mcl_size_t bytes = <%u>", p, bytes)
122 
123  p = realloc(p, bytes);
124 
125  VERBOSE_LEAVE("retVal = <%p>", p);
126  return p;
127 }
128 
129 void memory_free(void *p)
130 {
131  VERBOSE_ENTRY("void *p = <%p>", p)
132 
133  MCL_VERBOSE("Memory will be freed at location = <%p>.", p);
134  free(p);
135 
136  VERBOSE_LEAVE("retVal = void");
137 }
Memory module header file.
#define VERBOSE_LEAVE(...)
Definition: log_util.h:66
void * memory_allocate_with_zero(mcl_size_t count, mcl_size_t bytes, const char *function, unsigned line)
Definition: memory.c:39
#define ASSERT_MESSAGE(condition,...)
Definition: definitions.h:82
Log utility module header file.
void * memory_allocate(mcl_size_t bytes, const char *function, unsigned line)
Definition: memory.c:22
void * memory_malloc(mcl_size_t size)
malloc wrapper
Definition: memory.c:99
#define MCL_FREE(p)
Definition: memory.h:125
#define VERBOSE_ENTRY(...)
Definition: log_util.h:65
Definitions module header file.
void memory_free(void *p)
free wrapper
Definition: memory.c:129
void * memory_reallocate(void *p, mcl_size_t bytes, const char *function, unsigned line)
Definition: memory.c:57
size_t mcl_size_t
Definition: mcl_common.h:38
void memory_release(void *p, const char *function, unsigned line)
Definition: memory.c:83
#define MCL_VERBOSE(...)
Definition: log_util.h:57
#define MCL_NULL
Definition: definitions.h:24
void * memory_calloc(mcl_size_t count, mcl_size_t bytes)
calloc wrapper
Definition: memory.c:109
void * memory_realloc(void *p, mcl_size_t bytes)
realloc wrapper
Definition: memory.c:119