[1] | 1 | /* |
---|
[567] | 2 | * khm.h - Kernel Heap Manager definition. |
---|
[18] | 3 | * |
---|
[1] | 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
[619] | 5 | * Alain Greiner (2016,2017,2018,2019) |
---|
[1] | 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 | #ifndef _HEAP_MANAGER_H_ |
---|
| 26 | #define _HEAP_MANAGER_H_ |
---|
| 27 | |
---|
[14] | 28 | #include <kernel_config.h> |
---|
[457] | 29 | #include <hal_kernel_types.h> |
---|
[567] | 30 | #include <busylock.h> |
---|
[1] | 31 | |
---|
| 32 | /******************************************************************************************* |
---|
| 33 | * This structure defines a Kernel Heap Manager (KHM) in a given cluster. |
---|
[635] | 34 | * It is used to allocate memory objects, that are too large, or not enough replicated |
---|
[619] | 35 | * to use a dedicated KCM allocator. |
---|
[1] | 36 | ******************************************************************************************/ |
---|
| 37 | |
---|
| 38 | typedef struct khm_s |
---|
| 39 | { |
---|
[567] | 40 | busylock_t lock; /*! lock protecting KHM allocator */ |
---|
[1] | 41 | intptr_t base; /*! heap base address */ |
---|
| 42 | uint32_t size; /*! heap size (bytes) */ |
---|
| 43 | intptr_t next; /*! next free block address */ |
---|
| 44 | } |
---|
| 45 | khm_t; |
---|
| 46 | |
---|
| 47 | /******************************************************************************************* |
---|
| 48 | * This structure defines an allocated block descriptor for the KHM. |
---|
[18] | 49 | * This block descriptor is stored at the beginning of the allocated block. |
---|
[1] | 50 | * The returned pointer is the allocated memory block base + block descriptor size. |
---|
| 51 | ******************************************************************************************/ |
---|
| 52 | |
---|
| 53 | typedef struct khm_block_s |
---|
| 54 | { |
---|
[567] | 55 | uint32_t busy; /*! free block if zero */ |
---|
[635] | 56 | uint32_t size; /*! block size */ |
---|
[18] | 57 | } |
---|
[1] | 58 | khm_block_t; |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | /******************************************************************************************* |
---|
| 62 | * This function initializes a KHM heap manager in a given cluster. |
---|
[18] | 63 | * It is used to allocate variable size memory objects, that are not |
---|
[1] | 64 | * enough replicated to justify a dedicated KCM allocator. |
---|
| 65 | ******************************************************************************************* |
---|
| 66 | * @ khm : pointer on KHM to initialize. |
---|
| 67 | * @ heap_base : heap base address. |
---|
| 68 | * @ heap_size : heap size in bytes. |
---|
| 69 | ******************************************************************************************/ |
---|
| 70 | void khm_init( khm_t * khm ); |
---|
| 71 | |
---|
| 72 | /******************************************************************************************* |
---|
[18] | 73 | * This function allocates a memory block from the local KHM. |
---|
[1] | 74 | * The actual size of the allocated block is the requested size, plus the block descriptor |
---|
| 75 | * size, rounded to a cache line size. |
---|
| 76 | ******************************************************************************************* |
---|
| 77 | * @ khm : pointer on local kernel heap manager. |
---|
| 78 | * @ size : number of requested bytes (must be smaller than 2G bytes). |
---|
| 79 | * @ returns a pointer on memory block if success / returns NULL if failure. |
---|
| 80 | ******************************************************************************************/ |
---|
| 81 | void * khm_alloc( khm_t * khm, |
---|
| 82 | uint32_t size ); |
---|
| 83 | |
---|
| 84 | /******************************************************************************************* |
---|
| 85 | * This function releases a previoully allocated memory block to the local KHM. |
---|
| 86 | ******************************************************************************************* |
---|
| 87 | * @ ptr : pointer on released block. |
---|
| 88 | ******************************************************************************************/ |
---|
| 89 | void khm_free( void * ptr ); |
---|
| 90 | |
---|
| 91 | #endif /* _HEAP_MANAGER_H_ */ |
---|