#include <linux/fb.h>
#include "gfx.h"
+#include "shalloc.h"
#define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
+static unsigned char *framebuffer;
+static int dev_fd;
+
struct Graphics {
- unsigned char *framebuffer;
- int dev_fd;
Rect screen_rect;
Rect clipping_rect;
int color_depth;
bool init_gfx()
{
- if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
+ if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
return false;
}
- gfx->dev_fd = -1;
+ dev_fd = -1;
- if((gfx->dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
+ if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
return false;
}
fb_var_screeninfo sinfo;
- if(ioctl(gfx->dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
- close(gfx->dev_fd);
- gfx->dev_fd = -1;
+ if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
+ close(dev_fd);
+ dev_fd = -1;
fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
return false;
}
set_clipping_rect(gfx->screen_rect);
int sz = FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth);
- gfx->framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, gfx->dev_fd, 0);
+ framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
- if(gfx->framebuffer == (void*)-1) {
- close(gfx->dev_fd);
- gfx->dev_fd = -1;
+ if(framebuffer == (void*)-1) {
+ close(dev_fd);
+ dev_fd = -1;
fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
return false;
}
// TODO: uncomment when I find how to use intelfb instead of i915 GRRRR.-
fb_vblank vblank;
- if(ioctl(gfx->dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
+ if(ioctl(dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
// fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno));
}
/*
}
*/
- gfx->pixmap = new Pixmap;
+ if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
+ fprintf(stderr, "Failed to allocate pixmap.\n");
+ return false;
+ }
+
gfx->pixmap->width = gfx->screen_rect.width;
gfx->pixmap->height = gfx->screen_rect.height;
- gfx->pixmap->pixels = gfx->framebuffer;
+
+ int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
+ if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
+ fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
+ return false;
+ }
return true;
}
{
clear_screen(0, 0, 0);
- if(gfx->dev_fd != -1) {
- close(gfx->dev_fd);
+ if(dev_fd != -1) {
+ close(dev_fd);
}
- gfx->dev_fd = -1;
+ dev_fd = -1;
- munmap(gfx->framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth));
- gfx->framebuffer = 0;
+ munmap(framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth));
+ framebuffer = 0;
+ sh_free(gfx->pixmap->pixels);
gfx->pixmap->pixels = 0;
-
- free(gfx);
+ sh_free(gfx->pixmap);
+ sh_free(gfx);
}
unsigned char *get_framebuffer()
{
- return gfx->framebuffer;
+ return gfx->pixmap->pixels;
}
Pixmap *get_framebuffer_pixmap()
fb_cursor curs;
curs.enable = visible ? 1 : 0;
- if(ioctl(gfx->dev_fd, FBIO_CURSOR, &curs) == -1) {
+ if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
}
}
void gfx_update()
{
+ memcpy(framebuffer, gfx->pixmap->pixels, gfx->pixmap->width * gfx->pixmap->height * (gfx->color_depth / 8));
}
void wait_vsync()
{
unsigned long arg = 0;
- if(ioctl(gfx->dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
+ if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
// printf("ioctl error %s\n", strerror(errno));
}
}
#include <unistd.h>
#include "keyboard.h"
+#include "shalloc.h"
#include "window.h"
#include "wm.h"
bool init_keyboard()
{
- if(!(keyboard = (Keyboard*)malloc(sizeof *keyboard))) {
+ if(!(keyboard = (Keyboard*)sh_malloc(sizeof *keyboard))) {
return false;
}
keyboard->dev_fd = -1;
}
- free(keyboard);
+ sh_free(keyboard);
}
int get_keyboard_fd()
#include "geom.h"
#include "gfx.h"
#include "mouse.h"
+#include "shalloc.h"
#include "window.h"
#include "wm.h"
bool init_mouse()
{
- if(!(mouse = (Mouse*)malloc(sizeof *mouse))) {
+ if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
return false;
}
close(mouse->dev_fd);
mouse->dev_fd = -1;
}
- free(mouse);
+ sh_free(mouse);
}
void set_mouse_bounds(const Rect &rect)
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
+
#include "gfx.h"
+#include "shalloc.h"
+
+static SDL_Surface *fbsurf;
struct Graphics {
- SDL_Surface *fbsurf;
Rect screen_rect;
Rect clipping_rect;
int color_depth; // bits per pixel
return false;
}
- if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
+ if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
return false;
}
gfx->screen_rect = scr_rect;
gfx->color_depth = 32;
- if(!(gfx->fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
- fprintf(stderr, "failed to set video mode\n");
+ if(!(fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
+ fprintf(stderr, "Failed to set video mode\n");
return false;
}
SDL_ShowCursor(0);
- gfx->pixmap = new Pixmap;
+ if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
+ fprintf(stderr, "Failed to allocate pixmap.\n");
+ return false;
+ }
+
gfx->pixmap->width = gfx->screen_rect.width;
gfx->pixmap->height = gfx->screen_rect.height;
- gfx->pixmap->pixels = (unsigned char*)gfx->fbsurf->pixels;
+
+ int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
+ if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
+ fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
+ return false;
+ }
set_clipping_rect(gfx->screen_rect);
void destroy_gfx()
{
+ sh_free(gfx->pixmap->pixels);
gfx->pixmap->pixels = 0;
- delete gfx->pixmap;
- free(gfx);
+ sh_free(gfx->pixmap);
+ sh_free(gfx);
SDL_Quit();
}
unsigned char *get_framebuffer()
{
- return (unsigned char*)gfx->fbsurf->pixels;
+ return gfx->pixmap->pixels;
}
Pixmap *get_framebuffer_pixmap()
t.width;
sdl_rect.h = gfx->clipping_rect.height;
- SDL_SetClipRect(gfx->fbsurf, &sdl_rect);
+ SDL_SetClipRect(fbsurf, &sdl_rect);
}
const Rect &get_clipping_rect()
sdl_rect.w = rect.width;
sdl_rect.h = rect.height;
- SDL_FillRect(gfx->fbsurf, &sdl_rect, color);
+ SDL_FillRect(fbsurf, &sdl_rect, color);
}*/
void set_clipping_rect(const Rect &rect)
void gfx_update()
{
- SDL_UpdateRect(gfx->fbsurf, 0, 0, 0, 0);
+ if(SDL_MUSTLOCK(fbsurf)) {
+ SDL_LockSurface(fbsurf);
+ }
+ memcpy(fbsurf->pixels, gfx->pixmap->pixels, gfx->pixmap->width * gfx->pixmap->height * (gfx->color_depth / 8));
+ if(SDL_MUSTLOCK(fbsurf)) {
+ SDL_UnlockSurface(fbsurf);
+ }
+ SDL_UpdateRect(fbsurf, 0, 0, 0, 0);
}
void wait_vsync()
#include <SDL/SDL.h>
#include "mouse.h"
+#include "shalloc.h"
#include "window.h"
#include "wm.h"
bool init_mouse()
{
- if(!(mouse = (Mouse*)malloc(sizeof *mouse))) {
+ if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
return false;
}
return true;
void destroy_mouse()
{
- free(mouse);
+ sh_free(mouse);
}
void set_mouse_bounds(const Rect &rect)
-#include <map>
+#include <assert.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <map>
#include "shalloc.h"
#define POOL_SIZE 16777216
-#define BLOCK_SIZE 1024
+#define BLOCK_SIZE 512
+
+#define NUM_BLOCKS (POOL_SIZE / BLOCK_SIZE)
+#define BITMAP_SIZE (NUM_BLOCKS / 32)
+
+static bool is_allocated(int block_number);
+static int addr_to_block(unsigned char *addr);
+static unsigned char *block_to_addr(int block_number);
+static void alloc_blocks(int block_pos, int num_blocks);
+static void free_blocks(int block_pos, int num_blocks);
+
+static void print_stats();
static unsigned char *pool;
-static std::map<unsigned char*, size_t> alloc_sizes;
-//address size
+static std::map<int, int> alloc_sizes; //starting block -> number of blocks
+
+// 0 means not allocated 1 means allocated
+static uint32_t bitmap[BITMAP_SIZE];
+
+struct Statistics {
+ int alloc_num;
+ int free_num;
+ int alloc_memsize;
+ int free_memsize;
+};
+
+static Statistics stats;
bool init_shared_memory()
{
if(!(pool = (unsigned char *)malloc(POOL_SIZE))) {
return false;
}
+
+ for(int i=0; i<BITMAP_SIZE; i++) {
+ bitmap[i] = 0;
+ }
+
+ alloc_sizes.clear();
+ memset(&stats, 0, sizeof stats);
+
+ return true;
}
void destroy_shared_memory()
{
- //TODO print statistics
+ print_stats();
free(pool);
}
-// an zitaei 45 mb 8a vriskw posa blocks kai meta
-// 8a psaxnw tosa sunexomena bits sto bitmap
-// an den exei return 0
-// pool[arxidi]
+void *sh_malloc(size_t bytes)
+{
+ if(!bytes) {
+ return 0;
+ }
+
+ int num_blocks = (bytes + BLOCK_SIZE - 1) / BLOCK_SIZE;
+
+ int free_block;
+ int ctr = 0;
+ for(int i=0; i<NUM_BLOCKS; i++) {
+ if(!is_allocated(i)) {
+ if(!ctr) {
+ free_block = i;
+ }
+ ctr++;
+ }
+ else {
+ ctr = 0;
+ }
+
+ if(ctr == num_blocks) {
+ alloc_blocks(free_block, num_blocks);
+ return block_to_addr(free_block);
+ }
+ }
+
+ return 0;
+}
+
+void sh_free(void *ptr)
+{
+ int block = addr_to_block((unsigned char*)ptr);
+ std::map<int, int>::iterator it;
+ if((it = alloc_sizes.find(block)) != alloc_sizes.end()) {
+ int num_blocks = it->second;
+ free_blocks(block, num_blocks);
+ alloc_sizes.erase(it);
+ }
+ else {
+ fprintf(stderr, "Attempt to free non-existent blocks from: %d\n", block);
+ }
+}
+
+static bool is_allocated(int block_number)
+{
+ int idx = block_number / 32;
+ int bit_num = block_number % 32;
+
+ if((bitmap[idx] >> bit_num) & 1) {
+ return true;
+ }
+
+ return false;
+}
+
+static int addr_to_block(unsigned char *addr)
+{
+ assert(addr >= pool);
+ assert(addr < pool + POOL_SIZE);
+
+ return (addr - pool) / BLOCK_SIZE;
+}
+
+static unsigned char *block_to_addr(int block_number)
+{
+ assert(block_number >= 0);
+ assert(block_number < NUM_BLOCKS);
+
+ return pool + block_number * BLOCK_SIZE;
+}
+
+static void alloc_blocks(int block_pos, int num_blocks)
+{
+ for(int i=0; i<num_blocks; i++) {
+ int block_number = i + block_pos;
+ int idx = block_number / 32;
+ int bit_num = block_number % 32;
+
+ bitmap[idx] |= ((uint32_t)1 << bit_num); // or pow(2, i)
+ }
+
+ alloc_sizes[block_pos] = num_blocks;
+
+ stats.alloc_num++;
+ stats.alloc_memsize += BLOCK_SIZE * num_blocks;
+}
+
+static void free_blocks(int block_pos, int num_blocks)
+{
+ for(int i=0; i<num_blocks; i++) {
+ int block_number = i + block_pos;
+ int idx = block_number / 32;
+ int bit_num = block_number % 32;
+
+ bitmap[idx] &= ~((uint32_t)1 << bit_num);
+ }
+
+ stats.free_num++;
+ stats.free_memsize += BLOCK_SIZE * num_blocks;
+}
+
+static void print_stats()
+{
+ printf("Total allocated memory: %d\n", stats.alloc_memsize);
+ printf("Total deallocated memory: %d\n", stats.free_memsize);
+ printf("Number of allocations: %d\n", stats.alloc_num);
+ printf("Number of deallocations: %d\n", stats.free_num);
+}
+
#ifndef SHALLOC_H_
#define SHALLOC_H_
+#include <cstring>
+
bool init_shared_memory();
void destroy_shared_memory();
-void *shmalloc(size_t bytes);
-void shmfree(void *ptr);
+void *sh_malloc(size_t bytes);
+void sh_free(void *ptr);
#endif // SHALLOC_H_
#include <freetype/freetype.h>
#include "gfx.h"
+#include "shalloc.h"
#include "text.h"
#define DPI 72
bool init_text()
{
- if(!(text = (Text*)malloc(sizeof *text))) {
+ if(!(text = (Text*)sh_malloc(sizeof *text))) {
return false;
}
return true;
}
+void destroy_text()
+{
+ sh_free(text);
+}
+
void draw_text(const char *txt, Pixmap *pixmap)
{
if(!pixmap) {
#define TEXT_H_
bool init_text();
+void destroy_text();
void draw_text(const char *txt, Pixmap *pixmap = 0);
void set_text_position(int x, int y);
-#include "winnie.h"
#include "keyboard.h"
#include "mouse.h"
+#include "shalloc.h"
+#include "winnie.h"
bool winnie_init()
{
+ if(!init_shared_memory()) {
+ return false;
+ }
+
if(!init_gfx()) {
return false;
}
destroy_gfx();
destroy_keyboard();
destroy_mouse();
+ destroy_text();
+
+ destroy_shared_memory();
}