-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgrowable-heap-sizer.h
59 lines (50 loc) · 1.64 KB
/
growable-heap-sizer.h
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
59
#ifndef GROWABLE_HEAP_SIZER_H
#define GROWABLE_HEAP_SIZER_H
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "assert.h"
#include "heap-sizer.h"
// This is a simple heap-sizing algorithm that will grow the heap if it is
// smaller than a given multiplier of the live data size. It does not shrink
// the heap.
struct gc_growable_heap_sizer {
struct gc_heap *heap;
double multiplier;
pthread_mutex_t lock;
};
static void
gc_growable_heap_sizer_set_multiplier(struct gc_growable_heap_sizer *sizer,
double multiplier) {
pthread_mutex_lock(&sizer->lock);
sizer->multiplier = multiplier;
pthread_mutex_unlock(&sizer->lock);
}
static void
gc_growable_heap_sizer_on_gc(struct gc_growable_heap_sizer *sizer,
size_t heap_size, size_t live_bytes,
uint64_t pause_ns,
void (*set_heap_size)(struct gc_heap*, size_t)) {
pthread_mutex_lock(&sizer->lock);
size_t target_size = live_bytes * sizer->multiplier;
if (target_size > heap_size)
set_heap_size(sizer->heap, target_size);
pthread_mutex_unlock(&sizer->lock);
}
static struct gc_growable_heap_sizer*
gc_make_growable_heap_sizer(struct gc_heap *heap, double multiplier) {
struct gc_growable_heap_sizer *sizer;
sizer = malloc(sizeof(*sizer));
if (!sizer)
GC_CRASH();
memset(sizer, 0, sizeof(*sizer));
sizer->heap = heap;
sizer->multiplier = multiplier;
pthread_mutex_init(&sizer->lock, NULL);
return sizer;
}
static void
gc_destroy_growable_heap_sizer(struct gc_growable_heap_sizer *sizer) {
free(sizer);
}
#endif // GROWABLE_HEAP_SIZER_H