17 #define SHMNAME "/winnie.shm"
19 #define POOL_SIZE 16777216
20 #define BLOCK_SIZE 512
22 #define NUM_BLOCKS (POOL_SIZE / BLOCK_SIZE)
23 #define BITMAP_SIZE (NUM_BLOCKS / 32)
25 static bool is_allocated(int block_number);
26 static int addr_to_block(unsigned char *addr);
27 static unsigned char *block_to_addr(int block_number);
28 static void alloc_blocks(int block_pos, int num_blocks);
29 static void free_blocks(int block_pos, int num_blocks);
31 static void print_stats();
34 static unsigned char *pool;
35 static std::map<int, int> alloc_sizes; //starting block -> number of blocks
37 // 0 means not allocated 1 means allocated
38 static uint32_t bitmap[BITMAP_SIZE];
47 static Statistics stats;
49 bool init_shared_memory()
51 if(((fd = shm_open(SHMNAME, O_RDWR | O_CREAT, S_IRWXU)) == -1)) {
52 fprintf(stderr, "Failed to open shared memory: %s\n", strerror(errno));
55 ftruncate(fd, POOL_SIZE);
57 if((pool = (unsigned char*)mmap(0, POOL_SIZE, PROT_READ | PROT_WRITE,
58 MAP_SHARED, fd, 0)) == (void*)-1) {
59 fprintf(stderr, "Failed to map shared memory: %s\n", strerror(errno));
65 /*if(!(pool = (unsigned char *)malloc(POOL_SIZE))) {
69 for(int i=0; i<BITMAP_SIZE; i++) {
74 memset(&stats, 0, sizeof stats);
79 void destroy_shared_memory()
82 //free(pool); //TODO DELETE it
83 if(munmap(pool, POOL_SIZE) == -1) {
84 fprintf(stderr, "Failed to unmap shared memory: %s\n", strerror(errno));
88 void *sh_malloc(size_t bytes)
94 int num_blocks = (bytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
98 for(int i=0; i<NUM_BLOCKS; i++) {
99 if(!is_allocated(i)) {
109 if(ctr == num_blocks) {
110 alloc_blocks(free_block, num_blocks);
111 return block_to_addr(free_block);
118 void sh_free(void *ptr)
120 int block = addr_to_block((unsigned char*)ptr);
121 std::map<int, int>::iterator it;
122 if((it = alloc_sizes.find(block)) != alloc_sizes.end()) {
123 int num_blocks = it->second;
124 free_blocks(block, num_blocks);
125 alloc_sizes.erase(it);
128 fprintf(stderr, "Attempt to free non-existent blocks from: %d\n", block);
132 static bool is_allocated(int block_number)
134 int idx = block_number / 32;
135 int bit_num = block_number % 32;
137 if((bitmap[idx] >> bit_num) & 1) {
144 static int addr_to_block(unsigned char *addr)
146 assert(addr >= pool);
147 assert(addr < pool + POOL_SIZE);
149 return (addr - pool) / BLOCK_SIZE;
152 static unsigned char *block_to_addr(int block_number)
154 assert(block_number >= 0);
155 assert(block_number < NUM_BLOCKS);
157 return pool + block_number * BLOCK_SIZE;
160 static void alloc_blocks(int block_pos, int num_blocks)
162 for(int i=0; i<num_blocks; i++) {
163 int block_number = i + block_pos;
164 int idx = block_number / 32;
165 int bit_num = block_number % 32;
167 bitmap[idx] |= ((uint32_t)1 << bit_num); // or pow(2, i)
170 alloc_sizes[block_pos] = num_blocks;
173 stats.alloc_memsize += BLOCK_SIZE * num_blocks;
176 static void free_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);
187 stats.free_memsize += BLOCK_SIZE * num_blocks;
190 static void print_stats()
192 printf("Total allocated memory: %d\n", stats.alloc_memsize);
193 printf("Total deallocated memory: %d\n", stats.free_memsize);
194 printf("Number of allocations: %d\n", stats.alloc_num);
195 printf("Number of deallocations: %d\n", stats.free_num);