Skip to content

Commit

Permalink
more robust memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Feb 15, 2022
1 parent f5137c3 commit 36157ae
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "1.0.3"
VERSION = "1.0.4"
CC = clang
DEFINES = -DHAVE_CONFIG_H -DMACOS_X -DMACOS_X_DARWIN
LIBS = lib/libvim.a -lm -lncurses -liconv -framework Carbon -framework Cocoa
Expand Down Expand Up @@ -40,7 +40,7 @@ sign:
$(MAKE) universal
codesign -fs 'svim-cert' $(ODIR)/svim

bundle:
bundle: clean
$(MAKE) sign
@mkdir bundle
cp $(ODIR)/svim bundle/
Expand Down
1 change: 1 addition & 0 deletions src/ax.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ bool ax_get_selected_element(struct ax* ax) {
}

ax->role = role;
if (ax->selected_element) CFRelease(ax->selected_element);
ax->selected_element = selected_element;

return (error == kAXErrorSuccess) && role;
Expand Down
25 changes: 11 additions & 14 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,30 @@ void buffer_update_raw_text(struct buffer* buffer) {
}
raw[len - 1] = '\0';

if (buffer->raw && raw && strcmp(buffer->raw, raw) == 0) {
free(raw);
buffer->did_change = false;
}
else {
if (buffer->raw) free(buffer->raw);
buffer->raw = raw;
buffer->did_change = true;
}
if (buffer->raw) free(buffer->raw);
buffer->raw = raw;
}

void buffer_sync_text(struct buffer* buffer) {
buffer_update_raw_text(buffer);

for (int i = 0; i < buffer->line_count; i++)
uint32_t lines = vimBufferGetLineCount(buffer->vbuf);
uint32_t old_count = buffer->line_count;

for (int i = lines; i < buffer->line_count; i++) {
line_destroy(buffer->lines[i]);
buffer->lines[i] = NULL;
}

uint32_t lines = vimBufferGetLineCount(buffer->vbuf);
buffer->lines = realloc(buffer->lines, sizeof(struct line*) * lines);
memset(buffer->lines, 0, sizeof(struct line*) * lines);
buffer->line_count = lines;

buffer->did_change = false;
for (int i = 1; i <= lines; i++) {
char_u* line = vimBufferGetLine(buffer->vbuf, i);
buffer->lines[i - 1] = line_create();
if (i > old_count) buffer->lines[i - 1] = line_create();
line_set_text(buffer->lines[i - 1], line);
buffer->did_change |= buffer->lines[i - 1]->changed;
}
}

Expand Down Expand Up @@ -119,7 +117,6 @@ void buffer_sync_cursor(struct buffer* buffer) {
buffer->cursor.position -= inverted ? 0 : selection;
buffer->cursor.selection = selection + 1;
}

}
else
buffer->cursor.selection = 0;
Expand Down
22 changes: 17 additions & 5 deletions src/line.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,30 @@ struct line* line_create() {
return line;
}

bool line_compare_raw(struct line* line, char* raw) {
if (line->raw && raw && strcmp(line->raw, raw) == 0)
return true;
return false;
}

void line_set_text(struct line* line, char* text) {
if (!text) return;
line_clear(line);

if (line_compare_raw(line, text)) {
line->changed = false;
return;
}

uint32_t len = strlen(text);
line->text = malloc(sizeof(wchar_t) * (len + 1));
memset(line->text, 0, sizeof(wchar_t) * (len + 1));
swprintf(line->text, len + 1, L"%s\0", text);
line->text = realloc(line->text, sizeof(wchar_t) * (len + 1));
swprintf(line->text, len + 1, L"%s", text);
line->text[len] = '\0';
line->length = wcslen(line->text);

line->raw = malloc(sizeof(char) * (len + 1));
line->raw = realloc(line->raw, sizeof(char) * (len + 1));
memcpy(line->raw, text, sizeof(char) * (len + 1));
line->raw_length = len;
line->changed = true;
}

void line_clear(struct line* line) {
Expand Down
2 changes: 2 additions & 0 deletions src/line.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

struct line {
bool changed;
uint32_t length;
uint32_t raw_length;
wchar_t* text;
Expand Down
1 change: 1 addition & 0 deletions src/workspace.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ - (void)appSwitched:(NSNotification *)notification {
if (app) name = string_copy((char*)[[app localizedName] UTF8String]);
}

if (g_front_app) free(g_front_app);
g_front_app = name;
}

Expand Down

0 comments on commit 36157ae

Please sign in to comment.