50 #include "contiki-conf.h"
52 #include "profiling.h"
57 #define PRINTF(...) printf(__VA_ARGS__)
63 #define MMEM_SIZE MMEM_CONF_SIZE
65 #define MMEM_SIZE 4096
68 #ifdef MMEM_CONF_ALIGNMENT
69 #define MMEM_ALIGNMENT MMEM_CONF_ALIGNMENT
71 #define MMEM_ALIGNMENT 2
75 unsigned int avail_memory;
76 static char memory[MMEM_SIZE];
102 if((avail_memory < size) ) {
103 PRINTF(
"MMEM: %u < %u\n",avail_memory,size);
106 if (avail_memory> MMEM_SIZE){
107 PRINTF(
"MMEM: %u > %u\n",avail_memory,MMEM_SIZE);
118 m->ptr = &memory[MMEM_SIZE - avail_memory];
123 while( m->real_size % MMEM_ALIGNMENT != 0 ) {
128 avail_memory -= m->real_size;
146 if(m->real_size > MMEM_SIZE - avail_memory){
147 PRINTF(
"MMEM: too much free %u\n", m->real_size);
151 if(m->next !=
NULL) {
154 memmove(m->ptr, m->next->ptr,
155 &memory[MMEM_SIZE - avail_memory] - (
char *)m->next->ptr);
159 for(n = m->next; n !=
NULL; n = n->next) {
160 n->ptr = (
void *)((
char *)n->ptr - m->real_size);
164 avail_memory += m->real_size;
187 while( mysize % MMEM_ALIGNMENT != 0 ) {
191 int diff = (int)mysize - mem->real_size;
198 if (diff > 0 && diff > avail_memory) {
199 PRINTF(
"MMEM: realloc failed (diff: %i, avail: %u\n", diff, avail_memory);
205 if (mem->next !=
NULL) {
206 memmove((
char *)mem->next->ptr+diff, (
char *)mem->next->ptr,
207 &memory[MMEM_SIZE - avail_memory] - (
char *)mem->next->ptr);
211 for(n = mem->next; n !=
NULL; n = n->next) {
212 n->ptr = (
void *)((
char *)n->ptr + diff);
217 mem->real_size = mysize;
218 avail_memory -= diff;
237 m_new->ptr = m_old->ptr;
241 m_old->real_size = 0;
257 static int inited = 0;
259 PRINTF(
"Duplicate init\n");
263 avail_memory = MMEM_SIZE;