summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEleni Maria Stea <elene.mst@gmail.com>2013-03-10 03:29:03 +0200
committerEleni Maria Stea <elene.mst@gmail.com>2013-03-10 03:29:03 +0200
commite4076629a736e957739c31cd20bbbbb70327023b (patch)
tree431d5f8333f4fe93b227bd2151450ab08998a47c
parent12274901ac01898b01b93e15b5b87dec3348afae (diff)
added memory allocator
double buffer :-) TODO: allocate pool from shared memory
-rw-r--r--src/fbdev/gfx.cc63
-rw-r--r--src/fbdev/keyboard.cc5
-rw-r--r--src/fbdev/mouse.cc5
-rw-r--r--src/sdl/gfx.cc44
-rw-r--r--src/sdl/mouse.cc5
-rw-r--r--src/shalloc.cc160
-rw-r--r--src/shalloc.h6
-rw-r--r--src/text.cc8
-rw-r--r--src/text.h1
-rw-r--r--src/winnie.cc10
10 files changed, 251 insertions, 56 deletions
diff --git a/src/fbdev/gfx.cc b/src/fbdev/gfx.cc
index 9984422..7ebc656 100644
--- a/src/fbdev/gfx.cc
+++ b/src/fbdev/gfx.cc
@@ -14,12 +14,14 @@
#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;
@@ -30,21 +32,21 @@ static Graphics *gfx;
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;
}
@@ -60,18 +62,18 @@ bool init_gfx()
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));
}
/*
@@ -82,10 +84,19 @@ bool init_gfx()
}
*/
- 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;
}
@@ -94,23 +105,24 @@ void destroy_gfx()
{
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()
@@ -143,19 +155,20 @@ void set_cursor_visibility(bool visible)
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));
}
}
diff --git a/src/fbdev/keyboard.cc b/src/fbdev/keyboard.cc
index 625647f..c1589be 100644
--- a/src/fbdev/keyboard.cc
+++ b/src/fbdev/keyboard.cc
@@ -10,6 +10,7 @@
#include <unistd.h>
#include "keyboard.h"
+#include "shalloc.h"
#include "window.h"
#include "wm.h"
@@ -22,7 +23,7 @@ static Keyboard *keyboard;
bool init_keyboard()
{
- if(!(keyboard = (Keyboard*)malloc(sizeof *keyboard))) {
+ if(!(keyboard = (Keyboard*)sh_malloc(sizeof *keyboard))) {
return false;
}
@@ -80,7 +81,7 @@ void destroy_keyboard()
keyboard->dev_fd = -1;
}
- free(keyboard);
+ sh_free(keyboard);
}
int get_keyboard_fd()
diff --git a/src/fbdev/mouse.cc b/src/fbdev/mouse.cc
index 803e8e0..dae9b98 100644
--- a/src/fbdev/mouse.cc
+++ b/src/fbdev/mouse.cc
@@ -13,6 +13,7 @@
#include "geom.h"
#include "gfx.h"
#include "mouse.h"
+#include "shalloc.h"
#include "window.h"
#include "wm.h"
@@ -34,7 +35,7 @@ static Mouse *mouse;
bool init_mouse()
{
- if(!(mouse = (Mouse*)malloc(sizeof *mouse))) {
+ if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
return false;
}
@@ -55,7 +56,7 @@ void destroy_mouse()
close(mouse->dev_fd);
mouse->dev_fd = -1;
}
- free(mouse);
+ sh_free(mouse);
}
void set_mouse_bounds(const Rect &rect)
diff --git a/src/sdl/gfx.cc b/src/sdl/gfx.cc
index 9b708e0..cd1a4a6 100644
--- a/src/sdl/gfx.cc
+++ b/src/sdl/gfx.cc
@@ -2,10 +2,13 @@
#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
@@ -21,7 +24,7 @@ bool init_gfx()
return false;
}
- if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
+ if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
return false;
}
@@ -29,16 +32,25 @@ bool init_gfx()
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);
@@ -47,15 +59,16 @@ bool init_gfx()
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()
@@ -116,7 +129,7 @@ int get_color_depth()
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()
@@ -139,7 +152,7 @@ void fill_rect(const Rect &rect, int r, int g, int b)
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)
@@ -159,7 +172,14 @@ void set_cursor_visibility(bool visible)
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()
diff --git a/src/sdl/mouse.cc b/src/sdl/mouse.cc
index 224bc47..546f3bf 100644
--- a/src/sdl/mouse.cc
+++ b/src/sdl/mouse.cc
@@ -2,6 +2,7 @@
#include <SDL/SDL.h>
#include "mouse.h"
+#include "shalloc.h"
#include "window.h"
#include "wm.h"
@@ -17,7 +18,7 @@ static Mouse *mouse;
bool init_mouse()
{
- if(!(mouse = (Mouse*)malloc(sizeof *mouse))) {
+ if(!(mouse = (Mouse*)sh_malloc(sizeof *mouse))) {
return false;
}
return true;
@@ -25,7 +26,7 @@ bool init_mouse()
void destroy_mouse()
{
- free(mouse);
+ sh_free(mouse);
}
void set_mouse_bounds(const Rect &rect)
diff --git a/src/shalloc.cc b/src/shalloc.cc
index d8f97dc..b5c155f 100644
--- a/src/shalloc.cc
+++ b/src/shalloc.cc
@@ -1,29 +1,171 @@
-#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);
+}
+
diff --git a/src/shalloc.h b/src/shalloc.h
index ce9d184..2236b27 100644
--- a/src/shalloc.h
+++ b/src/shalloc.h
@@ -1,10 +1,12 @@
#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_
diff --git a/src/text.cc b/src/text.cc
index 4068663..c58924f 100644
--- a/src/text.cc
+++ b/src/text.cc
@@ -2,6 +2,7 @@
#include <freetype/freetype.h>
#include "gfx.h"
+#include "shalloc.h"
#include "text.h"
#define DPI 72
@@ -21,7 +22,7 @@ static Text *text;
bool init_text()
{
- if(!(text = (Text*)malloc(sizeof *text))) {
+ if(!(text = (Text*)sh_malloc(sizeof *text))) {
return false;
}
@@ -45,6 +46,11 @@ bool init_text()
return true;
}
+void destroy_text()
+{
+ sh_free(text);
+}
+
void draw_text(const char *txt, Pixmap *pixmap)
{
if(!pixmap) {
diff --git a/src/text.h b/src/text.h
index 55869e0..a6a1db5 100644
--- a/src/text.h
+++ b/src/text.h
@@ -2,6 +2,7 @@
#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);
diff --git a/src/winnie.cc b/src/winnie.cc
index 0f5b10e..a2c61ee 100644
--- a/src/winnie.cc
+++ b/src/winnie.cc
@@ -1,9 +1,14 @@
-#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;
}
@@ -33,4 +38,7 @@ void winnie_shutdown()
destroy_gfx();
destroy_keyboard();
destroy_mouse();
+ destroy_text();
+
+ destroy_shared_memory();
}