Choco OS  V.0.16.9.0
Join to the chocolate world
oc_stdlib.c
Go to the documentation of this file.
1 
27 #include <oc_stdlib.h>
28 #include <oc_memman.h>
29 #include <oc_processman.h>
30 
36 #define _________________________________________INTERFACE_FUNCTIONS_SECTION_______________________________________________________________
37 
38 //==========================================================================================================================================
39 //==========================================================================================================================================
40 void * _malloc( oC_UInt_t Size , const char * Function, uint32_t LineNumber , AllocationFlags_t Flags )
41 {
42  void * address = NULL;
43 
44  Allocator_t allocator = oC_ProcessMan_GetCurrentAllocator();
45 
46  if(allocator)
47  {
48  address = oC_MemMan_Allocate(Size,allocator,Function,LineNumber,Flags,0);
49  }
50 
51  return address;
52 }
53 
54 //==========================================================================================================================================
55 //==========================================================================================================================================
56 bool _free( void * Address , AllocationFlags_t Flags )
57 {
58  return oC_MemMan_Free(Address,Flags);
59 }
60 
61 //==========================================================================================================================================
62 //==========================================================================================================================================
63 void * _rawmalloc( oC_UInt_t Size , const char * Function, uint32_t LineNumber , AllocationFlags_t Flags)
64 {
65  void * address = NULL;
66 
67  oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
68 
69  if(oC_Process_IsCorrect(process))
70  {
71  oC_HeapMap_t map = oC_Process_GetHeapMap(process);
72 
73  if(map)
74  {
75  address = oC_MemMan_RawAllocate(map,Size,Function,LineNumber,Flags);
76  }
77  }
78 
79  return address;
80 }
81 
82 //==========================================================================================================================================
83 //==========================================================================================================================================
84 bool _rawfree( void * Address , oC_UInt_t Size)
85 {
86  bool released = false;
87 
88  oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
89 
90  if(oC_Process_IsCorrect(process))
91  {
92  oC_HeapMap_t map = oC_Process_GetHeapMap(process);
93 
94  if(map)
95  {
96  released = oC_MemMan_RawFree(map,Address,Size);
97  }
98  }
99 
100  return released;
101 }
102 
103 //==========================================================================================================================================
104 //==========================================================================================================================================
105 void * _kmalloc( oC_UInt_t Size , Allocator_t Allocator , const char * Function, uint32_t LineNumber , AllocationFlags_t Flags , oC_UInt_t Alignment )
106 {
107  return oC_MemMan_Allocate(Size,Allocator,Function,LineNumber,Flags,Alignment);
108 }
109 
110 //==========================================================================================================================================
111 //==========================================================================================================================================
112 bool _kfree( void * Address , AllocationFlags_t Flags )
113 {
114  return oC_MemMan_Free(Address,Flags);
115 }
116 
117 //==========================================================================================================================================
118 //==========================================================================================================================================
119 void * _krawmalloc( oC_HeapMap_t Map , oC_UInt_t Size , const char * Function, uint32_t LineNumber , AllocationFlags_t Flags )
120 {
121  return oC_MemMan_RawAllocate(Map,Size,Function,LineNumber,Flags);
122 }
123 
124 //==========================================================================================================================================
125 //==========================================================================================================================================
126 bool _krawfree( oC_HeapMap_t Map , void * Address , oC_UInt_t Size )
127 {
128  return oC_MemMan_RawFree(Map,Address,Size);
129 }
130 
131 //==========================================================================================================================================
132 //==========================================================================================================================================
133 void * _smartalloc( oC_UInt_t Size , const char * Function, uint32_t LineNumber , AllocationFlags_t Flags )
134 {
135  void * address = NULL;
136 
137  oC_UInt_t realRawAllocationSize = Size + ((Size/8) + (((Size%8) > 0) ? 1 : 0));
138  oC_UInt_t realAllocationSize = Size + oC_MemMan_GetAllocationBlockSize();
139 
140  if(realAllocationSize >= realRawAllocationSize)
141  {
142  address = _rawmalloc(Size,Function,LineNumber,Flags);
143  }
144  if(address == NULL)
145  {
146  address = _malloc(Size,Function,LineNumber,Flags);
147  }
148 
149  return address;
150 }
151 //==========================================================================================================================================
152 //==========================================================================================================================================
153 bool _smartfree( void * Address , oC_UInt_t Size , AllocationFlags_t Flags )
154 {
155  bool released = _rawfree(Address,Size);
156 
157  if(!released)
158  {
159  released = _free(Address,Flags);
160  }
161 
162  return released;
163 }
164 
165 //==========================================================================================================================================
166 //==========================================================================================================================================
167 void * _ksmartalloc( oC_UInt_t Size , Allocator_t Allocator , const char * Function, uint32_t LineNumber , AllocationFlags_t Flags)
168 {
169  void * address = _smartalloc(Size,Function,LineNumber,Flags);
170 
171  if(address == NULL)
172  {
173  address = _kmalloc(Size,Allocator,Function,LineNumber,Flags,0);
174  }
175 
176  return address;
177 }
178 
179 //==========================================================================================================================================
180 //==========================================================================================================================================
181 bool _ksmartfree( void * Address , oC_UInt_t Size , AllocationFlags_t Flags)
182 {
183  bool released = _smartfree(Address,Size,Flags);
184 
185  if(released == false)
186  {
187  released = _kfree(Address,Flags);
188  }
189 
190  return released;
191 }
192 
193 //==========================================================================================================================================
194 //==========================================================================================================================================
195 bool isram( const void * Address )
196 {
197  return oC_MemMan_IsRamAddress(Address);
198 }
199 
200 //==========================================================================================================================================
201 //==========================================================================================================================================
202 bool isrom( const void * Address )
203 {
204  return oC_MemMan_IsFlashAddress(Address);
205 }
206 
207 //==========================================================================================================================================
208 //==========================================================================================================================================
209 bool isbufferinram( const void * Buffer , oC_UInt_t Size )
210 {
211  return isram(Buffer) && isram(&((uint8_t*)Buffer)[Size-1]);
212 }
213 
214 //==========================================================================================================================================
215 //==========================================================================================================================================
216 bool isbufferinrom( const void * Buffer , oC_UInt_t Size )
217 {
218  return isrom(Buffer) && isrom(&((uint8_t*)Buffer)[Size-1]);
219 }
220 
221 //==========================================================================================================================================
222 //==========================================================================================================================================
223 bool isaddresscorrect( const void * Address )
224 {
225  return oC_MemMan_IsAddressCorrect(Address);
226 }
227 
228 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION_______________________________________________________________
229 
bool oC_MemMan_IsRamAddress(const void *Address)
checks if address is in ram section
Definition: oc_memman.c:1542
identifier for allocations
Definition: oc_stdlib.h:159
void * oC_MemMan_RawAllocate(oC_HeapMap_t Map, oC_UInt_t Size, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags)
allow to allocate memory in heap map
Definition: oc_memman.c:643
The file with interface for process manager.
bool oC_MemMan_RawFree(oC_HeapMap_t Map, void *Address, oC_UInt_t Size)
release memory in heap map
Definition: oc_memman.c:692
void * oC_MemMan_Allocate(oC_UInt_t Size, Allocator_t Allocator, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags, oC_UInt_t Alignment)
allocates memory on heap
Definition: oc_memman.c:770
oC_UInt_t oC_MemMan_GetAllocationBlockSize(void)
returns size of block needed for allocation
Definition: oc_memman.c:1466
bool oC_MemMan_IsAddressCorrect(const void *Address)
checks if address is correct - in RAM or in FLASH
Definition: oc_memman.c:1577
bool oC_MemMan_Free(void *Address, AllocationFlags_t Flags)
release allocated memory
Definition: oc_memman.c:908
The file with memory manager interface.
static const oC_Allocator_t Allocator
Definition: oc_eth.c:152
bool oC_MemMan_IsFlashAddress(const void *Address)
checks if address is placed in the flash section
Definition: oc_memman.c:1522
#define NULL
pointer to a zero
Definition: oc_null.h:37