Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53722b5

Browse files
committed
Started the safer malloc() system.
1 parent 439f3f4 commit 53722b5

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

draw.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ package ui
44

55
// #include <stdlib.h>
66
// #include "ui.h"
7+
// // TODO figure this one out
8+
// extern void *uimalloc(size_t);
79
// static uiDrawBrush *newBrush(void)
810
// {
911
// uiDrawBrush *b;
1012
//
11-
// b = (uiDrawBrush *) malloc(sizeof (uiDrawBrush));
12-
// // TODO
13+
// b = (uiDrawBrush *) uimalloc(sizeof (uiDrawBrush));
1314
// return b;
1415
// }
1516
// static uiDrawBrushGradientStop *newStops(size_t n)

util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,22 @@ import (
77
)
88

99
// #include <stdlib.h>
10+
// // TODO remove when switching to Go 1.7
11+
// #include <string.h>
1012
import "C"
1113

14+
// TODO move this to C.CBytes() when switching to Go 1.7
15+
16+
//export uimalloc
17+
func uimalloc(n C.size_t) unsafe.Pointer {
18+
p := C.malloc(n)
19+
if p == nil {
20+
panic("out of memory in uimalloc()")
21+
}
22+
C.memset(p, 0, n)
23+
return p
24+
}
25+
1226
func freestr(str *C.char) {
1327
C.free(unsafe.Pointer(str))
1428
}

0 commit comments

Comments
 (0)