Skip to content

Commit 6707cd1

Browse files
author
Sebastien Bourdeauducq
committedJun 1, 2012
Memory allocator
1 parent 3f1fd02 commit 6707cd1

18 files changed

+1726
-45
lines changed
 

‎Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ MISPDIR=.
22
include $(MISPDIR)/common.mak
33

44
OBJECTS=crt0.o isr.o luainit.o main.o
5-
OURLIBS=m yaffs2 glue lua
5+
OURLIBS=m mm yaffs2 glue lua
66

7-
CFLAGS+=-I$(MISPDIR)/libm/include -I$(LUADIR)/src
7+
CFLAGS+=-I$(MISPDIR)/libm/include -I$(MISPDIR)/libmm/include -I$(LUADIR)/src
88

99
all: misp.bin
1010

‎libmm/Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MISPDIR=..
2+
include $(MISPDIR)/common.mak
3+
4+
CFLAGS+=-I$(MISPDIR)/libmm/include
5+
OBJECTS=mm_addfreechunk.o mm_free.o mm_mallinfo.o mm_memalign.o mm_shrinkchunk.o mm_zalloc.o mm_calloc.o mm_initialize.o mm_malloc.o mm_realloc.o mm_size2ndx.o
6+
7+
all: libmm.a
8+
9+
# pull in dependency info for *existing* .o files
10+
-include $(OBJECTS:.o=.d)
11+
12+
libmm.a: $(OBJECTS)
13+
$(AR) clr libmm.a $(OBJECTS)
14+
$(RANLIB) libmm.a
15+
16+
%.o: %.c
17+
$(compile-dep)
18+
19+
.PHONY: clean
20+
21+
clean:
22+
rm -f $(OBJECTS) $(OBJECTS:.o=.d) libmm.a .*~ *~

‎libmm/include/malloc.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef __MALLOC_H
2+
#define __MALLOC_H
3+
4+
struct mallinfo
5+
{
6+
int arena; /* This is the total size of memory allocated
7+
* for use by malloc in bytes. */
8+
int ordblks; /* This is the number of free (not in use) chunks */
9+
int mxordblk; /* Size of the largest free (not in use) chunk */
10+
int uordblks; /* This is the total size of memory occupied by
11+
* chunks handed out by malloc. */
12+
int fordblks; /* This is the total size of memory occupied
13+
* by free (not in use) chunks.*/
14+
};
15+
16+
struct mallinfo mallinfo(void);
17+
void *memalign(size_t boundary, size_t size);
18+
19+
/* Those are non-standard */
20+
void mm_initialize(void *heapstart, size_t heapsize);
21+
void mm_addregion(void *heapstart, size_t heapsize);
22+
void *zalloc(size_t size);
23+
24+
#endif /* __MALLOC_H */

‎libmm/mm_addfreechunk.c

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/************************************************************************
2+
* mm/mm_addfreechunk.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/************************************************************************
40+
* mm_addfreechunk
41+
*
42+
* Description:
43+
* Add a free chunk to the node next
44+
*
45+
************************************************************************/
46+
47+
void mm_addfreechunk(struct mm_freenode_s *node)
48+
{
49+
struct mm_freenode_s *next;
50+
struct mm_freenode_s *prev;
51+
52+
/* Convert the size to a nodelist index */
53+
54+
int ndx = mm_size2ndx(node->size);
55+
56+
/* Now put the new node int the next */
57+
58+
for (prev = &g_nodelist[ndx], next = g_nodelist[ndx].flink;
59+
next && next->size && next->size < node->size;
60+
prev = next, next = next->flink);
61+
62+
/* Does it go in mid next or at the end? */
63+
64+
prev->flink = node;
65+
node->blink = prev;
66+
node->flink = next;
67+
68+
if (next)
69+
{
70+
/* The new node goes between prev and next */
71+
72+
next->blink = node;
73+
}
74+
}

‎libmm/mm_calloc.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/************************************************************************
2+
* mm_calloc.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/************************************************************************
40+
* calloc
41+
*
42+
* Descripton:
43+
* calloc calculates the size and calls zalloc
44+
************************************************************************/
45+
46+
void *calloc(size_t n, size_t elem_size)
47+
{
48+
void *ret = NULL;
49+
50+
if (n > 0 && elem_size > 0)
51+
{
52+
ret = zalloc(n * elem_size);
53+
}
54+
55+
return ret;
56+
}

‎libmm/mm_environment.h

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/****************************************************************************
2+
* mm/mm_environment.h
3+
*
4+
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
****************************************************************************/
35+
36+
#ifndef __MM_ENVIRONMENT_H
37+
#define __MM_ENVIRONMENT_H
38+
39+
#include <stdio.h>
40+
#include <stdlib.h>
41+
#include <stdint.h>
42+
#include <string.h>
43+
#include <errno.h>
44+
#include <assert.h>
45+
#include <malloc.h>
46+
47+
#define mm_errno errno
48+
#ifdef MM_DEBUG
49+
#define mvdbg(format, arg...) printf(format, ##arg)
50+
#else
51+
#define mvdbg(format, arg...)
52+
#endif
53+
54+
#define ASSERT(e) assert(e)
55+
#define DEBUGASSERT(e) assert(e)
56+
57+
#endif /* __MM_ENVIRONMENT_H */

‎libmm/mm_free.c

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/************************************************************************
2+
* mm/mm_free.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************************/
35+
36+
#include <assert.h>
37+
#include "mm_environment.h"
38+
#include "mm_internal.h"
39+
40+
/************************************************************************
41+
* free
42+
*
43+
* Description:
44+
* Returns a chunk of memory into the list of free nodes,
45+
* merging with adjacent free chunks if possible.
46+
*
47+
************************************************************************/
48+
49+
void free(void *mem)
50+
{
51+
struct mm_freenode_s *node;
52+
struct mm_freenode_s *prev;
53+
struct mm_freenode_s *next;
54+
55+
mvdbg("Freeing %p\n", mem);
56+
57+
/* Protect against attempts to free a NULL reference */
58+
59+
if (!mem)
60+
{
61+
return;
62+
}
63+
64+
/* Map the memory chunk into a free node */
65+
66+
node = (struct mm_freenode_s *)((char*)mem - SIZEOF_MM_ALLOCNODE);
67+
node->preceding &= ~MM_ALLOC_BIT;
68+
69+
/* Check if the following node is free and, if so, merge it */
70+
71+
next = (struct mm_freenode_s *)((char*)node + node->size);
72+
if ((next->preceding & MM_ALLOC_BIT) == 0)
73+
{
74+
struct mm_allocnode_s *andbeyond;
75+
76+
/* Get the node following the next node (which will
77+
* become the new next node). We know that we can never
78+
* index past the tail chunk because it is always allocated.
79+
*/
80+
81+
andbeyond = (struct mm_allocnode_s*)((char*)next + next->size);
82+
83+
/* Remove the next node. There must be a predecessor,
84+
* but there may not be a successor node.
85+
*/
86+
87+
DEBUGASSERT(next->blink);
88+
next->blink->flink = next->flink;
89+
if (next->flink)
90+
{
91+
next->flink->blink = next->blink;
92+
}
93+
94+
/* Then merge the two chunks */
95+
96+
node->size += next->size;
97+
andbeyond->preceding = node->size | (andbeyond->preceding & MM_ALLOC_BIT);
98+
next = (struct mm_freenode_s *)andbeyond;
99+
}
100+
101+
/* Check if the preceding node is also free and, if so, merge
102+
* it with this node
103+
*/
104+
105+
prev = (struct mm_freenode_s *)((char*)node - node->preceding);
106+
if ((prev->preceding & MM_ALLOC_BIT) == 0)
107+
{
108+
/* Remove the node. There must be a predecessor, but there may
109+
* not be a successor node.
110+
*/
111+
112+
DEBUGASSERT(prev->blink);
113+
prev->blink->flink = prev->flink;
114+
if (prev->flink)
115+
{
116+
prev->flink->blink = prev->blink;
117+
}
118+
119+
/* Then merge the two chunks */
120+
121+
prev->size += node->size;
122+
next->preceding = prev->size | (next->preceding & MM_ALLOC_BIT);
123+
node = prev;
124+
}
125+
126+
/* Add the merged node to the nodelist */
127+
128+
mm_addfreechunk(node);
129+
}

‎libmm/mm_initialize.c

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/****************************************************************************
2+
* mm/mm_initialize.c
3+
*
4+
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
****************************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/* This is the size of the heap provided to mm */
40+
41+
size_t g_heapsize;
42+
43+
/* This is the first and last nodes of the heap */
44+
45+
struct mm_allocnode_s *g_heapstart[CONFIG_MM_REGIONS];
46+
struct mm_allocnode_s *g_heapend[CONFIG_MM_REGIONS];
47+
48+
#if CONFIG_MM_REGIONS > 1
49+
int g_nregions;
50+
#endif
51+
52+
/* All free nodes are maintained in a doubly linked list. This
53+
* array provides some hooks into the list at various points to
54+
* speed searches for free nodes.
55+
*/
56+
57+
struct mm_freenode_s g_nodelist[MM_NNODES];
58+
59+
/****************************************************************************
60+
* Public Functions
61+
****************************************************************************/
62+
63+
/****************************************************************************
64+
* Function: mm_initialize
65+
*
66+
* Description:
67+
* This is an internal OS function called only at power-up
68+
* boot time.
69+
*
70+
* Parameters:
71+
* heapstart - Start of the initial heap region
72+
* heapsize - Size of the initial heap region
73+
*
74+
* Return Value:
75+
* None
76+
*
77+
* Assumptions:
78+
*
79+
****************************************************************************/
80+
81+
void mm_initialize(void *heapstart, size_t heapsize)
82+
{
83+
int i;
84+
85+
mvdbg("Heap: start=%p size=%u\n", heapstart, heapsize);
86+
87+
CHECK_ALLOCNODE_SIZE;
88+
CHECK_FREENODE_SIZE;
89+
90+
/* Set up global variables */
91+
92+
g_heapsize = 0;
93+
94+
#if CONFIG_MM_REGIONS > 1
95+
g_nregions = 0;
96+
#endif
97+
98+
/* Initialize the node array */
99+
100+
memset(g_nodelist, 0, sizeof(struct mm_freenode_s) * MM_NNODES);
101+
for (i = 1; i < MM_NNODES; i++)
102+
{
103+
g_nodelist[i-1].flink = &g_nodelist[i];
104+
g_nodelist[i].blink = &g_nodelist[i-1];
105+
}
106+
107+
/* Add the initial region of memory to the heap */
108+
109+
mm_addregion(heapstart, heapsize);
110+
}
111+
112+
/****************************************************************************
113+
* Function: mm_addregion
114+
*
115+
* Description:
116+
* This function gives a region of contiguous memory to
117+
* the memory manager
118+
*
119+
* Parameters:
120+
* heapstart - Start of the heap region
121+
* heapsize - Size of the heap region
122+
*
123+
* Return Value:
124+
* None
125+
*
126+
* Assumptions:
127+
*
128+
****************************************************************************/
129+
130+
void mm_addregion(void *heapstart, size_t heapsize)
131+
{
132+
struct mm_freenode_s *node;
133+
uintptr_t heapbase;
134+
uintptr_t heapend;
135+
#if CONFIG_MM_REGIONS > 1
136+
int IDX = g_nregions;
137+
#else
138+
# define IDX 0
139+
#endif
140+
141+
/* Adjust the provide heap start and size so that they are
142+
* both aligned with the MM_MIN_CHUNK size.
143+
*/
144+
145+
heapbase = MM_ALIGN_UP((uintptr_t)heapstart);
146+
heapend = MM_ALIGN_DOWN((uintptr_t)heapstart + (uintptr_t)heapsize);
147+
heapsize = heapend - heapbase;
148+
149+
mvdbg("Region %d: base=%p size=%u\n", IDX+1, heapstart, heapsize);
150+
151+
/* Add the size of this region to the total size of the heap */
152+
153+
g_heapsize += heapsize;
154+
155+
/* Create two "allocated" guard nodes at the beginning and end of
156+
* the heap. These only serve to keep us from allocating outside
157+
* of the heap.
158+
*
159+
* And create one free node between the guard nodes that contains
160+
* all available memory.
161+
*/
162+
163+
g_heapstart[IDX] = (struct mm_allocnode_s *)heapbase;
164+
g_heapstart[IDX]->size = SIZEOF_MM_ALLOCNODE;
165+
g_heapstart[IDX]->preceding = MM_ALLOC_BIT;
166+
167+
node = (struct mm_freenode_s *)(heapbase + SIZEOF_MM_ALLOCNODE);
168+
node->size = heapsize - 2*SIZEOF_MM_ALLOCNODE;
169+
node->preceding = SIZEOF_MM_ALLOCNODE;
170+
171+
g_heapend[IDX] = (struct mm_allocnode_s *)(heapend - SIZEOF_MM_ALLOCNODE);
172+
g_heapend[IDX]->size = SIZEOF_MM_ALLOCNODE;
173+
g_heapend[IDX]->preceding = node->size | MM_ALLOC_BIT;
174+
175+
#undef IDX
176+
177+
#if CONFIG_MM_REGIONS > 1
178+
g_nregions++;
179+
#endif
180+
181+
/* Add the single, large free node to the nodelist */
182+
183+
mm_addfreechunk(node);
184+
}

‎libmm/mm_internal.h

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/************************************************************************
2+
* mm/mm_internal.h
3+
*
4+
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************************/
35+
36+
#ifndef __MM_INTERNAL_H
37+
#define __MM_INTERNAL_H
38+
39+
/* Chunk Header Definitions *********************************************/
40+
/* These definitions define the characteristics of allocator
41+
*
42+
* MM_MIN_SHIFT is used to define MM_MIN_CHUNK.
43+
* MM_MIN_CHUNK - is the smallest physical chunk that can
44+
* be allocated. It must be at least a large as
45+
* sizeof(struct mm_freenode_s). Larger values may
46+
* improve performance slightly, but will waste memory
47+
* due to quantization losses.
48+
*
49+
* MM_MAX_SHIFT is used to define MM_MAX_CHUNK
50+
* MM_MAX_CHUNK is the largest, contiguous chunk of memory
51+
* that can be allocated. It can range from 16-bytes to
52+
* 4Gb. Larger values of MM_MAX_SHIFT can cause larger
53+
* data structure sizes and, perhaps, minor performance
54+
* losses.
55+
*/
56+
57+
# define MM_MIN_SHIFT 4 /* 16 bytes */
58+
# define MM_MAX_SHIFT 22 /* 4 Mb */
59+
60+
/* All other definitions derive from these two */
61+
62+
#define MM_MIN_CHUNK (1 << MM_MIN_SHIFT)
63+
#define MM_MAX_CHUNK (1 << MM_MAX_SHIFT)
64+
#define MM_NNODES (MM_MAX_SHIFT - MM_MIN_SHIFT + 1)
65+
66+
#define MM_GRAN_MASK (MM_MIN_CHUNK-1)
67+
#define MM_ALIGN_UP(a) (((a) + MM_GRAN_MASK) & ~MM_GRAN_MASK)
68+
#define MM_ALIGN_DOWN(a) ((a) & ~MM_GRAN_MASK)
69+
70+
/* An allocated chunk is distinguished from a free chunk by
71+
* bit 31 of the 'preceding' chunk size. If set, then this is
72+
* an allocated chunk.
73+
*/
74+
75+
#define MM_ALLOC_BIT 0x80000000
76+
#define MM_IS_ALLOCATED(n) \
77+
((int)((struct mm_allocnode_s*)(n)->preceding) < 0))
78+
79+
80+
/* Determine the size of the chunk size/offset type */
81+
82+
typedef size_t mmsize_t;
83+
#define MMSIZE_MAX SIZE_MAX
84+
85+
#define CONFIG_MM_REGIONS 1
86+
87+
/* This describes an allocated chunk. An allocated chunk is
88+
* distinguished from a free chunk by bit 15/31 of the 'preceding' chunk
89+
* size. If set, then this is an allocated chunk.
90+
*/
91+
92+
struct mm_allocnode_s
93+
{
94+
mmsize_t size; /* Size of this chunk */
95+
mmsize_t preceding; /* Size of the preceding chunk */
96+
};
97+
98+
/* What is the size of the allocnode? */
99+
100+
#define SIZEOF_MM_ALLOCNODE 8
101+
102+
#define CHECK_ALLOCNODE_SIZE \
103+
DEBUGASSERT(sizeof(struct mm_allocnode_s) == SIZEOF_MM_ALLOCNODE)
104+
105+
/* This describes a free chunk */
106+
107+
struct mm_freenode_s
108+
{
109+
mmsize_t size; /* Size of this chunk */
110+
mmsize_t preceding; /* Size of the preceding chunk */
111+
struct mm_freenode_s *flink; /* Supports a doubly linked list */
112+
struct mm_freenode_s *blink;
113+
};
114+
115+
/* What is the size of the freenode? */
116+
117+
#define SIZEOF_MM_FREENODE 16
118+
119+
#define CHECK_FREENODE_SIZE \
120+
DEBUGASSERT(sizeof(struct mm_freenode_s) == SIZEOF_MM_FREENODE)
121+
122+
123+
/* This is the size of the heap provided to mm */
124+
extern size_t g_heapsize;
125+
126+
/* This is the first and last nodes of the heap */
127+
extern struct mm_allocnode_s *g_heapstart[CONFIG_MM_REGIONS];
128+
extern struct mm_allocnode_s *g_heapend[CONFIG_MM_REGIONS];
129+
130+
#if CONFIG_MM_REGIONS > 1
131+
extern int g_nregions;
132+
#else
133+
#define g_nregions 1
134+
#endif
135+
136+
/* All free nodes are maintained in a doubly linked list. This
137+
* array provides some hooks into the list at various points to
138+
* speed searches for free nodes.
139+
*/
140+
141+
extern struct mm_freenode_s g_nodelist[MM_NNODES];
142+
143+
extern void mm_shrinkchunk(struct mm_allocnode_s *node,
144+
size_t size);
145+
extern void mm_addfreechunk(struct mm_freenode_s *node);
146+
extern int mm_size2ndx(size_t size);
147+
148+
#endif /* __MM_INTERNAL_H */

‎libmm/mm_mallinfo.c

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/****************************************************************************
2+
* mm/mm_mallinfo.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
****************************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
40+
/****************************************************************************
41+
* mallinfo
42+
*
43+
* Description:
44+
* mallinfo returns a copy of updated current mallinfo.
45+
*
46+
****************************************************************************/
47+
48+
struct mallinfo mallinfo(void)
49+
{
50+
struct mm_allocnode_s *node;
51+
size_t mxordblk = 0;
52+
int ordblks = 0; /* Number of non-inuse chunks */
53+
size_t uordblks = 0; /* Total allocated space */
54+
size_t fordblks = 0; /* Total non-inuse space */
55+
#if CONFIG_MM_REGIONS > 1
56+
int region;
57+
#else
58+
# define region 0
59+
#endif
60+
static struct mallinfo info;
61+
62+
/* Visit each region */
63+
64+
#if CONFIG_MM_REGIONS > 1
65+
for (region = 0; region < g_nregions; region++)
66+
#endif
67+
{
68+
/* Visit each node in the region */
69+
70+
for (node = g_heapstart[region];
71+
node < g_heapend[region];
72+
node = (struct mm_allocnode_s *)((char*)node + node->size))
73+
{
74+
mvdbg("region=%d node=%p preceding=%p\n", region, node, node->preceding);
75+
if (node->preceding & MM_ALLOC_BIT)
76+
{
77+
uordblks += node->size;
78+
}
79+
else
80+
{
81+
ordblks++;
82+
fordblks += node->size;
83+
if (node->size > mxordblk)
84+
{
85+
mxordblk = node->size;
86+
}
87+
}
88+
}
89+
90+
mvdbg("region=%d node=%p g_heapend=%p\n", region, node, g_heapend[region]);
91+
DEBUGASSERT(node == g_heapend[region]);
92+
uordblks += SIZEOF_MM_ALLOCNODE; /* account for the tail node */
93+
}
94+
#undef region
95+
96+
DEBUGASSERT(uordblks + fordblks == g_heapsize);
97+
98+
info.arena = g_heapsize;
99+
info.ordblks = ordblks;
100+
info.mxordblk = mxordblk;
101+
info.uordblks = uordblks;
102+
info.fordblks = fordblks;
103+
return info;
104+
}

‎libmm/mm_malloc.c

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/************************************************************
2+
* mm/mm_malloc.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/************************************************************
40+
* malloc
41+
*
42+
* Description:
43+
* Find the smallest chunk that satisfies the request.
44+
* Take the memory from that chunk, save the remaining,
45+
* smaller chunk (if any).
46+
*
47+
* 8-byte alignment of the allocated data is assured.
48+
*
49+
************************************************************/
50+
51+
void *malloc(size_t size)
52+
{
53+
struct mm_freenode_s *node;
54+
void *ret = NULL;
55+
int ndx;
56+
57+
/* Handle bad sizes */
58+
59+
if (size <= 0)
60+
{
61+
return NULL;
62+
}
63+
64+
/* Adjust the size to account for (1) the size of the allocated
65+
* node and (2) to make sure that it is an even multiple of
66+
* our granule size.
67+
*/
68+
69+
size = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE);
70+
71+
/* Get the location in the node list to start the search.
72+
* Special case really big alloctions
73+
*/
74+
75+
if (size >= MM_MAX_CHUNK)
76+
{
77+
ndx = MM_NNODES-1;
78+
}
79+
else
80+
{
81+
/* Convert the request size into a nodelist index */
82+
83+
ndx = mm_size2ndx(size);
84+
}
85+
86+
/* Search for a large enough chunk in the list of nodes.
87+
* This list is ordered by size, but will have occasional
88+
* zero sized nodes as we visit other g_nodelist[] entries.
89+
*/
90+
91+
for (node = g_nodelist[ndx].flink;
92+
node && node->size < size;
93+
node = node->flink);
94+
95+
/* If we found a node with non-zero size, then this is one
96+
* to use. Since the list is ordered, we know that is must be
97+
* best fitting chunk available.
98+
*/
99+
100+
if (node)
101+
{
102+
struct mm_freenode_s *remainder;
103+
struct mm_freenode_s *next;
104+
size_t remaining;
105+
106+
/* Remove the node. There must be a predecessor, but there may
107+
* not be a successor node.
108+
*/
109+
110+
DEBUGASSERT(node->blink);
111+
node->blink->flink = node->flink;
112+
if (node->flink)
113+
{
114+
node->flink->blink = node->blink;
115+
}
116+
117+
/* Check if we have to split the free node into one of the
118+
* allocated size and another smaller freenode. In some
119+
* cases, the remaining bytes can be smaller (they may be
120+
* SIZEOF_MM_ALLOCNODE). In that case, we will just carry
121+
* the few wasted bytes at the end of the allocation.
122+
*/
123+
124+
remaining = node->size - size;
125+
if (remaining >= SIZEOF_MM_FREENODE)
126+
{
127+
/* Get a pointer to the next node in physical memory */
128+
129+
next = (struct mm_freenode_s*)(((char*)node) + node->size);
130+
131+
/* Create the remainder node */
132+
133+
remainder = (struct mm_freenode_s*)(((char*)node) + size);
134+
remainder->size = remaining;
135+
remainder->preceding = size;
136+
137+
/* Adjust the size of the node under consideration */
138+
139+
node->size = size;
140+
141+
/* Adjust the 'preceding' size of the (old) next node,
142+
* preserving the allocated flag.
143+
*/
144+
145+
next->preceding = remaining | (next->preceding & MM_ALLOC_BIT);
146+
147+
/* Add the remainder back into the nodelist */
148+
149+
mm_addfreechunk(remainder);
150+
}
151+
152+
/* Handle the case of an exact size match */
153+
154+
node->preceding |= MM_ALLOC_BIT;
155+
ret = (void*)((char*)node + SIZEOF_MM_ALLOCNODE);
156+
}
157+
158+
mvdbg("Allocated %p, size %d\n", ret, size);
159+
return ret;
160+
}

‎libmm/mm_memalign.c

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/************************************************************
2+
* mm/mm_memalign.c
3+
*
4+
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/************************************************************
40+
* memalign
41+
*
42+
* Description:
43+
* memalign requests more than enough space from malloc,
44+
* finds a region within that chunk that meets the alignment
45+
* request and then frees any leading or trailing space.
46+
*
47+
* The alignment argument must be a power of two (not
48+
* checked). 8-byte alignment is guaranteed by normal
49+
* malloc calls.
50+
*
51+
************************************************************/
52+
53+
void *memalign(size_t alignment, size_t size)
54+
{
55+
struct mm_allocnode_s *node;
56+
size_t rawchunk;
57+
size_t alignedchunk;
58+
size_t mask = (size_t)(alignment - 1);
59+
size_t allocsize;
60+
61+
/* If this requested alignement less than or equal to the
62+
* natural alignment of malloc, then just let malloc do the
63+
* work.
64+
*/
65+
66+
if (alignment <= MM_MIN_CHUNK)
67+
{
68+
return malloc(size);
69+
}
70+
71+
/* Adjust the size to account for (1) the size of the allocated
72+
* node, (2) to make sure that it is an even multiple of
73+
* our granule size, and to include the alignment amount.
74+
*
75+
* Notice that we increase the allocation size by twice the
76+
* the requested alignment. We do this so that there will
77+
* be at least two valid alignment points within the allocated
78+
* memory.
79+
*
80+
* NOTE: These are sizes given to malloc and not chunk sizes.
81+
* The do not include SIZEOF_MM_ALLOCNODE.
82+
*/
83+
84+
size = MM_ALIGN_UP(size); /* Make multiples of our granule size */
85+
allocsize = size + 2*alignment; /* Add double full alignment size */
86+
87+
/* Then malloc that size */
88+
89+
rawchunk = (size_t)malloc(allocsize);
90+
if (rawchunk == 0)
91+
{
92+
return NULL;
93+
}
94+
95+
/* Get the node associated with the allocation and the next
96+
* node after the allocation.
97+
*/
98+
99+
node = (struct mm_allocnode_s*)(rawchunk - SIZEOF_MM_ALLOCNODE);
100+
101+
/* Find the aligned subregion */
102+
103+
alignedchunk = (rawchunk + mask) & ~mask;
104+
105+
/* Check if there is free space at the beginning of the aligned chunk */
106+
107+
if (alignedchunk != rawchunk)
108+
{
109+
struct mm_allocnode_s *newnode;
110+
struct mm_allocnode_s *next;
111+
size_t precedingsize;
112+
113+
/* Get the node the next node after the allocation. */
114+
115+
next = (struct mm_allocnode_s*)((char*)node + node->size);
116+
117+
/* Make sure that there is space to convert the preceding mm_allocnode_s
118+
* into an mm_freenode_s. I think that this should always be true
119+
*/
120+
121+
DEBUGASSERT(alignedchunk >= rawchunk + 8);
122+
123+
newnode = (struct mm_allocnode_s*)(alignedchunk - SIZEOF_MM_ALLOCNODE);
124+
125+
/* Preceding size is full size of the new 'node,' including
126+
* SIZEOF_MM_ALLOCNODE
127+
*/
128+
129+
precedingsize = (size_t)newnode - (size_t)node;
130+
131+
/* If we were unlucky, then the alignedchunk can lie in such
132+
* a position that precedingsize < SIZEOF_NODE_FREENODE. We
133+
* can't let that happen because we are going to cast 'node' to
134+
* struct mm_freenode_s below. This is why we allocated memory
135+
* large enough to support two alignment points. In this case,
136+
* we will simply use the second alignment point.
137+
*/
138+
139+
if (precedingsize < SIZEOF_MM_FREENODE)
140+
{
141+
alignedchunk += alignment;
142+
newnode = (struct mm_allocnode_s*)(alignedchunk - SIZEOF_MM_ALLOCNODE);
143+
precedingsize = (size_t)newnode - (size_t)node;
144+
}
145+
146+
/* Set up the size of the new node */
147+
148+
newnode->size = (size_t)next - (size_t)newnode;
149+
newnode->preceding = precedingsize | MM_ALLOC_BIT;
150+
151+
/* Reduce the size of the original chunk and mark it not allocated, */
152+
153+
node->size = precedingsize;
154+
node->preceding &= ~MM_ALLOC_BIT;
155+
156+
/* Fix the preceding size of the next node */
157+
158+
next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT);
159+
160+
/* Convert the newnode chunk size back into malloc-compatible
161+
* size by subtracting the header size SIZEOF_MM_ALLOCNODE.
162+
*/
163+
164+
allocsize = newnode->size - SIZEOF_MM_ALLOCNODE;
165+
166+
/* Add the original, newly freed node to the free nodelist */
167+
168+
mm_addfreechunk((struct mm_freenode_s *)node);
169+
170+
/* Replace the original node with the newlay realloaced,
171+
* aligned node
172+
*/
173+
174+
node = newnode;
175+
}
176+
177+
/* Check if there is free space at the end of the aligned chunk */
178+
179+
if (allocsize > size)
180+
{
181+
/* Shrink the chunk by that much -- remember, mm_shrinkchunk
182+
* wants internal chunk sizes that include SIZEOF_MM_ALLOCNODE,
183+
* and not the malloc-compatible sizes that we have.
184+
*/
185+
186+
mm_shrinkchunk(node, size + SIZEOF_MM_ALLOCNODE);
187+
}
188+
189+
return (void*)alignedchunk;
190+
}

‎libmm/mm_realloc.c

+328
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
1+
/************************************************************
2+
* mm/mm_realloc.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/************************************************************
40+
* realloc
41+
*
42+
* Description:
43+
* If the reallocation is for less space, then:
44+
* (1) the current allocation is reduced in size
45+
* (2) the remainder at the end of the allocation is
46+
* returned to the free list.
47+
*
48+
* If the request is for more space and the current
49+
* allocation can be extended, it will be extended by:
50+
* (1) Taking the additional space from the following
51+
* free chunk, or
52+
* (2) Taking the additional space from the preceding
53+
* free chunk.
54+
* (3) Or both
55+
*
56+
* If the request is for more space but the current chunk
57+
* cannot be extended, then malloc a new buffer, copy the
58+
* data into the new buffer, and free the old buffer.
59+
*
60+
************************************************************/
61+
62+
void *realloc(void *oldmem, size_t size)
63+
{
64+
struct mm_allocnode_s *oldnode;
65+
struct mm_freenode_s *prev;
66+
struct mm_freenode_s *next;
67+
size_t oldsize;
68+
size_t prevsize = 0;
69+
size_t nextsize = 0;
70+
void *newmem;
71+
72+
/* If oldmem is NULL, then realloc is equivalent to malloc */
73+
74+
if (!oldmem)
75+
{
76+
return malloc(size);
77+
}
78+
79+
/* If size is zero, then realloc is equivalent to free */
80+
81+
if (size <= 0)
82+
{
83+
free(oldmem);
84+
return NULL;
85+
}
86+
87+
/* Adjust the size to account for (1) the size of the allocated
88+
* node and (2) to make sure that it is an even multiple of
89+
* our granule size.
90+
*/
91+
92+
size = MM_ALIGN_UP(size + SIZEOF_MM_ALLOCNODE);
93+
94+
/* Map the memory chunk into an allocated node structure */
95+
96+
oldnode = (struct mm_allocnode_s *)((char*)oldmem - SIZEOF_MM_ALLOCNODE);
97+
98+
/* Check if this is a request to reduce the size of the allocation. */
99+
100+
oldsize = oldnode->size;
101+
if (size <= oldsize)
102+
{
103+
/* Handle the special case where we are not going to change the
104+
* size of the allocation.
105+
*/
106+
if (size < oldsize)
107+
{
108+
mm_shrinkchunk(oldnode, size);
109+
}
110+
111+
/* Then return the original address */
112+
return oldmem;
113+
}
114+
115+
/* This is a request to increase the size of the allocation, Get the
116+
* available sizes before and after the oldnode so that we can make
117+
* the best decision
118+
*/
119+
120+
next = (struct mm_freenode_s *)((char*)oldnode + oldnode->size);
121+
if ((next->preceding & MM_ALLOC_BIT) == 0)
122+
{
123+
nextsize = next->size;
124+
}
125+
126+
prev = (struct mm_freenode_s *)((char*)oldnode - (oldnode->preceding & ~MM_ALLOC_BIT));
127+
if ((prev->preceding & MM_ALLOC_BIT) == 0)
128+
{
129+
prevsize = prev->size;
130+
}
131+
132+
/* Now, check if we can extend the current allocation or not */
133+
134+
if (nextsize + prevsize + oldsize >= size)
135+
{
136+
size_t needed = size - oldsize;
137+
size_t takeprev = 0;
138+
size_t takenext = 0;
139+
140+
/* Check if we can extend into the previous chunk and if the
141+
* previous chunk is smaller than the next chunk.
142+
*/
143+
144+
if (prevsize > 0 && (nextsize >= prevsize || nextsize <= 0))
145+
{
146+
/* Can we get everything we need from the previous chunk? */
147+
148+
if (needed > prevsize)
149+
{
150+
/* No, take the whole previous chunk and get the
151+
* rest that we need from the next chunk.
152+
*/
153+
154+
takeprev = prevsize;
155+
takenext = needed - prevsize;
156+
}
157+
else
158+
{
159+
/* Yes, take what we need from the previous chunk */
160+
161+
takeprev = needed;
162+
takenext = 0;
163+
}
164+
165+
needed = 0;
166+
}
167+
168+
/* Check if we can extend into the next chunk and if we still
169+
* need more memory.
170+
*/
171+
172+
if (nextsize > 0 && needed)
173+
{
174+
/* Can we get everything we need from the next chunk? */
175+
176+
if (needed > nextsize)
177+
{
178+
/* No, take the whole next chunk and get the
179+
* rest that we need from the previous chunk.
180+
*/
181+
182+
takeprev = needed - nextsize;
183+
takenext = nextsize;
184+
}
185+
else
186+
{
187+
/* Yes, take what we need from the previous chunk */
188+
189+
takeprev = 0;
190+
takenext = needed;
191+
}
192+
}
193+
194+
/* Extend into the previous free chunk */
195+
196+
newmem = oldmem;
197+
if (takeprev)
198+
{
199+
struct mm_allocnode_s *newnode;
200+
201+
/* Remove the previous node. There must be a predecessor,
202+
* but there may not be a successor node.
203+
*/
204+
205+
DEBUGASSERT(prev->blink);
206+
prev->blink->flink = prev->flink;
207+
if (prev->flink)
208+
{
209+
prev->flink->blink = prev->blink;
210+
}
211+
212+
/* Extend the node into the previous free chunk */
213+
214+
newnode = (struct mm_allocnode_s *)((char*)oldnode - takeprev);
215+
216+
/* Did we consume the entire preceding chunk? */
217+
218+
if (takeprev < prevsize)
219+
{
220+
/* No.. just take what we need from the previous chunk
221+
* and put it back into the free list
222+
*/
223+
224+
prev->size -= takeprev;
225+
newnode->size = oldsize + takeprev;
226+
newnode->preceding = prev->size | MM_ALLOC_BIT;
227+
next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT);
228+
229+
/* Return the previous free node to the nodelist (with the new size) */
230+
231+
mm_addfreechunk(prev);
232+
233+
/* Now we want to return newnode */
234+
235+
oldnode = newnode;
236+
}
237+
else
238+
{
239+
/* Yes.. update its size (newnode->preceding is already set) */
240+
241+
newnode->size += oldsize;
242+
newnode->preceding |= MM_ALLOC_BIT;
243+
next->preceding = newnode->size | (next->preceding & MM_ALLOC_BIT);
244+
}
245+
246+
oldnode = newnode;
247+
oldsize = newnode->size;
248+
249+
/* Now we have to move the user contents 'down' in memory. memcpy should
250+
* should be save for this.
251+
*/
252+
253+
newmem = (void*)((char*)newnode + SIZEOF_MM_ALLOCNODE);
254+
memcpy(newmem, oldmem, oldsize - SIZEOF_MM_ALLOCNODE);
255+
}
256+
257+
/* Extend into the next free chunk */
258+
259+
if (takenext)
260+
{
261+
struct mm_freenode_s *newnode;
262+
struct mm_allocnode_s *andbeyond;
263+
264+
/* Get the chunk following the next node (which could be the tail chunk) */
265+
266+
andbeyond = (struct mm_allocnode_s*)((char*)next + nextsize);
267+
268+
/* Remove the next node. There must be a predecessor,
269+
* but there may not be a successor node.
270+
*/
271+
272+
DEBUGASSERT(next->blink);
273+
next->blink->flink = next->flink;
274+
if (next->flink)
275+
{
276+
next->flink->blink = next->blink;
277+
}
278+
279+
/* Extend the node into the next chunk */
280+
281+
oldnode->size = oldsize + takenext;
282+
newnode = (struct mm_freenode_s *)((char*)oldnode + oldnode->size);
283+
284+
/* Did we consume the entire preceding chunk? */
285+
286+
if (takenext < nextsize)
287+
{
288+
/* No, take what we need from the next chunk and return it
289+
* to the free nodelist.
290+
*/
291+
292+
newnode->size = nextsize - takenext;
293+
newnode->preceding = oldnode->size;
294+
andbeyond->preceding = newnode->size | (andbeyond->preceding & MM_ALLOC_BIT);
295+
296+
/* Add the new free node to the nodelist (with the new size) */
297+
298+
mm_addfreechunk(newnode);
299+
}
300+
else
301+
{
302+
/* Yes, just update some pointers. */
303+
304+
andbeyond->preceding = oldnode->size | (andbeyond->preceding & MM_ALLOC_BIT);
305+
}
306+
}
307+
return newmem;
308+
}
309+
310+
/* The current chunk cannot be extended. Just allocate a new chunk and copy */
311+
312+
else
313+
{
314+
/* Allocate a new block. On failure, realloc must return NULL but
315+
* leave the original memory in place.
316+
*/
317+
newmem = (void*)malloc(size);
318+
if (newmem)
319+
{
320+
memcpy(newmem, oldmem, oldsize);
321+
free(oldmem);
322+
}
323+
324+
return newmem;
325+
}
326+
327+
}
328+

‎libmm/mm_shrinkchunk.c

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/************************************************************************
2+
* mm/mm_shrinkchunk.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/************************************************************************
40+
* mm_shrinkchunk
41+
*
42+
* Description:
43+
* Reduce the size of the chunk specified by the node
44+
* structure to the specified size. this internal logic
45+
* is used both from memalign to dispose of any trailing
46+
* memory in the aligned allocation and also by realloc
47+
* when there is a request to reduce the size of an allocation.
48+
*
49+
* NOTE:
50+
* size is the whole chunk size (payload and header)
51+
*
52+
************************************************************************/
53+
54+
void mm_shrinkchunk(struct mm_allocnode_s *node, size_t size)
55+
{
56+
struct mm_freenode_s *next;
57+
58+
/* Get a reference to the next node */
59+
60+
next = (struct mm_freenode_s*)((char*)node + node->size);
61+
62+
/* Check if it is free */
63+
64+
if ((next->preceding & MM_ALLOC_BIT) == 0)
65+
{
66+
struct mm_allocnode_s *andbeyond;
67+
struct mm_freenode_s *newnode;
68+
69+
/* Get the chunk next the next node (which could be the tail chunk) */
70+
71+
andbeyond = (struct mm_allocnode_s*)((char*)next + next->size);
72+
73+
/* Remove the next node. There must be a predecessor, but there may
74+
* not be a successor node.
75+
*/
76+
77+
DEBUGASSERT(next->blink);
78+
next->blink->flink = next->flink;
79+
if (next->flink)
80+
{
81+
next->flink->blink = next->blink;
82+
}
83+
84+
/* Create a new chunk that will hold both the next chunk
85+
* and the tailing memory from the aligned chunk.
86+
*/
87+
88+
newnode = (struct mm_freenode_s*)((char*)node + size);
89+
90+
/* Set up the size of the new node */
91+
92+
newnode->size = next->size + node->size - size;
93+
newnode->preceding = size;
94+
node->size = size;
95+
andbeyond->preceding = newnode->size | (andbeyond->preceding & MM_ALLOC_BIT);
96+
97+
/* Add the new node to the freenodelist */
98+
99+
mm_addfreechunk(newnode);
100+
}
101+
102+
/* The next chunk is allocated. Try to free the end portion
103+
* at the end chunk to be shrunk.
104+
*/
105+
106+
else if (node->size >= size + SIZEOF_MM_FREENODE)
107+
{
108+
struct mm_freenode_s *newnode;
109+
110+
/* Create a new chunk that will hold both the next chunk
111+
* and the tailing memory from the aligned chunk.
112+
*/
113+
114+
newnode = (struct mm_freenode_s*)((char*)node + size);
115+
116+
/* Set up the size of the new node */
117+
118+
newnode->size = node->size - size;
119+
newnode->preceding = size;
120+
node->size = size;
121+
next->preceding = newnode->size | MM_ALLOC_BIT;
122+
123+
/* Add the new node to the freenodelist */
124+
125+
mm_addfreechunk(newnode);
126+
}
127+
}

‎libmm/mm_size2ndx.c

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/************************************************************************
2+
* mm/mm_size2ndx.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/* Convert the size to a nodelist index */
40+
41+
int mm_size2ndx(size_t size)
42+
{
43+
int ndx = 0;
44+
45+
if (size >= MM_MAX_CHUNK)
46+
{
47+
return MM_NNODES-1;
48+
}
49+
50+
size >>= MM_MIN_SHIFT;
51+
while (size > 1)
52+
{
53+
ndx++;
54+
size >>= 1;
55+
}
56+
57+
return ndx;
58+
}

‎libmm/mm_zalloc.c

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/************************************************************************
2+
* mm/mm_zalloc.c
3+
*
4+
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
5+
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in
15+
* the documentation and/or other materials provided with the
16+
* distribution.
17+
* 3. Neither the name NuttX nor the names of its contributors may be
18+
* used to endorse or promote products derived from this software
19+
* without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*
34+
************************************************************************/
35+
36+
#include "mm_environment.h"
37+
#include "mm_internal.h"
38+
39+
/************************************************************************
40+
* Name: zalloc
41+
*
42+
* Description:
43+
* zalloc calls malloc, then zeroes out the allocated chunk.
44+
*
45+
************************************************************************/
46+
47+
void *zalloc(size_t size)
48+
{
49+
void *alloc = malloc(size);
50+
if (alloc)
51+
{
52+
memset(alloc, 0, size);
53+
}
54+
55+
return alloc;
56+
}

‎linker.ld

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ SECTIONS
4848
*(COMMON)
4949
. = ALIGN(4);
5050
_ebss = .;
51-
_end = .;
51+
. = ALIGN(8);
52+
_heapstart = .;
5253
} > sdram
5354
}
5455

‎main.c

+5-42
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdio.h>
22
#include <string.h>
3+
#include <malloc.h>
34

45
#include <irq.h>
56
#include <uart.h>
@@ -8,62 +9,24 @@
89
#include <lauxlib.h>
910
#include <lualib.h>
1011

11-
void free(void *ptr)
12-
{
13-
printf("don't call me!\n");
14-
while(1);
15-
}
16-
17-
void *realloc(void *ptr, size_t size)
18-
{
19-
printf("don't call me!\n");
20-
while(1);
21-
}
22-
23-
static void *stupid_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
24-
{
25-
static char buffer[1024*1024];
26-
static int index;
27-
void *r;
28-
29-
if((nsize % 4) != 0)
30-
nsize += 4 - (nsize % 4);
31-
32-
if(nsize > 0) {
33-
if(ptr == NULL) {
34-
r = &buffer[index];
35-
index += nsize;
36-
if(index > sizeof(buffer)) {
37-
printf("alloc failed\n");
38-
return NULL;
39-
}
40-
return r;
41-
} else {
42-
r = stupid_alloc(NULL, NULL, 0, nsize);
43-
if(r == NULL) return NULL;
44-
memcpy(r, ptr, osize);
45-
return r;
46-
}
47-
} else
48-
return NULL;
49-
}
50-
5112
static void test_lua(void)
5213
{
5314
lua_State *L;
5415

55-
L = lua_newstate(stupid_alloc, NULL); // TODO: use luaL_newstate from lauxlib
16+
L = luaL_newstate();
5617
luaL_openlibs(L);
5718
luaL_dostring(L, "for i=10,1,-1 do print(i) end\n");
58-
5919
lua_close(L);
6020
}
6121

22+
extern void *_heapstart;
23+
6224
int main()
6325
{
6426
irq_setmask(0);
6527
irq_setie(1);
6628
uart_init();
29+
mm_initialize(&_heapstart, 64*1024*1024);
6730

6831
printf("Hello World\n");
6932
test_lua();

0 commit comments

Comments
 (0)
Please sign in to comment.