OpenCores
URL https://opencores.org/ocsvn/igor/igor/trunk

Subversion Repositories igor

[/] [igor/] [trunk/] [avr/] [eth-test/] [uip/] [memb.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 atypic
/*
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 uIP TCP/IP stack
30
 *
31
 * Author: Adam Dunkels <adam@sics.se>
32
 *
33
 * $Id: memb.c,v 1.1 2006/06/12 08:21:43 adam Exp $
34
 */
35
 
36
/**
37
 * \addtogroup memb
38
 * @{
39
 */
40
 
41
 /**
42
 * \file
43
 * Memory block allocation routines.
44
 * \author Adam Dunkels <adam@sics.se>
45
 */
46
#include <string.h>
47
 
48
#include "memb.h"
49
 
50
/*---------------------------------------------------------------------------*/
51
void
52
memb_init(struct memb_blocks *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_blocks *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_blocks *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
 
104
/** @} */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.