mem_brk.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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
  5. // +build libc.membrk,!libc.memgrind
  6. // This is a debug-only version of the memory handling functions. When a
  7. // program is built with -tags=libc.membrk a simple but safe version of malloc
  8. // and friends is used that works like sbrk(2). Additionally free becomes a
  9. // nop.
  10. package libc // import "modernc.org/libc"
  11. import (
  12. "unsafe"
  13. "modernc.org/libc/errno"
  14. "modernc.org/libc/sys/types"
  15. )
  16. const (
  17. heapAlign = 16
  18. memgrind = false
  19. )
  20. var (
  21. heap = make([]byte, heapSize)
  22. heapP = uintptr(unsafe.Pointer(&heap[heapAlign]))
  23. heapLast = uintptr(unsafe.Pointer(&heap[heapSize-1]))
  24. )
  25. // void *malloc(size_t size);
  26. func Xmalloc(t *TLS, n types.Size_t) uintptr {
  27. if n == 0 {
  28. return 0
  29. }
  30. allocMu.Lock()
  31. defer allocMu.Unlock()
  32. n2 := uintptr(n) + uintptrSize // reserve space for recording block size
  33. p := roundup(heapP, 16)
  34. if p+uintptr(n2) >= heapLast {
  35. t.setErrno(errno.ENOMEM)
  36. return 0
  37. }
  38. heapP = p + uintptr(n2)
  39. *(*uintptr)(unsafe.Pointer(p - uintptrSize)) = uintptr(n)
  40. return p
  41. }
  42. // void *calloc(size_t nmemb, size_t size);
  43. func Xcalloc(t *TLS, n, size types.Size_t) uintptr {
  44. return Xmalloc(t, n*size)
  45. }
  46. // void *realloc(void *ptr, size_t size);
  47. func Xrealloc(t *TLS, ptr uintptr, size types.Size_t) uintptr {
  48. switch {
  49. case ptr != 0 && size != 0:
  50. p := Xmalloc(t, size)
  51. sz0 := UsableSize(ptr)
  52. if p != 0 {
  53. copy((*RawMem)(unsafe.Pointer(p))[:size:size], (*RawMem)(unsafe.Pointer(ptr))[:sz0:sz0])
  54. }
  55. return p
  56. case ptr == 0 && size != 0:
  57. return Xmalloc(t, size)
  58. }
  59. return 0
  60. }
  61. // void free(void *ptr);
  62. func Xfree(t *TLS, p uintptr) {}
  63. func UsableSize(p uintptr) types.Size_t {
  64. return types.Size_t(*(*uintptr)(unsafe.Pointer(p - uintptrSize)))
  65. }
  66. // MemAuditStart locks the memory allocator, initializes and enables memory
  67. // auditing. Finaly it unlocks the memory allocator.
  68. //
  69. // Some memory handling errors, like double free or freeing of unallocated
  70. // memory, will panic when memory auditing is enabled.
  71. //
  72. // This memory auditing functionality has to be enabled using the libc.memgrind
  73. // build tag.
  74. //
  75. // It is intended only for debug/test builds. It slows down memory allocation
  76. // routines and it has additional memory costs.
  77. func MemAuditStart() {}
  78. // MemAuditReport locks the memory allocator, reports memory leaks, if any.
  79. // Finally it disables memory auditing and unlocks the memory allocator.
  80. //
  81. // This memory auditing functionality has to be enabled using the libc.memgrind
  82. // build tag.
  83. //
  84. // It is intended only for debug/test builds. It slows down memory allocation
  85. // routines and it has additional memory costs.
  86. func MemAuditReport() error { return nil }