1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/oneway-malloc/oneway-malloc.c
René Kijewski 1b89f334e3 msp430: provide oneway-malloc implicitly
For MSP430 boards oneway-malloc is already used *if* `malloc.h` was
included. The problem is that `malloc.h` is not a standard header, even
though it is common. `stdlib.h` in the right place to look for
`malloc()` and friends.

This change removes this discrepancy. `malloc()` is just named like
that, without the leading underscore. The symbols now are weak, which
means that they won't override library functions if MSP's standard
library will provide these functions at some point. (Unlikely, since
using `malloc()` on tiny systems is less then optimal ...)

Closes #1061 and #863.
2014-05-22 15:40:25 +02:00

79 lines
1.5 KiB
C

/*
* Copyright (C) 2013 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @addtogroup oneway_malloc
* @ingroup sys
* @{
*
* @file oneway_malloc.c
* @brief Simple malloc wrapper for SBRK
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include <string.h>
#include "malloc.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
extern void *sbrk(int incr);
void __attribute__((weak)) *malloc(size_t size)
{
if (size != 0) {
void *ptr = sbrk(size);
DEBUG("malloc(): allocating block of size %u at %p.\n", (unsigned int) size, ptr);
if (ptr != (void*) -1) {
return ptr;
}
}
return NULL;
}
void __attribute__((weak)) *realloc(void *ptr, size_t size)
{
if (ptr == NULL) {
return malloc(size);
}
else if (size == 0) {
free(ptr);
return NULL;
}
else {
void *newptr = malloc(size);
if (newptr) {
memcpy(newptr, ptr, size);
}
return newptr;
}
}
void __attribute__((weak)) *calloc(int size, size_t cnt)
{
void *mem = malloc(size * cnt);
if (mem) {
memset(mem, 0, size * cnt);
}
return mem;
}
void __attribute__((weak)) free(void *ptr)
{
/* who cares about pointers? */
(void) ptr;
DEBUG("free(): block at %p lost.\n", ptr);
}