Contiki-NG
memb.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
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.
16  *
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.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 /**
36  * \addtogroup memb
37  * @{
38  */
39 
40  /**
41  * \file
42  * Memory block allocation routines.
43  * \author Adam Dunkels <adam@sics.se>
44  */
45 #include <string.h>
46 
47 #include "contiki.h"
48 #include "lib/memb.h"
49 
50 /*---------------------------------------------------------------------------*/
51 void
52 memb_init(struct memb *m)
53 {
54  memset(m->count, 0, m->num);
55  memset(m->mem, 0, m->size * m->num);
56 }
57 /*---------------------------------------------------------------------------*/
58 void *
59 memb_alloc(struct memb *m)
60 {
61  int i;
62 
63  for(i = 0; i < m->num; ++i) {
64  if(m->count[i] == 0) {
65  /* If this block was unused, we increase the reference count to
66  indicate that it now is used and return a pointer to the
67  memory block. */
68  ++(m->count[i]);
69  return (void *)((char *)m->mem + (i * m->size));
70  }
71  }
72 
73  /* No free block was found, so we return NULL to indicate failure to
74  allocate block. */
75  return NULL;
76 }
77 /*---------------------------------------------------------------------------*/
78 char
79 memb_free(struct memb *m, void *ptr)
80 {
81  int i;
82  char *ptr2;
83 
84  /* Walk through the list of blocks and try to find the block to
85  which the pointer "ptr" points to. */
86  ptr2 = (char *)m->mem;
87  for(i = 0; i < m->num; ++i) {
88 
89  if(ptr2 == (char *)ptr) {
90  /* We've found to block to which "ptr" points so we decrease the
91  reference count and return the new value of it. */
92  if(m->count[i] > 0) {
93  /* Make sure that we don't deallocate free memory. */
94  --(m->count[i]);
95  }
96  return m->count[i];
97  }
98  ptr2 += m->size;
99  }
100  return -1;
101 }
102 /*---------------------------------------------------------------------------*/
103 int
104 memb_inmemb(struct memb *m, void *ptr)
105 {
106  return (char *)ptr >= (char *)m->mem &&
107  (char *)ptr < (char *)m->mem + (m->num * m->size);
108 }
109 /*---------------------------------------------------------------------------*/
110 int
111 memb_numfree(struct memb *m)
112 {
113  int i;
114  int num_free = 0;
115 
116  for(i = 0; i < m->num; ++i) {
117  if(m->count[i] == 0) {
118  ++num_free;
119  }
120  }
121 
122  return num_free;
123 }
124 /** @} */
Memory block allocation routines.
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79