Subversion Repositories shark

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
21
 CVS :        $Id: free.c,v 1.1.1.1 2002-03-29 14:12:52 pj Exp $
22
 
23
 File:        $File$
24
 Revision:    $Revision: 1.1.1.1 $
25
 Last update: $Date: 2002-03-29 14:12:52 $
26
 ------------
27
 
28
this code is from:
29
List-based Memory Management, from OsKit.
30
 
31
**/
32
 
33
/*
34
 * Copyright (C) 2000 Paolo Gai
35
 *
36
 * This program is free software; you can redistribute it and/or modify
37
 * it under the terms of the GNU General Public License as published by
38
 * the Free Software Foundation; either version 2 of the License, or
39
 * (at your option) any later version.
40
 *
41
 * This program is distributed in the hope that it will be useful,
42
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44
 * GNU General Public License for more details.
45
 *
46
 * You should have received a copy of the GNU General Public License
47
 * along with this program; if not, write to the Free Software
48
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
49
 *
50
 */
51
/*
52
 * Copyright (c) 1995, 1998-1999 University of Utah and the Flux Group.
53
 * All rights reserved.
54
 *
55
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
56
 * as "open source;" you can redistribute it and/or modify it under the terms
57
 * of the GNU General Public License (GPL), version 2, as published by the Free
58
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
59
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
60
 *
61
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
62
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
63
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
64
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
65
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
66
 */
67
 
68
#include <kernel/lmm.h>
69
#define assert(test) assertk(test)
70
 
71
void lmm_free(lmm_t *lmm, void *block, size_t size)
72
{
73
        struct lmm_region *reg;
74
        struct lmm_node *node = (struct lmm_node*)
75
                                ((DWORD)block & ~ALIGN_MASK);
76
        struct lmm_node *prevnode, *nextnode;
77
 
78
        assert(lmm != 0);
79
        assert(block != 0);
80
        assert(size > 0);
81
 
82
        size = (((DWORD)block & ALIGN_MASK) + size + ALIGN_MASK)
83
                & ~ALIGN_MASK;
84
 
85
        /* First find the region to add this block to.  */
86
        for (reg = lmm->regions; ; reg = reg->next)
87
        {
88
                assert(reg != 0);
89
                CHECKREGPTR(reg);
90
 
91
                if (((DWORD)node >= reg->min)
92
                    && ((DWORD)node < reg->max))
93
                        break;
94
        }
95
 
96
        /* Record the newly freed space in the region's free space counter.  */
97
        reg->free += size;
98
        assert(reg->free <= reg->max - reg->min);
99
 
100
        /* Now find the location in that region's free list
101
           at which to add the node.  */
102
        for (prevnode = 0, nextnode = reg->nodes;
103
             (nextnode != 0) && (nextnode < node);
104
             prevnode = nextnode, nextnode = nextnode->next);
105
 
106
        /* Coalesce the new free chunk into the previous chunk if possible.  */
107
        if ((prevnode) &&
108
            ((DWORD)prevnode + prevnode->size >= (DWORD)node))
109
        {
110
                assert((DWORD)prevnode + prevnode->size
111
                       == (DWORD)node);
112
 
113
                /* Coalesce prevnode with nextnode if possible.  */
114
                if (((DWORD)nextnode)
115
                    && ((DWORD)node + size >= (DWORD)nextnode))
116
                {
117
                        assert((DWORD)node + size
118
                               == (DWORD)nextnode);
119
 
120
                        prevnode->size += size + nextnode->size;
121
                        prevnode->next = nextnode->next;
122
                }
123
                else
124
                {
125
                        /* Not possible -
126
                           just grow prevnode around newly freed memory.  */
127
                        prevnode->size += size;
128
                }
129
        }
130
        else
131
        {
132
                /* Insert the new node into the free list.  */
133
                if (prevnode)
134
                        prevnode->next = node;
135
                else
136
                        reg->nodes = node;
137
 
138
                /* Try coalescing the new node with the nextnode.  */
139
                if ((nextnode) &&
140
                    (DWORD)node + size >= (DWORD)nextnode)
141
                {
142
                        node->size = size + nextnode->size;
143
                        node->next = nextnode->next;
144
                }
145
                else
146
                {
147
                        node->size = size;
148
                        node->next = nextnode;
149
                }
150
        }
151
}
152