|
| 1 | +/* Copyright 2014 Samsung Electronics Co., Ltd. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#ifndef MEM_HEAP_INTERNAL_H |
| 17 | +#define MEM_HEAP_INTERNAL_H |
| 18 | + |
| 19 | +#ifndef MEM_HEAP_INTERNAL |
| 20 | +# error "The header is for internal routines of memory heap component. Please, don't use the routines directly." |
| 21 | +#endif /* !MEM_HEAP_INTERNAL */ |
| 22 | + |
| 23 | +#include "mem-allocator.h" |
| 24 | +#include "mem-config.h" |
| 25 | + |
| 26 | +/** \addtogroup mem Memory allocation |
| 27 | + * @{ |
| 28 | + */ |
| 29 | + |
| 30 | +/* Calculate heap area size, leaving space for a pointer to the free list */ |
| 31 | +#define MEM_HEAP_AREA_SIZE (MEM_HEAP_SIZE - MEM_ALIGNMENT) |
| 32 | +#define MEM_HEAP_END_OF_LIST ((mem_heap_free_t *const) ~((uint32_t) 0x0)) |
| 33 | + |
| 34 | +/** |
| 35 | + * Free region node |
| 36 | + */ |
| 37 | +typedef struct |
| 38 | +{ |
| 39 | + uint32_t next_offset; /* Offset of next region in list */ |
| 40 | + uint32_t size; /* Size of region */ |
| 41 | +} mem_heap_free_t; |
| 42 | + |
| 43 | +#ifdef MEM_HEAP_PTR_64 |
| 44 | +#define MEM_HEAP_GET_OFFSET_FROM_ADDR(h, p) ((uint32_t) ((uint8_t *) (p) - (uint8_t *) (h)->area)) |
| 45 | +#define MEM_HEAP_GET_ADDR_FROM_OFFSET(h, u) ((mem_heap_free_t *) &(h)->area[u]) |
| 46 | +#else |
| 47 | +/* In this case we simply store the pointer, since it fits anyway. */ |
| 48 | +#define MEM_HEAP_GET_OFFSET_FROM_ADDR(h, p) ((uint32_t) (p)) |
| 49 | +#define MEM_HEAP_GET_ADDR_FROM_OFFSET(h, u) ((mem_heap_free_t *)(u)) |
| 50 | +#endif |
| 51 | + |
| 52 | +/** |
| 53 | + * Heap structure |
| 54 | + */ |
| 55 | +struct mem_heap |
| 56 | +{ |
| 57 | + mem_heap_free_t first; |
| 58 | + /* This is used to speed up deallocation. */ |
| 59 | + mem_heap_free_t *list_skip_p; |
| 60 | + /** |
| 61 | + * Size of allocated regions |
| 62 | + */ |
| 63 | + size_t allocated_size; |
| 64 | + |
| 65 | + /** |
| 66 | + * Current limit of heap usage, that is upon being reached, causes call of "try give memory back" callbacks |
| 67 | + */ |
| 68 | + size_t limit; |
| 69 | + |
| 70 | + /** |
| 71 | + * Heap area |
| 72 | + */ |
| 73 | + uint8_t area[MEM_HEAP_AREA_SIZE] __attribute__ ((aligned (MEM_ALIGNMENT))); |
| 74 | +}; |
| 75 | + |
| 76 | + |
| 77 | +/** |
| 78 | + * @} |
| 79 | + */ |
| 80 | + |
| 81 | +#endif /* MEM_HEAP_INTERNAL_H */ |
0 commit comments