added license GPL
[winnie] / 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 <stdlib.h>
25 #include <stdint.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 SHMNAME "/winnie.shm"
39
40 #define POOL_SIZE 16777216
41 #define BLOCK_SIZE 512
42
43 #define NUM_BLOCKS (POOL_SIZE / BLOCK_SIZE)
44 #define BITMAP_SIZE (NUM_BLOCKS / 32)
45
46 static bool is_allocated(int block_number);
47 static int addr_to_block(unsigned char *addr);
48 static unsigned char *block_to_addr(int block_number);
49 static void alloc_blocks(int block_pos, int num_blocks);
50 static void free_blocks(int block_pos, int num_blocks);
51
52 static void print_stats();
53 static int fd;
54
55 static unsigned char *pool;
56 static std::map<int, int> alloc_sizes; //starting block -> number of blocks
57
58 // 0 means not allocated 1 means allocated
59 static uint32_t bitmap[BITMAP_SIZE];
60
61 struct Statistics {
62         int alloc_num;
63         int free_num;
64         int alloc_memsize;
65         int free_memsize;
66 };
67
68 static Statistics stats;
69
70 bool init_shared_memory()
71 {
72         if(((fd = shm_open(SHMNAME, O_RDWR | O_CREAT, S_IRWXU)) == -1)) {
73                 fprintf(stderr, "Failed to open shared memory: %s\n", strerror(errno));
74                 return false;
75         }
76         ftruncate(fd, POOL_SIZE);
77
78         if((pool = (unsigned char*)mmap(0, POOL_SIZE, PROT_READ | PROT_WRITE,
79                                         MAP_SHARED, fd, 0)) == (void*)-1) {
80                 fprintf(stderr, "Failed to map shared memory: %s\n", strerror(errno));
81         }
82
83         shm_unlink(SHMNAME);
84
85         for(int i=0; i<BITMAP_SIZE; i++) {
86                 bitmap[i] = 0;
87         }
88
89         alloc_sizes.clear();
90         memset(&stats, 0, sizeof stats);
91
92         return true;
93 }
94
95 void destroy_shared_memory()
96 {
97         print_stats();
98         if(munmap(pool, POOL_SIZE) == -1) {
99                 fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno));
100         }
101 }
102
103 void *sh_malloc(size_t bytes)
104 {
105         if(!bytes) {
106                 return 0;
107         }
108
109         int num_blocks = (bytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
110         
111         int free_block;
112         int ctr = 0;
113         for(int i=0; i<NUM_BLOCKS; i++) {
114                 if(!is_allocated(i)) {
115                         if(!ctr) {
116                                 free_block = i;
117                         }
118                         ctr++;
119                 }
120                 else {
121                         ctr = 0;
122                 }
123
124                 if(ctr == num_blocks) {
125                         alloc_blocks(free_block, num_blocks);
126                         return block_to_addr(free_block);
127                 }
128         }
129
130         return 0;
131 }
132
133 void sh_free(void *ptr)
134 {
135         int block = addr_to_block((unsigned char*)ptr);
136         std::map<int, int>::iterator it;
137         if((it = alloc_sizes.find(block)) != alloc_sizes.end()) {
138                 int num_blocks = it->second;
139                 free_blocks(block, num_blocks);
140                 alloc_sizes.erase(it);
141         }
142         else {
143                 fprintf(stderr, "Attempt to free non-existent blocks from: %d\n", block);
144         }
145 }
146
147 static bool is_allocated(int block_number)
148 {
149         int idx = block_number / 32;
150         int bit_num = block_number % 32;
151
152         if((bitmap[idx] >> bit_num) & 1) {
153                 return true;
154         }
155
156         return false;
157 }
158
159 static int addr_to_block(unsigned char *addr)
160 {
161         assert(addr >= pool);
162         assert(addr < pool + POOL_SIZE);
163
164         return (addr - pool) / BLOCK_SIZE;
165 }
166
167 static unsigned char *block_to_addr(int block_number)
168 {
169         assert(block_number >= 0);
170         assert(block_number < NUM_BLOCKS);
171
172         return pool + block_number * BLOCK_SIZE;
173 }
174
175 static void alloc_blocks(int block_pos, int num_blocks)
176 {
177         for(int i=0; i<num_blocks; i++) {
178                 int block_number = i + block_pos;
179                 int idx = block_number / 32;
180                 int bit_num = block_number % 32;
181         
182                 bitmap[idx] |= ((uint32_t)1 << bit_num); // or pow(2, i)
183         }
184
185         alloc_sizes[block_pos] = num_blocks;
186
187         stats.alloc_num++;
188         stats.alloc_memsize += BLOCK_SIZE * num_blocks;
189 }
190
191 static void free_blocks(int block_pos, int num_blocks)
192 {
193         for(int i=0; i<num_blocks; i++) {
194                 int block_number = i + block_pos;
195                 int idx = block_number / 32;
196                 int bit_num = block_number % 32;
197
198                 bitmap[idx] &= ~((uint32_t)1 << bit_num);
199         }
200
201         stats.free_num++;
202         stats.free_memsize += BLOCK_SIZE * num_blocks;
203 }
204
205 static void print_stats()
206 {
207         printf("Total allocated memory: %d\n", stats.alloc_memsize);
208         printf("Total deallocated memory: %d\n", stats.free_memsize);
209         printf("Number of allocations: %d\n", stats.alloc_num);
210         printf("Number of deallocations: %d\n", stats.free_num);
211 }
212