[hackers] [wmii] Reduced the memory consumption of place_client. Replaced strncmps against literal strings with strcmps.

From: Kris Maglione <jg_AT_suckless.org>
Date: Fri Feb 16 02:06:52 2007

changeset: 1886:7b86d01eed8b
tag: tip
user: Kris Maglione <jg_AT_suckless.org>
date: Thu Feb 15 20:02:13 2007 -0500
summary: Reduced the memory consumption of place_client. Replaced strncmps against literal strings with strcmps.

diff -r 371e88690d83 -r 7b86d01eed8b area.c
--- a/area.c Thu Feb 15 16:46:31 2007 -0500
+++ b/area.c Thu Feb 15 20:02:13 2007 -0500
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <math.h>
 
 static void place_client(Area *a, Client *c);
 
@@ -214,10 +215,35 @@ detach_from_area(Frame *f) {
                 assert(a->sel);
 }
 
+void
+bit_twiddle(uint *field, uint width, uint x, uint y, Bool set) {
+ enum { devisor = sizeof(uint) * 8 };
+ uint bx, mask;
+
+ bx = x / devisor;
+ mask = 1 << x % devisor;
+ if(set)
+ field[y*width + bx] |= mask;
+ else
+ field[y*width + bx] &= ~mask;
+}
+
+static Bool
+bit_get(uint *field, uint width, uint x, uint y) {
+ enum { devisor = sizeof(uint) * 8 };
+ uint bx, mask;
+
+ bx = x / devisor;
+ mask = 1 << x % devisor;
+
+ return (field[y*width + bx] & mask) != 0;
+}
+
 static void
 place_client(Area *a, Client *c) {
- static uint mx, my;
- static Bool *field;
+ enum { devisor = sizeof(uint) * 8 };
+ static uint mwidth, mx, my;
+ static uint *field = nil;
         BlitzAlign align;
         XPoint p1 = {0, 0};
         XPoint p2 = {0, 0};
@@ -231,7 +257,6 @@ place_client(Area *a, Client *c) {
         num = 0;
         fit = False;
         align = CENTER;
- field = nil;
 
         f = c->sel;
 
@@ -246,13 +271,12 @@ place_client(Area *a, Client *c) {
         if(!field) {
                 mx = screen->rect.width / 8;
                 my = screen->rect.height / 8;
- field = emallocz(my * mx * sizeof(Bool));
- }
- for(y = 0; y < my; y++)
- for(x = 0; x < mx; x++)
- field[y*mx + x] = True;
- dx = screen->rect.width / mx;
- dy = screen->rect.height / my;
+ mwidth = ceil((float)mx / devisor);
+ field = emallocz(sizeof(uint) * mwidth * my);
+ }
+ memset(field, ~0, (sizeof(uint) * mwidth * my));
+ dx = 8;
+ dy = 8;
         for(fr=a->frame; fr; fr=fr->anext) {
                 if(fr == f) {
                         cx = f->rect.width / dx;
@@ -271,13 +295,18 @@ place_client(Area *a, Client *c) {
                 maxy = r_south(&fr->rect) / dy;
                 for(j = y; j < my && j < maxy; j++)
                         for(i = x; i < mx && i < maxx; i++)
- field[j*mx + i] = False;
+ bit_twiddle(field, mwidth, i, j, False);
+ }
+ for(y = 0; y < my; y++) {
+ for(x = 0; x < mx; x++)
+ fprintf(stderr, "%d", bit_get(field, mwidth, x, y));
+ fprintf(stderr, "\n");
         }
         for(y = 0; y < my; y++)
                 for(x = 0; x < mx; x++) {
- if(field[y*mx + x]) {
- for(i = x; (i < mx) && field[y*mx + i]; i++);
- for(j = y; (j < my) && field[j*mx + x]; j++);
+ if(bit_get(field, mwidth, x, y)) {
+ for(i = x; (i < mx) && bit_get(field, mwidth, i, y); i++);
+ for(j = y; (j < my) && bit_get(field, mwidth, x, j); j++);
                                 if(((i - x) * (j - y) > (p2.x - p1.x) * (p2.y - p1.y))
                                         && (i - x > cx) && (j - y > cy))
                                 {
diff -r 371e88690d83 -r 7b86d01eed8b fs.c
--- a/fs.c Thu Feb 15 16:46:31 2007 -0500
+++ b/fs.c Thu Feb 15 20:02:13 2007 -0500
@@ -233,38 +233,38 @@ message_root(char *message)
                 snprintf(buffer, BUFFER_SIZE, "%s ", message);
                 message = buffer;
         }
- if(!strncmp(message, "quit ", 5))
+ if(!strcmp(message, "quit "))
                 srv.running = 0;
- else if(!strncmp(message, "view ", 5))
+ else if(!strcmp(message, "view "))
                 select_view(&message[5]);
- else if(!strncmp(message, "selcolors ", 10)) {
+ else if(!strcmp(message, "selcolors ")) {
                 fprintf(stderr, "wmiiwm: warning: selcolors have been removed\n");
                 return Ebadcmd;
- }else if(!strncmp(message, "focuscolors ", 10)) {
+ }else if(!strcmp(message, "focuscolors ")) {
                 message += 10;
                 n = strlen(message);
                 return parse_colors(&message, (int *)&n, &def.focuscolor);
         }
- else if(!strncmp(message, "normcolors ", 11)) {
+ else if(!strcmp(message, "normcolors ")) {
                 message += 11;
                 n = strlen(message);
                 return parse_colors(&message, (int *)&n, &def.normcolor);
         }
- else if(!strncmp(message, "font ", 5)) {
+ else if(!strcmp(message, "font ")) {
                 message += 5;
                 free(def.font.fontstr);
                 def.font.fontstr = estrdup(message);
                 loadfont(&blz, &def.font);
                 resize_bar(screen);
         }
- else if(!strncmp(message, "border ", 7)) {
+ else if(!strcmp(message, "border ")) {
                 message += 7;
                 n = (uint)strtol(message, &message, 10);
                 if(*message)
                         return Ebadvalue;
                 def.border = n;
         }
- else if(!strncmp(message, "grabmod ", 8)) {
+ else if(!strcmp(message, "grabmod ")) {
                 message += 8;
                 ulong mod;
                 mod = mod_key_of_str(message);
@@ -375,7 +375,7 @@ lookup_file(FileId *parent, char *name)
                 if(!*dir->name) { /* strlen(dir->name) == 0 */
                         switch(parent->tab.type) {
                         case FsDClients:
- if(!name || !strncmp(name, "sel", 4)) {
+ if(!name || !strcmp(name, "sel")) {
                                         if((c = sel_client())) {
                                                 file = get_file();
                                                 *last = file;
@@ -407,7 +407,7 @@ lookup_file(FileId *parent, char *name)
                                 }
                                 break;
                         case FsDTags:
- if(!name || !strncmp(name, "sel", 4)) {
+ if(!name || !strcmp(name, "sel")) {
                                         if(screen->sel) {
                                                 file = get_file();
                                                 *last = file;
@@ -459,7 +459,7 @@ lookup_file(FileId *parent, char *name)
                         /* Special considerations: */
                         switch(file->tab.type) {
                         case FsDBars:
- if(!strncmp(file->tab.name, "lbar", 5))
+ if(!strcmp(file->tab.name, "lbar"))
                                         file->content.bar_p = &screen[0].lbar;
                                 else
                                         file->content.bar_p = &screen[0].rbar;
@@ -504,7 +504,7 @@ fs_walk(P9Req *r) {
         f = r->fid->aux;
         clone_files(f);
         for(i=0; i < r->ifcall.nwname; i++) {
- if(!strncmp(r->ifcall.wname[i], "..", 3)) {
+ if(!strcmp(r->ifcall.wname[i], "..")) {
                         if(f->next) {
                                 nf=f;
                                 f=f->next;
@@ -515,7 +515,7 @@ fs_walk(P9Req *r) {
                         if(!nf)
                                 break;
                         assert(!nf->next);
- if(strncmp(r->ifcall.wname[i], ".", 2)) {
+ if(strcmp(r->ifcall.wname[i], ".")) {
                                 nf->next = f;
                                 f = nf;
                         }
Received on Fri Feb 16 2007 - 02:06:52 UTC

This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:55:22 UTC