2 winnie - an experimental window system
4 Copyright (C) 2013 Eleni Maria Stea
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.
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.
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/>.
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
38 #define SHMNAME "/winnie.shm"
40 #define POOL_SIZE 16777216
41 #define BLOCK_SIZE 512
43 #define NUM_BLOCKS (POOL_SIZE / BLOCK_SIZE)
44 #define BITMAP_SIZE (NUM_BLOCKS / 32)
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);
52 static void print_stats();
55 static unsigned char *pool;
56 static std::map<int, int> alloc_sizes; //starting block -> number of blocks
58 // 0 means not allocated 1 means allocated
59 static uint32_t bitmap[BITMAP_SIZE];
68 static Statistics stats;
70 bool init_shared_memory()
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));
76 ftruncate(fd, POOL_SIZE);
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));
85 for(int i=0; i<BITMAP_SIZE; i++) {
90 memset(&stats, 0, sizeof stats);
95 void destroy_shared_memory()
98 if(munmap(pool, POOL_SIZE) == -1) {
99 fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno));
103 void *sh_malloc(size_t bytes)
109 int num_blocks = (bytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
113 for(int i=0; i<NUM_BLOCKS; i++) {
114 if(!is_allocated(i)) {
124 if(ctr == num_blocks) {
125 alloc_blocks(free_block, num_blocks);
126 return block_to_addr(free_block);
133 void sh_free(void *ptr)
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);
143 fprintf(stderr, "Attempt to free non-existent blocks from: %d\n", block);
147 static bool is_allocated(int block_number)
149 int idx = block_number / 32;
150 int bit_num = block_number % 32;
152 if((bitmap[idx] >> bit_num) & 1) {
159 static int addr_to_block(unsigned char *addr)
161 assert(addr >= pool);
162 assert(addr < pool + POOL_SIZE);
164 return (addr - pool) / BLOCK_SIZE;
167 static unsigned char *block_to_addr(int block_number)
169 assert(block_number >= 0);
170 assert(block_number < NUM_BLOCKS);
172 return pool + block_number * BLOCK_SIZE;
175 static void alloc_blocks(int block_pos, int num_blocks)
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;
182 bitmap[idx] |= ((uint32_t)1 << bit_num); // or pow(2, i)
185 alloc_sizes[block_pos] = num_blocks;
188 stats.alloc_memsize += BLOCK_SIZE * num_blocks;
191 static void free_blocks(int block_pos, int num_blocks)
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;
198 bitmap[idx] &= ~((uint32_t)1 << bit_num);
202 stats.free_memsize += BLOCK_SIZE * num_blocks;
205 static void print_stats()
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);