| 1 | /* |
|---|
| 2 | * khm.h - kernel heap manager used for variable size memory allocation. |
|---|
| 3 | * |
|---|
| 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
|---|
| 5 | * Mohamed Lamine Karaoui (2015) |
|---|
| 6 | * Alain Greiner (2016) |
|---|
| 7 | * |
|---|
| 8 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 9 | * |
|---|
| 10 | * This file is part of ALMOS-MKH. |
|---|
| 11 | * |
|---|
| 12 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| 13 | * under the terms of the GNU General Public License as published by |
|---|
| 14 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 15 | * |
|---|
| 16 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 19 | * General Public License for more details. |
|---|
| 20 | * |
|---|
| 21 | * You should have received a copy of the GNU General Public License |
|---|
| 22 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
|---|
| 23 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #ifndef _HEAP_MANAGER_H_ |
|---|
| 27 | #define _HEAP_MANAGER_H_ |
|---|
| 28 | |
|---|
| 29 | #include <kernel_config.h> |
|---|
| 30 | #include <hal_types.h> |
|---|
| 31 | #include <spinlock.h> |
|---|
| 32 | |
|---|
| 33 | /******************************************************************************************* |
|---|
| 34 | * This structure defines a Kernel Heap Manager (KHM) in a given cluster. |
|---|
| 35 | * It is used to allocate memory objects, that are not |
|---|
| 36 | * enough replicated to justify a dedicated KCM allocator. |
|---|
| 37 | ******************************************************************************************/ |
|---|
| 38 | |
|---|
| 39 | typedef struct khm_s |
|---|
| 40 | { |
|---|
| 41 | spinlock_t lock; /*! lock protecting exclusive access to heap */ |
|---|
| 42 | intptr_t base; /*! heap base address */ |
|---|
| 43 | uint32_t size; /*! heap size (bytes) */ |
|---|
| 44 | intptr_t next; /*! next free block address */ |
|---|
| 45 | } |
|---|
| 46 | khm_t; |
|---|
| 47 | |
|---|
| 48 | /******************************************************************************************* |
|---|
| 49 | * This structure defines an allocated block descriptor for the KHM. |
|---|
| 50 | * This block descriptor is stored at the beginning of the allocated block. |
|---|
| 51 | * The returned pointer is the allocated memory block base + block descriptor size. |
|---|
| 52 | ******************************************************************************************/ |
|---|
| 53 | |
|---|
| 54 | typedef struct khm_block_s |
|---|
| 55 | { |
|---|
| 56 | uint32_t busy:1; /*! free block if zero */ |
|---|
| 57 | uint32_t size:31; /*! size coded on 31 bits */ |
|---|
| 58 | } |
|---|
| 59 | khm_block_t; |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | /******************************************************************************************* |
|---|
| 63 | * This function initializes a KHM heap manager in a given cluster. |
|---|
| 64 | * It is used to allocate variable size memory objects, that are not |
|---|
| 65 | * enough replicated to justify a dedicated KCM allocator. |
|---|
| 66 | ******************************************************************************************* |
|---|
| 67 | * @ khm : pointer on KHM to initialize. |
|---|
| 68 | * @ heap_base : heap base address. |
|---|
| 69 | * @ heap_size : heap size in bytes. |
|---|
| 70 | ******************************************************************************************/ |
|---|
| 71 | void khm_init( khm_t * khm ); |
|---|
| 72 | |
|---|
| 73 | /******************************************************************************************* |
|---|
| 74 | * This function allocates a memory block from the local KHM. |
|---|
| 75 | * The actual size of the allocated block is the requested size, plus the block descriptor |
|---|
| 76 | * size, rounded to a cache line size. |
|---|
| 77 | ******************************************************************************************* |
|---|
| 78 | * @ khm : pointer on local kernel heap manager. |
|---|
| 79 | * @ size : number of requested bytes (must be smaller than 2G bytes). |
|---|
| 80 | * @ returns a pointer on memory block if success / returns NULL if failure. |
|---|
| 81 | ******************************************************************************************/ |
|---|
| 82 | void * khm_alloc( khm_t * khm, |
|---|
| 83 | uint32_t size ); |
|---|
| 84 | |
|---|
| 85 | /******************************************************************************************* |
|---|
| 86 | * This function releases a previoully allocated memory block to the local KHM. |
|---|
| 87 | ******************************************************************************************* |
|---|
| 88 | * @ ptr : pointer on released block. |
|---|
| 89 | ******************************************************************************************/ |
|---|
| 90 | void khm_free( void * ptr ); |
|---|
| 91 | |
|---|
| 92 | #endif /* _HEAP_MANAGER_H_ */ |
|---|