aid.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package al
  2. /*
  3. #include <stdlib.h>
  4. */
  5. import "C"
  6. import "unsafe"
  7. import "runtime"
  8. import "fmt"
  9. // Helper functions for working with C easier
  10. // Calls C malloc
  11. func malloc(size int) unsafe.Pointer {
  12. return (unsafe.Pointer(C.calloc(C.size_t(size), C.size_t(1))))
  13. }
  14. // Calls C free
  15. func free(ptr unsafe.Pointer) {
  16. C.free(ptr)
  17. }
  18. // Allocates a string with the given byte length
  19. // don't forget a call to defer cstrFree() !
  20. func cstrNew(size int) *C.char {
  21. return (*C.char)(malloc(size))
  22. }
  23. // free is a method on C char * strings to method to free the associated memory
  24. func cstrFree(self *C.char) {
  25. free(unsafe.Pointer(self))
  26. }
  27. // Coverts a string to a C string. This allocates memory,
  28. // so don't forget to add a "defer cstrFree(cstr)"
  29. func cstr(self string) *C.char {
  30. return C.CString(self)
  31. }
  32. // Shorthand for C.GoString. Yes, it's just laziness. :)
  33. func gostr(cstr *C.char) string {
  34. return C.GoString(cstr)
  35. }
  36. // Converts an int pointer to a C.int pointer.
  37. func cintptr(ptr *int) *C.int {
  38. return (*C.int)(unsafe.Pointer(ptr))
  39. }
  40. /*
  41. // Converts a byte pointer to a C.Uchar8 pointer.
  42. func cbyteptr(ptr * uint8) (*C.Uint8) {
  43. return (*C.Uint8)(unsafe.Pointer(ptr))
  44. }
  45. */
  46. // Converts ints to bools.
  47. func i2b(res int) bool {
  48. if res != 0 {
  49. return true
  50. }
  51. return false
  52. }
  53. // Converts bools to ints.
  54. func b2i(res bool) int {
  55. if res {
  56. return 1
  57. }
  58. return 0
  59. }
  60. // Interface for destructable objects
  61. type Destroyer interface {
  62. Destroy()
  63. }
  64. // Sets up a automatic finalizer for destructable objects
  65. // that will call Destroy using runtime.SetFinalizer
  66. // when the garbage collecter cleans up self.
  67. // self may also be nil in which case the destructor is NOT set up
  68. func SelfDestruct(self Destroyer) {
  69. if self == nil {
  70. return
  71. }
  72. clean := func(me Destroyer) {
  73. fmt.Printf("Finalizing %#v.\n", me)
  74. me.Destroy()
  75. }
  76. runtime.SetFinalizer(self, clean)
  77. }
  78. // this is too for laziness, but it's quite handy
  79. func cf(f float32) C.float {
  80. return C.float(f)
  81. }
  82. // this is too for laziness, but it's quite handy
  83. func ci(f int) C.int {
  84. return C.int(f)
  85. }
  86. // this is too for laziness, but it's quite handy
  87. func cd(f float64) C.double {
  88. return C.double(f)
  89. }
  90. /* This is the usual boilerplate for wrapping C types through a handle
  91. type XXX struct {
  92. handle * C.YYY
  93. }
  94. // Converts a zzz to it's underlying C pointer
  95. func (self * XXX) toC() *C.YYY {
  96. return (*C.YYY)(self.handle)
  97. }
  98. // Destroys the zzz.
  99. func (self *XXX) Destroy() {
  100. if self.handle != nil {
  101. C.al_destroy_zzz(self.toC())
  102. }
  103. self.handle = nil
  104. }
  105. // Wraps a C zzz into a go zzz
  106. func wrapXXXRaw(data *C.YYY) *XXX {
  107. if data == nil {
  108. return nil
  109. }
  110. return &XXX{data}
  111. }
  112. // Sets up a finalizer for this XXX that calls Destroy()
  113. func (self *XXX) SetDestroyFinalizer() *XXX {
  114. if self != nil {
  115. runtime.SetFinalizer(self, func(me *XXX) { me.Destroy() })
  116. }
  117. return self
  118. }
  119. // Wraps a C zzz into a go zzz and sets up a finalizer that calls Destroy()
  120. func wrapXXX(data *C.YYY) *XXX {
  121. self := wrapXXXRaw(data)
  122. return self.SetDestroyFinalizer()
  123. }
  124. */