2013-11-27 16:28:31 +01:00
|
|
|
/*
|
2010-09-22 15:10:42 +02:00
|
|
|
* simple malloc wrapper for sbrk
|
|
|
|
*
|
|
|
|
* Needed on platforms without malloc in libc, e.g. msb430
|
|
|
|
*
|
2013-06-18 17:21:38 +02:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2013-11-22 20:47:05 +01:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
2013-06-18 17:21:38 +02:00
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
2013-11-28 13:25:33 +01:00
|
|
|
*/
|
2013-11-27 16:28:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup core_util
|
2010-09-22 15:10:42 +02:00
|
|
|
* @{
|
2013-11-27 16:28:31 +01:00
|
|
|
*
|
|
|
|
* @file oneway_malloc.c
|
|
|
|
* @brief Simple malloc wrapper for SBRK
|
|
|
|
*
|
|
|
|
* Simple malloc implementation for plattforms without malloc in libc.
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
|
|
|
|
*
|
2010-09-22 15:10:42 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2010-12-03 18:42:03 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-07-24 00:36:06 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "debug.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-02-11 22:10:03 +01:00
|
|
|
extern void *sbrk(int incr);
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void *_malloc(size_t size)
|
|
|
|
{
|
|
|
|
void *ptr = sbrk(size);
|
|
|
|
|
2013-02-06 13:20:21 +01:00
|
|
|
DEBUG("_malloc(): allocating block of size %u at 0x%X.\n", (unsigned int) size, (unsigned int)ptr);
|
2013-06-20 18:18:29 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (ptr != (void*) - 1) {
|
2010-09-22 15:10:42 +02:00
|
|
|
return ptr;
|
2013-06-20 18:18:29 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-09-22 15:10:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void *_realloc(void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
void *newptr = _malloc(size);
|
2010-09-30 16:07:00 +02:00
|
|
|
memcpy(newptr, ptr, size);
|
|
|
|
free(ptr);
|
|
|
|
return newptr;
|
|
|
|
}
|
|
|
|
|
2013-08-06 19:25:39 +02:00
|
|
|
void *_calloc(int size, size_t cnt)
|
|
|
|
{
|
|
|
|
void *mem = _malloc(size * cnt);
|
|
|
|
if (mem) {
|
|
|
|
memset(mem, 0, size * cnt);
|
|
|
|
}
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void _free(void *ptr)
|
|
|
|
{
|
2013-08-08 18:34:51 +02:00
|
|
|
/* who cares about pointers? */
|
|
|
|
(void) ptr;
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
DEBUG("_free(): block at 0x%X lost.\n", (unsigned int)ptr);
|
|
|
|
}
|