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));
80 for(int i=0; i<BITMAP_SIZE; i++) {
85 memset(&stats, 0, sizeof stats);
90 void destroy_shared_memory()
93 if(munmap(pool, POOL_SIZE) == -1) {
94 fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno));
99 void *sh_malloc(size_t bytes)
105 int num_blocks = (bytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
109 for(int i=0; i<NUM_BLOCKS; i++) {
110 if(!is_allocated(i)) {
120 if(ctr == num_blocks) {
121 alloc_blocks(free_block, num_blocks);
122 return block_to_addr(free_block);
129 void sh_free(void *ptr)
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);
139 fprintf(stderr, "Attempt to free non-existent blocks from: %d\n", block);
148 static bool is_allocated(int block_number)
150 int idx = block_number / 32;
151 int bit_num = block_number % 32;
153 if((bitmap[idx] >> bit_num) & 1) {
160 static int addr_to_block(unsigned char *addr)
162 assert(addr >= pool);
163 assert(addr < pool + POOL_SIZE);
165 return (addr - pool) / BLOCK_SIZE;
168 static unsigned char *block_to_addr(int block_number)
170 assert(block_number >= 0);
171 assert(block_number < NUM_BLOCKS);
173 return pool + block_number * BLOCK_SIZE;
176 static void alloc_blocks(int block_pos, int num_blocks)
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;
183 bitmap[idx] |= ((uint32_t)1 << bit_num); // or pow(2, i)
186 alloc_sizes[block_pos] = num_blocks;
189 stats.alloc_memsize += BLOCK_SIZE * num_blocks;
192 static void free_blocks(int block_pos, int num_blocks)
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;
199 bitmap[idx] &= ~((uint32_t)1 << bit_num);
203 stats.free_memsize += BLOCK_SIZE * num_blocks;
206 static void print_stats()
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);