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 /* we assume that pagesize is a power of 2 */
49 size_t pagemask = (size_t)sysconf(_SC_PAGE_SIZE) - 1;
50 assert( (pagemask & (pagemask+1)) == 0 );
51 return (size + pagemask) & ~pagemask;
54 int heap_create( struct heap *heap, size_t capacity)
58 /* allocation of the heap */
59 capacity = pagealign(capacity ? capacity : 1);
60 data = mmap(NULL, capacity, PROT_READ|PROT_WRITE,
61 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
63 /* success of the allocation? */
64 if (data == MAP_FAILED)
67 /* yes. initialisae the heap */
69 heap->capacity = capacity;
74 int heap_destroy( struct heap *heap)
76 return munmap( heap->data, heap->capacity);
79 int heap_resize( struct heap *heap, size_t size)
81 /* has heap enought data? */
82 if (size > heap->capacity) {
84 /* no. resizing of the heap. */
85 /* compute the sizes and realloc */
86 size_t capa = pagealign(size);
87 char *data = mremap(heap->data, heap->capacity, capa, MREMAP_MAYMOVE);
89 /* error if failure */
90 if (data == MAP_FAILED)
93 /* record new parameters. */
95 heap->capacity = capa;
102 size_t heap_alloc( struct heap *heap, size_t count)
104 size_t result = heap->size;
105 if( heap_resize( heap, align( result + count)) != 0)
110 size_t heap_strndup( struct heap *heap, const char *value, size_t length)
115 offset = heap_alloc( heap, length+1);
116 if (offset != HNULL) {
117 string = heap_address( heap, offset);
118 memcpy( string, value, length);
124 size_t heap_strdup( struct heap *heap, const char *string)
126 return heap_strndup( heap, string, strlen(string));
129 int heap_read_write( struct heap *heap)
131 return mprotect(heap->data, heap->capacity, PROT_READ|PROT_WRITE);
134 int heap_read_only( struct heap *heap)
136 return mprotect(heap->data, heap->capacity, PROT_READ);