Contiki-NG
Toggle main menu visibility
Loading...
Searching...
No Matches
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->used, 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->used[i] ==
false
) {
65
/* If this block was unused, we set the used flag on
66
and return a pointer to the memory block. */
67
m->used[i] =
true
;
68
return
(
void
*)((
char
*)m->mem + (i * m->size));
69
}
70
}
71
72
/* No free block was found, so we return NULL to indicate failure to
73
allocate block. */
74
return
NULL;
75
}
76
/*---------------------------------------------------------------------------*/
77
int
78
memb_free
(
struct
memb *m,
void
*ptr)
79
{
80
int
i;
81
char
*ptr2;
82
83
/* Walk through the list of blocks and try to find the block to
84
which the pointer "ptr" points to. */
85
ptr2 = (
char
*)m->mem;
86
for
(i = 0; i < m->num; ++i) {
87
if
(ptr2 == (
char
*)ptr) {
88
/* We've found the block to which "ptr" points, so we check the allocation
89
status to detect the double-free error and free the block. */
90
if
(m->used[i] ==
false
)
91
return
-1;
92
m->used[i] =
false
;
93
return
0;
94
}
95
ptr2 += m->size;
96
}
97
return
-1;
98
}
99
/*---------------------------------------------------------------------------*/
100
int
101
memb_inmemb
(
struct
memb *m,
void
*ptr)
102
{
103
return
(
char
*)ptr >= (
char
*)m->mem &&
104
(
char
*)ptr < (
char
*)m->mem + (m->num * m->size);
105
}
106
/*---------------------------------------------------------------------------*/
107
size_t
108
memb_numfree
(
struct
memb *m)
109
{
110
int
i;
111
size_t
num_free = 0;
112
113
for
(i = 0; i < m->num; ++i) {
114
if
(m->used[i] ==
false
) {
115
++num_free;
116
}
117
}
118
119
return
num_free;
120
}
121
/** @} */
memb_free
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition
memb.c:78
memb_numfree
size_t memb_numfree(struct memb *m)
Count free memory blocks.
Definition
memb.c:108
memb_inmemb
int memb_inmemb(struct memb *m, void *ptr)
Check if a given address is within a memory area previously declared with MEMB().
Definition
memb.c:101
memb_alloc
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition
memb.c:59
memb_init
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition
memb.c:52
memb.h
Memory block allocation routines.
os
lib
memb.c
Generated on
for Contiki-NG by
1.17.0