2 * Copyright (c) 2005, Swedish Institute of Computer Science
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file is part of the Contiki operating system.
31 * @(#)$Id: mmem.c,v 1.2 2006/12/22 17:14:06 barner Exp $
41 * Implementation of the managed memory allocator
43 * Adam Dunkels <adam@sics.se>
50 #include "contiki-conf.h"
54 #define MMEM_SIZE MMEM_CONF_SIZE
56 #define MMEM_SIZE 4096
60 unsigned int avail_memory
;
61 static char memory
[MMEM_SIZE
];
63 /*---------------------------------------------------------------------------*/
65 * \brief Allocate a managed memory block
66 * \param m A pointer to a struct mmem.
67 * \param size The size of the requested memory block
68 * \return Non-zero if the memory could be allocated, zero if memory
70 * \author Adam Dunkels
72 * This function allocates a chunk of managed memory. The
73 * memory allocated with this function must be deallocated
74 * using the mmem_free() function.
76 * \note This function does NOT return a pointer to the
77 * allocated memory, but a pointer to a structure that
78 * contains information about the managed memory. The
79 * macro MMEM_PTR() is used to get a pointer to the
84 mmem_alloc(struct mmem
*m
, unsigned int size
)
86 /* Check if we have enough memory left for this allocation. */
87 if(avail_memory
< size
) {
91 /* We had enough memory so we add this memory block to the end of
92 the list of allocated memory blocks. */
93 list_add(mmemlist
, m
);
95 /* Set up the pointer so that it points to the first available byte
96 in the memory block. */
97 m
->ptr
= &memory
[MMEM_SIZE
- avail_memory
];
99 /* Remember the size of this memory block. */
102 /* Decrease the amount of available memory. */
103 avail_memory
-= size
;
105 /* Return non-zero to indicate that we were able to allocate
109 /*---------------------------------------------------------------------------*/
111 * \brief Deallocate a managed memory block
112 * \param m A pointer to the managed memory block
113 * \author Adam Dunkels
115 * This function deallocates a managed memory block that
116 * previously has been allocated with mmem_alloc().
120 mmem_free(struct mmem
*m
)
124 if(m
->next
!= NULL
) {
125 /* Compact the memory after the allocation that is to be removed
126 by moving it downwards. */
127 memmove(m
->ptr
, m
->next
->ptr
,
128 &memory
[MMEM_SIZE
- avail_memory
] - (char *)m
->next
->ptr
);
130 /* Update all the memory pointers that points to memory that is
131 after the allocation that is to be removed. */
132 for(n
= m
->next
; n
!= NULL
; n
= n
->next
) {
133 n
->ptr
= (void *)((char *)n
->ptr
- m
->size
);
137 avail_memory
+= m
->size
;
139 /* Remove the memory block from the list. */
140 list_remove(mmemlist
, m
);
142 /*---------------------------------------------------------------------------*/
144 * \brief Initialize the managed memory module
145 * \author Adam Dunkels
147 * This function initializes the managed memory module and
148 * should be called before any other function from the
156 avail_memory
= MMEM_SIZE
;
158 /*---------------------------------------------------------------------------*/