mem_brk.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2021 The Libc Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build libc.membrk && !libc.memgrind && !(linux && (amd64 || arm64 || loong64 || ppc64le || s390x || riscv64 || 386 || arm))
  5. // This is a debug-only version of the memory handling functions. When a
  6. // program is built with -tags=libc.membrk a simple but safe version of malloc
  7. // and friends is used that works like sbrk(2). Additionally free becomes a
  8. // nop.
  9. package libc // import "modernc.org/libc"
  10. import (
  11. "unsafe"
  12. "modernc.org/libc/errno"
  13. "modernc.org/libc/sys/types"
  14. )
  15. const (
  16. heapAlign = 16
  17. memgrind = false
  18. )
  19. var (
  20. heap = make([]byte, heapSize)
  21. heapP = uintptr(unsafe.Pointer(&heap[heapAlign]))
  22. heapLast = uintptr(unsafe.Pointer(&heap[heapSize-1]))
  23. )
  24. // void *malloc(size_t size);
  25. func Xmalloc(t *TLS, n types.Size_t) uintptr {
  26. if __ccgo_strace {
  27. trc("t=%v n=%v, (%v:)", t, n, origin(2))
  28. }
  29. if n == 0 {
  30. // malloc(0) should return unique pointers
  31. // (often expected and gnulib replaces malloc if malloc(0) returns 0)
  32. n = 1
  33. }
  34. allocMu.Lock()
  35. defer allocMu.Unlock()
  36. n2 := uintptr(n) + uintptrSize // reserve space for recording block size
  37. p := roundup(heapP, 16)
  38. if p+uintptr(n2) >= heapLast {
  39. t.setErrno(errno.ENOMEM)
  40. return 0
  41. }
  42. heapP = p + uintptr(n2)
  43. *(*uintptr)(unsafe.Pointer(p - uintptrSize)) = uintptr(n)
  44. return p
  45. }
  46. // void *calloc(size_t nmemb, size_t size);
  47. func Xcalloc(t *TLS, n, size types.Size_t) uintptr {
  48. if __ccgo_strace {
  49. trc("t=%v n=%v size=%v, (%v:)", t, n, size, origin(2))
  50. }
  51. return Xmalloc(t, n*size)
  52. }
  53. // void *realloc(void *ptr, size_t size);
  54. func Xrealloc(t *TLS, ptr uintptr, size types.Size_t) uintptr {
  55. if __ccgo_strace {
  56. trc("t=%v ptr=%v size=%v, (%v:)", t, ptr, size, origin(2))
  57. }
  58. switch {
  59. case ptr != 0 && size != 0:
  60. p := Xmalloc(t, size)
  61. sz0 := UsableSize(ptr)
  62. if p != 0 {
  63. copy((*RawMem)(unsafe.Pointer(p))[:size:size], (*RawMem)(unsafe.Pointer(ptr))[:sz0:sz0])
  64. }
  65. return p
  66. case ptr == 0 && size != 0:
  67. return Xmalloc(t, size)
  68. }
  69. return 0
  70. }
  71. // void free(void *ptr);
  72. func Xfree(t *TLS, p uintptr) {
  73. if __ccgo_strace {
  74. trc("t=%v p=%v, (%v:)", t, p, origin(2))
  75. }
  76. }
  77. func UsableSize(p uintptr) types.Size_t {
  78. return types.Size_t(*(*uintptr)(unsafe.Pointer(p - uintptrSize)))
  79. }
  80. type MemAllocatorStat struct {
  81. Allocs int
  82. Bytes int
  83. Mmaps int
  84. }
  85. // MemStat no-op for this build tag
  86. func MemStat() MemAllocatorStat {
  87. return MemAllocatorStat{}
  88. }
  89. // MemAuditStart locks the memory allocator, initializes and enables memory
  90. // auditing. Finaly it unlocks the memory allocator.
  91. //
  92. // Some memory handling errors, like double free or freeing of unallocated
  93. // memory, will panic when memory auditing is enabled.
  94. //
  95. // This memory auditing functionality has to be enabled using the libc.memgrind
  96. // build tag.
  97. //
  98. // It is intended only for debug/test builds. It slows down memory allocation
  99. // routines and it has additional memory costs.
  100. func MemAuditStart() {}
  101. // MemAuditReport locks the memory allocator, reports memory leaks, if any.
  102. // Finally it disables memory auditing and unlocks the memory allocator.
  103. //
  104. // This memory auditing functionality has to be enabled using the libc.memgrind
  105. // build tag.
  106. //
  107. // It is intended only for debug/test builds. It slows down memory allocation
  108. // routines and it has additional memory costs.
  109. func MemAuditReport() error { return nil }