source: trunk/kernel/mm/khm.c @ 44

Last change on this file since 44 was 20, checked in by max@…, 7 years ago

cosmetic and improve a few comments

File size: 4.2 KB
Line 
1/*
2 * khm.c - kernel heap manager implementation.
3 *
4 * Authors  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *          Alain Greiner (2016)
6 *
7 * Copyright (c)  UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <kernel_config.h>
26#include <hal_types.h>
27#include <hal_special.h>
28#include <spinlock.h>
29#include <bits.h>
30#include <printk.h>
31#include <thread.h>
32#include <cluster.h>
33#include <page.h>
34#include <ppm.h>
35#include <khm.h>
36
37
38////////////////////////////
39void khm_init( khm_t * khm )
40{
41        // check config parameters
42        assert( ((CONFIG_PPM_PAGE_SHIFT + CONFIG_PPM_HEAP_ORDER) < 32 ) , __FUNCTION__ ,
43                 "CONFIG_PPM_HEAP_ORDER too large" );
44
45        // initialize lock
46        spinlock_init( &khm->lock );
47
48        // compute kernel heap size
49        intptr_t heap_size = (1 << CONFIG_PPM_HEAP_ORDER) << CONFIG_PPM_PAGE_SHIFT;
50
51        // get kernel heap base from PPM
52        page_t * page      = ppm_alloc_pages( CONFIG_PPM_HEAP_ORDER );
53        void   * heap_base = ppm_page2base( page );
54
55        // initialize first block (complete heap)
56        khm_block_t * block = (khm_block_t *)heap_base;
57        block->size = heap_size;
58        block->busy = 0;
59
60        // initialize KHM fields
61        khm->base    = (intptr_t)heap_base;
62        khm->size    = heap_size;
63        khm->next    = (intptr_t)heap_base;
64}
65
66/////////////////////////////////
67void * khm_alloc( khm_t    * khm,
68                  uint32_t   size )
69{
70        khm_block_t  * current;
71        khm_block_t  * next;
72        uint32_t       effective_size;
73
74        // compute actual block size
75        effective_size = size + sizeof(khm_block_t);
76        effective_size = ARROUND_UP( effective_size, CONFIG_CACHE_LINE_SIZE );
77
78        // get lock protecting heap
79        spinlock_lock( &khm->lock );
80
81        // define a starting block to scan existing blocks
82        if( ((khm_block_t*)khm->next)->size < effective_size ) current = (khm_block_t*)khm->base;
83        else                                                   current = (khm_block_t*)khm->next;
84
85        // scan all existing blocks to find a free block large enough
86        while( current->busy || (current->size < effective_size))
87        {
88                // get next block pointer
89                current = (khm_block_t*)((char*)current + current->size);
90
91                if( (intptr_t)current >= (khm->base + khm->size) )  // heap full
92                {
93                        spinlock_unlock(&khm->lock);
94
95                        printk("\n[ERROR] in %s : failed to allocate block of size %d\n",
96                               __FUNCTION__ , effective_size );
97                        return NULL;
98                }
99        }
100
101        // split the current block if it is too large
102        if( (current->size - effective_size) >= CONFIG_CACHE_LINE_SIZE )
103        {
104                // update new free block features
105                next           = (khm_block_t *)((char*)current + effective_size);
106                next->size     = current->size - effective_size;
107                next->busy     = 0;
108
109                // register new free block
110                khm->next = (intptr_t)next;
111
112                // update allocated block features
113                current->size  = effective_size;
114                current->busy  = 1;
115        }
116        else
117        {
118                // change block state
119                current->busy  = 1;
120        }
121
122        // release lock protecting heap
123        spinlock_unlock( &khm->lock );
124
125        return (char*)current + sizeof(khm_block_t);
126}
127
128///////////////////////////
129void khm_free( void * ptr )
130{
131        khm_t * khm = &LOCAL_CLUSTER->khm;
132
133        khm_block_t * current;
134        khm_block_t * next;
135
136        if(ptr == NULL) return;
137
138        current = (khm_block_t *)((char*)ptr - sizeof(khm_block_t));
139
140        // get lock protecting heap
141        spinlock_lock(&khm->lock);
142
143        // release block
144        current->busy = 0;
145
146        // try to merge released block with the next
147        while ( 1 )
148        {
149                next = (khm_block_t*)((char*)current + current->size);
150                if ( ((intptr_t)next >= (khm->base + khm->size)) || (next->busy == 1) ) break;
151                current->size += next->size;
152        }
153
154        if( (intptr_t)current < khm->next ) khm->next = (intptr_t)current;
155
156        // release lock protecting heap
157        spinlock_unlock( &khm->lock );
158}
159
Note: See TracBrowser for help on using the repository browser.