Skip to content

Commit bc46364

Browse files
committed
New class/message-dispatch system. Pool allocator for Line objects.
These brought some memory improvements.
1 parent f802cd3 commit bc46364

13 files changed

+318
-148
lines changed

Buffer.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command) {
9898

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

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

105106
FileReader* file = FileReader_new(fileName, command);
@@ -109,20 +110,20 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command) {
109110
char* text = FileReader_readLine(file, &len);
110111
this->hl = Highlight_new(fileName, text);
111112
if (strchr(text, '\t')) this->tabCharacters = true;
112-
Line* line = Line_new(text, len, this);
113-
Panel_set(this->panel, i, (ListItem*) line);
113+
Line* line = Line_new(p->items, text, len, this->hl->mainContext);
114+
Panel_set(p, i, (ListItem*) line);
114115
while (!FileReader_eof(file)) {
115116
i++;
116117
char* text = FileReader_readLine(file, &len);
117118
if (text) {
118119
if (!this->tabCharacters && strchr(text, '\t')) this->tabCharacters = true;
119-
Line* line = Line_new(text, len, this);
120-
Panel_set(this->panel, i, (ListItem*) line);
120+
Line* line = Line_new(p->items, text, len, this->hl->mainContext);
121+
Panel_set(p, i, (ListItem*) line);
121122
}
122123
}
123124
} else {
124125
this->hl = Highlight_new(fileName, "");
125-
Panel_set(this->panel, 0, (ListItem*) Line_new(String_copy(""), 0, this));
126+
Panel_set(p, 0, (ListItem*) Line_new(p->items, String_copy(""), 0, this->hl->mainContext));
126127
}
127128
if (file)
128129
FileReader_delete(file);
@@ -271,7 +272,7 @@ void Buffer_draw(Buffer* this) {
271272
Buffer_highlightBracket(this);
272273

273274
p->cursorX = screenX - p->scrollH;
274-
Panel_draw(p, true);
275+
Panel_draw(p);
275276

276277
this->wasSelecting = this->selecting;
277278
}
@@ -376,7 +377,7 @@ inline void Buffer_highlightBracket(Buffer* this) {
376377
void Buffer_delete(Buffer* this) {
377378
Buffer_storePosition(this);
378379
free(this->fileName);
379-
((Object*) this->panel)->delete((Object*) this->panel);
380+
Msg0(Object, delete, this->panel);
380381

381382
Undo_delete(this->undo);
382383
Highlight_delete(this->hl);

Field.c

+21-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string.h>
44

55
#include "Prototypes.h"
6+
//#needs Object
67
//#needs List
78

89
/*{
@@ -20,22 +21,35 @@ struct Field_ {
2021
int cursor;
2122
};
2223
24+
struct FieldItemClass_ {
25+
ListItemClass super;
26+
};
27+
2328
struct FieldItem_ {
2429
ListItem super;
2530
char* text;
2631
int len;
2732
int w;
2833
};
2934
30-
extern char* FIELDITEM_CLASS;
35+
extern FieldItemClass FieldItemType;
3136
3237
}*/
3338

3439
#ifndef MIN
3540
#define MIN(a,b) ((a)<(b)?(a):(b))
3641
#endif
3742

38-
char* FIELDITEM_CLASS = "FieldItem";
43+
FieldItemClass FieldItemType = {
44+
.super = {
45+
.super = {
46+
.size = sizeof(FieldItem),
47+
.display = NULL,
48+
.equals = Object_equals,
49+
.delete = FieldItem_delete
50+
}
51+
}
52+
};
3953

4054
Field* Field_new(char* label, int x, int y, int w) {
4155
Field* this = (Field*) malloc(sizeof(Field));
@@ -45,7 +59,7 @@ Field* Field_new(char* label, int x, int y, int w) {
4559
this->labelColor = CRT_colors[StatusColor];
4660
this->fieldColor = CRT_colors[FieldColor];
4761
this->cursor = 0;
48-
this->history = List_new(FIELDITEM_CLASS);
62+
this->history = List_new( ClassAs(FieldItem, ListItem), NULL );
4963
this->current = NULL;
5064
this->label = malloc(w + 1);
5165
this->labelLen = strlen(label);
@@ -60,10 +74,9 @@ void Field_delete(Field* this) {
6074
free(this);
6175
}
6276

63-
FieldItem* FieldItem_new(int w) {
64-
FieldItem* this = (FieldItem*) malloc(sizeof(FieldItem));
65-
((Object*)this)->class = FIELDITEM_CLASS;
66-
((Object*)this)->delete = FieldItem_delete;
77+
FieldItem* FieldItem_new(List* list, int w) {
78+
FieldItem* this = Pool_allocate(list->pool);
79+
Bless(FieldItem);
6780
this->text = calloc(w, 1);
6881
this->len = 0;
6982
this->w = w;
@@ -81,13 +94,12 @@ void Field_printfLabel(Field* this, char* picture, ...) {
8194
void FieldItem_delete(Object* cast) {
8295
FieldItem* this = (FieldItem*) cast;
8396
free(this->text);
84-
free(this);
8597
}
8698

8799
void Field_start(Field* this) {
88100
this->current = (FieldItem*) List_getLast(this->history);
89101
if (!this->current || this->current->len > 0) {
90-
this->current = FieldItem_new(this->w);
102+
this->current = FieldItem_new(this->history, this->w);
91103
List_add(this->history, (ListItem*) this->current);
92104
}
93105
this->cursor = 0;

Highlight.c

+15-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
/*{
1414
15-
extern char* HIGHLIGHTRULE_CLASS;
15+
struct HighlightContextClass_ {
16+
ObjectClass super;
17+
};
1618
1719
struct HighlightContext_ {
1820
Object super;
@@ -23,7 +25,7 @@ struct HighlightContext_ {
2325
PatternMatcher* rules;
2426
};
2527
26-
extern char* HIGHLIGHTCONTEXT_CLASS;
28+
extern HighlightContextClass HighlightContextType;
2729
2830
struct Highlight_ {
2931
Vector* contexts;
@@ -39,9 +41,14 @@ struct Highlight_ {
3941
4042
}*/
4143

42-
char* HIGHLIGHTCONTEXT_CLASS = "HighlightContext";
43-
44-
char* HIGHLIGHTRULE_CLASS = "HighlightRule";
44+
HighlightContextClass HighlightContextType = {
45+
.super = {
46+
.size = sizeof(HighlightContext),
47+
.display = NULL,
48+
.equals = Object_equals,
49+
.delete = HighlightContext_delete
50+
}
51+
};
4552

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

67-
this->contexts = Vector_new(HIGHLIGHTCONTEXT_CLASS, true, DEFAULT_SIZE);
74+
this->contexts = Vector_new(ClassAs(HighlightContext, Object), true, DEFAULT_SIZE);
6875
this->currentContext = NULL;
6976
char highlightPath[4096];
7077
snprintf(highlightPath, 4095, "%s/.dit/highlight", getenv("HOME"));
@@ -84,7 +91,7 @@ Highlight* Highlight_new(const char* fileName, const char* firstLine) {
8491
bool success = true;
8592
this->mainContext = Highlight_addContext(this, NULL, NULL, NULL, NormalColor);
8693
HighlightContext* context = this->mainContext;
87-
Stack* contexts = Stack_new(HIGHLIGHTCONTEXT_CLASS, false);
94+
Stack* contexts = Stack_new(ClassAs(HighlightContext, Object), false);
8895
Stack_push(contexts, context, 0);
8996
int lineno = 0;
9097
this->toLower = false;
@@ -299,9 +306,7 @@ inline void Highlight_setContext(Highlight* this, HighlightContext* context) {
299306
}
300307

301308
HighlightContext* HighlightContext_new(int id, Color defaultColor) {
302-
HighlightContext* this = (HighlightContext*) malloc(sizeof(HighlightContext));
303-
Object_init((Object*) this, HIGHLIGHTCONTEXT_CLASS);
304-
((Object*)this)->delete = HighlightContext_delete;
309+
HighlightContext* this = Alloc(HighlightContext);
305310
this->id = id;
306311
this->follows = PatternMatcher_new();
307312
this->defaultColor = defaultColor;

Line.c

+27-19
Original file line numberDiff line numberDiff line change
@@ -11,55 +11,63 @@
1111
1212
#define TAB_WIDTH 8
1313
14+
struct LineClass_ {
15+
ListItemClass super;
16+
};
17+
1418
struct Line_ {
1519
ListItem super;
1620
char* text;
1721
int textSize;
1822
int len;
1923
HighlightContext* context;
20-
Buffer* buffer;
2124
};
2225
23-
extern char* LINE_CLASS;
26+
extern LineClass LineType;
2427
2528
}*/
2629

27-
char* LINE_CLASS = "Line";
30+
LineClass LineType = {
31+
.super = {
32+
.super = {
33+
.size = sizeof(Line),
34+
.display = Line_display,
35+
.equals = Line_equals,
36+
.delete = Line_delete
37+
}
38+
}
39+
};
2840

29-
Line* Line_new(char* text, int len, Buffer* buffer) {
30-
Line* this = (Line*) malloc(sizeof(Line));
31-
((Object*)this)->class = LINE_CLASS;
32-
((Object*)this)->display = Line_display;
33-
((Object*)this)->equals = Line_equals;
34-
((Object*)this)->delete = Line_delete;
35-
ListItem_init((ListItem*)this);
41+
Line* Line_new(List* list, char* text, int len, HighlightContext* context) {
42+
Line* this = Pool_allocate(list->pool);
43+
Bless(Line);
44+
Call0(ListItem, init, this);
3645
assert(len == strlen(text));
3746
this->text = text;
3847
this->textSize = len + 1;
3948
this->len = len;
40-
this->context = buffer->hl->mainContext;
41-
this->buffer = buffer;
49+
this->context = context;
4250
assert(this->text[this->len] == '\0');
4351
return this;
4452
}
4553

4654
void Line_delete(Object* cast) {
4755
Line* this = (Line*) cast;
4856
free(this->text);
49-
free(this);
5057
}
5158

5259
void Line_updateContext(Line* this) {
5360
// temporarily disable selection
54-
bool selecting = this->buffer->selecting;
55-
this->buffer->selecting = false;
61+
Buffer* buffer = (Buffer*)(this->super.list->data);
62+
bool selecting = buffer->selecting;
63+
buffer->selecting = false;
5664
Line_display((Object*)this, NULL);
57-
this->buffer->selecting = selecting;
65+
buffer->selecting = selecting;
5866
}
5967

6068
void Line_display(Object* cast, RichString* str) {
6169
Line* this = (Line*) cast;
62-
Buffer* buffer = this->buffer;
70+
Buffer* buffer = (Buffer*)(this->super.list->data);
6371
int scrollH = buffer->panel->scrollH;
6472
int y = buffer->panel->displaying;
6573
int len = this->len;
@@ -227,7 +235,7 @@ void Line_breakAt(Line* this, int at, int indent) {
227235
rest[restLen + indent] = '\0';
228236
this->text[at] = '\0';
229237
this->len = at;
230-
Line* newLine = Line_new(rest, restLen + indent, this->buffer);
238+
Line* newLine = Line_new(this->super.list, rest, restLen + indent, this->context);
231239
ListItem_addAfter((ListItem*) this, (ListItem*) newLine);
232240
assert(this->text[this->len] == '\0');
233241
}
@@ -399,7 +407,7 @@ void Line_insertBlock(Line* this, int x, char* block, int len, int* newX, int* n
399407
char* text = malloc(lineLen+1);
400408
text[lineLen] = '\0';
401409
memcpy(text, walk, lineLen);
402-
Line* newLine = Line_new(text, lineLen, this->buffer);
410+
Line* newLine = Line_new(this->super.list, text, lineLen, this->context);
403411
ListItem_addAfter((ListItem*) at, (ListItem*) newLine);
404412
at = newLine;
405413
walk = ++nl;

0 commit comments

Comments
 (0)