Skip to content

Commit

Permalink
New class/message-dispatch system. Pool allocator for Line objects.
Browse files Browse the repository at this point in the history
These brought some memory improvements.
  • Loading branch information
hishamhm committed May 30, 2006
1 parent f802cd3 commit bc46364
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 148 deletions.
19 changes: 10 additions & 9 deletions Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command) {

this->readOnly = (access(fileName, R_OK) == 0 && access(fileName, W_OK) != 0);

this->panel = Panel_new(x, y, w-1, h, CRT_colors[NormalColor], LINE_CLASS, true);
this->undo = Undo_new(this->panel->items);
Panel* p = Panel_new(x, y, w-1, h, CRT_colors[NormalColor], ClassAs(Line, ListItem), true, this);
this->panel = p;
this->undo = Undo_new(p->items);
this->fileName = String_copy(fileName);

FileReader* file = FileReader_new(fileName, command);
Expand All @@ -109,20 +110,20 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command) {
char* text = FileReader_readLine(file, &len);
this->hl = Highlight_new(fileName, text);
if (strchr(text, '\t')) this->tabCharacters = true;
Line* line = Line_new(text, len, this);
Panel_set(this->panel, i, (ListItem*) line);
Line* line = Line_new(p->items, text, len, this->hl->mainContext);
Panel_set(p, i, (ListItem*) line);
while (!FileReader_eof(file)) {
i++;
char* text = FileReader_readLine(file, &len);
if (text) {
if (!this->tabCharacters && strchr(text, '\t')) this->tabCharacters = true;
Line* line = Line_new(text, len, this);
Panel_set(this->panel, i, (ListItem*) line);
Line* line = Line_new(p->items, text, len, this->hl->mainContext);
Panel_set(p, i, (ListItem*) line);
}
}
} else {
this->hl = Highlight_new(fileName, "");
Panel_set(this->panel, 0, (ListItem*) Line_new(String_copy(""), 0, this));
Panel_set(p, 0, (ListItem*) Line_new(p->items, String_copy(""), 0, this->hl->mainContext));
}
if (file)
FileReader_delete(file);
Expand Down Expand Up @@ -271,7 +272,7 @@ void Buffer_draw(Buffer* this) {
Buffer_highlightBracket(this);

p->cursorX = screenX - p->scrollH;
Panel_draw(p, true);
Panel_draw(p);

this->wasSelecting = this->selecting;
}
Expand Down Expand Up @@ -376,7 +377,7 @@ inline void Buffer_highlightBracket(Buffer* this) {
void Buffer_delete(Buffer* this) {
Buffer_storePosition(this);
free(this->fileName);
((Object*) this->panel)->delete((Object*) this->panel);
Msg0(Object, delete, this->panel);

Undo_delete(this->undo);
Highlight_delete(this->hl);
Expand Down
30 changes: 21 additions & 9 deletions Field.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>

#include "Prototypes.h"
//#needs Object
//#needs List

/*{
Expand All @@ -20,22 +21,35 @@ struct Field_ {
int cursor;
};
struct FieldItemClass_ {
ListItemClass super;
};
struct FieldItem_ {
ListItem super;
char* text;
int len;
int w;
};
extern char* FIELDITEM_CLASS;
extern FieldItemClass FieldItemType;
}*/

#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif

char* FIELDITEM_CLASS = "FieldItem";
FieldItemClass FieldItemType = {
.super = {
.super = {
.size = sizeof(FieldItem),
.display = NULL,
.equals = Object_equals,
.delete = FieldItem_delete
}
}
};

Field* Field_new(char* label, int x, int y, int w) {
Field* this = (Field*) malloc(sizeof(Field));
Expand All @@ -45,7 +59,7 @@ Field* Field_new(char* label, int x, int y, int w) {
this->labelColor = CRT_colors[StatusColor];
this->fieldColor = CRT_colors[FieldColor];
this->cursor = 0;
this->history = List_new(FIELDITEM_CLASS);
this->history = List_new( ClassAs(FieldItem, ListItem), NULL );
this->current = NULL;
this->label = malloc(w + 1);
this->labelLen = strlen(label);
Expand All @@ -60,10 +74,9 @@ void Field_delete(Field* this) {
free(this);
}

FieldItem* FieldItem_new(int w) {
FieldItem* this = (FieldItem*) malloc(sizeof(FieldItem));
((Object*)this)->class = FIELDITEM_CLASS;
((Object*)this)->delete = FieldItem_delete;
FieldItem* FieldItem_new(List* list, int w) {
FieldItem* this = Pool_allocate(list->pool);
Bless(FieldItem);
this->text = calloc(w, 1);
this->len = 0;
this->w = w;
Expand All @@ -81,13 +94,12 @@ void Field_printfLabel(Field* this, char* picture, ...) {
void FieldItem_delete(Object* cast) {
FieldItem* this = (FieldItem*) cast;
free(this->text);
free(this);
}

void Field_start(Field* this) {
this->current = (FieldItem*) List_getLast(this->history);
if (!this->current || this->current->len > 0) {
this->current = FieldItem_new(this->w);
this->current = FieldItem_new(this->history, this->w);
List_add(this->history, (ListItem*) this->current);
}
this->cursor = 0;
Expand Down
25 changes: 15 additions & 10 deletions Highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

/*{
extern char* HIGHLIGHTRULE_CLASS;
struct HighlightContextClass_ {
ObjectClass super;
};
struct HighlightContext_ {
Object super;
Expand All @@ -23,7 +25,7 @@ struct HighlightContext_ {
PatternMatcher* rules;
};
extern char* HIGHLIGHTCONTEXT_CLASS;
extern HighlightContextClass HighlightContextType;
struct Highlight_ {
Vector* contexts;
Expand All @@ -39,9 +41,14 @@ struct Highlight_ {
}*/

char* HIGHLIGHTCONTEXT_CLASS = "HighlightContext";

char* HIGHLIGHTRULE_CLASS = "HighlightRule";
HighlightContextClass HighlightContextType = {
.super = {
.size = sizeof(HighlightContext),
.display = NULL,
.equals = Object_equals,
.delete = HighlightContext_delete
}
};

static Color Highlight_translateColor(char* color) {
if (String_eq(color, "bright")) return BrightColor;
Expand All @@ -64,7 +71,7 @@ Highlight* Highlight_new(const char* fileName, const char* firstLine) {
Highlight* this = (Highlight*) malloc(sizeof(Highlight));
// this->words = PatternMatcher_new();

this->contexts = Vector_new(HIGHLIGHTCONTEXT_CLASS, true, DEFAULT_SIZE);
this->contexts = Vector_new(ClassAs(HighlightContext, Object), true, DEFAULT_SIZE);
this->currentContext = NULL;
char highlightPath[4096];
snprintf(highlightPath, 4095, "%s/.dit/highlight", getenv("HOME"));
Expand All @@ -84,7 +91,7 @@ Highlight* Highlight_new(const char* fileName, const char* firstLine) {
bool success = true;
this->mainContext = Highlight_addContext(this, NULL, NULL, NULL, NormalColor);
HighlightContext* context = this->mainContext;
Stack* contexts = Stack_new(HIGHLIGHTCONTEXT_CLASS, false);
Stack* contexts = Stack_new(ClassAs(HighlightContext, Object), false);
Stack_push(contexts, context, 0);
int lineno = 0;
this->toLower = false;
Expand Down Expand Up @@ -299,9 +306,7 @@ inline void Highlight_setContext(Highlight* this, HighlightContext* context) {
}

HighlightContext* HighlightContext_new(int id, Color defaultColor) {
HighlightContext* this = (HighlightContext*) malloc(sizeof(HighlightContext));
Object_init((Object*) this, HIGHLIGHTCONTEXT_CLASS);
((Object*)this)->delete = HighlightContext_delete;
HighlightContext* this = Alloc(HighlightContext);
this->id = id;
this->follows = PatternMatcher_new();
this->defaultColor = defaultColor;
Expand Down
46 changes: 27 additions & 19 deletions Line.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,63 @@
#define TAB_WIDTH 8
struct LineClass_ {
ListItemClass super;
};
struct Line_ {
ListItem super;
char* text;
int textSize;
int len;
HighlightContext* context;
Buffer* buffer;
};
extern char* LINE_CLASS;
extern LineClass LineType;
}*/

char* LINE_CLASS = "Line";
LineClass LineType = {
.super = {
.super = {
.size = sizeof(Line),
.display = Line_display,
.equals = Line_equals,
.delete = Line_delete
}
}
};

Line* Line_new(char* text, int len, Buffer* buffer) {
Line* this = (Line*) malloc(sizeof(Line));
((Object*)this)->class = LINE_CLASS;
((Object*)this)->display = Line_display;
((Object*)this)->equals = Line_equals;
((Object*)this)->delete = Line_delete;
ListItem_init((ListItem*)this);
Line* Line_new(List* list, char* text, int len, HighlightContext* context) {
Line* this = Pool_allocate(list->pool);
Bless(Line);
Call0(ListItem, init, this);
assert(len == strlen(text));
this->text = text;
this->textSize = len + 1;
this->len = len;
this->context = buffer->hl->mainContext;
this->buffer = buffer;
this->context = context;
assert(this->text[this->len] == '\0');
return this;
}

void Line_delete(Object* cast) {
Line* this = (Line*) cast;
free(this->text);
free(this);
}

void Line_updateContext(Line* this) {
// temporarily disable selection
bool selecting = this->buffer->selecting;
this->buffer->selecting = false;
Buffer* buffer = (Buffer*)(this->super.list->data);
bool selecting = buffer->selecting;
buffer->selecting = false;
Line_display((Object*)this, NULL);
this->buffer->selecting = selecting;
buffer->selecting = selecting;
}

void Line_display(Object* cast, RichString* str) {
Line* this = (Line*) cast;
Buffer* buffer = this->buffer;
Buffer* buffer = (Buffer*)(this->super.list->data);
int scrollH = buffer->panel->scrollH;
int y = buffer->panel->displaying;
int len = this->len;
Expand Down Expand Up @@ -227,7 +235,7 @@ void Line_breakAt(Line* this, int at, int indent) {
rest[restLen + indent] = '\0';
this->text[at] = '\0';
this->len = at;
Line* newLine = Line_new(rest, restLen + indent, this->buffer);
Line* newLine = Line_new(this->super.list, rest, restLen + indent, this->context);
ListItem_addAfter((ListItem*) this, (ListItem*) newLine);
assert(this->text[this->len] == '\0');
}
Expand Down Expand Up @@ -399,7 +407,7 @@ void Line_insertBlock(Line* this, int x, char* block, int len, int* newX, int* n
char* text = malloc(lineLen+1);
text[lineLen] = '\0';
memcpy(text, walk, lineLen);
Line* newLine = Line_new(text, lineLen, this->buffer);
Line* newLine = Line_new(this->super.list, text, lineLen, this->context);
ListItem_addAfter((ListItem*) at, (ListItem*) newLine);
at = newLine;
walk = ++nl;
Expand Down
Loading

0 comments on commit bc46364

Please sign in to comment.