-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratch.c
58 lines (51 loc) · 1.24 KB
/
scratch.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdlib.h>
#include <string.h>
#include "scratch.h"
struct _scratch {
char *scratch;
char *free_scratch;
size_t available_scratch;
size_t capacity;
};
SCRATCH *scratch_new(size_t capacity) {
SCRATCH *s = malloc(sizeof(SCRATCH));
s->capacity = capacity;
s->scratch = malloc(capacity);
scratch_reset(s);
return s;
}
void *scratch_detach(SCRATCH *s) {
if (s) {
void *result = s->scratch;
s->scratch = malloc(s->capacity);
scratch_reset(s);
return result;
} else
return NULL;
}
void *scratch_head(SCRATCH *s) {
if (s) return s->scratch;
else return NULL;
}
void *scratch_get(SCRATCH *s, size_t size) {
void *result;
if (s && s->available_scratch >= size) {
result = s->free_scratch;
s->free_scratch += size;
s->available_scratch -= size;
} else result = NULL;
return result;
}
void scratch_reset(SCRATCH *s) {
if (s) {
memset(s->scratch, 0, s->capacity);
s->free_scratch = s->scratch;
s->available_scratch = s->capacity;
}
}
void scratch_free(SCRATCH *s, scratch_keep keep) {
if (s) {
if (keep == SCRATCH_FREE) free(s->scratch);
free(s);
}
}