aboutsummaryrefslogtreecommitdiff
path: root/src/cleanup.c
blob: f47c374a76d3950642b2b4f4b96980bd99fe0454 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// cleanup.c: implements cleanup system that's shared across jurl abstract options
#include "jurl.h"

void jurl_do_cleanup(struct jurl_cleanup **src) {
	while (*src) {
		struct jurl_cleanup *cur = *src;
		switch (cur->type) {
		case JURL_CLEANUP_TYPE_SLIST:
			curl_slist_free_all(cur->slist);
			break;
		default:
			janet_panic("unknown type of cleanup data in do_cleanup");
		}
		*src = cur->next;
		free(cur);
	}
}

struct jurl_cleanup *register_cleanup(struct jurl_cleanup **prev, enum jurl_cleanup_type type) {
	struct jurl_cleanup *out = malloc(sizeof(struct jurl_cleanup));

	switch (type) {
		case JURL_CLEANUP_TYPE_SLIST:
			out->slist = NULL; // we malloced
			break;
		default:
			free(out);
			janet_panic("unknown cleanup type in register_cleanup");
			return NULL;
	}

	out->next = *prev;
	*prev = out;
	out->type = type;
	return out;
}