casting in fbdev
[winnie] / libwinnie / src / shalloc.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33
34 #include <map>
35
36 #include "shalloc.h"
37
38 #define BLOCK_SIZE 512
39
40 #define NUM_BLOCKS (POOL_SIZE / BLOCK_SIZE)
41 #define BITMAP_SIZE (NUM_BLOCKS / 32)
42
43 static bool is_allocated(int block_number);
44 static int addr_to_block(unsigned char *addr);
45 static unsigned char *block_to_addr(int block_number);
46 static void alloc_blocks(int block_pos, int num_blocks);
47 static void free_blocks(int block_pos, int num_blocks);
48
49 static void print_stats();
50 static int fd;
51
52 static unsigned char *pool;
53 static std::map<int, int> alloc_sizes; //starting block -> number of blocks
54
55 // 0 means not allocated 1 means allocated
56 static uint32_t bitmap[BITMAP_SIZE];
57
58 struct Statistics {
59         int alloc_num;
60         int free_num;
61         int alloc_memsize;
62         int free_memsize;
63 };
64
65 static Statistics stats;
66
67 bool init_shared_memory()
68 {
69         if(((fd = shm_open(SHMNAME, O_RDWR | O_CREAT, S_IRWXU)) == -1)) {
70                 fprintf(stderr, "Failed to open shared memory: %s\n", strerror(errno));
71                 return false;
72         }
73         ftruncate(fd, POOL_SIZE);
74
75         if((pool = (unsigned char*)mmap(0, POOL_SIZE, PROT_READ | PROT_WRITE,
76                                         MAP_SHARED, fd, 0)) == (void*)-1) {
77                 fprintf(stderr, "Failed to map shared memory: %s\n", strerror(errno));
78         }
79
80         for(int i=0; i<BITMAP_SIZE; i++) {
81                 bitmap[i] = 0;
82         }
83
84         alloc_sizes.clear();
85         memset(&stats, 0, sizeof stats);
86
87         return true;
88 }
89
90 void destroy_shared_memory()
91 {
92         print_stats();
93         if(munmap(pool, POOL_SIZE) == -1) {
94                 fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno));
95         }
96         shm_unlink(SHMNAME);
97 }
98
99 void *sh_malloc(size_t bytes)
100 {
101         if(!bytes) {
102                 return 0;
103         }
104
105         int num_blocks = (bytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
106         
107         int free_block;
108         int ctr = 0;
109         for(int i=0; i<NUM_BLOCKS; i++) {
110                 if(!is_allocated(i)) {
111                         if(!ctr) {
112                                 free_block = i;
113                         }
114                         ctr++;
115                 }
116                 else {
117                         ctr = 0;
118                 }
119
120                 if(ctr == num_blocks) {
121                         alloc_blocks(free_block, num_blocks);
122                         return block_to_addr(free_block);
123                 }
124         }
125
126         return 0;
127 }
128
129 void sh_free(void *ptr)
130 {
131         int block = addr_to_block((unsigned char*)ptr);
132         std::map<int, int>::iterator it;
133         if((it = alloc_sizes.find(block)) != alloc_sizes.end()) {
134                 int num_blocks = it->second;
135                 free_blocks(block, num_blocks);
136                 alloc_sizes.erase(it);
137         }
138         else {
139                 fprintf(stderr, "Attempt to free non-existent blocks from: %d\n", block);
140         }
141 }
142
143 void *get_pool()
144 {
145         return (void*)pool;
146 }
147
148 static bool is_allocated(int block_number)
149 {
150         int idx = block_number / 32;
151         int bit_num = block_number % 32;
152
153         if((bitmap[idx] >> bit_num) & 1) {
154                 return true;
155         }
156
157         return false;
158 }
159
160 static int addr_to_block(unsigned char *addr)
161 {
162         assert(addr >= pool);
163         assert(addr < pool + POOL_SIZE);
164
165         return (addr - pool) / BLOCK_SIZE;
166 }
167
168 static unsigned char *block_to_addr(int block_number)
169 {
170         assert(block_number >= 0);
171         assert(block_number < NUM_BLOCKS);
172
173         return pool + block_number * BLOCK_SIZE;
174 }
175
176 static void alloc_blocks(int block_pos, int num_blocks)
177 {
178         for(int i=0; i<num_blocks; i++) {
179                 int block_number = i + block_pos;
180                 int idx = block_number / 32;
181                 int bit_num = block_number % 32;
182         
183                 bitmap[idx] |= ((uint32_t)1 << bit_num); // or pow(2, i)
184         }
185
186         alloc_sizes[block_pos] = num_blocks;
187
188         stats.alloc_num++;
189         stats.alloc_memsize += BLOCK_SIZE * num_blocks;
190 }
191
192 static void free_blocks(int block_pos, int num_blocks)
193 {
194         for(int i=0; i<num_blocks; i++) {
195                 int block_number = i + block_pos;
196                 int idx = block_number / 32;
197                 int bit_num = block_number % 32;
198
199                 bitmap[idx] &= ~((uint32_t)1 << bit_num);
200         }
201
202         stats.free_num++;
203         stats.free_memsize += BLOCK_SIZE * num_blocks;
204 }
205
206 static void print_stats()
207 {
208         printf("Total allocated memory: %d\n", stats.alloc_memsize);
209         printf("Total deallocated memory: %d\n", stats.free_memsize);
210         printf("Number of allocations: %d\n", stats.alloc_num);
211         printf("Number of deallocations: %d\n", stats.free_num);
212 }
213