memory.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package brotli
  2. /* Copyright 2016 Google Inc. All Rights Reserved.
  3. Distributed under MIT license.
  4. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  5. */
  6. /*
  7. Dynamically grows array capacity to at least the requested size
  8. T: data type
  9. A: array
  10. C: capacity
  11. R: requested size
  12. */
  13. func brotli_ensure_capacity_uint8_t(a *[]byte, c *uint, r uint) {
  14. if *c < r {
  15. var new_size uint = *c
  16. if new_size == 0 {
  17. new_size = r
  18. }
  19. for new_size < r {
  20. new_size *= 2
  21. }
  22. if cap(*a) < int(new_size) {
  23. var new_array []byte = make([]byte, new_size)
  24. if *c != 0 {
  25. copy(new_array, (*a)[:*c])
  26. }
  27. *a = new_array
  28. } else {
  29. *a = (*a)[:new_size]
  30. }
  31. *c = new_size
  32. }
  33. }
  34. func brotli_ensure_capacity_uint32_t(a *[]uint32, c *uint, r uint) {
  35. var new_array []uint32
  36. if *c < r {
  37. var new_size uint = *c
  38. if new_size == 0 {
  39. new_size = r
  40. }
  41. for new_size < r {
  42. new_size *= 2
  43. }
  44. if cap(*a) < int(new_size) {
  45. new_array = make([]uint32, new_size)
  46. if *c != 0 {
  47. copy(new_array, (*a)[:*c])
  48. }
  49. *a = new_array
  50. } else {
  51. *a = (*a)[:new_size]
  52. }
  53. *c = new_size
  54. }
  55. }