source: soft/giet_vm/giet_libs/malloc.h @ 560

Last change on this file since 560 was 550, checked in by alain, 10 years ago

Cosmetic.

  • Property svn:executable set to *
File size: 5.2 KB
RevLine 
[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.
16// - They are pre-classed in NB_SIZES linked lists, where all blocks in the
17//   same list have the same size.
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
42//   heap_base), all allocated blocks are aligned on the actual_size.
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
54// - The allocated[] array is stored at the end of heap segment. This consume
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////////////////////////////////////////////////////////////////////////////////
75typedef struct giet_heap_s
76{
77    unsigned int   init;            // initialised <=> value == HEAP_INITIALIZED
78    unsigned int   x;               // cluster X coordinate
79    unsigned int   y;               // cluster Y coordinate
80    unsigned int   heap_base;       // heap base address
81    unsigned int   heap_size;       // heap size (bytes)
82    unsigned int   alloc_base;      // alloc[] array base address
83    unsigned int   alloc_size;      // alloc[] array size (bytes)
[461]84    user_lock_t    lock;            // lock protecting exclusive access
[382]85    unsigned int   free[32];        // array of base addresses of free blocks
86                                    // (address of first block of a given size)
87} giet_heap_t;
88
89///////// user functions /////////////////
90
[541]91extern void heap_init( unsigned int x,
92                       unsigned int y );
93
[382]94extern void* malloc( unsigned int size );
95
96extern void* remote_malloc( unsigned int size, 
97                            unsigned int x,
98                            unsigned int y );
99
100extern void free(void * ptr);
101
102
[258]103#endif
104
105// Local Variables:
106// tab-width: 4
107// c-basic-offset: 4
108// c-file-offsets:((innamespace . 0)(inline-open . 0))
109// indent-tabs-mode: nil
110// End:
111// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
112
Note: See TracBrowser for help on using the repository browser.