1*10465441SEvalZero /*
2*10465441SEvalZero * Copyright (c) 2004, Swedish Institute of Computer Science.
3*10465441SEvalZero * All rights reserved.
4*10465441SEvalZero *
5*10465441SEvalZero * Redistribution and use in source and binary forms, with or without
6*10465441SEvalZero * modification, are permitted provided that the following conditions
7*10465441SEvalZero * are met:
8*10465441SEvalZero * 1. Redistributions of source code must retain the above copyright
9*10465441SEvalZero * notice, this list of conditions and the following disclaimer.
10*10465441SEvalZero * 2. Redistributions in binary form must reproduce the above copyright
11*10465441SEvalZero * notice, this list of conditions and the following disclaimer in the
12*10465441SEvalZero * documentation and/or other materials provided with the distribution.
13*10465441SEvalZero * 3. Neither the name of the Institute nor the names of its contributors
14*10465441SEvalZero * may be used to endorse or promote products derived from this software
15*10465441SEvalZero * without specific prior written permission.
16*10465441SEvalZero *
17*10465441SEvalZero * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18*10465441SEvalZero * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*10465441SEvalZero * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*10465441SEvalZero * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21*10465441SEvalZero * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*10465441SEvalZero * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*10465441SEvalZero * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*10465441SEvalZero * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*10465441SEvalZero * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*10465441SEvalZero * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*10465441SEvalZero * SUCH DAMAGE.
28*10465441SEvalZero *
29*10465441SEvalZero * This file is part of the uIP TCP/IP stack
30*10465441SEvalZero *
31*10465441SEvalZero * Author: Adam Dunkels <[email protected]>
32*10465441SEvalZero *
33*10465441SEvalZero * $Id: memb.c,v 1.1 2006/06/12 08:21:43 adam Exp $
34*10465441SEvalZero */
35*10465441SEvalZero
36*10465441SEvalZero /**
37*10465441SEvalZero * \addtogroup memb
38*10465441SEvalZero * @{
39*10465441SEvalZero */
40*10465441SEvalZero
41*10465441SEvalZero /**
42*10465441SEvalZero * \file
43*10465441SEvalZero * Memory block allocation routines.
44*10465441SEvalZero * \author Adam Dunkels <[email protected]>
45*10465441SEvalZero */
46*10465441SEvalZero #include <string.h>
47*10465441SEvalZero
48*10465441SEvalZero #include "memb.h"
49*10465441SEvalZero
50*10465441SEvalZero /*---------------------------------------------------------------------------*/
51*10465441SEvalZero void
memb_init(struct memb_blocks * m)52*10465441SEvalZero memb_init(struct memb_blocks *m)
53*10465441SEvalZero {
54*10465441SEvalZero memset(m->count, 0, m->num);
55*10465441SEvalZero memset(m->mem, 0, m->size * m->num);
56*10465441SEvalZero }
57*10465441SEvalZero /*---------------------------------------------------------------------------*/
58*10465441SEvalZero void *
memb_alloc(struct memb_blocks * m)59*10465441SEvalZero memb_alloc(struct memb_blocks *m)
60*10465441SEvalZero {
61*10465441SEvalZero int i;
62*10465441SEvalZero
63*10465441SEvalZero for(i = 0; i < m->num; ++i) {
64*10465441SEvalZero if(m->count[i] == 0) {
65*10465441SEvalZero /* If this block was unused, we increase the reference count to
66*10465441SEvalZero indicate that it now is used and return a pointer to the
67*10465441SEvalZero memory block. */
68*10465441SEvalZero ++(m->count[i]);
69*10465441SEvalZero return (void *)((char *)m->mem + (i * m->size));
70*10465441SEvalZero }
71*10465441SEvalZero }
72*10465441SEvalZero
73*10465441SEvalZero /* No free block was found, so we return NULL to indicate failure to
74*10465441SEvalZero allocate block. */
75*10465441SEvalZero return NULL;
76*10465441SEvalZero }
77*10465441SEvalZero /*---------------------------------------------------------------------------*/
78*10465441SEvalZero char
memb_free(struct memb_blocks * m,void * ptr)79*10465441SEvalZero memb_free(struct memb_blocks *m, void *ptr)
80*10465441SEvalZero {
81*10465441SEvalZero int i;
82*10465441SEvalZero char *ptr2;
83*10465441SEvalZero
84*10465441SEvalZero /* Walk through the list of blocks and try to find the block to
85*10465441SEvalZero which the pointer "ptr" points to. */
86*10465441SEvalZero ptr2 = (char *)m->mem;
87*10465441SEvalZero for(i = 0; i < m->num; ++i) {
88*10465441SEvalZero
89*10465441SEvalZero if(ptr2 == (char *)ptr) {
90*10465441SEvalZero /* We've found to block to which "ptr" points so we decrease the
91*10465441SEvalZero reference count and return the new value of it. */
92*10465441SEvalZero if(m->count[i] > 0) {
93*10465441SEvalZero /* Make sure that we don't deallocate free memory. */
94*10465441SEvalZero --(m->count[i]);
95*10465441SEvalZero }
96*10465441SEvalZero return m->count[i];
97*10465441SEvalZero }
98*10465441SEvalZero ptr2 += m->size;
99*10465441SEvalZero }
100*10465441SEvalZero return -1;
101*10465441SEvalZero }
102*10465441SEvalZero /*---------------------------------------------------------------------------*/
103*10465441SEvalZero
104*10465441SEvalZero /** @} */
105