version 1.0
[platform/core/system/tizen-platform-wrapper.git] / src / heap.c
1 /*
2  * Copyright (C) 2013 Intel Corporation.
3  * 
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.
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  * Library General Public License for more details.
13  *
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
17  *
18  * Authors:
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>
22  *
23  */
24 #define _GNU_SOURCE
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <unistd.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <sys/mman.h>
34
35 #include "heap.h"
36
37 /* align to a size_t size */
38 inline static size_t align(size_t size)
39 {
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);
43 }
44
45 /* align to a page size */
46 inline static size_t pagealign( size_t size)
47 {
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;
52 }
53
54 int heap_create( struct heap *heap, size_t capacity)
55 {
56     char *data;
57
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);
62
63     /* success of the allocation? */
64     if (data == MAP_FAILED)
65         return -1;
66
67     /* yes. initialisae the heap */
68     heap->data = data;
69     heap->capacity = capacity;
70     heap->size = 0;
71     return 0;
72 }
73
74 int heap_destroy( struct heap *heap)
75 {
76     return munmap( heap->data, heap->capacity);
77 }
78
79 int heap_resize( struct heap *heap, size_t size)
80 {
81     /* has heap enought data? */
82     if (size > heap->capacity) {
83         
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);
88
89         /* error if failure */
90         if (data == MAP_FAILED)
91             return -1;
92
93         /* record new parameters. */
94         heap->data = data;
95         heap->capacity = capa;
96     }
97     heap->size = size;
98
99     return 0;
100 }
101
102 size_t heap_alloc( struct heap *heap, size_t count)
103 {
104     size_t result = heap->size;
105     if( heap_resize( heap, align( result + count)) != 0)
106         result = HNULL;
107     return result;
108 }
109
110 size_t heap_strndup( struct heap *heap, const char *value, size_t length)
111 {
112     size_t offset;
113     char *string;
114
115     offset = heap_alloc( heap, length+1);
116     if (offset != HNULL) {
117         string = heap_address( heap, offset);
118         memcpy( string, value, length);
119         string[length] = 0;
120     }
121     return offset;
122 }
123
124 size_t heap_strdup( struct heap *heap, const char *string)
125 {
126     return heap_strndup( heap, string, strlen(string));
127 }
128
129 int heap_read_write( struct heap *heap)
130 {
131     return mprotect(heap->data, heap->capacity, PROT_READ|PROT_WRITE);
132 }
133
134 int heap_read_only( struct heap *heap)
135 {
136     return mprotect(heap->data, heap->capacity, PROT_READ);
137 }
138
139
140