Fix coding rule violation
[platform/core/system/tizen-platform-config.git] / src / heap.c
1 /*
2  * Copyright (C) 2013-2014 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 static inline 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 static inline size_t pagealign(size_t size)
47 {
48         static size_t pagemask = 0;
49         /* we assume that pagesize is a power of 2 */
50         if (!pagemask) {
51                 pagemask = (size_t)sysconf(_SC_PAGE_SIZE) - 1;
52                 assert(pagemask);
53                 assert((pagemask & (pagemask+1)) == 0);
54         }
55         return (size + pagemask) & ~pagemask;
56 }
57
58 int heap_create(struct heap *heap, size_t capacity)
59 {
60         char *data;
61
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);
66
67         /* success of the allocation? */
68         if (data == MAP_FAILED)
69                 return -1;
70
71         /* yes. initialisae the heap */
72         heap->data = data;
73         heap->capacity = capacity;
74         heap->size = 0;
75         return 0;
76 }
77
78 int heap_destroy(struct heap *heap)
79 {
80         return munmap(heap->data, heap->capacity);
81 }
82
83 int heap_resize(struct heap *heap, size_t size)
84 {
85         /* has heap enought data? */
86         if (size > heap->capacity) {
87
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);
92
93                 /* error if failure */
94                 if (data == MAP_FAILED)
95                         return -1;
96
97                 /* record new parameters. */
98                 heap->data = data;
99                 heap->capacity = capa;
100         }
101         heap->size = size;
102
103         return 0;
104 }
105
106 size_t heap_alloc(struct heap *heap, size_t count)
107 {
108         size_t result = heap->size;
109         if (heap_resize(heap, align(result + count)) != 0)
110                 result = HNULL;
111         return result;
112 }
113
114 size_t heap_strndup(struct heap *heap, const char *value, size_t length)
115 {
116         size_t offset;
117         char *string;
118
119         offset = heap_alloc(heap, length+1);
120         if (offset != HNULL) {
121                 string = heap_address(heap, offset);
122                 memcpy(string, value, length);
123                 string[length] = 0;
124         }
125         return offset;
126 }
127
128 size_t heap_strdup(struct heap *heap, const char *string)
129 {
130         return heap_strndup(heap, string, strlen(string));
131 }
132
133 int heap_read_write(struct heap *heap)
134 {
135         return mprotect(heap->data, heap->capacity, PROT_READ|PROT_WRITE);
136 }
137
138 int heap_read_only(struct heap *heap)
139 {
140         return mprotect(heap->data, heap->capacity, PROT_READ);
141 }
142