ardupilot/libraries/AP_HAL_ChibiOS/hwdef/common/malloc.c
2018-01-15 11:46:02 +11:00

65 lines
1.6 KiB
C

/*
* Copyright (C) Siddharth Bharat Purohit 2017
* This file is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
wrappers for allocation functions
Relies on linker wrap options
Note that not all functions that have been wrapped are implemented
here. The others are wrapped to ensure the function is not used
without an implementation. If we need them then we can implement as
needed.
*/
#include <stdio.h>
#include <string.h>
#include <hal.h>
#include <chheap.h>
#include <stdarg.h>
#define MIN_ALIGNMENT 8
void *malloc(size_t size)
{
if (size == 0) {
return NULL;
}
void *p = chHeapAllocAligned(NULL, size, MIN_ALIGNMENT);
if (p) {
memset(p, 0, size);
}
return p;
}
void *calloc(size_t nmemb, size_t size)
{
if (nmemb * size == 0) {
return NULL;
}
void *p = chHeapAllocAligned(NULL, nmemb*size, MIN_ALIGNMENT);
if (p != NULL) {
memset(p, 0, nmemb*size);
}
return p;
}
void free(void *ptr)
{
if(ptr != NULL) {
chHeapFree(ptr);
}
}