Xenomai 3.3.2
Loading...
Searching...
No Matches
heapmem.h
1/*
2 * Copyright (C) 2018 Philippe Gerum <rpm@xenomai.org>.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 */
18#ifndef _BOILERPLATE_HEAPMEM_H
19#define _BOILERPLATE_HEAPMEM_H
20
21#include <sys/types.h>
22#include <stdint.h>
23#include <limits.h>
24#include <boilerplate/list.h>
25#include <boilerplate/lock.h>
26#include <boilerplate/avl.h>
27
28#define HEAPMEM_PAGE_SHIFT 9 /* 2^9 => 512 bytes */
29#define HEAPMEM_PAGE_SIZE (1UL << HEAPMEM_PAGE_SHIFT)
30#define HEAPMEM_PAGE_MASK (~(HEAPMEM_PAGE_SIZE - 1))
31#define HEAPMEM_MIN_LOG2 4 /* 16 bytes */
32/*
33 * Use bucketed memory for sizes between 2^HEAPMEM_MIN_LOG2 and
34 * 2^(HEAPMEM_PAGE_SHIFT-1).
35 */
36#define HEAPMEM_MAX (HEAPMEM_PAGE_SHIFT - HEAPMEM_MIN_LOG2)
37#define HEAPMEM_MIN_ALIGN (1U << HEAPMEM_MIN_LOG2)
38/* Max size of an extent (4Gb - HEAPMEM_PAGE_SIZE). */
39#define HEAPMEM_MAX_EXTSZ (4294967295U - HEAPMEM_PAGE_SIZE + 1)
40/* Bits we need for encoding a page # */
41#define HEAPMEM_PGENT_BITS (32 - HEAPMEM_PAGE_SHIFT)
42
43/* Each page is represented by a page map entry. */
44#define HEAPMEM_PGMAP_BYTES sizeof(struct heapmem_pgentry)
45
46struct heapmem_pgentry {
47 /* Linkage in bucket list. */
48 unsigned int prev : HEAPMEM_PGENT_BITS;
49 unsigned int next : HEAPMEM_PGENT_BITS;
50 /* page_list or log2. */
51 unsigned int type : 6;
52 /*
53 * We hold either a spatial map of busy blocks within the page
54 * for bucketed memory (up to 32 blocks per page), or the
55 * overall size of the multi-page block if entry.type ==
56 * page_list.
57 */
58 union {
59 uint32_t map;
60 uint32_t bsize;
61 };
62};
63
64/*
65 * A range descriptor is stored at the beginning of the first page of
66 * a range of free pages. heapmem_range.size is nrpages *
67 * HEAPMEM_PAGE_SIZE. Ranges are indexed by address and size in AVL
68 * trees.
69 */
70struct heapmem_range {
71 struct avlh addr_node;
72 struct avlh size_node;
73 size_t size;
74};
75
76struct heapmem_extent {
77 struct pvholder next;
78 void *membase; /* Base of page array */
79 void *memlim; /* Limit of page array */
80 struct avl addr_tree;
81 struct avl size_tree;
82 struct heapmem_pgentry pagemap[0]; /* Start of page entries[] */
83};
84
85struct heap_memory {
86 pthread_mutex_t lock;
87 struct pvlistobj extents;
88 size_t arena_size;
89 size_t usable_size;
90 size_t used_size;
91 /* Heads of page lists for log2-sized blocks. */
92 uint32_t buckets[HEAPMEM_MAX];
93};
94
95#define __HEAPMEM_MAP_SIZE(__nrpages) \
96 ((__nrpages) * HEAPMEM_PGMAP_BYTES)
97
98#define __HEAPMEM_ARENA_SIZE(__size) \
99 (__size + \
100 __align_to(sizeof(struct heapmem_extent) + \
101 __HEAPMEM_MAP_SIZE((__size) >> HEAPMEM_PAGE_SHIFT), \
102 HEAPMEM_MIN_ALIGN))
103
104/*
105 * Calculate the minimal size of the memory arena needed to contain a
106 * heap of __user_size bytes, including our meta data for managing it.
107 * Usable at build time if __user_size is constant.
108 */
109#define HEAPMEM_ARENA_SIZE(__user_size) \
110 __HEAPMEM_ARENA_SIZE(__align_to(__user_size, HEAPMEM_PAGE_SIZE))
111
112#ifdef __cplusplus
113extern "C" {
114#endif
115
116int heapmem_init(struct heap_memory *heap,
117 void *mem, size_t size);
118
119int heapmem_extend(struct heap_memory *heap,
120 void *mem, size_t size);
121
122void heapmem_destroy(struct heap_memory *heap);
123
124void *heapmem_alloc(struct heap_memory *heap,
125 size_t size) __alloc_size(2);
126
127int heapmem_free(struct heap_memory *heap,
128 void *block);
129
130static inline
131size_t heapmem_arena_size(const struct heap_memory *heap)
132{
133 return heap->arena_size;
134}
135
136static inline
137size_t heapmem_usable_size(const struct heap_memory *heap)
138{
139 return heap->usable_size;
140}
141
142static inline
143size_t heapmem_used_size(const struct heap_memory *heap)
144{
145 return heap->used_size;
146}
147
148ssize_t heapmem_check(struct heap_memory *heap,
149 void *block);
150
151#ifdef __cplusplus
152}
153#endif
154
155#endif /* _BOILERPLATE_HEAPMEM_H */