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

Last change on this file since 5 was 1, checked in by alain, 8 years ago

First import

File size: 4.4 KB
RevLine 
[1]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 <almos_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    if( (CONFIG_PPM_PAGE_SHIFT + CONFIG_PPM_HEAP_ORDER) >= 32 )
43    {
44        printk("\n[PANIC] in %s : CONFIG_PPM_HEAP_ORDER too large\n", __FUNCTION__ );
45        hal_core_sleep();
46    }
47
48    // initialize lock
49        spinlock_init( &khm->lock ); 
50   
51    //  compute kernel heap size
52    intptr_t heap_size = (1 << CONFIG_PPM_HEAP_ORDER) << CONFIG_PPM_PAGE_SHIFT;
53
54    // get kernel heap base from PPM
55    page_t * page      = ppm_alloc_pages( CONFIG_PPM_HEAP_ORDER );
56    void   * heap_base = ppm_page2base( page );
57
58    // initializes first block == complete heap
59        khm_block_t * block = (khm_block_t *)heap_base;
60        block->size = heap_size;
61        block->busy = 0;
62
63    // initializes KHM fields
64        khm->base    = (intptr_t)heap_base;
65        khm->size    = heap_size;
66        khm->next    = (intptr_t)heap_base;
67}
68
69/////////////////////////////////
70void * khm_alloc( khm_t    * khm, 
71                  uint32_t   size )
72{
73        khm_block_t  * current;       
74        khm_block_t  * next;
75        uint32_t       effective_size;
76
77    // compute actual block size
78        effective_size = size + sizeof(khm_block_t);
79        effective_size = ARROUND_UP( effective_size, CONFIG_CACHE_LINE_SIZE );
80
81    // get lock protecting heap
82        spinlock_lock( &khm->lock );
83 
84    // define a starting block to scan existing blocks
85    if( ((khm_block_t*)khm->next)->size < effective_size ) current = (khm_block_t*)khm->base;
86    else                                                   current = (khm_block_t*)khm->next;
87
88    // scan all existing blocks to find a large enough free block
89        while( current->busy || (current->size < effective_size)) 
90        {
91        // get next block pointer
92                current = (khm_block_t*)((char*)current + current->size);
93   
94                if( (intptr_t)current >= (khm->base + khm->size) )  // heap full
95                {
96                        spinlock_unlock(&khm->lock);
97
98                        printk("\n[ERROR] in %s : failed to allocate block of size %d\n", 
99                               __FUNCTION__ , effective_size );
100                        return NULL;
101                }
102        }
103
104    // split the current block if current block is too large
105        if( (current->size - effective_size) >= CONFIG_CACHE_LINE_SIZE )
106        {
107        // update new free block features
108                next           = (khm_block_t *)((char*)current + effective_size);
109                next->size     = current->size - effective_size;
110                next->busy     = 0;
111
112        // register new free block
113                khm->next = (intptr_t)next;
114
115        // update allocated block features
116                current->size  = effective_size;
117                current->busy  = 1;
118        }
119        else
120    {
121        // change block state
122                current->busy  = 1;
123    }
124
125    // release lock protecting heap
126        spinlock_unlock( &khm->lock );
127
128        return (char*)current + sizeof(khm_block_t);
129}
130
131///////////////////////////
132void khm_free( void * ptr )
133{
134        khm_t * khm = &LOCAL_CLUSTER->khm;
135
136        khm_block_t * current;
137        khm_block_t * next;
138 
139        if(ptr == NULL) return;
140 
141        current = (khm_block_t *)((char*)ptr - sizeof(khm_block_t));
142 
143    // get lock protecting heap
144        spinlock_lock(&khm->lock);
145
146    // release block
147        current->busy = 0;
148 
149    // try to merge released block with the next
150        while ( 1 )
151        { 
152        next = (khm_block_t*)((char*)current + current->size);
153                if ( ((intptr_t)next >= (khm->base + khm->size)) || (next->busy == 1) ) break;
154                current->size += next->size;
155        }
156
157        if( (intptr_t)current < khm->next ) khm->next = (intptr_t)current;
158 
159    // release lock protecting heap
160        spinlock_unlock( &khm->lock );
161}
162
Note: See TracBrowser for help on using the repository browser.