Browse Source
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.dev/timer

9 changed files with 101 additions and 153 deletions
@ -1,75 +0,0 @@
|
||||
/*
|
||||
* 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 core_util |
||||
* @{ |
||||
* |
||||
* @file oneway_malloc.h |
||||
* @brief Simple malloc wrapper for sbrk |
||||
* |
||||
* @author Freie Universität Berlin, Computer Systems & Telematics |
||||
* @author Kaspar Schleiser <kaspar@schleiser.de> |
||||
* |
||||
* @detail Simple malloc implementation for plattforms |
||||
* without malloc in libc, e.g. msb430. |
||||
*/ |
||||
|
||||
#ifndef _MALLOC_H_ |
||||
#define _MALLOC_H_ |
||||
|
||||
#include <stddef.h> |
||||
|
||||
/**
|
||||
* @brief Allocates a block of `size` bytes of memory |
||||
* |
||||
* @param[in] Size of the memory block, in bytes. |
||||
* |
||||
* @return On success, a pointer to the memory block allocated by the function. |
||||
* @return NULL otherwise. |
||||
*/ |
||||
void *_malloc(size_t size); |
||||
|
||||
/**
|
||||
* @brief Changes the size of the memory block pointed to by ptr. |
||||
* |
||||
* @param[in] Pointer to a memory block previously allocated with malloc, |
||||
* calloc or realloc. |
||||
* |
||||
* @param[in] New size for the memory block, in bytes. |
||||
* |
||||
* @return A pointer to the reallocated memory block, which may be either |
||||
* the same as ptr or a new location. |
||||
*/ |
||||
void *_realloc(void *ptr, size_t size); |
||||
|
||||
/**
|
||||
* @brief Allocates a block of memory for an array of num elements, |
||||
* each of them size bytes long, and initializes all its bits to zero. |
||||
* |
||||
* @param[in] Number of elements to allocate. |
||||
* @param[in] Size of each element. |
||||
* |
||||
* @return On success, a pointer to the memory block allocated by the function. |
||||
* If the function failed to allocate the requested block of memory, |
||||
* a null pointer is returned. |
||||
*/ |
||||
void *_calloc(int size, size_t cnt); |
||||
|
||||
/**
|
||||
* @brief A block of memory previously allocated by a call to malloc, |
||||
* calloc or realloc is deallocated, making it available again |
||||
* for further allocations. |
||||
* |
||||
* @param[in] Pointer to a memory block previously allocated with malloc, |
||||
* calloc or realloc. |
||||
*/ |
||||
void _free(void *ptr); |
||||
|
||||
/** @} */ |
||||
#endif /* _MALLOC_H_ */ |
@ -1,67 +0,0 @@
|
||||
/*
|
||||
* 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @ingroup core_util |
||||
* @{ |
||||
* |
||||
* @file oneway_malloc.c |
||||
* @brief Simple malloc wrapper for SBRK |
||||
|
||||
* @author Kaspar Schleiser <kaspar@schleiser.de> |
||||
* |
||||
* @} |
||||
*/ |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include <stdlib.h> |
||||
|
||||
#define ENABLE_DEBUG (0) |
||||
#include "debug.h" |
||||
|
||||
extern void *sbrk(int incr); |
||||
|
||||
void *_malloc(size_t size) |
||||
{ |
||||
void *ptr = sbrk(size); |
||||
|
||||
DEBUG("_malloc(): allocating block of size %u at 0x%X.\n", (unsigned int) size, (unsigned int)ptr); |
||||
|
||||
if (ptr != (void*) - 1) { |
||||
return ptr; |
||||
} |
||||
else { |
||||
return NULL; |
||||
} |
||||
} |
||||
|
||||
void *_realloc(void *ptr, size_t size) |
||||
{ |
||||
void *newptr = _malloc(size); |
||||
memcpy(newptr, ptr, size); |
||||
free(ptr); |
||||
return newptr; |
||||
} |
||||
|
||||
void *_calloc(int size, size_t cnt) |
||||
{ |
||||
void *mem = _malloc(size * cnt); |
||||
if (mem) { |
||||
memset(mem, 0, size * cnt); |
||||
} |
||||
return mem; |
||||
} |
||||
|
||||
void _free(void *ptr) |
||||
{ |
||||
/* who cares about pointers? */ |
||||
(void) ptr; |
||||
|
||||
DEBUG("_free(): block at 0x%X lost.\n", (unsigned int)ptr); |
||||
} |
@ -1 +1,3 @@
|
||||
INCLUDES += -I$(RIOTBASE)/cpu/msp430-common/include/
|
||||
|
||||
DEFAULT_MODULE += oneway_malloc
|
||||
|
@ -1,11 +0,0 @@
|
||||
#ifndef __MALLOC_H |
||||
#define __MALLOC_H |
||||
|
||||
#include "oneway_malloc.h" |
||||
|
||||
#define malloc _malloc |
||||
#define calloc _calloc |
||||
#define realloc _realloc |
||||
#define free _free |
||||
|
||||
#endif /* __MALLOC_H */ |
@ -0,0 +1,3 @@
|
||||
MODULE = oneway_malloc
|
||||
|
||||
include $(RIOTBASE)/Makefile.base |
@ -0,0 +1,11 @@
|
||||
#ifndef __MALLOC_H |
||||
#define __MALLOC_H |
||||
|
||||
#include <stdlib.h> |
||||
|
||||
void *malloc(size_t size); |
||||
void *realloc(void *ptr, size_t size); |
||||
void *calloc(int size, size_t cnt); |
||||
void free(void *ptr); |
||||
|
||||
#endif /* __MALLOC_H */ |
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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); |
||||
} |
Loading…
Reference in new issue