mem.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. package libc // import "modernc.org/libc"
  7. import (
  8. "modernc.org/libc/errno"
  9. "modernc.org/libc/sys/types"
  10. "modernc.org/memory"
  11. )
  12. const memgrind = false
  13. var (
  14. allocator memory.Allocator
  15. )
  16. // void *malloc(size_t size);
  17. func Xmalloc(t *TLS, n types.Size_t) uintptr {
  18. if n == 0 {
  19. return 0
  20. }
  21. allocMu.Lock()
  22. defer allocMu.Unlock()
  23. p, err := allocator.UintptrMalloc(int(n))
  24. if err != nil {
  25. t.setErrno(errno.ENOMEM)
  26. return 0
  27. }
  28. return p
  29. }
  30. // void *calloc(size_t nmemb, size_t size);
  31. func Xcalloc(t *TLS, n, size types.Size_t) uintptr {
  32. rq := int(n * size)
  33. if rq == 0 {
  34. return 0
  35. }
  36. allocMu.Lock()
  37. defer allocMu.Unlock()
  38. p, err := allocator.UintptrCalloc(int(n * size))
  39. if err != nil {
  40. t.setErrno(errno.ENOMEM)
  41. return 0
  42. }
  43. return p
  44. }
  45. // void *realloc(void *ptr, size_t size);
  46. func Xrealloc(t *TLS, ptr uintptr, size types.Size_t) uintptr {
  47. allocMu.Lock()
  48. defer allocMu.Unlock()
  49. p, err := allocator.UintptrRealloc(ptr, int(size))
  50. if err != nil {
  51. t.setErrno(errno.ENOMEM)
  52. return 0
  53. }
  54. return p
  55. }
  56. // void free(void *ptr);
  57. func Xfree(t *TLS, p uintptr) {
  58. if p == 0 {
  59. return
  60. }
  61. allocMu.Lock()
  62. defer allocMu.Unlock()
  63. allocator.UintptrFree(p)
  64. }
  65. func UsableSize(p uintptr) types.Size_t {
  66. allocMu.Lock()
  67. defer allocMu.Unlock()
  68. return types.Size_t(memory.UintptrUsableSize(p))
  69. }
  70. // MemAuditStart locks the memory allocator, initializes and enables memory
  71. // auditing. Finaly it unlocks the memory allocator.
  72. //
  73. // Some memory handling errors, like double free or freeing of unallocated
  74. // memory, will panic when memory auditing is enabled.
  75. //
  76. // This memory auditing functionality has to be enabled using the libc.memgrind
  77. // build tag.
  78. //
  79. // It is intended only for debug/test builds. It slows down memory allocation
  80. // routines and it has additional memory costs.
  81. func MemAuditStart() {}
  82. // MemAuditReport locks the memory allocator, reports memory leaks, if any.
  83. // Finally it disables memory auditing and unlocks the memory allocator.
  84. //
  85. // This memory auditing functionality has to be enabled using the libc.memgrind
  86. // build tag.
  87. //
  88. // It is intended only for debug/test builds. It slows down memory allocation
  89. // routines and it has additional memory costs.
  90. func MemAuditReport() error { return nil }