-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathoc_memb.c
179 lines (167 loc) · 5.02 KB
/
oc_memb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2004, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
* Author: Adam Dunkels <[email protected]>
*
*/
#include "oc_memb.h"
#include "port/oc_log.h"
#include <string.h>
#ifdef OC_MEMORY_TRACE
#include "oc_mem_trace.h"
#endif
/*---------------------------------------------------------------------------*/
void
oc_memb_init(struct oc_memb *m)
{
if (m->num > 0) {
memset(m->count, 0, m->num);
memset(m->mem, 0, (unsigned)m->num * sizeof(char *));
}
}
/*---------------------------------------------------------------------------*/
void *
_oc_memb_alloc(
#ifdef OC_MEMORY_TRACE
const char *func,
#endif
struct oc_memb *m)
{
if (!m) {
OC_ERR("oc_memb is NULL");
return NULL;
}
int i = m->num;
void *ptr = NULL;
if (m->num > 0) {
for (i = 0; i < m->num; i++) {
if (m->count[i] == 0) {
/* If this block was unused, we increase the reference count to
indicate that it now is used and return a pointer to the
memory block. */
++(m->count[i]);
break;
}
}
if (i < m->num) {
int offset = i * (int)m->size;
ptr = (void *)((char *)m->mem + offset);
memset(ptr, 0, m->size);
}
}
#ifdef OC_DYNAMIC_ALLOCATION
else {
ptr = calloc(1, m->size);
// OC_DBG("==> calloc %p size=%d", ptr, m->size);
}
#endif /* OC_DYNAMIC_ALLOCATION */
if (!ptr) {
/* No free block was found, so we return NULL to indicate failure to
allocate block. */
return NULL;
}
#ifdef OC_MEMORY_TRACE
oc_mem_trace_add_pace(func, m->size, MEM_TRACE_ALLOC, ptr);
#endif
return ptr;
}
/*---------------------------------------------------------------------------*/
char
_oc_memb_free(
#ifdef OC_MEMORY_TRACE
const char *func,
#endif
struct oc_memb *m, void *ptr)
{
if (!m) {
OC_ERR("oc_memb is NULL");
return -1;
}
#ifdef OC_MEMORY_TRACE
oc_mem_trace_add_pace(func, m->size, MEM_TRACE_FREE, ptr);
#endif
int i = m->num;
char *ptr2 = NULL;
if (m->num > 0) {
/* Walk through the list of blocks and try to find the block to
which the pointer "ptr" points to. */
ptr2 = (char *)m->mem;
for (i = 0; i < m->num; ++i) {
if (ptr2 == (char *)ptr) {
/* We've found to block to which "ptr" points so we decrease the
reference count and return the new value of it. */
if (m->count[i] > 0) {
/* Make sure that we don't deallocate free memory. */
--(m->count[i]);
}
break;
}
ptr2 += m->size;
}
}
#ifdef OC_DYNAMIC_ALLOCATION
else {
free(ptr);
// OC_DBG(" ==< free %p", ptr);
}
#endif /* OC_DYNAMIC_ALLOCATION */
if (m->buffers_avail_cb) {
m->buffers_avail_cb(oc_memb_numfree(m));
}
return 0;
}
/*---------------------------------------------------------------------------*/
int
oc_memb_inmemb(struct oc_memb *m, void *ptr)
{
return ((char *)ptr >= (char *)m->mem) &&
((char *)ptr < ((char *)m->mem + (m->num * m->size)));
}
/*---------------------------------------------------------------------------*/
int
oc_memb_numfree(struct oc_memb *m)
{
int i;
int num_free = 0;
for (i = 0; i < m->num; ++i) {
if (m->count[i] == 0) {
++num_free;
}
}
return num_free;
}
/*---------------------------------------------------------------------------*/
void
oc_memb_set_buffers_avail_cb(struct oc_memb *m,
oc_memb_buffers_avail_callback_t cb)
{
m->buffers_avail_cb = cb;
}
/*---------------------------------------------------------------------------*/