2 * Copyright (C) 2013 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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.
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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
37 /* align to a size_t size */
38 inline static size_t align(size_t size)
40 /* we assume that sizeof(size_t) is a power of 2 */
41 assert( (sizeof(size_t) & (sizeof(size_t)-1)) == 0 );
42 return (size + (sizeof(size_t)-1)) & ~(sizeof(size_t)-1);
45 /* align to a page size */
46 inline static size_t pagealign( size_t size)
48 static size_t pagemask = 0;
49 /* we assume that pagesize is a power of 2 */
51 pagemask = (size_t)sysconf(_SC_PAGE_SIZE) - 1;
53 assert( (pagemask & (pagemask+1)) == 0 );
55 return (size + pagemask) & ~pagemask;
58 int heap_create( struct heap *heap, size_t capacity)
62 /* allocation of the heap */
63 capacity = pagealign(capacity ? capacity : 1);
64 data = mmap(NULL, capacity, PROT_READ|PROT_WRITE,
65 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
67 /* success of the allocation? */
68 if (data == MAP_FAILED)
71 /* yes. initialisae the heap */
73 heap->capacity = capacity;
78 int heap_destroy( struct heap *heap)
80 return munmap( heap->data, heap->capacity);
83 int heap_resize( struct heap *heap, size_t size)
85 /* has heap enought data? */
86 if (size > heap->capacity) {
88 /* no. resizing of the heap. */
89 /* compute the sizes and realloc */
90 size_t capa = pagealign(size);
91 char *data = mremap(heap->data, heap->capacity, capa, MREMAP_MAYMOVE);
93 /* error if failure */
94 if (data == MAP_FAILED)
97 /* record new parameters. */
99 heap->capacity = capa;
106 size_t heap_alloc( struct heap *heap, size_t count)
108 size_t result = heap->size;
109 if( heap_resize( heap, align( result + count)) != 0)
114 size_t heap_strndup( struct heap *heap, const char *value, size_t length)
119 offset = heap_alloc( heap, length+1);
120 if (offset != HNULL) {
121 string = heap_address( heap, offset);
122 memcpy( string, value, length);
128 size_t heap_strdup( struct heap *heap, const char *string)
130 return heap_strndup( heap, string, strlen(string));
133 int heap_read_write( struct heap *heap)
135 return mprotect(heap->data, heap->capacity, PROT_READ|PROT_WRITE);
138 int heap_read_only( struct heap *heap)
140 return mprotect(heap->data, heap->capacity, PROT_READ);