forked from sendgrid/go-gmime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
janitor.go
50 lines (40 loc) · 755 Bytes
/
janitor.go
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
package gmime
/*
#cgo pkg-config: gmime-2.6
#include <stdlib.h>
#include <gmime/gmime.h>
*/
import "C"
import (
"runtime"
)
// Janitor to clean up wasteful resources
type Janitor interface {
pointer() C.gpointer
finalize()
}
func AssignJanitor(self Janitor) {
ref(self.pointer())
runtime.SetFinalizer(self, func(j Janitor) {
j.finalize()
})
}
type PointerMixin struct {
ptr C.gpointer
}
func CastPointer(p C.gpointer) *PointerMixin {
pp := &PointerMixin{ptr: p}
AssignJanitor(pp)
return pp
}
// for newly allocated only
func NewPointer(p C.gpointer) *PointerMixin {
defer unref(p)
return CastPointer(p)
}
func (self *PointerMixin) finalize() {
unref(self.ptr)
}
func (self *PointerMixin) pointer() C.gpointer {
return self.ptr
}