diff options
author | koekeishiya <aasvi93@hotmail.com> | 2018-07-19 21:02:35 +0200 |
---|---|---|
committer | koekeishiya <aasvi93@hotmail.com> | 2018-07-19 21:02:35 +0200 |
commit | 2f370cc12adb6e4050ee1766a5348576083415b1 (patch) | |
tree | 40499830604b6f8186a471bdf3fdc341db84f296 /src | |
parent | 22147c7516a06e8c2d6db42fe453119a7a49ded2 (diff) | |
download | skhd-2f370cc12adb6e4050ee1766a5348576083415b1.tar.gz skhd-2f370cc12adb6e4050ee1766a5348576083415b1.zip |
simplify hotloader; get rid of calls to malloc in callback
Diffstat (limited to '')
-rw-r--r-- | src/hotload.c | 31 | ||||
-rw-r--r-- | src/hotload.h | 1 |
2 files changed, 11 insertions, 21 deletions
diff --git a/src/hotload.c b/src/hotload.c index ad02d21..6e8ae02 100644 --- a/src/hotload.c +++ b/src/hotload.c @@ -50,7 +50,7 @@ resolve_symlink(char *file) } if (!S_ISLNK(buffer.st_mode)) { - return file; + return copy_string(file); } ssize_t size = buffer.st_size + 1; @@ -72,18 +72,9 @@ hotloader_watched_file(struct hotloader *hotloader, char *absolutepath) struct watched_file *result = NULL; for (unsigned index = 0; result == NULL && index < hotloader->watch_count; ++index) { struct watched_file *watch_info = hotloader->watch_list + index; - - char *directory = file_directory(absolutepath); - char *filename = file_name(absolutepath); - - if (strcmp(watch_info->directory, directory) == 0) { - if (strcmp(watch_info->filename, filename) == 0) { - result = watch_info; - } + if (strcmp(watch_info->absolutepath, absolutepath) == 0) { + result = watch_info; } - - free(filename); - free(directory); } return result; @@ -99,7 +90,7 @@ internal FSEVENT_CALLBACK(hotloader_handler) for (unsigned index = 0; index < count; ++index) { char *absolutepath = files[index]; if ((watch_info = hotloader_watched_file(hotloader, absolutepath))) { - hotloader->callback(absolutepath, watch_info->directory, watch_info->filename); + hotloader->callback(watch_info->absolutepath, watch_info->directory, watch_info->filename); } } } @@ -109,14 +100,11 @@ void hotloader_add_file(struct hotloader *hotloader, char *file) if (!hotloader->enabled) { char *real_path = resolve_symlink(file); if (real_path) { - struct watched_file watch_info; - watch_info.directory = file_directory(real_path); - watch_info.filename = file_name(real_path); - - if (real_path != file) { - free(real_path); - } - + struct watched_file watch_info = { + .absolutepath = real_path, + .directory = file_directory(real_path), + .filename = file_name(real_path) + }; hotloader->watch_list[hotloader->watch_count++] = watch_info; printf("hotload: watching file '%s' in directory '%s'\n", watch_info.filename, watch_info.directory); } else { @@ -168,6 +156,7 @@ void hotloader_end(struct hotloader *hotloader) CFIndex count = CFArrayGetCount(hotloader->path); for (unsigned index = 0; index < count; ++index) { CFStringRef string_ref = (CFStringRef) CFArrayGetValueAtIndex(hotloader->path, index); + free(hotloader->watch_list[index].absolutepath); free(hotloader->watch_list[index].directory); free(hotloader->watch_list[index].filename); CFRelease(string_ref); diff --git a/src/hotload.h b/src/hotload.h index ad782fb..78ed07b 100644 --- a/src/hotload.h +++ b/src/hotload.h @@ -9,6 +9,7 @@ typedef HOTLOADER_CALLBACK(hotloader_callback); struct watched_file { + char *absolutepath; char *directory; char *filename; }; |