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 BLOCK_SIZE 512
40 #define NUM_BLOCKS (POOL_SIZE / BLOCK_SIZE)
41 #define BITMAP_SIZE (NUM_BLOCKS / 32)
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);
49 static void print_stats();
52 static unsigned char *pool;
53 static std::map<int, int> alloc_sizes; //starting block -> number of blocks
55 // 0 means not allocated 1 means allocated
56 static uint32_t bitmap[BITMAP_SIZE];
65 static Statistics stats;
67 bool init_shared_memory()
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));
73 ftruncate(fd, POOL_SIZE);
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));
82 for(int i=0; i<BITMAP_SIZE; i++) {
87 memset(&stats, 0, sizeof stats);
92 void destroy_shared_memory()
95 if(munmap(pool, POOL_SIZE) == -1) {
96 fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno));
100 void *sh_malloc(size_t bytes)
106 int num_blocks = (bytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
110 for(int i=0; i<NUM_BLOCKS; i++) {
111 if(!is_allocated(i)) {
121 if(ctr == num_blocks) {
122 alloc_blocks(free_block, num_blocks);
123 return block_to_addr(free_block);
130 void sh_free(void *ptr)
132 int block = addr_to_block((unsigned char*)ptr);
133 std::map<int, int>::iterator it;
134 if((it = alloc_sizes.find(block)) != alloc_sizes.end()) {
135 int num_blocks = it->second;
136 free_blocks(block, num_blocks);
137 alloc_sizes.erase(it);
140 fprintf(stderr, "Attempt to free non-existent blocks from: %d\n", block);
149 static bool is_allocated(int block_number)
151 int idx = block_number / 32;
152 int bit_num = block_number % 32;
154 if((bitmap[idx] >> bit_num) & 1) {
161 static int addr_to_block(unsigned char *addr)
163 assert(addr >= pool);
164 assert(addr < pool + POOL_SIZE);
166 return (addr - pool) / BLOCK_SIZE;
169 static unsigned char *block_to_addr(int block_number)
171 assert(block_number >= 0);
172 assert(block_number < NUM_BLOCKS);
174 return pool + block_number * BLOCK_SIZE;
177 static void alloc_blocks(int block_pos, int num_blocks)
179 for(int i=0; i<num_blocks; i++) {
180 int block_number = i + block_pos;
181 int idx = block_number / 32;
182 int bit_num = block_number % 32;
184 bitmap[idx] |= ((uint32_t)1 << bit_num); // or pow(2, i)
187 alloc_sizes[block_pos] = num_blocks;
190 stats.alloc_memsize += BLOCK_SIZE * num_blocks;
193 static void free_blocks(int block_pos, int num_blocks)
195 for(int i=0; i<num_blocks; i++) {
196 int block_number = i + block_pos;
197 int idx = block_number / 32;
198 int bit_num = block_number % 32;
200 bitmap[idx] &= ~((uint32_t)1 << bit_num);
204 stats.free_memsize += BLOCK_SIZE * num_blocks;
207 static void print_stats()
209 printf("Total allocated memory: %d\n", stats.alloc_memsize);
210 printf("Total deallocated memory: %d\n", stats.free_memsize);
211 printf("Number of allocations: %d\n", stats.alloc_num);
212 printf("Number of deallocations: %d\n", stats.free_num);