[382] | 1 | //////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 2 | // File : malloc.h |
---|
| 3 | // Date : 05/03/2013 |
---|
[382] | 4 | // Author : Jean-Baptiste Bréjon / alain greiner |
---|
[258] | 5 | // Copyright (c) UPMC-LIP6 |
---|
[382] | 6 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 7 | // Initialisation policy: |
---|
[550] | 8 | // The user application must initialize the heap(x,y) structure before |
---|
| 9 | // using the malloc() or remote_malloc() functions, and this initialization |
---|
| 10 | // must be done by a single task. |
---|
[382] | 11 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 12 | // Free blocks organisation: |
---|
| 13 | // - All free blocks have a size that is a power of 2, larger or equal |
---|
| 14 | // to MIN_BLOCK_SIZE (typically 128 bytes). |
---|
| 15 | // - All free blocks are aligned. |
---|
[588] | 16 | // - They are pre-classed in NB_SIZES linked lists, where all blocks in a |
---|
| 17 | // given list have the same size. |
---|
[382] | 18 | // - The NEXT pointer implementing those linked lists is written |
---|
| 19 | // in the 4 first bytes of the block itself, using the unsigned int type. |
---|
| 20 | // - The pointers on the first free block for each size are stored in an |
---|
| 21 | // array of pointers free[32] in the heap(x,y) descriptor. |
---|
| 22 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 23 | // Allocation policy: |
---|
| 24 | // - The block size required by the user can be any value, but the allocated |
---|
| 25 | // block size can be larger than the requested size: |
---|
| 26 | // - The allocator computes actual_size, that is the smallest power of 2 |
---|
| 27 | // value larger or equal to the requested size AND larger or equal to |
---|
| 28 | // MIN_BLOCK_SIZE. |
---|
| 29 | // - It pop the linked list of free blocks corresponding to actual_size, |
---|
| 30 | // and returns the block B if the list[actual_size] is not empty. |
---|
| 31 | // - If the list[actual_size] is empty, it pop the list[actual_size * 2]. |
---|
| 32 | // If a block B' is found, it break this block in 2 B/2 blocks, returns |
---|
| 33 | // the first B/2 block and push the other B/2 block into list[actual_size]. |
---|
| 34 | // - If the list[actual_size * 2] is empty, it pop the list[actual_size * 4]. |
---|
| 35 | // If a block B is found, it break this block in 3 blocks B/4, B/4 and B/2, |
---|
| 36 | // returns the first B/4 block, push the other blocks B/4 and B/2 into |
---|
| 37 | // the proper lists. etc... |
---|
| 38 | // - If no block satisfying the request is available it returns a failure |
---|
| 39 | // (NULL pointer). |
---|
| 40 | // - This allocation policy has the nice following property: |
---|
| 41 | // If the heap segment is aligned (the heap_base is a multiple of the |
---|
[588] | 42 | // heap_size), all allocated blocks are aligned on the actual_size. |
---|
[382] | 43 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 44 | // Free policy: |
---|
| 45 | // - Each allocated block is registered in an alloc[] array of unsigned char. |
---|
| 46 | // - This registration is required by the free() operation, because the size |
---|
[550] | 47 | // of the allocated block must be obtained from the base address of the block. |
---|
[382] | 48 | // - The number of entries in this array is equal to the max number |
---|
| 49 | // of allocated block is : heap_size / 128. |
---|
| 50 | // - For each allocated block, the value registered in the alloc[] array |
---|
| 51 | // is log2( size_of_allocated_block ). |
---|
| 52 | // - The index in this array is computed from the allocated block base address: |
---|
| 53 | // index = (block_base - heap_base) / MIN_BLOCK_SIZE |
---|
[588] | 54 | // - The alloc[] array is stored at the end of heap segment. This consume |
---|
[382] | 55 | // (1 / MIN_BLOCK_SIZE) of the total heap storage capacity. |
---|
| 56 | //////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 57 | |
---|
| 58 | #ifndef _MALLOC_H_ |
---|
| 59 | #define _MALLOC_H_ |
---|
[295] | 60 | |
---|
[258] | 61 | #include "giet_config.h" |
---|
[461] | 62 | #include "user_lock.h" |
---|
[258] | 63 | |
---|
[461] | 64 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 65 | // magic number indicating that heap(x,y) has been initialized. |
---|
| 66 | //////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 67 | |
---|
[382] | 68 | #define HEAP_INITIALIZED 0xDEADBEEF |
---|
[258] | 69 | |
---|
[382] | 70 | #define MIN_BLOCK_SIZE 0x80 |
---|
[258] | 71 | |
---|
[382] | 72 | //////////////////////////////////////////////////////////////////////////////// |
---|
| 73 | // heap(x,y) descriptor (one per cluster) |
---|
| 74 | //////////////////////////////////////////////////////////////////////////////// |
---|
[588] | 75 | |
---|
[382] | 76 | typedef struct giet_heap_s |
---|
| 77 | { |
---|
[588] | 78 | user_lock_t lock; // lock protecting exclusive access |
---|
[382] | 79 | unsigned int init; // initialised <=> value == HEAP_INITIALIZED |
---|
| 80 | unsigned int x; // cluster X coordinate |
---|
| 81 | unsigned int y; // cluster Y coordinate |
---|
| 82 | unsigned int heap_base; // heap base address |
---|
| 83 | unsigned int heap_size; // heap size (bytes) |
---|
| 84 | unsigned int alloc_base; // alloc[] array base address |
---|
| 85 | unsigned int alloc_size; // alloc[] array size (bytes) |
---|
| 86 | unsigned int free[32]; // array of base addresses of free blocks |
---|
| 87 | // (address of first block of a given size) |
---|
| 88 | } giet_heap_t; |
---|
| 89 | |
---|
| 90 | ///////// user functions ///////////////// |
---|
| 91 | |
---|
[541] | 92 | extern void heap_init( unsigned int x, |
---|
| 93 | unsigned int y ); |
---|
| 94 | |
---|
[382] | 95 | extern void* malloc( unsigned int size ); |
---|
| 96 | |
---|
| 97 | extern void* remote_malloc( unsigned int size, |
---|
| 98 | unsigned int x, |
---|
| 99 | unsigned int y ); |
---|
| 100 | |
---|
| 101 | extern void free(void * ptr); |
---|
| 102 | |
---|
| 103 | |
---|
[258] | 104 | #endif |
---|
| 105 | |
---|
| 106 | // Local Variables: |
---|
| 107 | // tab-width: 4 |
---|
| 108 | // c-basic-offset: 4 |
---|
| 109 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 110 | // indent-tabs-mode: nil |
---|
| 111 | // End: |
---|
| 112 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 113 | |
---|