Skip to content

Commit 2a96d26

Browse files
committed
Use cleanup in a few more places to simplify the code
1 parent a097f5d commit 2a96d26

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

kitty/data-types.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,17 @@ static PyObject*
4141
process_group_map() {
4242
int num_of_processes = proc_listallpids(NULL, 0);
4343
size_t bufsize = sizeof(pid_t) * (num_of_processes + 1024);
44-
pid_t *buf = malloc(bufsize);
44+
FREE_AFTER_FUNCTION pid_t *buf = malloc(bufsize);
4545
if (!buf) return PyErr_NoMemory();
4646
num_of_processes = proc_listallpids(buf, (int)bufsize);
4747
PyObject *ans = PyTuple_New(num_of_processes);
48-
if (ans == NULL) { free(buf); return PyErr_NoMemory(); }
48+
if (ans == NULL) { return PyErr_NoMemory(); }
4949
for (int i = 0; i < num_of_processes; i++) {
5050
long pid = buf[i], pgid = getpgid(buf[i]);
5151
PyObject *t = Py_BuildValue("ll", pid, pgid);
52-
if (t == NULL) { free(buf); Py_DECREF(ans); return NULL; }
52+
if (t == NULL) { Py_DECREF(ans); return NULL; }
5353
PyTuple_SET_ITEM(ans, i, t);
5454
}
55-
free(buf);
5655
return ans;
5756
}
5857
#endif

kitty/data-types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#define zero_at_ptr_count(p, count) memset((p), 0, (count) * sizeof((p)[0]))
3838
void log_error(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
3939
#define fatal(...) { log_error(__VA_ARGS__); exit(EXIT_FAILURE); }
40+
static inline void cleanup_free(void *p) { free(*(void**)p); }
41+
#define FREE_AFTER_FUNCTION __attribute__((cleanup(cleanup_free)))
4042

4143
typedef unsigned long long id_type;
4244
typedef uint32_t char_type;

kitty/disk-cache.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,14 @@ open_cache_file(const char *cache_path) {
9191
}
9292
#else
9393
size_t sz = strlen(cache_path) + 16;
94-
char *buf = calloc(1, sz);
94+
FREE_AFTER_FUNCTION char *buf = calloc(1, sz);
9595
if (!buf) { errno = ENOMEM; return -1; }
9696
snprintf(buf, sz - 1, "%s/disk-cache-XXXXXXXXXXXX", cache_path);
9797
while (fd < 0) {
9898
fd = mkostemp(buf, O_CLOEXEC);
9999
if (fd > -1 || errno != EINTR) break;
100100
}
101101
if (fd > -1) unlink(buf);
102-
free(buf);
103102
#endif
104103
return fd;
105104
}
@@ -171,8 +170,8 @@ typedef struct {
171170
static void
172171
defrag(DiskCache *self) {
173172
int new_cache_file = -1;
174-
DefragEntry *defrag_entries = NULL;
175-
uint8_t *buf = NULL;
173+
FREE_AFTER_FUNCTION DefragEntry *defrag_entries = NULL;
174+
FREE_AFTER_FUNCTION uint8_t *buf = NULL;
176175
const size_t bufsz = 1024 * 1024;
177176
bool lock_released = false, ok = false;
178177

@@ -235,8 +234,6 @@ defrag(DiskCache *self) {
235234
if (s) s->pos_in_cache_file = e->new_offset;
236235
}
237236
}
238-
if (defrag_entries) free(defrag_entries);
239-
if (buf) free(buf);
240237
if (new_cache_file > -1) safe_close(new_cache_file, __FILE__, __LINE__);
241238
}
242239

@@ -484,7 +481,7 @@ add_to_disk_cache(PyObject *self_, const void *key, size_t key_sz, const void *d
484481
if (!ensure_state(self)) return false;
485482
if (key_sz > MAX_KEY_SIZE) { PyErr_SetString(PyExc_KeyError, "cache key is too long"); return false; }
486483
CacheEntry *s = NULL;
487-
uint8_t *copied_data = malloc(data_sz);
484+
FREE_AFTER_FUNCTION uint8_t *copied_data = malloc(data_sz);
488485
if (!copied_data) { PyErr_NoMemory(); return false; }
489486
memcpy(copied_data, data, data_sz);
490487

@@ -503,8 +500,6 @@ add_to_disk_cache(PyObject *self_, const void *key, size_t key_sz, const void *d
503500
self->total_size += s->data_sz;
504501
end:
505502
mutex(unlock);
506-
507-
if (copied_data) free(copied_data);
508503
if (PyErr_Occurred()) return false;
509504
wakeup_write_loop(self);
510505
return true;

0 commit comments

Comments
 (0)