Explicitly set CBM filetypes (PRG, USR).
[contiki-2.x.git] / core / lib / mmem.c
blob52e580fad55c65e379a73336b6ac5780cc2ca30b
1 /*
2 * Copyright (c) 2005, Swedish Institute of Computer Science
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
27 * SUCH DAMAGE.
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 $
34 /**
35 * \addtogroup mmem
36 * @{
39 /**
40 * \file
41 * Implementation of the managed memory allocator
42 * \author
43 * Adam Dunkels <adam@sics.se>
48 #include "mmem.h"
49 #include "list.h"
50 #include "contiki-conf.h"
51 #include <string.h>
53 #ifdef MMEM_CONF_SIZE
54 #define MMEM_SIZE MMEM_CONF_SIZE
55 #else
56 #define MMEM_SIZE 4096
57 #endif
59 LIST(mmemlist);
60 unsigned int avail_memory;
61 static char memory[MMEM_SIZE];
63 /*---------------------------------------------------------------------------*/
64 /**
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
69 * was not available.
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
80 * allocated memory.
83 int
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) {
88 return 0;
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. */
100 m->size = size;
102 /* Decrease the amount of available memory. */
103 avail_memory -= size;
105 /* Return non-zero to indicate that we were able to allocate
106 memory. */
107 return 1;
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().
119 void
120 mmem_free(struct mmem *m)
122 struct mmem *n;
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
149 * module.
152 void
153 mmem_init(void)
155 list_init(mmemlist);
156 avail_memory = MMEM_SIZE;
158 /*---------------------------------------------------------------------------*/
160 /** @} */